/* faq component, write logic of faq module in this class */
var faq = new Component("faq");
app.registerComponent(faq);
faq.on( "load", function(){
	var self = this;
	app.doAjaxRequest = true;
	return faq.render("template/faq.html", 
    		"mainContent", 
    		"faqCategory/list/" + lang.currentLang
    		).done(function(template, data) {
    			faq.data = data;
    			self.init();
    	        self.buttonEvent();
    	        trackAction("FAQ - List");
    	    	faq.initialMoreEvent();
    	    	$(".question").hide();
    	    	$(".answer").hide();
    	    	$("[step='0']").trigger("click");
    	    	app.doAjaxRequest = false;
    	    }); 
});

faq.findFaq = function(cateId, faqId){
	var array = this.data.data.data;
	for(var i=0;i < array.length; i++){
		if(array[i].cateId == cateId){
			 var arr = array[i].faqs;
			 for(var j=0;j<arr.length;j++){
					if(arr[j].faqId == faqId){
						return arr[j];
					}
				}
		}
	}
};

faq.initialMoreEvent = function(){
	$(".answer").each(function(i,item){
		var answerHtml = $(item).find("pre").html();
		if(lang.currentLang=="en") {
			if(answerHtml.length > 100){
				var answerHtmlPart1=answerHtml.substring(0,100);
				var answerHtmlPart2=answerHtml.substring(100,answerHtml.length);
				if(answerHtmlPart2.indexOf(' ')>=0) {
					$(item).find("pre").html(answerHtmlPart1.replace(/\n/g, "<br/>") + answerHtmlPart2.substring(0,answerHtmlPart2.indexOf(' ')).replace(/\n/g, "<br/>") + "...<br/><a href='javascript:;' name='answerMore' style='font-weight:bold;' msgid='publicMore'>"+lang.getMessage('publicMore')+"</a>");
				}
			}
		} else {
			if(answerHtml.length > 50){
				$(item).find("pre").html(answerHtml.replace(/\<br\>|\<br\/\>/g, "\n").substring(0,50).replace(/\n/g, "<br/>") + "...<br/><a href='javascript:;' name='answerMore' style='font-weight:bold;' msgid='publicMore'>"+lang.getMessage('publicMore')+"</a>");
			}
		}
	});
	
	$("a[name='answerMore']").die().live("click", function(){
		var str = $(this).parent("pre").attr("condition");
		var arr = str.split("_");
		var data = faq.findFaq(arr[0], arr[1]);
		$(this).parent("pre").html(data.faqAnswer.replace(/\n/g, "<br/>") + "<br/><a href='javascript:;' name='answerCompress' style='font-weight:bold;' msgid='compress'>"+lang.getMessage('compress')+"</a>");	
	});
	
	$("a[name='answerCompress']").die().live("click", function(){
		var answerHtml = $(this).parent("pre").html();
		if(lang.currentLang=="en") {
			if(answerHtml.length > 100){
				var answerHtmlPart1=answerHtml.substring(0,100);
				var answerHtmlPart2=answerHtml.substring(100,answerHtml.length);
				if(answerHtmlPart2.indexOf(' ')>=0) {
					$(this).parent("pre").html(answerHtmlPart1+answerHtmlPart2.substring(0,answerHtmlPart2.indexOf(' ')) + "...<br/><a href='javascript:;' name='answerMore' style='font-weight:bold;' msgid='publicMore'>"+lang.getMessage('publicMore')+"</a>");
				}
			}
		} else {
			if(answerHtml.length > 50){
				$(this).parent("pre").html(answerHtml.replace(/\<br\>|\<br\/\>/g, "\n").substring(0,50).replace(/\n/g, "<br/>") + "...<br/><a href='javascript:;' name='answerMore' style='font-weight:bold;' msgid='publicMore'>"+lang.getMessage('publicMore')+"</a>");
			}
		}
	});
};

faq.init = function() {
	$("#questions").show();
	$(".question").eq(0).show();
};

faq.buttonEvent = function() {
//	$(".menu_head").click(function(){
//		trackAction("FAQ - SwitchCategory");
//		//$(this).css({backgrundImage: "url(down.png)"}).next("div.menu_body").slideToggle(300).
//		//siblings("div.menu_body").slideUp("slow");
//		//$(this).siblings().css({backgroundImage:"url(left.png)"});
//		var element = $(this).next("div.menu_body");
//		$(element).slideToggle(500).siblings("div.menu_body").slideUp("normal");
//		$(element).css({"display": "block"});
//		$("div.menu_body").not(element).css({"display": "none"});
//	});
	
	$(".menu_head").click(function(){
		if($(this).siblings(".question").is(':visible') ) {
			$(this).siblings(".question").hide();
			$(this).find("div").attr("style", "");
		} else {	
			$('.menu_head>div').attr("style", "");
			$($($('.menu_head').parent()).siblings()).find(".question").hide();
			$(this).siblings(".question").show();
			$(this).find("div").attr("style", "background : url('images/icon-sprite.gif?version=|||xxx|||') no-repeat -391px -702px;");
		}
	});

//	$(".question").click(function(){
//		trackAction("FAQ - View");
//		var element = $(this).find(".answer");
//		$(element).slideToggle(300).siblings(".answer").slideDown("normal");
//		$(element).css({"display": "block"});
//		$("div.answer").not(element).css({"display": "none"});
//		//$("div.answer").not(element).slideUp("slow").css({"display": "none"});
//	});
	$("div[name='questionTitle']").click(function(){
		if($(this).siblings(".answer").is(':visible') ) {
			$(this).siblings(".answer").hide();
			$(this).find("#questionImg").attr("style", "");
		} else {
			$(".answer").hide();
			$(".questionImage").attr("style", "");
			$(this).siblings(".answer").show();
			$(this).find("#questionImg").attr("style", "background : url('images/icon-sprite.gif?version=|||xxx|||') no-repeat -389px -740px;");
		}
	});
};
