// JavaScript Document
$(document).ready(function()
{
	var link_site = 'http://www.natalencantadorionegrinho.com.br/';
	
	/** 
	 * Função para checar a resolução do usuário
	 */
	checkWindowSize(); //checa qndo abre a pag
	$(window).resize(checkWindowSize); //checa qndo o usuario redimensiona 
	
	function checkWindowSize() {
        if ($(window).width() >= 1024 ) {
            $('#left-efect').show();
            $('#right-efect').show();
        }
        else{
            $('#left-efect').hide();
            $('#right-efect').hide();
        }
	}
	
	var activePage = true;
	  
	$(window)
	    .focus(function() { activePage = true; })
	    .blur(function() { activePage = false; });

	var intervalo = setInterval(function(){		
   		if(activePage){
   			var prox;
   			var x = $('#x').val();
   			
   			if(x >= 5)
   				prox = 1;
   			else
   				prox = parseInt(x) + 1;
   			
   			var banner = "#banner" + prox;
   			
   			$('#banner'+x).fadeOut('fast');
   			$('#x').val(prox);	
   			
   			$('.clk-pg').removeClass('spg');
   			$('#n' + x).parent('li').removeClass('num-sel');
   			$('#n'+prox).parent('li').addClass('num-sel');
   			$('#n'+prox).addClass('spg');
   			
   			$(banner).fadeIn(700);
    	}
    }, 15000);
	
    //banner da página inicial
    $('.frmMaps').click(function () {	
    	var endereco = $('#fromAddress').val();
    	var action = $(this).attr('id');
    	$('#act-map').val(action);
    	$('#endereco-map').val(endereco);
    });
    
	//banner da página inicial
	$('.clk-pg').click(function () {	
		var id = $(this).attr("title");
		var banner = "#banner" + id;
		var x = $('#x').val();
		
		if($(this).attr('class')=='clk-pg')	
			$('.spg').removeClass('spg');
		
		if($(this).attr('class')!='clk-pg spg'){		
			clearInterval(intervalo);
			$('#banner'+x).fadeOut(2000);
			$('#x').val(id);	
			$('#n' + x).parent('li').removeClass('num-sel');
			$(this).parent('li').addClass('num-sel');
			$(banner).fadeIn(900);
			$(this).addClass('spg');
			intervalo = setInterval( 'slider()' , 15000 );
		}
	});
	
   	
   	var imgEmpresa = $('.bg-empresa img').height();
   	var altura = imgEmpresa * 0.387;
   	$('#conteudo-empresa').css({'margin-top' : '-' + altura + 'px'});
   	
   	//Mudança de abas na notícias view 
   	$(".menu-not-view ul li a").click(function(){   		
   		var rel = $(this).attr('rel');
   		if(rel=='1'){
   			$('.aba-view-1').show();
   			$('.aba-view-2').hide();
   			$('.sel-not-view').removeClass('sel-not-view');
   			$(this).parent('li').addClass('sel-not-view');
   		}   
   		else{
   			$('.aba-view-2').show();
   			$('.aba-view-1').hide();
   			$('.sel-not-view').removeClass('sel-not-view');
   			$(this).parent('li').addClass('sel-not-view');
   		}
   	});
   	
   	//Mudança de abas seriços
   	$(".menu-servico li").click(function(){
   		
   		var item_sel = $('.sel-servico').attr('value');
   		var item_click = $(this).attr('value');
   		
   		$(".menu-servico li").each(function(){
   	        $(this).removeClass("sel-servico");
   	     });
   		$(this).addClass('sel-servico');
   		
   		$("#aba" + item_sel).slideUp('fast');
   		$("#aba" + item_click).slideDown('fast');
   	});
   	
   	$('.envia_busca').click(function(){
   		this.form.submit();
   	});
	
      //amplia imagens na página de fotos
    	$(".mini-galeria a").click(function(){	
    		
    		$('.sel-img').hide();
    		$('.sel-img').removeClass('sel-img');
    		
    		var index = $(this).attr('rel');
    		$('.img'+index).addClass('sel-img');
    		$('.img'+index).fadeIn('slow');
    		
    		var x = $('.sel-item').find('a').attr('rel');
    		
			$('.li'+x).removeClass('sel-item');
			$('.li'+x).removeClass('li-sel'+x);
    		
    		$('.li'+index).addClass('li-sel'+index);
    		$('.li'+index).addClass('sel-item');
    		
    		return false;
    	});
    	
    	$("#pg01").click(function(){
    		$("#agd01").show();
    		$("#agd02").hide();
		});
    	
    	$("#pg02").click(function(){
    		$("#agd02").show();
    		$("#agd01").hide();
		});
    	
    	//exibe as informações das imagens
    	$(".img_destaque").hover(function(){
    		$(this).next(".texto_destaque").show();
    	} , function(){
    		$(this).next(".texto_destaque").hide();
    	});
    	
    	$(".texto_destaque").hover(function(){
    		$(this).show();
    	} , function(){
    		$(this).hide();
    	});
    	
    	//amplia imagens na página de fotos
    	$(".mini-galeria2 a").click(function(){	
    		$(".galeria-grande2").html();
    		var imagem = $(this).attr('href');
    		
    		$(".mini-galeria2 ul li").each(function(){     
    			$(this).removeClass('sel-item2');
			}); 
    		
    		$(this).parent('li').addClass('sel-item2');

    		$(".galeria-grande2").html('<img src="' + imagem + '" title="" alt="" />');
    		
    		return false;
    	}); 
    	
    	//ampliar imagem
    	$("ul.thumb li div").hover(function() {
    			$(this).css({'z-index' : '10'});
    			$(this).find('img').addClass("hover").stop()
    				.animate({
    					marginTop: '-10px', 
    					marginLeft: '-25px', 
    					top: '50%', 
    					left: '50%', 
    					width: '150px', 
    					height: '105px',
    					padding: '0' 
    				}, 200);
    			
    			} , function() {
    			$(this).css({'z-index' : '0'});
    			$(this).find('img').removeClass("hover").stop()
    				.animate({
    					marginTop: '0', 
    					marginLeft: '0',
    					/*top: '5px', 
    					left: '5px', */
    					width: '114px', 
    					height: '85px'
    					/*padding: '5px'*/
    				}, 400);
    		});
       
      //Tipo o valor default dos campos input
		$('.inputdefault').focus(function() {	
			if (this.value == this.defaultValue){ 
		    	this.value = '';
			}
			if(this.value != this.defaultValue){
				this.select();
			}
		});
		$('.inputdefault').blur(function() {
			if ($.trim(this.value) == ''){
		    	this.value = (this.defaultValue ? this.defaultValue : '');
			}
		});

	
	/*var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});*/
	
	//linka a div no menu principal
	$(".borda-men div").click(function(){	
		var link = $(this).next('a').attr('href');
		window.location = link;
	});
	
	//Efeito imagens do contato
	$(".hovercontato").hover(function(){
		$(this).find(".sombra-recado").slideDown('medium');
	},
	function()
	{
		$(this).find(".sombra-recado").slideUp('medium');
	});	
	
	//abre o box de listagem de estabelecimentos comercias (onde encontrar)
	$(".ul-cidades li div a").click(function(){	
		$('.box-lojas').addClass('carregando');
		
		//abre o box de escolha do tipo de estabelecimento
		$('.ul-opt-onde').show();
		$('.opt-sel').removeClass('opt-sel');
		$('#tipo_loja').addClass('opt-sel');
		
		var cidade = $(this).attr('href');
		
		$('.cid-sel').removeClass('cid-sel');
		$(this).parents('li').addClass('cid-sel');
		
		$('.carrega-lojas').load(link_site + 'ajax/lojas.php?cidade=' + cidade, function() {
			$('.box-lojas').removeClass('carregando');
		});
		
		return false;
	});
	
	//alterna entre loja e representante (onde encontrar)
	$(".ul-opt-onde ul li a").click(function(){	
		$('.box-lojas').addClass('carregando');
		
		var estab = $(this).attr('href');
		var cidade = $('.cid-sel div a').attr('href');
		
		$('.opt-sel').removeClass('opt-sel');
		$(this).parent('li').addClass('opt-sel');
		
		$('.carrega-lojas').load(link_site + 'ajax/lojas.php?cidade=' + cidade + '&estab=' + estab, function() {
			$('.box-lojas').removeClass('carregando');
		});
		
		return false;
	});
	
	//amplia imagens na página de notícias
	$(".tam-thumb").click(function(){	
		$(".img-view").html();
		var imagem = $(this).attr('href');
		$(".img-view").html('<img src="' + imagem + '" title="" alt="" />');

		return false;
	});
	
	/** Troca de input contato **/
	//campos de formulário de contato
	$('.field-contato').focus(function() {
		$(this).addClass('field-contato-hover');
	});
	$('.field-contato').blur(function() {
		$(this).removeClass('field-contato-hover');
	});
	
	/** Realiza validação de Campos **/
	//coloca a mascara nos campos numericos
	
	//funcao para verificar se o campo é digito
	function validaDigito(e){
						if((e.which!= '8' && e.which!='0') && (e.which<'48' || e.which>'57'))
						return false;
							}
	
	$("#celular").mask("(99)9999-9999");
	$("#telefone").mask("(99)9999-9999");
	$("#cnpj").mask("99.999.999/9999-99");
	$("#cep").mask("99999-999");
	$("#valor").maskMoney({symbol:"R$",decimal:".",thousands:""});
	$("#data").mask("99/99/9999");
	$("#hora").mask("99:99:99");
	$("#cpf").mask("999.999.999-99");
	
	///função para validar e-mail
	function checkMail(mail)
	{
    	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    	if(typeof(mail) == "string")
    	{
        	if(er.test(mail)) 
        		return true;
    	}
    	else if(typeof(mail) == "object")
    	{
        	if(er.test(mail.value))
        	{
                return true;
            }
    	}
    	else
       	 	return false;      
	};
	
	//Aceita apenas número
	$("#numero_festa").keypress(function(e) 
		{  
		return validaDigito(e);
		});

	// CADASTRO EM NEWSLETTER
	$("#voltar-promo").click(function()
	{
		$('.conf-promocoes').hide();
		$('.promocoes').show();
		$("#msg-promo").hide();
		$("#msg-ant").show();
		$("#frase2-cad").hide();
	});
	
	//FORMULÁRIO CADASTRO NEWSLETTER
	$("#frmPromocao").submit(function()
	{				
		if(!$("#nome-news").val() || $("#nome-news").val() == 'Nome')
		{
			$("#msg-news").show();
			$("#msg-news").text("Por favor informe seu nome");
			$("#nome-news").focus();
			return false;
		}
					
		if(!$("#email-news").val() || $("#email-news").val() == 'E-mail')
		{
			$("#msg-news").show();
			$("#msg-news").text("Por favor informe seu e-mail");
			$("#email-news").focus();
			return false;
		}
		
		if(!checkMail($("#email-news").val()))
		{
			$("#msg-news").show();
			$("#msg-news").text("E-mail inválido");
			$("#email-news").focus();
			return false;
		}
		
		$("#msg-news").hide();
		
		var nome = $("#nome-news",this).val();
		var email = $("#email-news",this).val();
		
		$.post(link_site + "ajax/envia_news.php",
		{
			nome: nome,
			email: email
		},
		function(data)
			{ 
				$("#nome-news").val('Nome');
				$("#email-news").val('E-mail');

				$('#news').hide();
				$('.conf-promocoes').show(); 			
			});
					
		return false;

	});
	$("#voltar-promo").click(function()
	{
		$('#news').show();
		$('.conf-promocoes').hide();
	});
	//FIM CADASTRO NEWSLETTER
	
	/**
	* Script para validação de formulário
	* A classe do formulário deve ser validateForm
	* 
	* - Para msg de erro em div deve ser colocado um atributo rel no formulário com o nome da id da div
	* - Para msg de erro em alert de ser colocado um atributo rel em branco no formulário
	* - Os campos que são obrigatórios devem ter a classe required e o title com a mensagem de erro
	*
	**/
	$('.validateForm').submit(function(){
		
		var campo = $(this).attr('class').replace("validateForm ","");
		
	    if(!makeValidate(campo)){
	    	return false;
	    }
	    else{
	    	return true;
	    }
	});
	
	//slide de imagens de plano de fundo
	
	
	//amplia imagens na página de fotos
	$(".imagem-mini a").click(function(){	
		$(".imagem-grande").html();
		var imagem = $(this).attr('href');

		$(".imagem-grande").html('<img src="' + imagem + '" title="" alt="" />');
		
		return false;
	});
	
	function makeValidate(campo){
		var erro = 0;
		var title = '';
		
		if(campo != "validateForm"){
			$('.required').each(function(){	 
				if($(this).attr('id') == 'email'){
					if(!checkMail($(this).val())){
						$(this).focus();
			            title = $(this).attr('title');
			            $("#" + campo).show('fast');
						$("#" + campo).text(title);
			            erro = 1;
			            return false;
			        }	
				}
				else{
					if(!$(this).val()){
						$(this).focus();
			            title = $(this).attr('title');
			            $("#" + campo).show('fast');
						$("#" + campo).text(title);
			            erro = 1;
			            return false;
			        }	
				}
		    });
		}
		else{
			$('.required').each(function(){	 
				if($(this).attr('id') == 'email'){
					if(!checkMail($(this).val())){
			            $(this).focus();
						title = $(this).attr('title');
			            alert(title);
			            erro = 1;
			            return false;
			        }	
				}
				else{
					if(!$(this).val()){
			            $(this).focus();
						title = $(this).attr('title');
			            alert(title);
			            erro = 1;
			            return false;
			        }	
				}
		    });
		}
		
	    if(erro == 0)
	    	return true;
	}
	
	$(".video").colorbox({iframe:true, innerWidth:640, innerHeight:390});
	
	//newsletter
	$(".newsletter-box").colorbox({iframe:true,innerWidth:510, innerHeight:305});
	
	//termos de uso
	$(".termos-uso").colorbox({iframe:true,innerWidth:600, innerHeight:450});
	
	//newsletter
	$(".img-news").colorbox({iframe:true,innerWidth:530, innerHeight:415});
	
	//como-chegar
	$(".como-chegar").colorbox({iframe:true,innerWidth:800, innerHeight:700});
});

//função do slider
function slider(){
	var prox;
	var x = $('#x').val();
	
	if(x >= 5)
		prox = 1;
	else
		prox = parseInt(x) + 1;
	
	var banner = "#banner" + prox;
	
	$('#banner'+x).fadeOut('fast');
	$('#x').val(prox);	
	
	$('.clk-pg').removeClass('spg');
	$('#n' + x).parent('li').removeClass('num-sel');
	$('#n'+prox).parent('li').addClass('num-sel');
	$('#n'+prox).addClass('spg');
	
	$(banner).fadeIn(700);
}

function Limpa(ObjId)
{
	ObjId.value = '';
}
