$(document).ready( function(){
	/* Seta Tamanho das tabelas */
	carregaRolon = function(id){
		var totalTds = "";
		var totalTrs = "";
		var tiraScroll = 0;
		$("#"+id+" div").each( function(i){
			div = $(this).attr("class");
			if(div == "cima"){
				totalTds = $(this).children().children().children(":first-child").children().length;
			}
			if(div != "esquerdo"){ $(this).children().css("width",(totalTds*91)+1+"px"); }
			if(div == "conteudo"){
				//elementos = $(this).children().children().children(":first-child").children().length;
				elementos = totalTds;
				var tiraScroll = new Array();
				if(elementos < 6){
					$(this).css("width",(elementos*91)+16+"px");
					tiraScroll[0] = true;
				}
				elementos = $(this).children().children().children().length;
				if(elementos < 10){
					if(!tiraScroll[0]){
						$(this).css("height",(elementos*16)+33+"px");
						$(this).parent().css("height",(elementos*16)+49+"px");
					}else{
						$(this).css("height",(elementos*16)+17+"px");
						$(this).parent().css("height",(elementos*16)+33+"px");
						$("#"+id+" .conteudo table").css("margin-bottom","0");
						$("#"+id+" .esquerdo").css("height","176px");
					}
					tiraScroll[1] = true;
				}
				if(tiraScroll[0] && tiraScroll[1]){
					$(this).css("overflow","hidden");
					$("#"+id+" .rodape").css("bottom","0");
					$("#"+id+" .total").css("bottom","0");
				}
			}
		});
		/* Sincroniza Scrolls */
		$("#"+id+" div.conteudo").scroll( function(){
			//console.log("Calculando Scroll");
			$("#"+id+" div.esquerdo").scrollTop($(this).scrollTop());
			$("#"+id+" div.cima").scrollLeft($(this).scrollLeft());
			$("#"+id+" div.rodape").scrollLeft($(this).scrollLeft());
		});
		/* Colorização */
		var posicaoTd = 0;
		var posicaoTr = 0;
		overRolon = function(alvo){
			$(alvo).addClass("tabela_rolon_tdover ativo").parent().addClass("ativo");
			$(alvo).parent().children().each( function(i){
				classe = $(this).attr("class");
				if(classe != null){	if(classe.indexOf("ativo") != -1){ posicaoTd = i; } }
			});
			$(alvo).parent().parent().children().each( function(i){
				classe = $(this).attr("class");
				if(classe != null){	if(classe.indexOf("ativo") != -1){ posicaoTr = i; } }
			});
			$("#"+id+" div.cima td").eq(posicaoTd).addClass("tabela_rolon_tdover");
			$("#"+id+" div.esquerdo tr").eq(posicaoTr).children().addClass("tabela_rolon_tdover");
			$("#"+id+" div.rodape td").eq(posicaoTd).addClass("tabela_rolon_tdover");
		} 
		outRolon = function(alvo){
			$(alvo).removeClass("tabela_rolon_tdover ativo").parent().removeClass("ativo");
			$("#"+id+" div.cima td").eq(posicaoTd).removeClass("tabela_rolon_tdover");
			$("#"+id+" div.esquerdo tr").eq(posicaoTr).children().removeClass("tabela_rolon_tdover");
			$("#"+id+" div.rodape td").eq(posicaoTd).removeClass("tabela_rolon_tdover");
		}
	}
	tabela1 = function(){
		$(".tabela1Jav").children("tbody").children().hover( function(){
			$(this).children().addClass("tabela1_over").children("a").addClass("linkhover");
		}, function(){
			$(this).children().removeClass("tabela1_over").children("a").removeClass("linkhover");
		});
		$(".tabela1Jav").children("tbody").children().children("td[@class!=ignorar]").click( function(){
			funcao = $(this).parent().children(":first").children("a").attr("rel");
			eval(funcao);
		});
	}
	tabela3 = function(){
		$(".tabela3 td").each( function(i){
			if(i % 2 == 0){
				$(this).addClass("esq");	
			}
		});
	}
	carregaRolon();

	/* Carregando */
	carregaCarrega = function(){
		carrega = function(carregaPos){
			alturaCarregando = $("body").height() > $(window).height() ? $("body").height() : $(window).height();
			$("#carregando").css({height:alturaCarregando,opacity:0.5}).next().css({position:"absolute", left:"50%", margin:"0 0 0 -35px", zIndex:"9999"});
			if(carregaPos != "fim"){
				carregaTop = $(window).height();
				carregaTop = Math.round(carregaTop / 2);
				carregaTop += $(window).scrollTop();
				//console.log(carregaTop);
				$("#carregando").fadeIn("fast", function(){ $("#carregandoImg").css({top:carregaTop}).fadeIn("fast"); });
			}else{
				$("#carregandoImg").fadeOut("fast", function(){ $("#carregando").fadeOut("fast", function(){$("#carregandoImg").hide(); }); });
			}
		}
	}
	carregaCarrega();
	$(window).resize(function(){carregaCarrega()});
	
	// Image Box
	$.ImageBox.init(
		{
			loaderSRC: 'imagens/carregando.gif'
		}
	);
});
