var carregando = '<img src="./img/layout/carregando.gif" alt="carregando..." />';
function fecha(){
	$('#box-menu-produtos >li> ul').hide();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// FUNCOES PARA EXIBIR O CARREGANDO
function mostraCarregandoFora(elemento){
	mostraCarregando(elemento, true, false);
}
function escondeCarregandoFora(elemento){
	escondeCarregando(elemento, true, false);
}
function mostraCarregandoDentro(elemento){
	mostraCarregando(elemento, false, false);
}
function mostraCarregandoEmCima(elemento){
	mostraCarregando(elemento, false, true);
}
function mostraCarregando(elemento, fora, esconde){
	var margemCarregandoCima = parseInt(elemento.css('height').replace(/px/,'') / 2) - 8;
	var larguraElemento = parseInt(elemento.css('width').replace(/px/,'')) + 20;
	var margemCarregando = margemCarregandoCima+'px 0 0 0';
	
	// console.log('larguraElemento: '+larguraElemento);
	// console.log('margemCarregandoCima: '+margemCarregandoCima);
	// console.log('margemCarregando: '+margemCarregando);
	
	if(fora == true){
		elemento.css('width', larguraElemento);
	}
	if(esconde == true){
		elemento.hide();
		elemento = elemento.parent();
		elemento.css('text-align', 'center');
		margemCarregando = '0px auto';
	}
	elemento.append(carregando);
	elemento.children('img').hide();
	elemento.children('img').slideToggle();
	elemento.children('img').css({'float':'right', 'margin':margemCarregando});
}

function escondeCarregando(elemento, fora, esconde){
	var larguraElemento = parseInt(elemento.css('width').replace(/px/,'')) - 20;
	if(fora == true){
		elemento.css('width', larguraElemento);
	}
	elemento.children('img').remove();
}

// FUNCAO PARA EXIBIR A MENSAGEM DE ERRO COM EFEITO
function mostraMensagem(tipo, mensagem, elemento){
	elemento.removeClass();
	elemento.addClass('Esquerda');
	elemento.addClass('Mensagem'+tipo).html(mensagem).slideDown();
}




/*
 * funcao para validar cpf
 * add by jackson f. de a. mafra
 * convertida para jquery
 */
function remove(str){
	return str.replace(/\W/g,'');
}

function logar(msg){
	console.log(msg);
}

function validarCPF(cpf){
    var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
    if(!filtro.test(cpf)){
        return false;
    }
    cpf = remove(cpf);

    if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
        cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
        cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
        cpf == "88888888888" || cpf == "99999999999"){
        return false;
    }

    soma = 0;
    for(i = 0; i < 9; i++)
        soma += parseInt(cpf.charAt(i)) * (10 - i);
    resto = 11 - (soma % 11);
    if(resto == 10 || resto == 11)
        resto = 0;
    if(resto != parseInt(cpf.charAt(9))){
        return false;
    }
    soma = 0;
    for(i = 0; i < 10; i ++)
        soma += parseInt(cpf.charAt(i)) * (11 - i);
    resto = 11 - (soma % 11);
    if(resto == 10 || resto == 11)
        resto = 0;
    if(resto != parseInt(cpf.charAt(10))){
        return false;
    }
    return true;
}
$().ready(function() {
	var nc			= $(window).height() - 365;
	var $conteudo	= $("#conteudo");
	if($conteudo.height() < nc){
		$conteudo.css({
			'min-height': nc + 'px'
			});
		if($.browser.msie && $.browser.version < 7) $conteudo.css({
			'height': nc + 'px'
		});
	}
	$(":input + strong").hide();
	$("input").cleanField();

	$("#form-busca-site").submit(function(e){
		e.preventDefault();
		$(":input + strong", this).hide('slow');
		if($("input[name='termo']", this).val().length>0)
			window.location.href= siteURL + 'busca/'+ $("input[name='termo']", this).val().replace(/ /g, '-');
		else
			$("input[name='termo'] + strong", this).html('Preencha o campo!').show('slow');
	});


	$('#box-menu-produtos>li>h2').click(function() {
			var altura = $(this).next().css('height');
			$(this).next().animate({height: 'toggle', opacity: 'toggle'},300);


			if(altura == 'auto' || altura == '0px'){
				$(this).parent().addClass('Ativo');
			}else{
				$(this).parent().removeClass('Ativo');
			}
	});
	$("a.LinkExterno").click(function(){
		$(this).attr("target", "_blank");
	});
	/**
	 * funções paras o cadasro de newsletter...
	 */
	$('#form-news').submit(function(e){
		e.preventDefault();
		var nome = $('#news-nome').val();
		var email = $('#news-email').val();
		var valida_email = /^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[0-9a-zA-Z]{2,9}$/;
		var erro = false;
		var msg = '';
		if(nome.length < 3 || nome == 'Seu nome'){
			msg += 'Nome N&atilde;o informado!<br />';
			erro = true;
		}
		if(email.length == 0 || email == 'Seu e-mail'){
			msg += 'E-mail n&atilde;o informado!';
			erro = true;
		}else{
			if(!valida_email.test(email)){
				msg += 'E-mail inv&aacute;lido!';
				erro = true;
			}
		}
		if(!erro){
			$.post('./ajax',{nome:nome,email:email,acao:'form-news'},function(dados){
				$('#interacao').html(dados).fadeIn('slow');
			});
			//$('#form-news').resetForm();
		}else{
			$('#interacao').html(msg).fadeIn('slow');
		}
	});

	/**
	 * final das funções de newsletter
	 */

	 // SE TEM FLASH, CARREGA OS SWFs
 	if($().flash.hasFlash()){
		$('.carregaFlash').each(function(){
			var elemento = $(this).children();
			var swf = elemento.attr('src').replace('.jpg','.swf');
			var altura = elemento.attr('height');
			var largura = elemento.attr('width');
			// REMOVE O ELEMENTO
			elemento.remove();

			$(this).flash({
				src: swf,
				width: largura,
				height: altura,
				wmode: 'transparent',
				menu:true
			},{update: false});

		});
	}

	// LINKS COM rel="externo" ABREM EM NOVA PAGINA
	$('a[rel="externo"]').attr('target', '_blank');
});
/*
jQuery(document).bind('keydown', 'Alt+c',function (){window.location.href = siteURL + "carrinho/";});
jQuery(document).bind('keydown', 'Alt+l',function (){window.location.href = siteURL + "login/";});
jQuery(document).bind('keydown', 'Alt+s',function (){window.location.href = siteURL + "cadastro/";});
jQuery(document).bind('keydown', 'Alt+i',function (){window.location.href = siteURL + "home/";});
jQuery(document).bind('keydown', 'Alt+a',function (){window.location.href = siteURL + "ajuda/";});
jQuery(document).bind('keydown', 'Alt+d',function (){window.location.href = siteURL + "meus-dados/";});
jQuery(document).bind('keydown', 'Shift+e',function (){$.post(siteURL + "ajax/", { acao:"carrinho-esvaziar"},function(data){window.location.href = siteURL + "carrinho/";});});
*/

