$(document).ready(function(){

	jQuery('a[rel*=facebox]').facebox() 
	 
	//MASKARA FORMS
	$('#fone,#con_telefone,#orc_telefone,#tel,#celular').mask("(99) 9999-9999");

	$('#cep').mask("99999-999");

	$('#cpf').mask("999.999.999-99");

	$('#datanasc').mask("99/99/9999");

	//FECHAR DIVS
	var div = $('.bt-fechar').attr('rel');
	$('.bt-fechar').click(function(){
		$(div).fadeOut(250);
	});	
	
	//	LINKS EXTERNOS
	$("a[@rel=externo]").attr('target','_blank');
	
	//	MENU
	$("#nav ul").css({display:"none"});
	$("#nav li").hover(
	function(){
			var ancor = $(this).find("a").attr('href');
			$(this).click(function(){ location.href = ancor; });
			$(this).addClass("hovered", 0);
			$(this).find('ul:first').css({visibility:"visible",display:"none"}).show(0);
		},
		function(){
			$(this).removeClass("hovered", 0);
			$(this).find('ul:first').hide(0);
	});
	
	$("#fk_departamento").change(function(){
		$("#form-search").submit();
	});
	
	//FALSH's
	$('#flash-times').flash({
	    src: 'swf/times.swf',
	    width: 637,
	    height: 279,
		wmode: 'transparent',
		expressInstall: true
	});
	$('#flash-personalisadas').flash({
	    src: 'swf/personalizadas.swf',
	    width: 637,
	    height: 279,
		wmode: 'transparent',
		expressInstall: true
	});	
	
	//mostra departamentos no campo de busca
	$("#fk_marca").change(function(){		
		$.ajax( {
			type: "POST",
			url: "incs/funcoes_jQuery.php",
			data: "acao=exibeCategoria&id=" + this.value,
			beforeSend: function() {
				// enquanto a função esta sendo processada...
				$('#fk_departamento').html('<option>Carregando...</option>');
			},
			success: function(txt) {
				// troca o conteúdo
				$('#fk_departamento').html(txt);
			},		
			error: function(txt) {
				// em caso de erro pode dar um alert('erro');
				alert('Erro: '+txt);
			}
		});
	});
	
	//	PROMOCOES DA HOME DO SITE
	$(".enqueteSalva").click(function(){
		var id = $('input:radio[name=enquete]:checked').val();
        if (id>0) {
            //alert(id);
		    $.ajax({
			    type: "POST",
			    url: "incs/funcoes_jQuery.php",
			    data: "acao=salvaEnquete&id=" + id,
			    beforeSend: function(){
				    // enquanto a função esta sendo processada...
				    $('#enquete').html('<img src="img/ajax-loader.gif" />');
			    },
			    success: function(txt){
				    // troca o conteúdo
				    $('#enquete').html(txt).hide().fadeIn(500);
			    },
			    error: function(txt){
				    // em caso de erro pode dar um alert('erro');
				    alert('Erro: '+txt);
			    }
		    });
        } else {
            alert('Selecione uma resposta.');
        }
	});
	
	$("#imagens a").click(function() {				
		$.ajax( {
			type: "POST",
			url: "incs/funcoes_jQuery.php",
			data: "acao=exibeFoto&id=" + this.id,
			beforeSend: function() {
				$('#img_grande').html('<img src="img/ajax-loader.gif">');
			},
			success: function(txt) {
				$('#img_grande').html(txt).hide().fadeIn(500);
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	});

	$("#bt-avise-produto").click(function() {						
		msg = "";
		flag = false;
		if(document.getElementById("avi_nome").value == "") {
			flag = true;
			msg += "Campo Seu nome não está preenchido.\n";
		}
		
		if(document.getElementById("avi_email").value == "") {
			flag = true;
			msg += "Campo Seu e-mail não está preenchido.\n";
		} else {
			if(!validaEmail(document.getElementById("avi_email").value)) {
				msg += "Campo Seu e-mail inválido.\n";
				flag = true;
			}
		}
		if(flag) {
			alert(msg);
		} else {
		
			$.ajax( {
				type: "POST",
				url: "incs/funcoes_jQuery.php",
				data: "acao=exibeAvise&fk_produto=" + document.getElementById("fk_produto").value + "&nome=" + document.getElementById("avi_nome").value + "&email=" + document.getElementById("avi_email").value,
				beforeSend: function() {
					$('#opac').html('<img src="img/ajax-loader.gif">');
				},
				success: function(txt) {
					$('#opac').html(txt);
					document.getElementById("avi_nome").value = "";
					document.getElementById("avi_email").value = "";
				},
				error: function(txt) {
					alert('Erro: '+txt);
				}
			});
		}
	});

});

function paginadorProduto(pag) {
	$.ajax({
		type: "POST",
		url: "incs/promocoes.php?pag=" + pag,		
		beforeSend: function(){
			$('#promocoes .alvo-ajax').html('<img src="img/ajax-loader.gif" />');
		},
		success: function(txt){
			$('#promocoes .alvo-ajax').html(txt).hide().fadeIn(500);
		},
		error: function(txt){
			alert('Erro: '+txt);
		}
	});	
}

//	VALIDACAO DOS FORMULORIOS
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj 	= document.getElementById(form).elements[i].id;
		var relObj 	= document.getElementById(form).elements[i].getAttribute('rel');
				
		if ((relObj == "email")) {
			expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
			if(!expressao.test(document.getElementById(form).elements[i].value)){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].value = "";
				document.getElementById(form).elements[i].className = "campo-requerido";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}else if (relObj == "required") {
			if((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].className = "campo-requerido";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

/**
  * Função que valida o e-mail
  * @param String $value e-mail
  * @return boolean
  **/
function validaEmail(value) {
	expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
	if(!expressao.test(value)) {
		return false;
	}
	return true;
}

function validarNews(quant_g) {	
	var msg = "";
	var flag = false;
	var cont_new = 0;

	if((document.getElementById("nome").value == "") || (document.getElementById("nome").value == "Nome")) {
		flag = true;
		msg += "Campo Nome não está preenchido.\n";
	}
	
	if((document.getElementById("email").value == "") || (document.getElementById("email").value == "E-mail")) {
		flag = true;
		msg += "Campo E-mail não está preenchido.\n";
	} else {
		if(!validaEmail(document.getElementById("email").value)) {
			msg += "Campo E-mail inválido.\n";
			flag = true;
		}
	}
	
	
	for(i = 1; i <= quant_g; i++) {				
		if(document.getElementById("fk_grupo_" + i).checked == true) {
			cont_new++;
		}
	}

	if(cont_new == 0) {
		msg += "Campo Grupo não está preenchido.\n";
		flag = true;
	}

	if(flag) {
		alert(msg);
	} else {
		document.getElementById("formNews").submit();
		document.getElementById("formNews").reset();
	}	
}

function votarCamisaPersonalizada(id) {
	$.ajax({
		type: "POST",
		url: "incs/funcoes_jQuery.php",
		data: "acao=votar&id=" + id,
		beforeSend: function(){
		},
		success: function(txt){
			//alert("Obrigado por participar da nossa promoção.");
			$('#votos' + id).html(txt);			
		},
		error: function(txt){
			alert('Erro: '+txt);
		}
	});	
}

function camisaPersonalizada(id) {
	$.ajax( {
		type: "POST",
		url: "incs/funcoes_jQuery.php",
		data: "incs/funcoes_jQuery.php?acao=personalize&id=" + id,
		beforeSend: function() {
			$("#div-personalize-sua-camisa").html("Carregando...");
		},
		success: function(txt) {
			$("#div-personalize-sua-camisa").html(txt).hide().fadeIn(500);			
		},
		error: function(txt){
			alert('Erro: '+txt);
		}
	});	
}