var objTipos = new Object([
	 {id: 2, name: "Apartamento", min:50000, max:1000000, dorm: true, vagas: true, area_util: true} 
	,{id: 5, name: "Área", min:10000, max:1000000, area_total: true}
	,{id: 6, name: "Barracao", min:50000, max:1000000, vagas: true, area_total: true}
	,{id: 7, name: "Casa", min:50000, max:1000000, dorm: true, vagas: true, area_construida: true}
	,{id: 8, name: "Chácara", min:50000, max:1000000, dorm: true, vagas: true, area_total: true}
	,{id: 11, name: "Fazenda", min:100000, max:5000000, area_total: true}
	,{id: 12, name: "Galp&atilde;o", min:50000, max:1000000, vagas: true, area_total: true}
	,{id: 14, name: "Haras", min:100000, max:5000000, area_total: true}
	,{id: 15, name: "Ponto", min:100000, max:3000000, vagas: true, area_util: true}
	,{id: 16, name: "Prédio", min:100000, max:5000000, area_construida: true}
	,{id: 17, name: "Sala", min:100000, max:3000000, vagas: true, area_util: true}
	,{id: 18, name: "Salao", min:100000, max:3000000, vagas: true, area_util: true}
	,{id: 19, name: "Sítio", min:50000, max:1000000, area_total: true}
	,{id: 20, name: "Terreno", min:5000, max:1000000, area_total: true}	
	,{id: 22, name: "Kitnet", min:20000, max:500000, dorm: true, vagas: true, area_util: true, area_total: false}	
	,{id: 23, name: "Flat", min:50000, max:3000000, dorm: true, vagas: true, area_util: true, area_total: false}	
	,{id: 24, name: "Cobertura", min:100000, max:2000000, dorm: true, vagas: true, area_util: true, area_total: false}
	,{id: 25, name: "Sobrado", min:50000, max:1000000, dorm: true, vagas: true, area_construida: true}
	,{id: 26, name: "Loja", min:100000, max:3000000, vagas: true, area_util: true}	
	,{id: 27, name: "Conjunto", min:100000, max:3000000, dorm: false, vagas: true, area_construida: true}		
]);

$(function(){

		$("#frmBusca").submit(function(){
		    if($("#cidade").val() != "")
		        return(true);
            else
                return(false);
        });
		
		$("#cidade").change(function(){
		    if($("#cidade").val() != ""){
		        $("#ddlBairro").attr("disabled",false);
		        $("#btnEnviar").attr("disabled",false);
			    $("#btnEnviar").attr("class", "enabled");
			}
            else{
                $("#ddlBairro").attr("disabled",true);
                $("#btnEnviar").attr("disabled",true);
                $("#btnEnviar").attr("class", "disabled");
            }
            updateBairros("");
		}).change();
		
		$("#ddlBairro").click(function(){
		    link = $("#lnkBairro").attr("href");
		    $("#lnkBairro").attr("href", link + "?idCidade=" + $("#cidade").val() + "&idBairros=" + $("#idBairros").val());
		    $("#lnkBairro").trigger("click");
		    $("#lnkBairro").attr("href", link);
		    $(this).blur();	    
		    return false;
		})
		
		//CHANGE DAS SELECTS
		$("#ddlPretensao, #ddlTipo").change(function(){buscaRapida()}).change();
		

});

function getObjTipo(number){
	number = number.substr(0,number.indexOf("|"));
	number = number.split(",")[0];
	for(i = 0; i < objTipos.length; i++){
		if(number == objTipos[i].id)
			return(objTipos[i]);
	}
}

function zeraTudo()
{
	$("#boxEncontreImovel select").each(function(){
		if($(this).attr("disabled") == true)
			$(this).val("");
	});
}

function sliderFormat(rotulo, value1, value2, objTo, hidden1, hidden2){	
	var valorMin = getObjTipo($("#ddlTipo").val()).min;
	var valorMax = getObjTipo($("#ddlTipo").val()).max;
	if($("#ddlPretensao").val() == 2){
		valorMin = valorMin  * 0.002;
		valorMax = valorMax * 0.002;
	}
	value1Format = value1.formatMoney(0,',','.');
	value2Format = value2.formatMoney(0,',','.');		
	//se a barra estiver no meio
	if(parseInt(value2) < valorMax && parseInt(value1) > valorMin){
		$(hidden1).val(value1Format);
		$(hidden2).val(value2Format);				
		objTo.html(rotulo + ': de <strong>' + value1Format + '</strong> &agrave; <strong>' + value2Format + "</strong>");
	//se a barra estiver nos extremos
	}else if(parseInt(value2) >= valorMax && parseInt(value1) <= valorMin){		
			$(hidden1).val("");
			$(hidden2).val("");
			objTo.html(rotulo + ': <strong>indiferente</strong>');	
	}else{
		//se a barra estiver no canto direito
		if(parseInt(value2) >= valorMax){
			$(hidden1).val(value1Format);
			$(hidden2).val("");
			objTo.html(rotulo + ': de <strong>' + value1Format + '</strong> &agrave; <strong>indiferente</strong>');
		//se a barra estiver no canto esquerdo			
		}else{
			$(hidden1).val("");
			$(hidden2).val(value2Format);
			objTo.html(rotulo + ': <strong>indiferente</strong> &agrave; <strong>' + value2Format + "</strong>");
		}
	}
	return(true);
}
function sliderFormatArea(valor){
	valor = parseInt(valor);
	if(valor > 0 && valor < 1000){
		$("#txtArea").attr("value",valor);
		valor = valor.formatMoney(0,",",".");
		
		$("#label-area").html("&Aacute;rea  m&iacute;nima: <strong>" + valor + 'm&sup2; </strong> (aprox.)');
	}else{
		$("#txtArea").attr("value","");
		$("#label-area").html("&Aacute;rea  m&iacute;nima: <strong>Indiferente</strong>");
	}
	return(true);
}

function buscaRapida(){
	//Desabilita todos campos da busca
	$("#boxEncontreImovel input, #boxEncontreImovel select").attr("disabled",true);
	
	//Menos o tipo e pretensão
	$("#ddlPretensao, #ddlTipo, #txtImb, #txtRef, #idBairros").attr("disabled",false);
	
	//Somente com o valor maximo e minimo
	$("#txtValorMin, #txtValorMax").parent().parent().parent().hide();
	$("#txtValorMin, #txtValorMax").removeAttr("disabled");
	$("#txtArea").parent().parent().parent().hide();
	
	
	$("#slider-range-valor").remove();
	$("#slider-range-area").remove();
	$("#label-valor").remove();
	$("#label-area").remove();
	
	if($("#ddlTipo").val() != ""){
		var valorMinMax = new Array();
		if($("#ddlPretensao").val() == 1)
			valorMinMax = Array(getObjTipo($("#ddlTipo").val()).min, getObjTipo($("#ddlTipo").val()).max);
		else
			valorMinMax = Array((getObjTipo($("#ddlTipo").val()).min * 0.002), (getObjTipo($("#ddlTipo").val()).max * 0.002));

		$("#cidade").attr("disabled",false);	
		$("#cidade").change();
			
		if(getObjTipo($("#ddlTipo").val()).dorm)
			$("#ddlDorms").removeAttr("disabled");
		else
			$("#ddlDorms").attr("disabled",true);
			
		if(getObjTipo($("#ddlTipo").val()).vagas)
			$("#ddlVagas").removeAttr("disabled");
		else
			$("#ddlVagas").attr("disabled",true);
		
		//slider de valor
		$("#txtValorMax").parent().parent().parent().after("<div id='slider-range-valor'>");
		$("#slider-range-valor").before("<div id='label-valor' class='slider-rotulo'>");
		$("#slider-range-valor").slider({
			range: true,
			animate: true,
			min: valorMinMax[0],
			max: valorMinMax[1],
			step: valorMinMax[0],
			values: [valorMinMax[0],valorMinMax[1]],
			slide: function(event, ui) {
				sliderFormat("Valor",ui.values[0],ui.values[1],$("#label-valor"),$("#txtValorMin"),$("#txtValorMax"));
			}
		});
		sliderFormat("Valor",$("#slider-range-valor").slider("values", 0),$("#slider-range-valor").slider("values", 1),$("#label-valor"),$("#txtValorMin"),$("#txtValorMax"));
		
		//slider de área
		$("#txtArea").parent().parent().parent().after("<div id='slider-range-area'>");
		$("#slider-range-area").before("<div id='label-area' class='slider-rotulo'>");
		
		$("#slider-range-area").slider({
			animate: true,
			range: "min",
			min: 0,
			max: 1000,
			step: 50,
			value: 0,
			slide: function(event, ui){
				sliderFormatArea(ui.value);
			}
		});
		
		sliderFormatArea($("#slider-range-area").slider("value"));
	}else{
		$("#ddlDorms").attr("disabled",true);
		$("#ddlVagas").attr("disabled",true);
		$("#txtValorMin").attr("disabled",true);
		$("#txtValorMax").attr("disabled",true);
		$("#boxEncontreImovel input:type=submit").attr("disabled",true);
		$("#boxEncontreImovel input:type=submit").attr("class", "disabled");
		$("#slider-range-valor").slider("disable");
		$("#slider-range-area").slider("disable");
	}
	zeraTudo();
	updateBairros("");
}
function updateBairros(strTitulo){
	if(strTitulo == "" || !strTitulo){
		$("#ddlBairro, #idBairros").empty();
		$("#ddlBairro").append("<option value=''>Selecione o bairro</option>");
	}else{
		var txt = new String();
		txt = strTitulo;
		if(txt.length > 18)
			txt = txt.substring(0, 18) + "...";
		else
			txt = txt.substr(0, txt.lastIndexOf(","));
		$("#ddlBairro").empty();
		$("#ddlBairro").append("<option value=''>" + txt + "</option>");
	}
}
