//global da lista
var topics;
var qsParm = new Array();
qsParm['whichTema'] = null;
qsParm['whichQuestion'] = null;
qsParm['whichTemaTexto'] = null;
qsParm['whichQuestionTexto'] = null;

function GetSelectedFaq() {
    var href = window.location.href;
    var parts = href.split('/');
    if (parts.length >= 8)
        qsParm["whichTema"] = parts[parts.length-2];
    if (parts.length == 9)
        qsParm["whichQuestion"] = parts[parts.length-1];
}

$(function() {
	//fix para IE:
	$("#header").css("zIndex", 100000);
	
	/*------ FAQS -----*/
	var topicDivTitle = $(".texto li h5");
	var topicDesc = $(".texto li div");
	//esconde todas as respostas;
	topicDesc.hide();
	//mostra uma das respostas
	GetSelectedFaq();
	//mostra essa resposta
	if (qsParm['whichQuestion']){
	     //$(".texto li:eq("+qsParm['whichQuestion']+")> div").show();
		 $(".texto li[@UrlPergunta = '"+qsParm['whichQuestion']+"']> div").show();
		 //muda a class:
		 $(".texto li[@UrlPergunta = '"+qsParm['whichQuestion']+"']> h5").addClass("clicked");
	}
	//agora as perguntas:
	topicDivTitle.css("cursor","pointer");
	//topicDivTitle.css("font-weight","normal");
	$(".texto li h5").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	topicDivTitle.click(function() {
		openedDivs = $(this).parent().siblings('li').find('div:visible');
		openedDivs.siblings('h5').removeClass("clicked");
		$(this).toggleClass("clicked").next('div').slideToggle('fast').parent().siblings('li').find('div:visible').slideUp('fast');
	});
	/*------ FAQS -----*/
    
    /*-------- Lista de Grupos de Tópicos  ---------*/
    topicDivTitle = $("div.col h3");
    topicDivTitle.css("cursor","pointer");
	topicDivTitle = $("div.banner_col h3");
	topicDivTitle.css("cursor","pointer");
//    topicDivTitle.click(function(){
//        alert("link para este tópico");
//    });
    /*-------- COMBOS ---------*/
    
    /*----- Resultado da Pesquisa */
    topicDivTitle = $(".cgroup .header");
	topicDivTitle.css("background-image","none");
//    topicDivTitle.click(function(){
//        window.location.href = topicDivTitle.attr("link");
//    });
    /*----- Resultado da Pesquisa */

    $("#botPesquisar").css("cursor", "pointer");
//    $("#botPesquisar").bind("click", function () {
//        var UrlPesquisa = $("#InfoSubmitForm").data().UrlPesquisa;
//        var keyword = $("#search").val();
//        window.location.href = UrlPesquisa + "?Keyword=" + window.escape(keyword);
//    });

    $("#botPesquisar").bind("click", SubmitPesquisa);
    $("#search").bind("keyup", function(evt) {
        if (13 == evt.keyCode)
            SubmitPesquisa();
    });
    
    $("#botTodasFaqs").css("cursor", "pointer");
    $("#botTodasFaqs").bind("click", function() {
        $.meta.setType("elem", "script");
        var UrlFaqs = $("#InfoSubmitForm").data().UrlFaqs;
        $.meta.setType("");
        var selectedTema = $("div.combobox_header INPUT[@type=hidden]", $(".combobox_container .combobox_header INPUT[@name^=temasFrequentes]",$(".combobox_list").parents("div")).parents(".combobox_container"));
        if (selectedTema.val() != 0)
            window.location.href = UrlFaqs + "/" + selectedTema.val();
        else
            window.location.href = UrlFaqs;
    });
    
    $("#botFaq").css("cursor", "pointer");
    $("#botFaq").bind("click", function() {
        $.meta.setType("elem", "script");
        var UrlFaqs = $("#InfoSubmitForm").data().UrlFaqs;
        $.meta.setType("");
        var selectedTema = $("div.combobox_header INPUT[@type=hidden]", $(".combobox_container .combobox_header INPUT[@name^=temasFrequentes]",$(".combobox_list").parents("div")).parents(".combobox_container"));
        var selectedFaq = $("div.combobox_header INPUT[@type=hidden]", $(".combobox_container .combobox_header INPUT[@name^=questoesFrequentes]",$(".combobox_list").parents("div")).parents(".combobox_container"));
        if (selectedFaq.val() != 0) {
            window.location.href = UrlFaqs + "/" + selectedTema.val() + "/" + selectedFaq.val();
        }
    });

	generateCombos();
});
	
function formatTextFromJson(text)
{
    if ("" != text)
        //return text.replace("&apos;", "'");
        return $("<div/>").html(unescape(text)).text().replace("<", "&lt;").replace(">", "&gt;").toUpperCase();
    return text;
}
function generateCombos(){
    $.meta.setType("elem", "script");
	if($("div SPAN.data").length > 0)
	{
        topics = $("div SPAN.data").data().topicsArray;
        //coloca a combo:
        select_tema = $(".combobox_container .combobox_header INPUT[@name^=temasFrequentes]",$(".combobox_list").parents("div")).parents(".combobox_container");
        lista = $(".combobox_list", select_tema);
        input_text = $("div.combobox_header INPUT[@type=text]",select_tema);
        input_value = $("div.combobox_header INPUT[@type=hidden]",select_tema);
        input_value.val('');
        input_text.val(''); 
        lista.empty();
        //cria a combo, ignorando o primeiro item!:
        for(theID in topics) 
            if(isValidNumber(theID)) 
                if(topics[theID].id_tema != "starter") 
                {
                    if(qsParm["whichTema"] && (topics[theID].id_tema.toLowerCase() == qsParm["whichTema"].toLowerCase()))
                        qsParm["whichTemaTexto"] = formatTextFromJson(topics[theID].tema);
                    lista.append('<a href="#" data="'+topics[theID].id_tema+'">'+formatTextFromJson(topics[theID].tema)+'</a><br/>');
                }
        select_tema.get(0).rebuild();
    //    if (qsParm["whichTemaTexto"])
    //    {
    //        input_text.val(qsParm["whichTemaTexto"]);
    //        input_value.val(qsParm["whichTema"]);
    //        if (qsParm["whichTema"])
    //        {
    //            change_topic("", qsParm["whichTema"], "");
    //        }
    //    }
    //    else
    //    {
            input_text.val(topics[0].tema.toUpperCase());
            input_value.val(0);
    //    }
        //fix:
        $(".combobox_header INPUT[@name^=combotext_temasFrequentes]").css("width", ($('.combobox_container').width()-24)+'px');
        $(".combobox_header INPUT[@name^=combotext_questoesFrequentes]").css("width", ($('.combobox_container').width()-24)+'px');

        // BEGIN - PN: 2009-11-29 - preselect temas frequentes dropdown with current tema                       
        if (null != qsParm["whichTemaTexto"] && 0 != qsParm["whichTemaTexto"].length)
            $(".combobox_header INPUT[@name^=combotext_temasFrequentes]").attr('value', qsParm["whichTemaTexto"].toUpperCase());
        // END - PN: 2009-11-29
            
	    //EM 08.03.008, desactivar a combo da resposta enquanto não se escolheu a pergunta:
	    select_tema = $(".ver_faq .combobox_container");
	    //retirar tb o cursor!
	    select_tema.unbind("click").css("cursor","default");
	    $(".ver_faq").css("cursor","default");
	    $("div.combobox_header", select_tema).css("cursor","default");
	    $("INPUT[@name^=combotext_questoesFrequentes]", select_tema).css("cursor","default");
    	
	    //esconder o bt "ver faq":
	    $("#faqs_form label:eq(1)").hide();
	    
        $("a", lista).each(function() {
            $(this).bind("click", function() { $("#faqs_form label:eq(1)").hide(); } );
        });

	
	}
	$.meta.setType("");

}

function change_topic(text,value,container){
    for(theID in topics) if(topics[theID].id_tema == value) break;
    doTheChange(theID);

		
		//repôr o cursor:
		select_tema = $(".ver_faq .combobox_container");
		select_tema.css("cursor","pointer");
		$(".ver_faq").css("cursor","pointer");
		$("div.combobox_header", select_tema).css("cursor","pointer");
		$("INPUT[@name^=combotext_questoesFrequentes]", select_tema).css("cursor","pointer");
		
		//EM 08.03.008 activa bt "ver faq"
		//$("#faqs_form label:eq(1)").show();
}

function doTheChange(whichID){
    //cria a combo dependendo do item escolhido na combo Nº1:
    select_tema = $(".combobox_container .combobox_header INPUT[@name^=questoesFrequentes]",$(".combobox_list").parents("div")).parents(".combobox_container");
    lista = $(".combobox_list", select_tema);
    input_text = $("div.combobox_header INPUT[@type=text]",select_tema);
    input_value = $("div.combobox_header INPUT[@type=hidden]",select_tema);
    input_value.val('');
    input_text.val(''); 
    lista.empty();
    //cria a combo, ignorando o primeiro item!:
    for(theID in topics[whichID].listaQuestoes) 
        if(isValidNumber(theID)) 
            if(topics[whichID].listaQuestoes[theID].id_tema != "starter") 
            {
                if(qsParm["whichQuestion"] && (topics[whichID].listaQuestoes[theID].id_tema.toLowerCase() == qsParm["whichQuestion"].toLowerCase()))
                    qsParm["whichQuestionTexto"] = formatTextFromJson(topics[whichID].listaQuestoes[theID].tema.toUpperCase());
                lista.append('<a href="#" data="'+topics[whichID].listaQuestoes[theID].id_tema+'">'+formatTextFromJson(topics[whichID].listaQuestoes[theID].tema.toUpperCase())+'</a><br/>');
            }
    select_tema.get(0).rebuild();
//    if (qsParm["whichQuestionTexto"])
//    {
//        input_text.val(qsParm["whichQuestionTexto"]);
//        input_value.val(qsParm["whichQuestion"]);
//    }
//    else
//    {
        input_text.val(topics[whichID].listaQuestoes[0].tema.toUpperCase());
        input_value.val(0);
        
        $("a", lista).each(function() {
            $(this).bind("click", function() { $("#faqs_form label:eq(1)").show(); } );
        });
//    }
}

function SubmitPesquisa()
{
    $.meta.setType("elem", "script");
    var UrlPesquisa = $("#InfoSubmitForm").data().UrlPesquisa;
    $.meta.setType("elem", "script");
    var keyword = $("#search").val();
    if(window.escape(keyword)!="")
    window.location.href = UrlPesquisa + "?Keyword=" + window.escape(keyword);
}


