// smart 3d
var compLoadImg1 = 0;
var compLoadImg2 = 0;
var compLoadImg3 = 0;
var compLoadImg4 = 0;
var compLoadImg5 = 0;
var compLoadImg6 = 0;
var compLoadImg7 = 0;
var compLoadImg8 = 0;
var compLoadImg9 = 0;
var compLoadImg10 = 0;
var compLoadImg11 = 0;
var compLoadImg12 = 0;

function loadImg1(){compLoadImg1 = 1;}
function loadImg2(){compLoadImg2 = 1;}
function loadImg3(){compLoadImg3 = 1;}
function loadImg4(){compLoadImg4 = 1;}
function loadImg5(){compLoadImg5 = 1;}
function loadImg6(){compLoadImg6 = 1;}
function loadImg7(){compLoadImg7 = 1;}
function loadImg8(){compLoadImg8 = 1;}
function loadImg9(){compLoadImg9 = 1;}
function loadImg10(){compLoadImg10 = 1;}
function loadImg11(){compLoadImg11 = 1;}
function loadImg12(){compLoadImg12 = 1;}
 
$(document).ready(function() {
	// accordion
	if(paginaJs == "contato"){
		jQuery('.menuContato').accordion({autoheight: false});
	}	
	
	// carousel
	if(subPaginaJs == "historia"){
		$('#carouselYears').jcarousel({animation:1000, scroll:5});
	}

	// corrige target
	$('a[rel~=external]').attr('target','_blank');
	
	// controle tamanho fonte
	var arrayFontSize = new Array("", "10px", "11px", "12px", "13px", "14px");
	var arrayLineHeight = new Array("", "12px", "14px", "16px", "18px", "20px");
	
	function atualizaFontSizeSite($tamanho){
		$.ajax({
			url: 'ajax/control-text.php',
			type: "POST",
			data: ({'tamanho':$tamanho})
		});
	}

	function verificaFontSizeSite(tipo){
		if(tipo == "positivo"){
			if(fontSizeActual < 5){
				fontSizeActual++
			}
		}
		
		if(tipo == "negativo"){
			if(fontSizeActual > 1){
				fontSizeActual--
			}
		}

		atualizaFontSizeSite(fontSizeActual);
		$('.fontSizeActual').css({fontSize: arrayFontSize[fontSizeActual]});
		$('.fontSizeActual').css({lineHeight: arrayLineHeight[fontSizeActual]});
	}
	
	verificaFontSizeSite();
	
	$(".linkCtrlTextPos").click(function() {
		verificaFontSizeSite("positivo");
	});
	
	$(".linkCtrlTextNeg").click(function() {
		verificaFontSizeSite("negativo");
	});
	
	// exibe botao submit ao carregar javascript
	$(".inputSubmit").css({display: "inline"});

	// fancybox
	if(paginaJs == "empresa" || paginaJs == "estrutura" || paginaJs == "fabricas" || paginaJs == "noticias" || paginaJs == "pagina-inicial" || paginaJs == "produtos"){
		$(".fancyBoxIframeGoogleMaps").fancybox({
			'width'				: 600,
			'height'			: 500,
			'autoScale'			: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'iframe'
			}
		);
		
		$(".fancyBoxIframeVideo").fancybox({
			'width'				: 564,
			'height'			: 352,
			'autoScale'			: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'iframe'
			}
		);
		
		$(".fancyBoxIframeCertificado").fancybox({
			'width'				: 750,
			'height'			: 740,
			'autoScale'			: false,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'type'				: 'iframe'
			}
		);
		
		$(".fancyBoxPhotos").fancybox();
	}
	
	// formulário contato
	if(paginaJs == "contato"){
		$("#formContato").validate({errorLabelContainer: $("#formContato div.error")});
		$formContato = $('#formContato');
		var dadosFormContato;
		var msgAjaxContato;
	
		function resetFieldsContato(){
			$('#inputSubmitContato').css({display:'block'});
			$('#requiredFieldsContato').css({display: 'block'});
			$('#statusContato').css({display: 'none'});
			if(idioma == "en"){
				$('#statusContato').html('Sending...');
			} else {
				$('#statusContato').html('Enviando...');
			}
		}
		
		function msgContato(){
			$('#statusContato').html(msgAjaxNews);
			dadosFormContato.reset();
			window.setTimeout(resetFieldsContato, 4000);
		}
	
		$formContato.bind('submit', function(){
			if($("#formContato").valid()){
				$('#inputSubmitContato').css({display:'none'});
				var params = $(this.elements).serialize();
				dadosFormContato = this;
				
				$.ajax({
					type: 'POST',
					url: this.action,
					data: params,
					beforeSend: function(){
						$('#statusContato').css({display: 'block'});
						$('#requiredFieldsContato').css({display: 'none'});
					},
					success: function(txt){
						msgAjaxNews = txt;
						window.setTimeout(msgContato, 1000);
					},
					error: function(txt){
						msgAjaxNews = txt;
						window.setTimeout(msgContato, 1000);
					}
				})
				
				return false;
			}
		});
	}
	
	// formulário newsletter
	$("#formNewsletter").validate({errorLabelContainer: $("#formNewsletter div.error")});
    $formNewsletter = $('#formNewsletter');
	var dadosFormNews;
	var msgAjaxNews;

	function resetFieldsNews(){
		$('#inputSubmitNews').css({display:'block'});
		$('#requiredFieldsNews').css({display: 'block'});
		$('#statusNewsletter').css({display: 'none'});
		if(idioma == "en"){
			$('#statusNewsletter').html('Sending...');
		} else {
			$('#statusNewsletter').html('Enviando...');
		}
	}
	
	function msgNews(){
		$('#statusNewsletter').html(msgAjaxNews);
		dadosFormNews.reset();
		window.setTimeout(resetFieldsNews, 4000);
	}

    $formNewsletter.bind('submit', function(){
		if($("#formNewsletter").valid()){
			$('#inputSubmitNews').css({display:'none'});
			var params = $(this.elements).serialize();
			dadosFormNews = this;
			
			$.ajax({
				type: 'POST',
				url: this.action,
				data: params,
				beforeSend: function(){
					$('#statusNewsletter').css({display: 'block'});
					$('#requiredFieldsNews').css({display: 'none'});
				},
				success: function(txt){
					msgAjaxNews = txt;
					window.setTimeout(msgNews, 1000);
				},
				error: function(txt){
					msgAjaxNews = txt;
					window.setTimeout(msgNews, 1000);
				}
			})
			
			return false;
		}
    });
	
	// fotos da faixa no topo
	if(paginaJs == "empresa" || paginaJs == "estrutura" || paginaJs == "fabricas"){
		$(".stripGallery .line div").hover(function() {
			var thumbOver = $(this).find("img").attr("src");
			$(this).find("a.thumbStripGallery").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
			$(this).find("span").stop().fadeTo(200, 0 , function() { 
				$(this).hide()}); 
			} , function() {
				$(this).find("span").stop().fadeTo(200, 1).show();
			}
		);
	}

	// galeira de fotos
	if(paginaJs == "estrutura" || paginaJs == "fabricas" || paginaJs == "produtos"){
		$('#sliderPhotos').nivoSlider({ieHeight:'230px'});
	}
	
	// galeira de fotos do historico
	if(subPaginaJs == "historia"){
		var delaySliderPhotos;
		$('#codaSlider').codaSlider({autoHeight: false});
		
		var funcSliderPhotos = function(){
			clearInterval(delaySliderPhotos);
			$('.lineHistory').css({display: "block"});
			$('#sliderHistoria-1974').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-1975').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-1976').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-1979').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-2006').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-2007').nivoSlider({ieHeight:'230px'});
			$('#sliderHistoria-2009').nivoSlider({ieHeight:'230px'});
			
			$('.loadingHistory').css({'display':'none'});
		}
		
		delaySliderPhotos = setInterval(funcSliderPhotos, 2000);
	}

	// input replacement
	if(paginaJs != "erro-404" && paginaJs != "video-institucional" && paginaJs != "google-maps"){
		if(idioma == "en"){
			$('#busca').input_replacement({ text: 'Search' });
		} else {
			$('#busca').input_replacement({ text: 'Buscar' });
		}
	}

	// links
	$(".coda-nav ul li a").mouseover(function() {
		if(!$(this).hasClass("current")){$(this).animate({color:'#006DAD'}, 100);}
	}).mouseout(function(){
		if(!$(this).hasClass("current")){$(this).animate({color:'#666666'}, 100);}
	});
	
	$(".coda-nav ul li a").click(function() {
		$(".coda-nav ul li a").animate({color:'#666666'}, 100);
		$(this).animate({color:'#006DAD'}, 100);	
	});
	
	$(".inputSubmit").mouseover(function() {
		$(this).animate({color:'#000000'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#FFFFFF'}, 100)
	});
	
	$(".link").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#666666'}, 100)
	});
	
	$(".link2").mouseover(function() {
		$(this).animate({color:'#333333'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#006DAD'}, 100)
	});
	
	$(".link3").mouseover(function() {
		$(this).animate({color:'#333333'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#006DAD'}, 100)
	});
	
	$(".linkBt").mouseover(function() {
		$(this).animate({color:'#000000'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#FFFFFF'}, 100)
	});
	
	$(".linkChangLangEn").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#999999'}, 100)
	});
	
	$(".linkChangLangPt").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#999999'}, 100)
	});
	
	$(".linkCtrlTextNeg").mouseover(function() {
		$(this).animate({color:'#000000'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#FFFFFF'}, 100)
	});
	
	$(".linkCtrlTextPos").mouseover(function() {
		$(this).animate({color:'#000000'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#FFFFFF'}, 100)
	});
	
	$(".linkImg").mouseover(function() {
		$(this).animate({opacity: 0.75}, 150);
	}).mouseout(function(){
		$(this).animate({opacity: 1}, 100)
	});
	
	$(".linkMenuBody").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#666666'}, 100)
	});
	
	$(".linkMenuContato").mouseover(function() {
		if(!$(this).hasClass("selected")){$(this).animate({color:'#006DAD'}, 100);}
	}).mouseout(function(){
		if(!$(this).hasClass("selected")){$(this).animate({color:'#333333'}, 100);}
	});
	
	$(".linkMenuContato").click(function() {
		$(".linkMenuContato").animate({color:'#333333'}, 100);
		$(this).animate({color:'#006DAD'}, 100);	
	});
	
	$(".linkMenuFoot").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#666666'}, 100)
	});
	
	$(".linkMenuHead").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 100);
	}).mouseout(function(){
		$(this).animate({color:'#666666'}, 100)
	});
	
	$(".linkMenuSocialNetworks").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 150);
	}).mouseout(function(){
		$(this).animate({color:'#999999'}, 100)
	});
	
	$("#linkNewsletter").mouseover(function() {
		$(this).animate({color:'#006DAD'}, 150);
	}).mouseout(function(){
		$(this).animate({color:'#999999'}, 100)
	});
	
	$(".linkVideoInst").mouseover(function() {
		$(this).animate({opacity: 1}, 150);
	}).mouseout(function(){
		$(this).animate({opacity: 0.75}, 100)
	});
	
	$(".linkVideoInst").animate({opacity: 0.75}, 150);
	
	$(".logoEvox").mouseover(function() {
		$(this).animate({opacity: 1}, 150);
	}).mouseout(function(){
		$(this).animate({opacity: 0.5}, 100)
	});
	
	$(".logoEvox").animate({opacity: 0.5}, 150);
	$(".logoEvox").click(function() {$(this).animate({opacity: 1}, 200)});
	
	// link newsletter
	var linkNewsOpen = false;
	
	$("#linkNewsletter").click(function() {
		if(!linkNewsOpen){
			$("#newsletter").fadeIn(250, function(){ linkNewsOpen = true;});
		} else {
			$("#newsletter").fadeOut(250, function(){ linkNewsOpen = false;});
		}
	});
	
	// logos afiliados
	if(paginaJs == "afiliadas"){
		$(".logoAffiliate").hover(function() {
			var thumbOver = $(this).find("img").attr("src");
			$(this).find("a.thumbLogoAffiliate").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
			$(this).find("span").stop().fadeTo(200, 0 , function() { 
				$(this).hide()}); 
			} , function() {
				$(this).find("span").stop().fadeTo(200, 1).show();
			}
		);
	}
	
	// smart 3d
	if(paginaJs == "pagina-inicial"){
		var smart3dStart;
		var nVerLoad = 0;
		
		var browserName = navigator.appName; 
		var browserVer = parseInt(navigator.appVersion);
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			var ieversion = new Number(RegExp.$1);
		}
	
		var funcVerificaLoadImg = function(){
			if(compLoadImg1 == 1 && compLoadImg2 == 1 && compLoadImg3 == 1 && compLoadImg4 == 1 && compLoadImg5 == 1 && compLoadImg6 == 1 && compLoadImg7 == 1 && compLoadImg8 == 1 && compLoadImg9 == 1 && compLoadImg10 == 1 && compLoadImg11 == 1 && compLoadImg12 == 1){
				$('.stripBlue3Preload').animate({opacity: 0}, 500, function(){
					$('.stripBlue3Preload').css({display: "none"});
					funcAnimaObjectsStart();
				});
				smart3dStart = setInterval(funcSmart3dStart, 4000);
			} else {
				if(nVerLoad == 30){
					clearInterval(animaObjectsStart);
					$('.stripBlue3Preload').animate({opacity: 0}, 250, function(){
						$('.stripBlue3PreloadErro').css({display:"block"});
						$('.stripBlue3PreloadErro').css({opacity: 0});
						$('.stripBlue3PreloadErro').animate({opacity: 1}, 250);
					});
				} else {
					nVerLoad++
				}
			}
		}
		
		var funcSmart3dStart = function(){
			$('.smartDemo3').smart3d();
			clearInterval(smart3dStart);
		}
		
		// loop beija-flor
		function loopBeijaFlor(){
			$('.smartBeijaFlor2').delay(2000).animate({paddingLeft:'550px'}, 5000, function(){
				$('.smartBeijaFlor2').animate({paddingLeft:'450px', paddingTop:'40px'}, 4000, function(){
					$('.smartBeijaFlor2').css({opacity:0});
					$('.smartBeijaFlor1').css({paddingLeft:'450px', paddingTop:'40px'});
					$('.smartBeijaFlor1').animate({paddingLeft:'1100px', paddingTop:'100px'}, 3000, function(){
						$('.smartBeijaFlor2').css({opacity:1, paddingLeft:'1100px', paddingTop:'40px'});
						loopBeijaFlor();
					});
				});
			});
		}
		
		var funcAnimaObjectsStart = function (){
			
			clearInterval(animaObjectsStart);
			
			$('.smartDemo3').css({display:'block'});
			
			$('.smartLogo62Anos').css({opacity:0, paddingTop:'70px'});
			$('.smartLogoThinkGreen').css({opacity:0, paddingLeft:'60px', paddingTop:'10px'});
			$('.smartLogoThinkGreenImg').animate({height:'181px', width:'181px'});
			$('.smartTxt1').css({opacity:0, paddingLeft:'180px'});
			$('.smartTxt2').css({opacity:0, paddingLeft:'212px'});
			$('.smartTxt3').css({opacity:0, paddingLeft:'218px'});
			$('.smartTxt4').css({opacity:0, paddingLeft:'215px'});
			$('.smartTxt5').css({opacity:0, paddingLeft:'200px'});
			$('.smartSeta1').css({opacity:0});
			$('.smartSeta2').css({opacity:0});
			$('.smartSeta3').css({opacity:0});
			$('.smartSeta4').css({opacity:0});
			$('.smartBobinas').css({opacity:0, paddingTop:'0px'});
			$('.smartGrama1').css({opacity:0, paddingLeft:'50px'});
			$('.smartGrama2').css({opacity:0, paddingLeft:'25px'});
			$('.smartBeijaFlor1').css({opacity:1, paddingLeft:'1100px'});
			$('.smartBeijaFlor2').css({opacity:1, paddingLeft:'1100px'});

			loopBeijaFlor();
			
			if (ieversion<=7) $('.stripBlue3BgAbsolute').css({marginLeft: "0"}); else $('.stripBlue3BgAbsolute').css({marginLeft: "15px"});
			$('.stripBlue3BgAbsolute').delay(500).animate({opacity: 0}, 500, function(){
				$('.stripBlue3BgAbsolute').css({display: "none"});
				$('.stripBlue3Preload').css({display: "none"});
			});

			$('.smartLogo62Anos').delay(1000).animate({opacity: 1, paddingTop:'20px'}, 500);
			$('.smartGrama1').delay(1500).animate({opacity:1, paddingLeft:'0px'}, 750);
			$('.smartGrama2').delay(1750).animate({opacity:1, paddingLeft:'0px'}, 750);
			$('.smartBobinas').delay(2000).animate({opacity:1, paddingTop:'20px'}, 500);
			$('.smartLogoThinkGreenImg').delay(2000).animate({width:'141px', height:'141px'}, 500);
			$('.smartLogoThinkGreen').delay(2500).animate({opacity: 1, paddingLeft:'70px', paddingTop:'20px'}, 500);
			$('.smartTxt1').delay(3000).animate({opacity: 1, paddingLeft:'200px'}, 500);
			$('.smartSeta1').delay(3250).animate({opacity: 1}, 500);
			$('.smartTxt2').delay(3250).animate({opacity: 1, paddingLeft:'232px'}, 500);
			$('.smartSeta2').delay(3500).animate({opacity: 1}, 500);
			$('.smartTxt3').delay(3500).animate({opacity: 1, paddingLeft:'238px'}, 500);
			$('.smartSeta3').delay(3750).animate({opacity: 1}, 500);
			$('.smartTxt4').delay(3750).animate({opacity: 1, paddingLeft:'235px'}, 500);
			$('.smartSeta4').delay(4000).animate({opacity: 1}, 500);
			$('.smartTxt5').delay(4000).animate({opacity: 1, paddingLeft:'220px'}, 500);
		}
		
		if (ieversion<=6) {
			$('#stripBlue3ID').html('<div><img src="img/anima-home/animacao-estatica-'+idioma+'.jpg" alt="Animação Pensando Verde Madem" /></div>');
			$('#stripBlue3ID').addClass('stripBlue3-ie6').removeClass('stripBlue3');
		} else {
			var animaObjectsStart = setInterval(funcVerificaLoadImg, 2000);
		}
	}
});
