﻿var v_order = " order by inf_valvenda asc";
var pagina = 1;
var galeria_on = 1;

var mytime;

var v_fotos = new Array();
var index_foto = 0;

var v_referencia = 0;

function rawurldecode(str) {
    return unescape(str);
}

function rawurlencode(str) {
    return escape(str);
}


function utf8_decode(str_data) {
    // Converts a UTF-8 encoded string to ISO-8859-1    
    //   
    // version: 810.1317  
    // discuss at: http://phpjs.org/functions/utf8_decode  
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)  
    // +      input by: Aman Gupta  
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +   improved by: Norman "zEh" Fuchs  
    // +   bugfixed by: hitwork  
    // +   bugfixed by: Onno Marsman  
    // *     example 1: utf8_decode('Kevin van Zonneveld');  
    // *     returns 1: 'Kevin van Zonneveld'  
    var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;

    str_data += '';

    while (i < str_data.length) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);
            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

$(document).ready(function () {
    /*$(document).bind("keydown", function (event) {

        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 8 || keycode == 166) {
            return false;
        }

    });*/

    ArrDormitorios = $("#hdnDormitorios").val().split(',');
    for (i = 0; i < ArrDormitorios.length; i++) {
        set_dormitorio(ArrDormitorios[i]);
    }

    ArrSuite = $("#hdnSuites").val().split(',');
    for (i = 0; i < ArrSuite.length; i++) {
        set_suite(ArrSuite[i]);
    }

    ArrVaga = $("#hdnVagas").val().split(',');
    for (i = 0; i < ArrVaga.length; i++) {
        set_vaga(ArrVaga[i]);
    }


    ArrArea = $("#hdnArea").val().split(',');
    for (i = 0; i < ArrArea.length; i++) {
        switch ($.trim(ArrArea[i])) {
            case "60":
                set_area('1');
                break;
            case "100":
                set_area('2');
                break;
            case "200":
                set_area('3');
                break;
            case "300":
                set_area('4');
                break;
            case "400":
                set_area('5');
                break;
        }

    }

    atualiza_finalidade();


    $("#cboTipo").val("CASA");


    $("#aba_2").click(function () {
        $("#aba_1").attr('class', 'aba_1_sel');
        $("#aba_2").attr('class', 'aba_2');
        $("#miolo").hide();
        $("#miolo_descricao").show();
    });

    $("#aba_1").click(function () {
        $("#aba_2").attr('class', 'aba_2_sel');
        $("#aba_1").attr('class', 'aba_1');
        $("#miolo_descricao").hide();
        $("#miolo").show();
    });

    $("#tour").click(function () {
        if (typeof ($("#txt_previsite").val()) != "undefined") {
            $("#div_tour").html("<div style='float:right; margin-top: 5px; margin-right: 5px; text-decoration: underline; color:#FFF; cursor:pointer;' onclick='$(\"#div_tour\").hide();$(\"#div_tour\").html(\"<input type=text id=txt_previsite value=" + $("#txt_previsite").val() + " ></input>\");$(\"#fundo\").hide();'>Fechar(x)</div><iframe src='" + $("#txt_previsite").val() + "' width='675' height='450' scrolling='no' frameborder='0'></iframe>");
        }
        $("#fundo").show();
        $("#div_tour").show();
    });

    $("#indicar").click(function () {
        if (typeof ($("#txt_indicar").val()) != "undefined")
            $("#div_indique").html("<div style='float:right; margin-top: 5px; margin-right: 5px; text-decoration: underline; color:#000; cursor:pointer;' onclick='$(\"#div_indique\").hide();$(\"#fundo\").hide();'>Fechar(x)</div><iframe src='" + $("#txt_indicar").val() + "' width='415' height='320' scrolling='no' frameborder='0'></iframe>");

        $("#fundo").show();
        $("#div_indique").show();
    });

    $("#agendar").click(function () {
        if (typeof ($("#txt_agendar").val()) != "undefined")
            $("#div_agendar").html("<div style='float:right; margin-top: 5px; margin-right: 5px; text-decoration: underline; color:#000; cursor:pointer;' onclick='$(\"#div_agendar\").hide();$(\"#fundo\").hide();'>Fechar(x)</div><iframe src='" + $("#txt_agendar").val() + "' width='410' height='345' scrolling='no' frameborder='0'></iframe>");
        $("#fundo").show();
        $("#div_agendar").show();
    });

    $("#ligue_gratis").click(function () {
        $("#div_ligue").html("<div style='float:right; margin-top: 5px; margin-right: 5px; text-decoration: underline; color:#000; cursor:pointer;' onclick='$(\"#div_ligue\").hide();$(\"#div_ligue\").html(\"\");$(\"#fundo\").hide();'>Fechar(x)</div><iframe src='http://webservices.blap.com.br/liguegratis.aspx?id=551938018525&cid=10005' width='430' height='378' scrolling='no' frameborder='0'></iframe>");
        $("#fundo").show();
        $("#div_ligue").show();
    });


    $("#add_favoritos").click(function () {
        $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "set_favoritos", pkimovel: $("#pkimovel").val() }, function (response) {
            $("#add_favoritos").hide();
            $("#rem_favoritos").show();
        });
    });

    $("#rem_favoritos").click(function () {
        $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "del_favoritos", pkimovel: $("#pkimovel").val() }, function (response) {
            $("#add_favoritos").show();
            $("#rem_favoritos").hide();
        });
    });

    IsChildOf = function (parent, child) {
        var res = false;
        if (child != null) {
            while (child.parentNode) {
                if ((child = child.parentNode) == parent) {
                    res = true; break;
                }
            }
        }
        return res;
    };

    FixOnMouseOut = function (element, event, callback) {
        var e = { _target: (event.toElement) ? event.toElement : event.relatedTarget };
        var current_mouse_target = e._target;
        if (!IsChildOf(element, current_mouse_target) && element != current_mouse_target) {
            callback.apply(this);
        }
    };

    //$("#teste_movimento").mouseout(function () {
    //minimiza_controles();
    //});

    $("#teste_movimento").mouseover(function () {
        maximiza_controles();
    });


    $("#cboFinalidade").change(function () {
        atualiza_finalidade();
    });

    $("#cboSubtipo").change(function () {
        atualiza_cidade();
    });


    $("#DropDownList2").change(function () {
        atualiza_bairros();
        atualiza_tipo();
    });

    $("#cboTipo").change(function () {
        pesquisar();
        switch ($.trim($("#cboTipo").val().toUpperCase())) {
            case "CASA":
                $("#tipo_area").html("construída");
                break;
            case "TERRENO":
                $("#tipo_area").html("do terreno");
                break;
            case "APARTAMENTO":
                $("#tipo_area").html("útil");
                break;
            case "SALA":
                $("#tipo_area").html("útil");
                break;
            case "SALÃO":
                $("#tipo_area").html("útil");
                break;
            case "ESCRITÓRIO":
                $("#tipo_area").html("útil");
                break;
            case "BARRACÃO":
                $("#tipo_area").html("total");
                break;
            case "GALPÃO":
                $("#tipo_area").html("total");
                break;
            case "PRÉDIO":
                $("#tipo_area").html("total");
                break;
            case "ÁREA":
                $("#tipo_area").html("total");
                break;
            case "SÍTIO":
                $("#tipo_area").html("total");
                break;
            case "HARAS":
                $("#tipo_area").html("total");
                break;
            case "CHÁCARA":
                $("#tipo_area").html("total");
                break;
        }
    });

    $("#cboValMin").change(function () {
        switch ($("#cboFinalidade").val()) {
            case "alugar":
                $("#hdnMinimo").val($(this).val());
                break;
            case "comprar":
                $("#hdnMinimoVenda").val($(this).val());
                break;
            case "temporada":
                $("#hdnMinimo").val($(this).val());
                break;
        }
        pesquisar();
    });

    $("#cboValMax").change(function () {
        switch ($("#cboFinalidade").val()) {
            case "alugar":
                $("#hdnMaximo").val($(this).val());
                break;
            case "comprar":
                $("#hdnMaximoVenda").val($(this).val());
                break;
            case "temporada":
                $("#hdnMaximo").val($(this).val());
                break;
        }
        pesquisar();
    });
});


function atualiza_finalidade() {
    atualiza_cidade();
    switch ($("#cboFinalidade").val()) {
        case "alugar":
            $("#cboValMin > option").remove();
            $("#cboValMin").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMinimo").val()) == "1")
                $("#cboValMin").append($("<option Selected></option>").val("1").html("1 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("1").html("1 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "2")
                $("#cboValMin").append($("<option Selected></option>").val("2").html("2 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("2").html("2 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "3")
                $("#cboValMin").append($("<option Selected></option>").val("3").html("3 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("3").html("3 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "5")
                $("#cboValMin").append($("<option Selected></option>").val("5").html("5 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("5").html("5 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "10")
                $("#cboValMin").append($("<option Selected></option>").val("10").html("10 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("10").html("10 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "15")
                $("#cboValMin").append($("<option Selected></option>").val("15").html("15 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("15").html("15 Mil"));



            $("#cboValMax > option").remove();
            $("#cboValMax").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMaximo").val()) == "1")
                $("#cboValMax").append($("<option Selected></option>").val("1").html("1 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("1").html("1 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "2")
                $("#cboValMax").append($("<option Selected></option>").val("2").html("2 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("2").html("2 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "3")
                $("#cboValMax").append($("<option Selected></option>").val("3").html("3 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("3").html("3 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "5")
                $("#cboValMax").append($("<option Selected></option>").val("5").html("5 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("5").html("5 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "10")
                $("#cboValMax").append($("<option Selected></option>").val("10").html("10 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("10").html("10 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "15")
                $("#cboValMax").append($("<option Selected></option>").val("15").html("15 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("15").html("14 Mil"));
            if (parseInt($.trim($("#hdnMaximo").val())) > 15)
                $("#cboValMax").append($("<option Selected></option>").val(">15").html(">15 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val(">15").html(">15 Mil"));

            break;
        case "comprar":
            $("#cboValMin > option").remove();
            $("#cboValMin").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMinimoVenda").val()) == "100")
                $("#cboValMin").append($("<option Selected></option>").val("100").html("100 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("100").html("100 Mil"));

            if ($.trim($("#hdnMinimoVenda").val()) == "150")
                $("#cboValMin").append($("<option Selected></option>").val("150").html("150 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("150").html("150 Mil"));

            if ($.trim($("#hdnMinimoVenda").val()) == "200")
                $("#cboValMin").append($("<option Selected></option>").val("200").html("200 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("200").html("200 Mil"));

            if ($.trim($("#hdnMinimoVenda").val()) == "250")
                $("#cboValMin").append($("<option Selected></option>").val("250").html("250 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("250").html("250 Mil"));

            if ($.trim($("#hdnMinimoVenda").val()) == "500")
                $("#cboValMin").append($("<option Selected></option>").val("500").html("500 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("500").html("500 Mil"));

            if ($.trim($("#hdnMinimoVenda").val()) == "1000")
                $("#cboValMin").append($("<option Selected></option>").val("1000").html("1000 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("1000").html("1 Milhão"));


            $("#cboValMax > option").remove();
            $("#cboValMax").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMaximoVenda").val()) == "100")
                $("#cboValMax").append($("<option Selected></option>").val("100").html("100 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("100").html("100 Mil"));
            if ($.trim($("#hdnMaximoVenda").val()) == "150")
                $("#cboValMax").append($("<option Selected></option>").val("150").html("150 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("150").html("150 Mil"));
            if ($.trim($("#hdnMaximoVenda").val()) == "200")
                $("#cboValMax").append($("<option Selected></option>").val("200").html("200 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("200").html("200 Mil"));
            if ($.trim($("#hdnMaximoVenda").val()) == "250")
                $("#cboValMax").append($("<option Selected></option>").val("250").html("250 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("250").html("250 Mil"));
            if ($.trim($("#hdnMaximoVenda").val()) == "500")
                $("#cboValMax").append($("<option Selected></option>").val("500").html("500 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("500").html("500 Mil"));
            if ($.trim($("#hdnMaximoVenda").val()) == "1000")
                $("#cboValMax").append($("<option Selected></option>").val("1000").html("1 Milhão"));
            else
                $("#cboValMax").append($("<option></option>").val("1000").html("1 Milhão"));
                
            if (parseInt($.trim($("#hdnMaximoVenda").val())) > 1000)
                $("#cboValMax").append($("<option Selected></option>").val(">1000").html("Acima de 1 Milhão"));
            else
                $("#cboValMax").append($("<option></option>").val(">1000").html("Acima de 1 Milhão"));        
                
            break;
        case "temporada":
            $("#cboValMin > option").remove();
            $("#cboValMin").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMinimo").val()) == "1")
                $("#cboValMin").append($("<option Selected></option>").val("1").html("1 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("1").html("1 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "2")
                $("#cboValMin").append($("<option Selected></option>").val("2").html("2 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("2").html("2 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "3")
                $("#cboValMin").append($("<option Selected></option>").val("3").html("3 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("3").html("3 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "5")
                $("#cboValMin").append($("<option Selected></option>").val("5").html("5 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("5").html("5 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "10")
                $("#cboValMin").append($("<option Selected></option>").val("10").html("10 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("10").html("10 Mil"));

            if ($.trim($("#hdnMinimo").val()) == "15")
                $("#cboValMin").append($("<option Selected></option>").val("15").html("15 Mil"));
            else
                $("#cboValMin").append($("<option></option>").val("15").html("15 Mil"));


            $("#hdnMinimo").val('');

            $("#cboValMax > option").remove();
            $("#cboValMax").append($("<option></option>").val("TODOS").html("TODOS"));
            if ($.trim($("#hdnMaximo").val()) == "1")
                $("#cboValMax").append($("<option Selected></option>").val("1").html("1 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("1").html("1 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "2")
                $("#cboValMax").append($("<option Selected></option>").val("2").html("2 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("2").html("2 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "3")
                $("#cboValMax").append($("<option Selected></option>").val("3").html("3 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("3").html("3 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "5")
                $("#cboValMax").append($("<option Selected></option>").val("5").html("5 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("5").html("5 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "10")
                $("#cboValMax").append($("<option Selected></option>").val("10").html("10 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("10").html("10 Mil"));
            if ($.trim($("#hdnMaximo").val()) == "15")
                $("#cboValMax").append($("<option Selected></option>").val("15").html("15 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val("15").html("14 Mil"));
            if (parseInt($.trim($("#hdnMaximo").val())) > 15)
                $("#cboValMax").append($("<option Selected></option>").val(">15").html(">15 Mil"));
            else
                $("#cboValMax").append($("<option></option>").val(">15").html(">15 Mil"));

            $("#hdnMaximo").val('');            
            break;
    }
}

function atua_ficha(v_pkimovel) {
    parent.window.location = "http://dimarzio.insourceit.com.br/Ficha.aspx?imovel=" + v_pkimovel;   
}


function set_dormitorio(id) {
    if ($("#dormitorios_" + id).attr("class") == "selecione") {    
        $("#dormitorios_" + id).attr("class", "selecione_sel");
    }
    else {        
        $("#dormitorios_" + id).attr("class", "selecione");
    }   
}

function set_suite(id) {
    if ($("#suites_" + id).attr("class") == "selecione") {
        $("#suites_" + id).attr("class", "selecione_sel");
    }
    else {
        $("#suites_" + id).attr("class", "selecione");
    }
}

function set_vaga(id) {
    if ($("#vagas_" + id).attr("class") == "selecione") {
        $("#vagas_" + id).attr("class", "selecione_sel");
    }
    else {
        $("#vagas_" + id).attr("class", "selecione");
    }
}

function set_area(id) {
    if ($("#area_" + id).attr("class") == "selecione") {
        $("#area_" + id).attr("class", "selecione_sel");
    }
    else {
        $("#area_" + id).attr("class", "selecione");
    }
}

function pesquisar() {
    var exclusividade = $("#exclusividade").val();

    //$('#resultados').html('Carregando');
    v_finalidade = $("#cboFinalidade").val();
    switch (v_finalidade) {
        case "comprar":
            v_finalidade = " and inf_venda = 1 ";
            break;
        case "alugar":
            v_finalidade = " and inf_locacao = 1 ";
            break;
        case "temporada":
            v_finalidade = " and inf_temporada = 1 ";
            break;
    }

    var v_padra = "";

    if (exclusividade == "1") { v_padra = " and inf_padra = 'ALTO' ";  }

    //   alert('finalidade -->' + v_finalidade);

    v_subtipo = " and inf_subtipo = '" + $("#cboSubtipo").val() + "' ";
    //alert(v_subtipo);

    v_cidade = $("#DropDownList2").val();
    v_cidade_ficha = "";
    v_uf_ficha = "";
    if (v_cidade != "TODAS") {
        a_cidade = v_cidade.split("-");
        v_cidade_ficha = $.trim(a_cidade[0]);
        v_uf_ficha = $.trim(a_cidade[1]);
        v_cidade = " and inf_cidade = '" + v_cidade_ficha + "' and inf_uf = '" + v_uf_ficha + "' ";
    }
    else {
        v_cidade = "";
    }     
    //alert('cidade -->' + v_cidade);    

    v_bairros = "";
    v_bairros_ficha = "";
    $(".chkbairro").each(function () {
        if ($(this).attr("checked")) {
            if ($.trim($(this).val()) == "TODOS") {
                return false;
            }
            else {
                if (v_bairros == "") {
                    v_bairros = " and inf_bairro in ('" + $.trim($(this).val()) + "'";
                    v_bairros_ficha = $.trim($(this).val()).replace(/ /gi, '+');
                }
                else {
                    v_bairros += ", '" + $.trim($(this).val()) + "'";
                    v_bairros_ficha += "," + $.trim($(this).val()).replace(/ /gi, '+');
                }
            }
        }
    });
    if (v_bairros != "")
        v_bairros += ")";
    //alert('bairros -->' + v_bairros_ficha);
    //alert('bairros -->' + v_bairros);
 
    v_tipo = $("#cboTipo").val();
    if (v_tipo != "")
        v_tipo = " and inf_tipo = '" + $.trim(v_tipo) + "' ";
    //   alert('tipo -->' + v_tipo);

    switch ($.trim($("#cboFinalidade").val())) {
        case "alugar":
            v_val_min = $("#cboValMin").val();
            switch (v_val_min) {
                case "TODOS":
                    v_val_min = "";
                    break;
                case "1":
                    v_val_min = " and inf_vallocacao > = 1000 ";
                    break;
                case "2":
                    v_val_min = " and inf_vallocacao > = 2000 ";
                    break;
                case "3":
                    v_val_min = " and inf_vallocacao > = 3000 ";
                    break;
                case "5":
                    v_val_min = " and inf_vallocacao > = 5000 ";
                    break;
                case "10":
                    v_val_min = " and inf_vallocacao > = 10000 ";
                    break;
                case "15":
                    v_val_min = " and inf_vallocacao > = 15000 ";
                    break;
            }
            //alert('minimo -->' + v_val_min);

            v_val_max = $("#cboValMax").val();
            switch (v_val_max) {
                case "TODOS":
                    v_val_max = "";
                    break;
                case "1":
                    v_val_max = " and inf_vallocacao < = 1000 ";
                    break;
                case "2":
                    v_val_max = " and inf_vallocacao < = 2000 ";
                    break;
                case "3":
                    v_val_max = " and inf_vallocacao < = 3000 ";
                    break;
                case "5":
                    v_val_max = " and inf_vallocacao < = 5000 ";
                    break;
                case "10":
                    v_val_max = " and inf_vallocacao < = 10000 ";
                    break;
                case "15":
                    v_val_max = " and inf_vallocacao < = 15000 ";
                    break;
                case ">15":
                    v_val_max = " and inf_vallocacao > 15000 ";
                    break;
            }
            //    alert('maximo -->' + v_val_max);
            break;
        case "comprar":
            v_val_min = $("#cboValMin").val();
            switch (v_val_min) {
                case "TODOS":
                    v_val_min = "";
                    break;
                case "100":
                    v_val_min = " and inf_valvenda > = 100000 ";
                    break;
                case "150":
                    v_val_min = " and inf_valvenda > = 150000 ";
                    break;
                case "200":
                    v_val_min = " and inf_valvenda > = 200000 ";
                    break;
                case "250":
                    v_val_min = " and inf_valvenda > = 250000 ";
                    break;
                case "500":
                    v_val_min = " and inf_valvenda > = 500000 ";
                    break;
                case "1000":
                    v_val_min = " and inf_valvenda > = 1000000 ";
                    break;
            }
            //alert('minimo -->' + v_val_min);

            v_val_max = $("#cboValMax").val();
            switch (v_val_max) {
                case "TODOS":
                    v_val_max = "";
                    break;
                case "100":
                    v_val_max = " and inf_valvenda < = 100000 ";
                    break;
                case "150":
                    v_val_max = " and inf_valvenda < = 150000 ";
                    break;
                case "200":
                    v_val_max = " and inf_valvenda < = 200000 ";
                    break;
                case "250":
                    v_val_max = " and inf_valvenda < = 250000 ";
                    break;
                case "500":
                    v_val_max = " and inf_valvenda < = 500000 ";
                    break;
                case "1000":
                    v_val_max = " and inf_valvenda < = 1000000 ";
                    break;
                case ">1000":
                    v_val_max = " and inf_valvenda > 1000000 ";
                    break;
            }
            //    alert('maximo -->' + v_val_max);
            break;
        case "temporada":
            v_val_min = $("#cboValMin").val();
            switch (v_val_min) {
                case "TODOS":
                    v_val_min = "";
                    break;
                case "1":
                    v_val_min = " and inf_vallocacao > = 1000 ";
                    break;
                case "2":
                    v_val_min = " and inf_vallocacao > = 2000 ";
                    break;
                case "3":
                    v_val_min = " and inf_vallocacao > = 3000 ";
                    break;
                case "5":
                    v_val_min = " and inf_vallocacao > = 5000 ";
                    break;
                case "10":
                    v_val_min = " and inf_vallocacao > = 10000 ";
                    break;
                case "15":
                    v_val_min = " and inf_vallocacao > = 15000 ";
                    break;
            }
            //alert('minimo -->' + v_val_min);

            v_val_max = $("#cboValMax").val();
            switch (v_val_max) {
                case "TODOS":
                    v_val_max = "";
                    break;
                case "1":
                    v_val_max = " and inf_vallocacao < = 1000 ";
                    break;
                case "2":
                    v_val_max = " and inf_vallocacao < = 2000 ";
                    break;
                case "3":
                    v_val_max = " and inf_vallocacao < = 3000 ";
                    break;
                case "5":
                    v_val_max = " and inf_vallocacao < = 5000 ";
                    break;
                case "10":
                    v_val_max = " and inf_vallocacao < = 10000 ";
                    break;
                case "15":
                    v_val_max = " and inf_vallocacao < = 15000 ";
                    break;
                case ">15":
                    v_val_max = " and inf_vallocacao > 15000 ";
                    break;
            }
            //    alert('maximo -->' + v_val_max);
            break;
    }

    v_dormitorios = "";
    v_dormitorios_ficha = "";
    if ($("#dormitorios_1").attr("class") == "selecione_sel") {
        if (v_dormitorios == "") {
            v_dormitorios = "and (det_dormitorios = 1";
            v_dormitorios_ficha += "1";
        }
        else {
            v_dormitorios += " or det_dormitorios = 1";
            v_dormitorios_ficha += ",1";
        }
    }
    if ($("#dormitorios_2").attr("class") == "selecione_sel") {
        if (v_dormitorios == "") {
            v_dormitorios = "and (det_dormitorios = 2";
            v_dormitorios_ficha += "2";
        }
        else {
            v_dormitorios += " or det_dormitorios = 2";
            v_dormitorios_ficha += ",2";
        }
    }
    if ($("#dormitorios_3").attr("class") == "selecione_sel") {
        if (v_dormitorios == "") {
            v_dormitorios = "and (det_dormitorios = 3";
            v_dormitorios_ficha += "3";
        }
        else {
            v_dormitorios += " or det_dormitorios = 3";
            v_dormitorios_ficha += ",3";
        }
    }
    if ($("#dormitorios_4").attr("class") == "selecione_sel") {
        if (v_dormitorios == "") {
            v_dormitorios = "and (det_dormitorios = 4";
            v_dormitorios_ficha += "4";
        }
        else {
            v_dormitorios += " or det_dormitorios = 4";
            v_dormitorios_ficha += ",4";
        }
    }
    if ($("#dormitorios_5").attr("class") == "selecione_sel") {
        if (v_dormitorios == "") {
            v_dormitorios = "and (det_dormitorios >= 5";
            v_dormitorios_ficha += "5";
        }
        else {
            v_dormitorios += " or det_dormitorios >= 5";
            v_dormitorios_ficha += ",5";
        }
    }

    if (v_dormitorios != "")
        v_dormitorios += ") ";


    v_suites = "";
    v_suites_ficha = "";
    if ($("#suites_1").attr("class") == "selecione_sel") {
        if (v_suites == "") {
            v_suites = "and (det_Suite = 1";
            v_suites_ficha += "1";
        }
        else {
            v_suites += " or det_Suite = 1";
            v_suites_ficha += ",1";
        }
    }
    if ($("#suites_2").attr("class") == "selecione_sel") {
        if (v_suites == "") {
            v_suites = "and (det_Suite = 2";
            v_suites_ficha += "2";
        }
        else {
            v_suites += " or det_Suite = 2";
            v_suites_ficha += ",2";
        }
    }
    if ($("#suites_3").attr("class") == "selecione_sel") {
        if (v_suites == "") {
            v_suites = "and (det_Suite = 3";
            v_suites_ficha += "3";
        }
        else {
            v_suites += " or det_Suite = 3";
            v_suites_ficha += ",3";
        }
    }
    if ($("#suites_4").attr("class") == "selecione_sel") {
        if (v_suites == "") {
            v_suites = "and (det_Suite = 4";
            v_suites_ficha += "4";
        }
        else {
            v_suites += " or det_Suite = 4";
            v_suites_ficha += ",4";
        }
    }
    if ($("#suites_5").attr("class") == "selecione_sel") {
        if (v_suites == "") {
            v_suites = "and (det_Suite >= 5";
            v_suites_ficha += "5";
        }
        else {
            v_suites += " or det_Suite >= 5";
            v_suites_ficha += ",5";
        }
    }

    if (v_suites != "") {
        v_suites += ") ";
    }

    v_vagas = "";
    v_vagas_ficha = "";
    if ($("#vagas_1").attr("class") == "selecione_sel") {
        if (v_vagas == "") {
            v_vagas = "and (det_Garagens = 1";
            v_vagas_ficha += "1";
        }
        else {
            v_vagas += " or det_Garagens = 1";
            v_vagas_ficha += ",1";
        }
    }
    if ($("#vagas_2").attr("class") == "selecione_sel") {
        if (v_vagas == "") {
            v_vagas = "and (det_Garagens = 2";
            v_vagas_ficha += "2";
        }
        else {
            v_vagas += " or det_Garagens = 2";
            v_vagas_ficha += ",2";
        }
    }
    if ($("#vagas_3").attr("class") == "selecione_sel") {
        if (v_vagas == "") {
            v_vagas = "and (det_Garagens = 3";
            v_vagas_ficha += "3";
        }
        else {
            v_vagas += " or det_Garagens = 3";
            v_vagas_ficha += ",3";
        } 
    }
    if ($("#vagas_4").attr("class") == "selecione_sel") {
        if (v_vagas == "") {
            v_vagas = "and (det_Garagens = 4";
            v_vagas_ficha += "4";
        }
        else {
            v_vagas += " or det_Garagens = 4";
            v_vagas_ficha += ",4";
        } 
    }
    if ($("#vagas_5").attr("class") == "selecione_sel") {
        if (v_vagas == "") {
            v_vagas = "and (det_Garagens >= 5";
            v_vagas_ficha += "5";
        }
        else {
            v_vagas += " or det_Garagens >= 5";
            v_vagas_ficha += ",5";
        }
    }

    if (v_vagas != "")
        v_vagas += ") ";

    v_area = "";
    v_area_ficha = ""; 
    switch ($.trim($("#cboTipo").val().toUpperCase())) {
        case "CASA":
            v_campo = "det_AreaConst";
            break;
        case "TERRENO":
            v_campo = "det_AreaTerreno";
            break;
        case "APARTAMENTO":
            v_campo = "det_AreaUtil";
            break;
        case "SALA":
            v_campo = "det_AreaUtil";
            break;
        case "SALÃO":
            v_campo = "det_AreaUtil";
            break;
        case "ESCRITÓRIO":
            v_campo = "det_AreaUtil";
            break;
        case "BARRACÃO":
            v_campo = "det_AreaTotal";
            break;
        case "GALPÃO":
            v_campo = "det_AreaTotal";
            break;
        case "PRÉDIO":
            v_campo = "det_AreaTotal";
            break;
        case "ÁREA":
            v_campo = "det_AreaTotal";
            break;
        case "SÍTIO":
            v_campo = "det_AreaTotal";
            break;
        case "HARAS":
            v_campo = "det_AreaTotal";
            break;
        case "CHÁCARA":
            v_campo = "det_AreaTotal";
            break;
    }
    
    if ($("#area_1").attr("class") == "selecione_sel") {
        if (v_area == "") {
            v_area = "and (" + v_campo + " <= 60";
            v_area_ficha += "60";
        }
        else {
            v_area += " or " + v_campo + " <= 60";
            v_area_ficha += ",60";
        } 
    }
    if ($("#area_2").attr("class") == "selecione_sel") {
        if (v_area == "") {
            v_area = "and (" + v_campo + " <= 100";
            v_area_ficha += "100";
        }
        else {
            v_area += " or " + v_campo + " <= 100";
            v_area_ficha += ",100";
        } 
    }
    if ($("#area_3").attr("class") == "selecione_sel") {
        if (v_area == "") {
            v_area = "and (" + v_campo + " <= 200";
            v_area_ficha += "200";
        }
        else {
            v_area += " or " + v_campo + " <= 200";
            v_area_ficha += ",200";
        } 
    }
    if ($("#area_4").attr("class") == "selecione_sel") {
        if (v_area == "") {
            v_area = "and (" + v_campo + " <= 300 ";
            v_area_ficha += "300";
        }
        else {
            v_area += " or " + v_campo + " <= 300";
            v_area_ficha += ",300";
        } 
    }
    if ($("#area_5").attr("class") == "selecione_sel") {
        if (v_area == "") {
            v_area = "and (" + v_campo + " >= 301";
            v_area_ficha += "301";
        }
        else {
            v_area += " or " + v_campo + " >= 301";
            v_area_ficha += ",301";
        } 
    }
    if (v_area != "")
        v_area += ") ";         

    
    if ($("#chkCondominio").attr("checked"))
        v_condominio = " and inf_condominio = 1";
    else
        v_condominio = "";
    //   alert('condominio -->' + v_condominio);        
 
    if ($("#chkTour").attr("checked"))
        v_tour = " and tour = 1 and idprevisite <> '' and lkprevisite <> '' ";
    else
        v_tour = "";
    //  alert('tour -->' + v_tour);

    //      alert($("#fkempresa").val() + ' - ' +       v_finalidade + ' - ' +       v_subtipo + ' - ' +       v_cidade + ' - ' +       v_bairros + ' - ' +       v_tipo + ' - ' +       v_val_min + ' - ' +       v_val_max + ' - ' +       v_dormitorios + ' - ' +       v_suites + ' - ' +       v_vagas + ' - ' +       v_area + ' - ' +       v_condominio + ' - ' +       v_tour + ' - ' +       v_order + ' - ' +       pagina + ' - ' +       $("#txt_referencia").val() + ' - ' +       $("#cboTipo").val() + ' - ' +       $("#hdnFavorito").val())


    $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "pesquisar", v_finalidade: v_finalidade, v_subtipo: v_subtipo, v_cidade: v_cidade, v_cidade_ficha: v_cidade_ficha, v_uf_ficha: v_uf_ficha, v_bairros: v_bairros, v_bairros_ficha: v_bairros_ficha, v_tipo: v_tipo, v_val_min: v_val_min, v_val_max: v_val_max, v_dormitorios: v_dormitorios, v_dormitorios_ficha: v_dormitorios_ficha, v_suites: v_suites + v_padra, v_vagas: v_vagas, v_area: v_area, v_condominio: v_condominio, v_tour: v_tour, v_order: v_order, v_pagina: pagina, v_referencia: $("#txt_referencia").val(), tipo: $("#cboTipo").val(), recentes: $("#hdnRecente").val(), favoritos: $("#hdnFavorito").val(), pks: $("#hdnPks").val(), v_suites_ficha: v_suites_ficha, v_vagas_ficha: v_vagas_ficha, v_area_ficha: v_area_ficha }, function (response) {
        eval(response);
        $("#txt_referencia").val('');
        $("#hdnRecente").val('');
        $("#hdnFavorito").val('');
        $("#hdnPks").val('');
    });
}

function ordenacao(id) {
    $(".odrdem_sel").attr("class", "odrdem");
    $("#ordem_" + id).attr("class", "odrdem_sel");

    switch ($.trim($("#cboTipo").val().toUpperCase())) {
        case "CASA":
            v_campo = "det_AreaConst";
            break;
        case "TERRENO":
            v_campo = "det_AreaTerreno";
            break;
        case "APARTAMENTO":
            v_campo = "det_AreaUtil";
            break;
        case "SALA":
            v_campo = "det_AreaUtil";
            break;
        case "SALÃO":
            v_campo = "det_AreaUtil";
            break;
        case "ESCRITÓRIO":
            v_campo = "det_AreaUtil";
            break;
        case "BARRACÃO":
            v_campo = "det_AreaTotal";
            break;
        case "GALPÃO":
            v_campo = "det_AreaTotal";
            break;
        case "PRÉDIO":
            v_campo = "det_AreaTotal";
            break;
        case "ÁREA":
            v_campo = "det_AreaTotal";
            break;
        case "SÍTIO":
            v_campo = "det_AreaTotal";
            break;
        case "HARAS":
            v_campo = "det_AreaTotal";
            break;
        case "CHÁCARA":
            v_campo = "det_AreaTotal";
            break;
    }    
    
    switch (id) {
        case "1":
            v_order = " order by inf_valvenda desc";
            break;
        case "2":
            v_order = " order by inf_valvenda asc";
            break;
        case "3":
            v_order = " order by " + v_campo + " desc";
            break;
        case "4":
            v_order = " order by " + v_campo + " asc";
            break;
        case "5":
            v_order = " order by site_visitas desc";
            break;
    }
    pesquisar();
}

function atualiza_cidade() {
    $("#DropDownList2 > option").remove();
    $("#DropDownList2").append($("<option></option>").val("2").html("Carregando..."));


    v_cidade = $("#hdnCidade").val();
    v_uf = $("#hdnUf").val();

    $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "finalidade", finalidade: $("#cboFinalidade").val(), subtipo: $("#cboSubtipo").val(), cidade: v_cidade, uf: v_uf }, function (response) {
        $("#DropDownList2 > option").remove();        
        if (response == "") {
            $("#DropDownList2").append($("<option></option>").val("").html("Nenhum imóvel para " + $("#cboFinalidade").val() + "..."));
            $('#lista_bairros').html('<center style="margin-top: 40px;">Selecione uma Cidade...</center>');
            $("#cboTipo > option").remove();
            $("#cboTipo").append($("<option></option>").val("").html("Nenhum imóvel para " + $("#cboFinalidade").val() + "..."));
        }
        else {            
            eval(response);
            atualiza_bairros();            
        }
    });
}

function atualiza_bairros() {
    $('#lista_bairros').html('<center style="margin-top: 40px;">Carregando...</center>');
    $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "bairro", finalidade: $("#cboFinalidade").val(), subtipo: $("#cboSubtipo").val(), cidade: $("#DropDownList2").val() }, function (response) {
        eval(response);
        ArrBairros = $("#hdnBairros").val().split(',');
        for (i = 0; i < ArrBairros.length; i++) {
            $(".chkbairro").each(function () {
                if (ArrBairros[i].toUpperCase() == $(this).val().toUpperCase()) {
                    $(this).attr("checked", true);
                }
            });
        }        
        atualiza_tipo();
    });
}

function atualiza_tipo() {
    $("#cboTipo > option").remove();
    $("#cboTipo").append($("<option></option>").val("2").html("Carregando..."));
    $.post("http://busca-a.insourceit.com.br/processa.aspx", { fkempresa: $("#fkempresa").val(), action: "tipo", finalidade: $("#cboFinalidade").val(), subtipo: $("#cboSubtipo").val(), cidade: $("#DropDownList2").val(), padrao: $("#hdnTipo").val() }, function (response_tipo) {
        $("#hdnTipo").val("");
        $("#cboTipo > option").remove();
        if (response_tipo == "") {
            $("#cboTipo").append($("<option></option>").val("").html("Nenhum imóvel para " + $("#cboFinalidade").val() + "..."));
        }
        else {
            eval(response_tipo);            
            pesquisar();
        }
        switch ($.trim($("#cboTipo").val().toUpperCase())) {
            case "CASA":
                $("#tipo_area").html("construída");
                break;
            case "TERRENO":
                $("#tipo_area").html("do terreno");
                break;
            case "APARTAMENTO":
                $("#tipo_area").html("útil");
                break;
            case "SALA":
                $("#tipo_area").html("útil");
                break;
            case "SALÃO":
                $("#tipo_area").html("útil");
                break;
            case "ESCRITÓRIO":
                $("#tipo_area").html("útil");
                break;
            case "BARRACÃO":
                $("#tipo_area").html("total");
                break;
            case "GALPÃO":
                $("#tipo_area").html("total");
                break;
            case "PRÉDIO":
                $("#tipo_area").html("total");
                break;
            case "ÁREA":
                $("#tipo_area").html("total");
                break;
            case "SÍTIO":
                $("#tipo_area").html("total");
                break;
            case "HARAS":
                $("#tipo_area").html("total");
                break;
            case "CHÁCARA":
                $("#tipo_area").html("total");
                break;
        }

    });
}

function pag_ant() {
    $("#seta_prox").show();    
    $(".pagina_sel").attr("class", "pagina");
    pagina -= 1;
    $("#pagina_" + pagina).attr("class", "pagina_sel");
    if (pagina == 1)
        $("#seta_ant").hide();
    pesquisar();        
}

function pag_prox() {
    $("#seta_ant").show();
    $(".pagina_sel").attr("class", "pagina");
    pagina += 1;
    $("#pagina_" + pagina).attr("class", "pagina_sel");
    if (pagina == 7)
        $("#seta_prox").hide();
    pesquisar();  
}

function seleciona_pagina(id) {
    $(".pagina_sel").attr("class", "pagina");
    pagina = id;
    $("#pagina_" + pagina).attr("class", "pagina_sel");
    
    if (pagina == 1) {
        $("#seta_ant").hide();
        $("#seta_prox").show();
    }
    else if (pagina == 7) {
        $("#seta_prox").hide();
        $("#seta_ant").show();
    }
    else {
        $("#seta_ant").show();
        $("#seta_prox").show();
    }
    pesquisar();
}

function marcar_todos() {
    $(".chkbairro").attr("checked", true);
    pesquisar();
}

function desmarcar_todos() {
    $(".chkbairro").attr("checked", false);
    pesquisar();
}


function aumenta_foto(v_contador, url) {
    index_foto = parseInt(v_contador);
    $(".fotos").hide();
    //$("#foto_1").attr("class", "foto_minia_sel");        
	//$("#foto_1").css({'height':'0px', 'width': '0px', 'margin-top': '0px', 'margin-left':'0px'});						
    //$("#foto_1").show();
    $(".foto_atual").html("<img src=\""+url+"\" width=\"424\" height=\"282\">");
    $(".foto_atual").show();
    //$("#foto_1").animate({ height: '280px', 'width': "150px", 'margin-top': '0px', 'margin-left': '0px' }, function () { });    
}

function minimiza_controles() {
    $("#controle_galeria").animate({ 'marginTop': '70px', 'height': '0px' }, function () {
        $("#controles").hide();
    });
}

function maximiza_controles() {
    $("#controle_galeria").animate({ 'marginTop': '0px', 'height': '70px' }, function () {
        $("#controles").show();
    });
}

function show_galeria() {  
    $(".foto_atual").hide();
    $(".fotos").show();
    galeria_pause();    
}


function galeria_preview() {
    galeria_on = 0;
    $("#ply_pause").hide();
    $("#ply_play").show();    
    index_foto -= 1; 
    if (index_foto < 0) {
        index_foto = (parseInt(v_fotos.length) - 1);
    }
    $(".foto_atual").html("<img src=\"" + v_fotos[index_foto] + "\" width=\"424\" height=\"282\">");   
}

function galeria_next() {
    galeria_on = 0;
    $("#ply_pause").hide();
    $("#ply_play").show();
    index_foto += 1;
    if (index_foto == v_fotos.length) {
        index_foto = 0;
    }
    $(".foto_atual").html("<img src=\"" + v_fotos[index_foto] + "\" width=\"424\" height=\"282\">");
}

function galeria_play() {
    galeria_on = 1;
    $("#ply_play").hide();        
    $("#ply_pause").show();    
}

function galeria_pause() {
    galeria_on = 0;
    $("#ply_pause").hide();
    $("#ply_play").show();    
}


function galeria(id) {
    var i = 0;
    index_foto = 0;    
    v_fotos.length = 0;    
    
    $(".fotos_" + id).each(function () {
        v_fotos[i] = $(this).val();
        i += 1;
    });
    clearTimeout(mytime);
    roda_galeria();
}

function roda_galeria() {
    if (galeria_on == 1) {
        if (index_foto == v_fotos.length) {
            index_foto = 0;
        }
        $(".foto_atual").html("<img src=\"" + v_fotos[index_foto] + "\" width=\"424\" height=\"282\">");
        index_foto += 1;
    }
    mytime = setTimeout('roda_galeria();', 15000);
}

function desabilita_postback() {
    if (window.event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
    }
}
