// Função explode (similar ao php);
function explode(delimiter,string,limit){var emptyArray={0:''};if(arguments.length<2||typeof arguments[0]=='undefined'||typeof arguments[1]=='undefined'){return null;}
if(delimiter===''||delimiter===false||delimiter===null){return false;}
if(typeof delimiter=='function'||typeof delimiter=='object'||typeof string=='function'||typeof string=='object'){return emptyArray;}
if(delimiter===true){delimiter='1';}
if(!limit){return string.toString().split(delimiter.toString());}else{var splitted=string.toString().split(delimiter.toString());var partA=splitted.splice(0,limit-1);var partB=splitted.join(delimiter.toString());partA.push(partB);return partA;}
}

// Função number format (similar ao php);
function number_format(number,decimals,dec_point,thousands_sep){number=(number+'').replace(',','').replace(' ','');var n=!isFinite(+number)?0:+number,prec=!isFinite(+decimals)?0:Math.abs(decimals),sep=(typeof thousands_sep==='undefined')?',':thousands_sep,dec=(typeof dec_point==='undefined')?'.':dec_point,s='',toFixedFix=function(n,prec){var k=Math.pow(10,prec);return''+Math.round(n*k)/k;};s=(prec?toFixedFix(n,prec):''+Math.round(n)).split('.');if(s[0].length>3){s[0]=s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,sep);}
if((s[1]||'').length<prec){s[1]=s[1]||'';s[1]+=new Array(prec-s[1].length+1).join('0');}
return s.join(dec);}





var url = 'http://www.centralaviamentos.com.br/loja';


$(document).ready(function(){
	//envia form pagseguro
	$("#form_pagseguro").submit();
	
	// Chamada no plugin nivoSlider
	$("#slider").nivoSlider();
	
	// quebra de linha na listagem de produtos
	$(".list-geral-produtos li:nth-child(4n)").addClass('last');
	
	//$(".quebra:nth(4n)").addClass('clear');
	
	// Logo Inovar
	$("#logo-inovar").hover(function(){
		$('#circ').stop(true, true).animate({
		  marginRight: "10px"
		},100,
		function(){
			$("#texto").stop(true, true).slideToggle(200);
		});
	}, function(){
		$("#texto").stop(true, true).slideToggle(200);
		$('#circ').stop(true, true).animate({
		  marginRight: "0px"
		},100);
	});

	// Jquery Zoom
	$('.jqzoom').jqzoom({
		zoomType: 'standard',
		lens:true,
		preloadImages: false,
		alwaysOn:false,
		preloadText:'Carregando...',
	});
	
	// Correção da lista de thumbnails na página interna de produtos
	$("ul.thumb-prod li:nth-child(3n)").addClass('last');
	
	
	// Todas as Categorias
	$(".cat-all").stop(true, true).hover(function(){
		$(".submenu").stop(true, true).slideToggle('fast');
	}, 
	function(){
		$(".submenu").stop(true, true).slideToggle('fast');
	});
	
	// Boxes com alturas iguais
	$(".list-geral-produtos li").equalHeights(320);
	
	//atualiza valor do produto com variações
	$(".variacao").change(function(){
		var valor=0.00;
		var aux;
		
		 $(".variacao").each(function (i) {
			 
			 if(this.value==''){
				valor = valor + 0; 
			 }
			 else
			 {
				aux = explode(':', this.value);			
				valor = valor + parseFloat(aux[2]); 
				
			 }
			
        });
		valor = valor + parseFloat($("#price").attr("value"));
		$("#valor").val(number_format(valor,2,',','.'));
	});

//validação do cadastro
$("#frm_cadastro").validate({
	rules:{ 
		nome:{ 
			required: true,
		},
		fone:{ 
			required: true,
			minlength: 14,
		},
		cel:{ 
			required: true,
			minlength: 14,
		},
		email: {
			required: true,
			email: true,
		},
		senha: {
			required: true,
			minlength: 6,
		},
		senha2:{
			required: true,
		},
		cep:{
			required: true,
		},
		endereco:{
			required: true,
		},
		numero:{
			required: true,
		},
		complemento:{
			required: true,
		},
		bairro:{
			required: true,
		},
		cidade:{
			required: true,
		},
		estado:{
			required: true,
		},
	},

});	


//validação do contato
$("#frm_contato").validate({
	rules:{ 
		nome:{ 
			required: true,
		},
		fone:{ 
			required: true,
			minlength: 14,
		},
		email: {
			required: true,
			email: true,
		},
		assunto:{
			required: true,
		},
		mensagem:{
			required: true,
		},
	},

});	


//busca frete
	$(".cep, .frete").change(function(){
		var vl_cep;
		var tipo_frete;
		var aux = new Array();
		
		
		$('input:radio[name=cep]').each(function() {
			if ($(this).is(':checked'))
			  aux = explode(':',$(this).val());
			  vl_cep = aux[0];
		})	
		
		$('input:radio[name=frete]').each(function() {
			if ($(this).is(':checked'))
			  tipo_frete = $(this).val();
		})
		
		var caminho_post = url+'/finalizar/calcula_frete';
		
		$("#vl_frete").html('');
		$("#loading").show();

		$.post(caminho_post, { cep: vl_cep, frete: tipo_frete },
			function(data) {
				var aux = explode(':',data);
				$("#loading").hide();
				$("#vl_frete").html(aux[0]);
				$("#vl_total").html(number_format(aux[1],2,',','.'));				
		});
					
	});
	
	/*##################
	# Box nova senha   #
	##################*/
	
	$(".forgot-pass").click(function(){
		$("#form-senha").slideToggle();
	});
	
	
});

