var linhaTempo = {

    totalCategoria : 0,
    totalAno       : 0,
    
    catSel         : "all",
    anoSel         : "",

    start : function() {
        this.totalCategoria = $("#abas li").length -1;
        
        if(this.totalCategoria == -1)
        	this.totalCategoria = "all";

        this.totalAno       = $("#anos table thead tr th").length;
        this.anoSel         = $("#anos table thead tr th.ativo").attr("rel");

        this.showContent();
    },

    setWidth : function() {
        var divsLinha = $("#conteudo-linha div.texto:visible");
        $("#conteudo-linha").css("width", "0px");
        var qtd = divsLinha.length;
        var tam = 0;

        $.each(divsLinha, function() {
            tam += $(this).width();
            tam += 30;
        });

        $("#conteudo-linha").css("width", (tam + qtd) + "px");
    },


    changeAba : function(obj) {
        // li clicada
        var liClicked = $(obj).parent();
        
        // remove o ativo da aba atual e altera estilo da clicada
        $("#abas li").removeClass("ativo noborder");
        $(liClicked).addClass("ativo").prev("li").addClass("noborder");

        // qual foi a categoria selecionada
        this.catSel = $(liClicked).attr("rel");

        this.showContent();
    },
    
    
    changeYear : function(obj) {
        // th clicada
        var thClicked = $(obj).parent();
        
        // remove o ativo do ano atual e altera estilo do clicado
        $("#anos table thead tr th").removeClass("ativo");
        $(thClicked).addClass("ativo");

				// o indice da th clicada
				var ths = $("#anos table thead tr th");
				for(var x = 0; x < ths.length; x++) {
					if( $(ths[x]).attr("class") == "ativo") {
						var indice = x;
						break;
					}
				}

				if( $("#anos table tbody tr td.sem-ano").length > 0 )
					indice = indice * 2;

				$("#anos table tbody tr td").removeClass("ativo");
				$("#anos table tbody tr td:eq(" + indice + ")").addClass("ativo");

        // qual foi a categoria selecionada
        this.anoSel = $(thClicked).attr("rel");

				$("#anos div.seta").animate({
					left: parseInt( $("#anos table tbody tr td:eq(" + indice + ")")[0].offsetLeft + ($("#anos table tbody tr td:eq(" + indice + ")").width()/2) - 4 ) + "px"
				}, 1000);

        this.showContent();

    },

    showContent : function() {
        // esconde todos
        $("#conteudo-linha div").hide();

        if(this.catSel == "all")
            $("#conteudo-linha div[ano='" + this.anoSel + "']").show();
        else
            $("#conteudo-linha div[cat='" + this.catSel + "'][ano='" + this.anoSel + "']").show();

        this.setWidth();
    }

}


// Aguarda o jQuery estar carregado para chamar a função de setWidth
// do div da linha do tempo
function dependsJQuery() {
    if(typeof jQuery != "undefined") {
        linhaTempo.start();
        linhaTempo.setWidth();
        
				if(area) {
					$("#abas li[rel='"+area+"'] a").click();
				}
				
				if(ano) {
					$("#anos table tr th[rel='"+ano+"'] a").click();
				}
				
				if(scroll) {
					$("#scroll").scrollLeft( $("#scroll div.texto:visible").get(parseInt(scroll) - 1).offsetLeft );
				}
        
    }
    else
        setTimeout("dependsJQuery()", 500);
}
dependsJQuery();



