$(document).ready(function() {
													 
	$('div.pngFix').pngFix();
	
	atual = 0;
	ultima = $("div[id^=foto-produto-]").length-1;
	
	$("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("ajax-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();
			}
		});
		
	});
	
	// PEGAR DADOS CEP
	$("#cep").blur(function () {
		cep_pesquisa = $("#cep").val();
		browser = navigator.userAgent;
		if(cep_pesquisa.length >= 9) {
			$("#carregando").show();
			
			if(browser.indexOf("MSIE 6.0") > 0) {
				$("#sexo").hide();
				$("#uf").hide();
			}
			
			$.ajax({  
				type: "GET",  
				url: "ver-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!");
						} else {
							//if(cidade_xml == "Cascavel"){
							  // alert("Cadastros - Compras de Cascavel, favor entrar em contato com a Fábrica: (45) 3224-3833");
					     	//} else {
								$("#endereco").val(tipo_logradouro_xml+' '+logradouro_xml);
								$("#bairro").val(bairro_xml);
								$("#cidade").val(cidade_xml);
								$("#uf").val(uf_xml);
							
								$("#num").focus();
							//}
							
							
						}
						$("#carregando").hide();
						
						if(browser.indexOf("MSIE 6.0") > 0) {
							$("#sexo").show();
							$("#uf").show();
						}
					});
				}
			});
		}
	});
	
	$("#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);
	});
	
	if(ultima > 0) {
		$("#bt-prox").fadeIn("normal");
	}
	
});

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>');
}


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

function trocaFoto(metodo) {
	
	$("#bt-prox").fadeIn();
	$("#bt-ant").fadeIn();
	
	if(metodo == 'prox') {
		$("#foto-produto-"+atual).fadeOut();
		atual++;
		$("#foto-produto-"+atual).fadeIn();
		$("#foto-conta").html(atual+1);
	} else {
		$("#foto-produto-"+atual).fadeOut();
		atual--;
		$("#foto-produto-"+atual).fadeIn();
		$("#foto-conta").html(atual+1);
	}
	if(atual <= 0) { $("#bt-ant").hide(); atual = 0; }
	if(atual >= ultima) { $("#bt-prox").hide(); atual = ultima; }
	
	$("#bt-ampliar").removeAttr("href");
	$("#bt-ampliar").attr("href", $("#src-foto-produto-"+atual).val());
}
