$(document).ready( function(){

// Correção de bug do ie para o bg do site
	if(navigator.appName.indexOf("Explorer") != -1){
		tfAjustaBg = function(){
			$("#apoio").css("margin-left",$(window).width() % 2 == 1 ? "17px" : "16px");
			$("#sombra").css("right",$(window).width() % 2 == 1 ? "11px" : "12px")
		}
		tfAjustaBg();
		$(window).resize(function(){ tfAjustaBg(); });
	}
	
// Efeito Sombra
	tfSombraHtml = "";
	for(i=1;i<=7;i++){
		tfSombraHtml += "<div class=\"sombra_bg"+i+"\">";
	}
	for(i=1;i<=7;i++){
		tfSombraHtml += "</div>";
	}
	$(".sombra").wrapInner(tfSombraHtml);
	
// Lista tipo 1
	$("ul.lista1").wrap("<div class=\"lista1\"><div></div></div>");

// Faixa de Novo Site
	$(window).resize(function(){ $("#novoSite").css("left",($(window).width() < 1220 ? -(1220 - $(window).width()) : 0) + "px"); });

// Scroll
	tfItensLargura = $("#testeira .bg4 .bg5 .conteudo ul li").length * 169;
	$("#testeira .bg4 .bg5 .conteudo ul").css("width",tfItensLargura + "px");
	
	if(tfItensLargura > 850){

		// Barra
		tfBarra = $("#testeira .bg4 .bg5 .scroll .centro");
		tfCaminho = $("#testeira .bg4 .bg5 .scroll div");
		$(tfBarra)
			.bind('mousedown', function(e){
				tfDiferenca = parseInt(e.clientX) - parseInt($(tfBarra).offset().left);
				$().bind('mousemove', function(e){
					tfMoveBarra(parseInt(e.clientX), parseInt($(tfCaminho).offset().left), tfDiferenca);
				});
				$().bind('mouseup',function(){
					$().unbind('mousemove');
				});
			})
			.bind('mouseup', function(e){
				$().unbind('mousemove');
				$().unbind('mouseup');
			})
		;
		
		// Função que move o scroll
		tfMoveBarra = function(mouseX, caminhoX, diferencaX){
			barraEsq = parseInt($(tfBarra).css("left"));
			tfPosicao = (mouseX - caminhoX) - diferencaX;
			$(tfBarra).css({
				"left": tfPosicao < 0 ? 0 : tfPosicao > 898 ? 898 : tfPosicao + "px"
			});
			
			tfPorc = ( 100 * tfPosicao ) / 898;
			tfPorc = tfPorc > 100 ? 100 : tfPorc;
			
			tfMoveElementos(tfPorc);
		}
		
		// Função que move os elementos
		tfMoveElementos = function(tfPorcent){
			tfMargemDif = ( tfItensLargura - 954 ) + 74;
			tfMargem = -(tfPorcent * tfMargemDif) / 100;
			tfMargem = tfMargem > 0 ? 0 : tfMargem;
			
			$("#testeira .bg4 .bg5 .conteudo ul").css("margin-left",tfMargem + "px");
		}
		
		// Setas
		$("#testeira .bg4 .bg5 .scroll .esq").hover(function(){
			tfVoltaElementos();
			tfMovendo = setInterval(function(){ tfVoltaElementos(); },10);
		},function(){
			clearInterval(tfMovendo);
		});
		$("#testeira .bg4 .bg5 .scroll .dir").hover(function(){
			tfAvancaElementos();
			tfMovendo = setInterval(function(){ tfAvancaElementos(); },10);
		},function(){
			clearInterval(tfMovendo);
		});
		
		tfVoltaElementos = function(){
			tfDiferenca = 0
			tfFalsoX = parseInt($(tfBarra).offset().left) - 5;
			tfFalsoX = tfFalsoX < parseInt($(tfCaminho).offset().left) ? parseInt($(tfCaminho).offset().left) : tfFalsoX;
			tfMoveBarra(tfFalsoX,parseInt($(tfCaminho).offset().left),tfDiferenca);
		}
		tfAvancaElementos = function(){
			tfDiferenca = 0
			tfFalsoX = parseInt($(tfBarra).offset().left) + 5;
			tfFalsoX = tfFalsoX > parseInt($(tfCaminho).offset().left) + 898 ? parseInt($(tfCaminho).offset().left) + 898 : tfFalsoX;
			tfMoveBarra(tfFalsoX,parseInt($(tfCaminho).offset().left),tfDiferenca);
		}
	
	}else{
		$("#testeira .bg4 .bg5 .scroll").children().hide();
		$("#testeira .bg4 .bg5 .conteudo ul").css("padding","0").centroH();
	}

// Image Box
	$.ImageBox.init({
		loaderSRC: 'imagens/carregando.gif'
	});

// Grava Newsletter
	$("#form_newsletter").submit(function(){
		if(validaForm(this)){
			ajax('newsletterGravaEmail','',false,false,'form_newsletter','');
		}
		return false;
	});
});