// JavaScript Document
var config = {    
	over: function(){
		$(this).animate({
			marginTop: 10
		}, 300).find("img").fadeIn(300);
	}, // function = onMouseOver callback (REQUIRED)    
	timeout: 200, // number = milliseconds delay before onMouseOut    
	out: function(){
		var tempData = $(this).data("data");
		if(tempData.section != visibleSection){
			$(this).animate({
				marginTop: 30
			}, 300).find("img").fadeOut(300);
		}
	} // function = onMouseOut callback (REQUIRED)    
};

var visibleSection = 0;
var sectionFlag = false;
var tempData;
var interval;

function force_highslide_close(){
	$(".highslide-controls:first").parents("div:first").empty();
	//alert($(".highslide-controls").parents("div").length);
}

$(document).ready(function(){
	
	$(".navegation a").hoverIntent(config);
	$(".sections").height(windowHeight());
	$(".init a").click(function(){ $("body").scrollTo($(".section1"), 1500,  { easing:'easeInOutCubic'});});
	
	$(".navegation a:eq(0)").click(goToSection1).data("data", {section:1});
	$(".navegation a:eq(1)").click(goToSection2).data("data", {section:2});
	$(".navegation a:eq(2)").click(goToSection3).data("data", {section:3});
	$(".navegation a:eq(3)").click(goToSection4).data("data", {section:4});
	$(".navegation a:eq(4)").click(goToSection5).data("data", {section:5});
		
	for(i=1; i<=5; i++){
		$(".section"+i).css({position:"absolute", top: (windowHeight())*i}).fadeIn();
	}
	
	$('.carrosel').jcarousel({
        vertical: true,
        scroll: 2
    });

	
	$("input[title], textarea[title]").each(function(){
		if($(this).val()==""){
			$(this).val($(this).attr("title"));
		}
		$(this).focus(function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");
			}
		})
		.blur(function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("title"));
			}	
		});
	});
	
	if($(".form_submit").length>0){
		$(".form_submit").each(function(){
			$(this).css("cursor","pointer");
			$(this).unbind("click");
			$(this).click(function(){
				$(this).parents("form").submit();
			});
		});
	}
	
	if($("form").length>0){
		$("form").unbind("submit");
		$("form").submit(function(){
			switch(this.name){
				case "form_contato":
					Envia_Contato(this);
				break
			}
		});
	}
	
	$("dl.cases dd ul.carrosel li a").click(function(){
		$("body").popup({url:"case.php?id="+$(this).attr("rel"), width:980, height:520, closebutton:".ctrl_cls"})
	});
	
});

$(window).scroll(function(){
	if(scrollPosition() > 410){
		$(".navegation").css({position:"fixed", top:0});
	}else{
		$(".navegation").css({position:"absolute", top:410});
	}
	
	for(i=0; i<6; i++){
		
		//VERIFICA POSIÇÃO DO SCROLL
		if(scrollPosition() > (windowHeight()*i)-200 && scrollPosition() < (windowHeight()*(i+1))-200){
			
			//VERIFICA SE MUDOU A SECTION ATUAL
			if(visibleSection != i){
				clearInterval(interval);
				
				//ESPERA UM INTERVALO PARA SABER SE A SECTION VAI MUDAR NOVAMENTE
				interval = setInterval(function(){
					clearInterval(interval);
					
					$(".navegation a").each(function(j){
						if(j!=(visibleSection-1)){
							$(this).animate({ marginTop: 30}, 300).find("img").fadeOut(300);
						}else{
							$(this).animate({ marginTop: 10}, 300).find("img").fadeIn(300);
						}
					});
					
				}, 400);
			}
			
			//MUDA SECTION ATUAL
			visibleSection = i;
		}
	}
	
});


$(window).resize(function(){
	$(".sections").height(windowHeight());
	
	for(i=1; i<=5; i++){
		$(".section"+i).css({position:"absolute", top: (windowHeight())*i}).fadeIn();
	}
});



$.easing.easeInOutCubic = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}


function goToSection0(){ $("body").scrollTo($(".header"), 3000,  { easing:'easeInOutCubic'}); }
function goToSection1(){ $("body").scrollTo($(".section1"), 1500,  { easing:'easeInOutCubic'}); }
function goToSection2(){ $("body").scrollTo($(".section2"), 1500,  { easing:'easeInOutCubic'}); }
function goToSection3(){ $("body").scrollTo($(".section3"), 1500,  { easing:'easeInOutCubic'}); }
function goToSection4(){ $("body").scrollTo($(".section4"), 1500,  { easing:'easeInOutCubic'}); }
function goToSection5(){ $("body").scrollTo($(".section5"), 1500,  { easing:'easeInOutCubic'}); }

function scrollPosition(){
	if($.browser.safari){
		return document.body.scrollTop;
	}else{
		return document.documentElement.scrollTop;
	}
}

function windowHeight(){
	return $(window).height()+100;
}

function form_titles_init(){
	
	$("input[title], textarea[title]").each(function(){
		if($(this).val()==""){
			$(this).val($(this).attr("title"));
		}
		$(this).focus(function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");
			}
		})
		.blur(function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("title"));
			}	
		});
	});
	
}
function validaEmail(email){
	ER = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64})*\.[a-z]{2,4}$");
	if (ER.test(email)){
		return true;
	}else{
		return false;
	}
}

function controleEnvioFormularios(){
	
	if($(".form_submit").length>0){
		$(".form_submit").each(function(){
			$(this).css("cursor","pointer");
			$(this).unbind("click");
			$(this).click(function(){
				$(this).parents("form").submit();
			});
		});
	}
	
	if($("form").length>0){
		$("form").unbind("submit");
		$("form").submit(function(){
			switch(this.name){
				case "form_contato":
					Envia_Contato(this);
				break
			}
		});
	}
}

	function Envia_Contato(form){
		var nome 		= $('#txtNome').val();
		var email 		= $('#txtEmail').val();
		var telefone 	= $('#txtFone').val();
		var mensagem 	= $('#txtMensagem').val();
		if (nome=="" || nome == "Nome"){
			alert("Digite o Nome.");											
			form.txtNome.focus();
			return false;
		}else if (email=="" || email == "E-mail"){
			alert("Digite o Email.");													
			form.txtEmail.focus();
			return false;
		}else if(!validaEmail(email)){
			alert("Formato do Email Incorreto.");													
			form.txtEmail.focus();
			return false;
		} else if (telefone=="" || telefone=="Telefone"){
			alert("Digite o Telefone.");											
			form.txtFone.focus();
			return false;
		}else if (mensagem=="" || mensagem=="Mensagem"){
			alert("Digite a Mensagem.");											
			form.txtMensagem.focus();
			return false;
		} else {
			//$.get('http://www.clickeria.com.br/scripts/recebi_yes.php?tipo=2&nome='+nome+'&email='+email+'&tel='+telefone+'&mensagem='+mensagem,function(data){alert(data);});
			$.post('scripts/contato.php',$(form).serialize(),function(data){
				form.reset();
				alert("Mensagem Enviada com Sucesso");
			});
		}
	}		

//MÁSCARA DE VALORES
function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8 || nTecla == 0) {
			return true;
		}
	}
    sValue = objeto.value;
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++;
	  }else{
        sCod += sValue.charAt(nCount);
        nCount++;
      }
      i++;
    }
    objeto.value = sCod;

	if (nTecla != 8) { // backspace ou tab
		if(sMask.charAt(i-1) == "9"){ // apenas números...
			return ((nTecla > 47) && (nTecla < 58));
		}else{ // qualquer caracter...
			return true;
		} 
	}else{
		return true;
	}
}

