jQuery(
	function()
	{		
		$("#estados").load('/ajax/busca-2.0.php?tipo=estado&estado=0&c=' + Math.random());
		$("#cidades").load('/ajax/busca-2.0.php?tipo=cidade&cidade=0&c=' + Math.random());
		$("#bairros").load('/ajax/busca-2.0.php?tipo=bairro&bairro=0&c=' + Math.random());
		$("#categorias").load('/ajax/busca-2.0.php?tipo=categoria&categoria=0&c=' + Math.random());
		
		var estado = $('#estados').val();				
		var cidade = $('#cidades').val();				
		var bairro = $('#bairros').val();				
		var categoria = $('#categorias').val();
		
		$('#estados').change(
			function() {
				var estado = $('#estados').val();				
				$('#cidades').html('<option>Carregando...</option>');
				$('#cidades').load(
					'/ajax/busca-2.0.php?tipo=cidade&estado=' + estado 
					+ '&c=' + Math.random()
				);
				$('#bairros').html('<option value="0">Qualquer Bairro</option>');		
				$('#categorias').html('<option>Carregando...</option>');
				$('#categorias').load(
					'/ajax/busca-2.0.php?tipo=categoria&estado=' + estado 
					+ '&c=' + Math.random()
				);	
				$("#fake1").show();
				$("#fake2").show();					
				$("#fake3").show();					
				$("#suites").html('<option value="0"></option>').hide();
				$("#suitesMaster").html('<option value="0"></option>').hide();
				$("#dormitorios").html('<option value="0"></option>').hide();				
			}
		);
		$('#cidades').change(
				function() {
					var estado = $('#estados').val();				
					var cidade = $('#cidades').val();				
					$('#bairros').html('<option>Carregando...</option>');
					$('#bairros').load(
						'/ajax/busca-2.0.php?tipo=bairro&estado=' + estado 
						+ '&cidade=' + escape( cidade ) 
						+ '&c=' + Math.random()
					);
					$('#categorias').html('<option>Carregando...</option>');
					$('#categorias').load(
						'/ajax/busca-2.0.php?tipo=categoria&estado=' + estado 
						+ '&cidade=' + escape( cidade ) 
						+ '&c=' + Math.random()
					);		
					$("#fake1").show();
					$("#fake2").show();					
					$("#fake3").show();					
					$("#suites").html('<option value="0"></option>').hide();
					$("#suitesMaster").html('<option value="0"></option>').hide();
					$("#dormitorios").html('<option value="0"></option>').hide();					
				}
		);

		$('#bairros').change(
				function() {
					var estado = $('#estados').val();				
					var cidade = $('#cidades').val();				
					var bairro = $('#bairros').val();				
					$('#categorias').html('<option>Carregando...</option>');
					$('#categorias').load(
						'/ajax/busca-2.0.php?tipo=categoria&estado=' + estado 
						+ '&cidade=' + escape( cidade ) 
						+ '&bairro=' + escape( bairro ) 
						+ '&c=' + Math.random()
					);
					$("#fake1").show();
					$("#fake2").show();					
					$("#fake3").show();					
					$("#suites").html('<option value="0"></option>').hide();
					$("#suitesMaster").html('<option value="0"></option>').hide();
					$("#dormitorios").html('<option value="0"></option>').hide();					
				}
		);

		$("#categorias").change(
				function()
				{
					var estado = $('#estados').val();				
					var cidade = $('#cidades').val();				
					var bairro = $('#bairros').val();	
					var suite = $('#suites').val();	
					var suitemaster = $('#suitesMaster').val();	
					var categoria = $(this).val();	
					
					if ( $(this).val() == '1' || 
					     $(this).val() == '2' || 
						 $(this).val() == '3' || 
						 $(this).val() == '6') 
					{
						$("#fake1").hide();
						$("#fake2").hide();
						$("#fake3").hide();
						
						$("#suites").html('<option value="0">Aguarde...</option>').show();
						$("#suitesMaster").html('<option value="0">Aguarde...</option>').show();
						$("#dormitorios").html('<option value="0">Aguarde...</option>').show();
						
						$('#suites').load(
							'/ajax/busca-2.0.php?tipo=suite' 
							+ '&estado=' + escape( estado ) 
							+ '&cidade=' + escape( cidade ) 
							+ '&bairro=' + escape( bairro ) 
							+ '&categoria=' + escape( categoria ) 
							+ '&c=' + Math.random()
						);
						$('#suitesMaster').load(
								'/ajax/busca-2.0.php?tipo=suitemaster' 
								+ '&estado=' + escape( estado ) 
								+ '&cidade=' + escape( cidade ) 
								+ '&bairro=' + escape( bairro ) 
								+ '&categoria=' + escape( categoria )								 
								//+ '&suite=' + escape( suite )								 
								+ '&c=' + Math.random()
						);
						$('#dormitorios').load(
								'/ajax/busca-2.0.php?tipo=dormitorio' 
								+ '&estado=' + escape( estado ) 
								+ '&cidade=' + escape( cidade ) 
								+ '&bairro=' + escape( bairro ) 
								+ '&categoria=' + escape( categoria )
								//+ '&suite=' + escape( suite )
								+ '&c=' + Math.random()
						);						
						
					} else {
						$("#fake1").show();
						$("#fake2").show();					
						$("#fake3").show();					
						$("#suites").html('<option value="0"></option>').hide();
						$("#suitesMaster").html('<option value="0"></option>').hide();
						$("#dormitorios").html('<option value="0"></option>').hide();
					}
					
				}
			);		

			$("#suites").change(
				function()
				{
					var estado = $('#estados').val();				
					var cidade = $('#cidades').val();				
					var bairro = $('#bairros').val();	
					var suite = $('#suites').val();	
					var categoria = $('#categorias').val();					
					
					$("#suitesMaster").html('<option value="0">Aguarde...</option>').show();
					$("#dormitorios").html('<option value="0">Aguarde...</option>').show();
					
					$('#suitesMaster').load(
							'/ajax/busca-2.0.php?tipo=suitemaster' 
							+ '&estado=' + escape( estado ) 
							+ '&cidade=' + escape( cidade ) 
							+ '&bairro=' + escape( bairro ) 
							+ '&categoria=' + escape( categoria )								 
							+ '&suite=' + escape( suite )								 
							+ '&c=' + Math.random()
					);
					$('#dormitorios').load(
							'/ajax/busca-2.0.php?tipo=dormitorio' 
							+ '&estado=' + escape( estado ) 
							+ '&cidade=' + escape( cidade ) 
							+ '&bairro=' + escape( bairro ) 
							+ '&categoria=' + escape( categoria )
							+ '&suite=' + escape( suite )
							+ '&c=' + Math.random()
					);	
				}
			);	
			
			
			$("#suitesMaster").change(
					function()
					{
						var estado = $('#estados').val();				
						var cidade = $('#cidades').val();				
						var bairro = $('#bairros').val();	
						var suite = $('#suites').val();	
						var suitemaster = $('#suitesMaster').val();	
						var categoria = $('#categorias').val();					
						
						$("#dormitorios").html('<option value="0">Aguarde...</option>').show();
						
						$('#dormitorios').load(
								'/ajax/busca-2.0.php?tipo=dormitorio' 
								+ '&estado=' + escape( estado ) 
								+ '&cidade=' + escape( cidade ) 
								+ '&bairro=' + escape( bairro ) 
								+ '&categoria=' + escape( categoria )
								+ '&suite=' + escape( suite )
								+ '&suitemaster=' + escape( suitemaster )
								+ '&c=' + Math.random()
						);	
					}
			);		

		
			$('#palavraChave').click(
				function() {
					if( $(this).val() == 'Para o título, descrição e localização somente') {
						$(this).val("")
					}
				}
			);
			
			$('#palavraChave').blur(
					function() {
						if( $(this).val() == "") {
							$(this).val("Para o título, descrição e localização somente")
						}						
					}
			)
			$('#big-search-button').click(
					function() {
						if( 
							$('#palavraChave').val() == 'Para o título, descrição e localização somente' 
						  ) {
							$('#palavraChave').val("");
						}
					}
				);
							
			$('#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
	 */

		