jQuery(document).ready(
	function() 
	{
		/*
		 * Carregamento inicial automático das localizações
		 */
		$("#estados").load('/ajax/localizacoes.php?ini=true&tipo=estados&valor=BRA&c=' + Math.random());
		$("#cidades").load('/ajax/localizacoes.php?ini=true&tipo=cidades&valor=SC&c=' + Math.random());
		$("#bairros").load('/ajax/localizacoes.php?ini=true&tipo=bairros&valor=Balne%E1rio%20Cambori%FA&c=' + Math.random());
		$("#categorias").load('/ajax/categorias.php?ini=true&cidade=Balne%E1rio%20Cambori%FA&bairro=Centro&c=' + Math.random());
		/*
		 * Quando as localizações são mudadas
		 */
		$('#estados').change(
			function()
			{
				var valor = $(this).val();
				$("#cidades").html("<option>Carregando...</option>");
				$("#cidades").load('/ajax/localizacoes.php?ini=false&tipo=cidades&valor='+valor+'&c=' + Math.random());
				$("#bairros").html("<option> Qualquer Bairro </option>");
			}
		);
		$('#cidades').change(
				function()
				{
					var valor = escape($(this).val());
					$("#bairros").html("<option>Carregando...</option>");
					$("#bairros").load('/ajax/localizacoes.php?ini=false&tipo=bairros&valor='+valor+'&c=' + Math.random());
					var cidade = escape($(this).val());
					$("#categorias").html("<option>Carregando...</option>");
					$("#categorias").load('/ajax/categorias.php?ini=false&cidade='+cidade+'&bairro=false&c=' + Math.random());
				}
		);
		$('#bairros').change(
				function()
				{
					var bairro = escape($(this).val());
					var cidade = escape($("#cidades").val());
					$("#categorias").html("<option>Carregando...</option>");
					$("#categorias").load('/ajax/categorias.php?ini=false&cidade='+cidade+'&bairro='+bairro+'&c=' + Math.random());
				}
		);
		/*
		 * Demais campos dinamicos
		 */
		$("#categorias").change(
			function()
			{		
				if($(this).val() == '1' || 
						$(this).val() == '2' || 
						 	$(this).val() == '3' || 
						 		$(this).val() == '6') 
				{
					$("#fake1").hide();
					$("#fake2").hide();
					$("#suites").show();
					$("#dormitorios").show();
				} else {
					$("#fake1").show();
					$("#fake2").show();					
					$("#suites").hide();
					$("#dormitorios").hide();
				}
				
			}
		);
		
		$('#mascara').css('opacity', 0.6);
		
		$('a.interesse').click(
			function(e) {
				e.preventDefault();	
				
				var alturaMascara = $('#container').height() + $('#footer').height() + 12;							
				
				$('#mascara').css('height', alturaMascara);
				
				var referencia = $(this).attr('id');
				var acao = $(this).attr('title');
				
				if(acao == 'Remover') {
					$(this).html('<img src="/img/botao_salvar_interesse.jpg" />');
					$(this).attr('title', 'Inserir');
				} 
				
				if(acao == 'Inserir') {
					$(this).html('<img src="/img/botao_remover_interesse.jpg" />');
					$(this).attr('title', 'Remover');
				}
				
				$('#mascara').show();
				$('.jqmWindow .auxiliar').html('Aguarde...');
				$('.jqmWindow').show();
				$('.jqmWindow .auxiliar').load('/ajax/interesse.php?acao=' + acao + '&ref=' + referencia + '&c=' + Math.random());
				
				$('#painelInteresses').load('/ajax/quantidadeInteresses.php?c=' + Math.random());				
			}
		);
		$('a.quantidadeInteresse').click(
				function(e) {
					e.preventDefault();	
					
					var alturaMascara = $('#container').height() + $('#footer').height() + 12;							
					
					$('#mascara').css('height', alturaMascara);
					
					var referencia = $(this).attr('id');
					var acao = $(this).attr('title');
					
					
					$('#mascara').show();
					$('.jqmWindow .auxiliar').html('Aguarde...');
					$('.jqmWindow').show();
					$('.jqmWindow .auxiliar').load('/ajax/interesse.php?acao=' + acao + '&ref=' + referencia + '&c=' + Math.random());
												
				}
			);		
		$('.close').click(
			function() {
				$('#mascara').fadeOut("slow");
				$('.jqmWindow').hide();
			}
		);		
	}
);


function Limpar(valor, validos) {
	  var result = "";
	  var aux;
	  for ( var i = 0; i < valor.length; i++) {
	    aux = validos.indexOf(valor.substring(i, i + 1));
	    if (aux >= 0) {
	      result += aux;
	    }
	  }
	  return result;
	}
	function Formata(campo, tammax, teclapres, decimal) {
	  var tecla = teclapres.keyCode;
	  vr = Limpar(campo.value, "0123456789");
	  tam = vr.length;
	  dec = decimal;
	  if (tam < tammax && tecla != 8) {
	    tam = vr.length + 1;
	  }
	  if (tecla == 8) {
	    tam = tam - 1;
	  }
	  if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
	    if (tam <= dec) {
	      campo.value = vr;
	    }
	    if ((tam > dec) && (tam <= 5)) {
	      campo.value = vr.substr(0, tam - 2) + ","
	      + vr.substr(tam - dec, tam);
	    }
	    if ((tam >= 6) && (tam <= 8)) {
	      campo.value = vr.substr(0, tam - 5) + "." + vr.substr(tam - 5, 3)
	      + "," + vr.substr(tam - dec, tam);
	    }
	    if ((tam >= 9) && (tam <= 11)) {
	      campo.value = vr.substr(0, tam - 8) + "." + vr.substr(tam - 8, 3)
	      + "." + vr.substr(tam - 5, 3) + ","
	      + vr.substr(tam - dec, tam);
	    }
	    if ((tam >= 12) && (tam <= 14)) {
	      campo.value = vr.substr(0, tam - 11) + "." + vr.substr(tam - 11, 3)
	      + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3)
	      + "," + vr.substr(tam - dec, tam);
	    }
	    if ((tam >= 15) && (tam <= 17)) {
	      campo.value = vr.substr(0, tam - 14) + "." + vr.substr(tam - 14, 3)
	      + "." + vr.substr(tam - 11, 3) + "."
	      + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + ","
	      + vr.substr(tam - 2, tam);
	    }
	  }
	}
/*
 * IE-CONFLICT/BEGIN
 * Resolve problemas de fixed position para o IE
 * http://dev.jdenny.co.uk/css/ie_fixed.html
 * 
 *  Para resolver os problemas com a posição
 *  top: expression(eval(document.body.scrollTop)+10);
 *  
 *  Para resolver os problemas com a imagem de fundo
 *  background-position: expression((calculateBgX(this))+"px "+(calculateBgY(this))+"px");
 */
function calculateBgX(oElement) {
	return document.body.scrollLeft - getOffsetLeft(oElement);
}
function calculateBgY(oElement) {
	return document.body.scrollTop - getOffsetTop(oElement);
}
 
function getOffsetTop(oElement) {
	var iResult= oElement.offsetTop;
	while (oElement.offsetParent) {
		oElement = oElement.offsetParent;
		iResult += oElement.offsetTop;
	}
	return iResult;
}
function getOffsetLeft(oElement) {
	var iResult= oElement.offsetLeft;
	while (oElement.offsetParent) {
		oElement = oElement.offsetParent;
		iResult += oElement.offsetLeft;
	}
	return iResult;
}
/*
 * IE-CONFLICT/END
 */

	