$(document).ready(function() {
													 
	$('div.pngFix').pngFix();
	
	$("#txtCEPFrete").mask("99999-999");
	
	$("input[id^=quantidade_]").blur(function () {
		key = $(this).attr('id');
		key = key.replace("quantidade_", "");
		
		quantidade = $("#quantidade_"+key).val();
		
		produto = $("#produto_"+key).val();
		
		if(quantidade < 1) { quantidade = 1; $("#quantidade_"+key).val(quantidade); }
		
		$("#carregando").show();
		
		$.getJSON("atualiza-carrinho.php?produto="+produto+"&quantidade="+quantidade+"&key="+key,  
		function(result){
			if(result.max) {
				$("#quantidade_"+key).val(result.max);
				$("#sub_total_"+key).html(result.sub_total);
				$("#total_compra").html(result.total);
				
				$("#carregando-info").hide();
				$("#max-quantidade").html(result.max);
				$("#carregando-limite").show();
			} else {
				$("#sub_total_"+key).html(result.sub_total);
				$("#total_compra").html(result.total);
				
				$("#carregando").hide();
				
			}
			
		});
		
	});
	
	$("#cep").keyup(function () {
							  
			cep = $("#cep").val();
			if(cep.length == 5){
				$("#cep2").focus();
			}
	  });
	
	// PEGAR DADOS CEP
	$("#cep2").blur(function () {
		cep_pesquisa = $("#cep").val()+$("#cep2").val();
		
		if(cep_pesquisa.length == 8) {
			
			$.ajax({  
				type: "GET",  
				url: "web_cep.php?cep="+cep_pesquisa,  
				dataType: "xml", 
				success: function(xml) {  
					$(xml).find('webservicecep').each(function(){
						resultado_xml = $(this).find('resultado').text();
						uf_xml = $(this).find('uf').text();
						cidade_xml = $(this).find('cidade').text();
						bairro_xml = $(this).find('bairro').text();
						tipo_logradouro_xml = $(this).find('tipo_logradouro').text();
						logradouro_xml = $(this).find('logradouro').text();
						
						
						if((resultado_xml == '1') || resultado_xml == '2') {
							$("#endereco_xml").val(tipo_logradouro_xml+' '+logradouro_xml);
							$("#bairro_xml").val(bairro_xml);
							$("#cidade_xml").val(cidade_xml);
							$("#uf_xml").val(uf_xml);
							$("#control_xml").val(1);
							
						} else {
							alert("CEP NÃO ENCONTRADO!");
							$("#control_xml").val(0);
						}
						
					});
				}
			});
			
		} 
	});
	
	
	
	
	// PEGAR DADOS CEP TELA CADASTRO
	$("#postcode").blur(function () {
		cep_pesquisa = $("#postcode").val();
		$("#carregando").show();
		
			
			$.ajax({  
				type: "GET",  
				url: "web_cep.php?cep="+cep_pesquisa,  
				dataType: "xml", 
				success: function(xml) {  
					$(xml).find('webservicecep').each(function(){
						resultado_xml = $(this).find('resultado').text();
						uf_xml = $(this).find('uf').text();
						cidade_xml = $(this).find('cidade').text();
						bairro_xml = $(this).find('bairro').text();
						tipo_logradouro_xml = $(this).find('tipo_logradouro').text();
						logradouro_xml = $(this).find('logradouro').text();
						
						
						if(resultado_xml !== '1') {
							alert("CEP NÃO ENCONTRADO!");
							$("#street_address").val('');
							$("#suburb").val('');
							$("#city").val('');
							$("#state").val('');
							
						} else {
							$("#street_address").val(tipo_logradouro_xml+' '+logradouro_xml);
							$("#suburb").val(bairro_xml);
							$("#city").val(cidade_xml);
							$("#state").val(uf_xml);
							
						}
						$("#carregando").hide();
						
					});
				}
			});
			
		
	});

	$("#bt-ant").click(function () {
		trocaFoto('ant');
	});
	
	$("#bt-prox").click(function () {
		trocaFoto('prox');
	});
	
	$("#exibe-zoom").mouseover(function () {
		$("#bt-zoom").show();
	});
	
	$("#exibe-zoom").mouseout(function () {
		$("#bt-zoom").hide();
	});
	
	$("#exibe-zoom").click(function () {
		url = $("#src-foto-produto-"+atual).val();
		tb_show(null, url, false);
	});
	
});

function swf($arquivo,$largura,$altura,$bg,$wmode,$title){
	document.writeln('<object type="application/x-shockwave-flash" data="' + $arquivo + '" width="' + $largura + '" height="' + $altura + '" title="' + $title + '">');
	document.writeln('<param name="movie" value="' + $arquivo + '">');
	document.writeln('<param name="bgcolor" value="#' + $bg + '">');
	document.writeln('<param name="wmode" value="' + $wmode + '">');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('</object>');
}




// PEGAR DADOS CEP
function verificaCep(){
	    cep_pesquisa = $("#cep").val()+$("#cep2").val();
		
		if(cep_pesquisa.length == 8) {
			if($("#control_xml").val() == 1){
				return true;
			}else{
				return false;
			}
		} else {
			alert("CEP NÃO ENCONTRADO!");
			return false;
		}
	return true;
}

function fechaDialog() {
	$("#carregando-info").show();
	$("#carregando-limite").hide();
	
	$("#carregando").hide();
}



function abrirPopup(ID) {
	
window.open("popup_image.php?id="+ ID +" ","miniwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=50,left=10,width=779,height=600");

}



function abrirPagamento(FORM){
	
window.open('', 'pagseguro.php', 'height=500,width=600,scrollbars=yes');
FORM.target='pagseguro.php';
return true;

	
}


function calcularFrete(){
	
	CEP_DESTINO = $("#txtCEPFrete").val(); 
	if(CEP_DESTINO.length >= 8){
		  $().ajaxStart(function() { $('#loading_simulador').show();$('#cont').hide(); });
		  $("#cont").load("calcula.php",{cep_destino:CEP_DESTINO});
		  $().ajaxStop(function() { $('#loading_simulador').hide(); $('#cont').show();});
	} else {
	   alert('Preencha o CEP corretamente!');	
	}
  
	
}
