﻿$.carregarmaximoquartos = function(usaSessao) {
    var retorno = 4;
    if (usaSessao == null) usaSessao = true;
    $.exec("wsImoveis.asmx/ValorMaximoQuartos", "{usaSessao:'" + usaSessao + "'}", false, function(response) { retorno = response.d; }, function() { retorno = 4; });
    return retorno;
}
$.carregarvalormaximocondominio = function(usaSessao) {
    var retorno = 150;
    if (usaSessao == null) usaSessao = true;
    $.exec("wsImoveis.asmx/ValorMaximoCondominio", "{usaSessao:'" + usaSessao + "'}", false, function(response) { retorno = response.d; }, function() { retorno = 150; });
    return retorno;
}
$.carregarvalormaximo = function(usaSessao) {
    var retorno = 500;
    if (usaSessao == null) usaSessao = true;
    $.exec("wsImoveis.asmx/ValorMaximoImovel", "{usaSessao:'" + usaSessao + "'}", false, function(response) { retorno = response.d; }, function() { retorno = 500; });
    return retorno;
}
$.indicaimovel = function() {
    var idimovel = $('#idDetalheImovel').val();
    var email = true;
    var txt = 'E-mail:<br /><input type="text" id="popupEmail" name="popupEmail" value="" style="color:black; border: 1px solid; width: 100%" />';
    
    $.prompt(txt, { submit: function(v, m, f) {
        if (v) {
            if (f.popupEmail == '') email = false;
            if (email) $.exec("wsImoveis.asmx/Indicaimovel", "{id: '" + idimovel + "',email:'" + f.popupEmail + "'}", false);
        }
    }, buttons: { Ok: true, Cancel: false }, focus: 1
    });
    if (!email) $.show("Informe o e-mail.");
}
$.carregaimoveiscidade = function(idcidade, idestado) {
    if (idcidade == null) idcidade = $('#dpCidades').val();
    if (idestado == null) idestado = $('#dpEstados').val();
    $.exec("wsImoveis.asmx/listarimoveisporcidade", "{idcidade: '" + idcidade + "',idestado:'" + idestado + "'}", false, function() {
        $.montasliders(true);
    });
}
$.formataimovel = function(imovel) {
    var htmlPopup;
    var img = "";
    var imgQuad = "";
    var htmlOrg = "";
    var checkImovel = $.checaimovelnoroteiro(imovel.IdImovel);
    if (imovel.Galeria != null) if (imovel.Galeria.Fotos.length > 0) {
        img = "<img style=\"width:256px; height:192px\" border=\"0px\" src=\"admin/" + imovel.Galeria.Fotos[0].Caminhos.Pequeno + "\"/><br/>";
        imgQuad = "<img style=\"width:50px; height:50px; float:left; padding-right:10px\" border=\"0px\" src=\"admin/" + imovel.Galeria.Fotos[0].Caminhos.Quadrado + "\">"; ;
    }

    htmlOrg = imgQuad + "<b>" + imovel.Titulo + "</b><br/>" + imovel.Descricao +
                "<br/><br/><a href='detalhes.aspx?idImov=" + imovel.IdImovel + "' target='_blank'><img src='../img_site/bot_detalhes.gif' style='border:0px;' alt='Veja Detalhes deste Imóvel'/></a>";
    if (!checkImovel) htmlOrg += "<span id=\"spanID_" + imovel.IdImovel + "\"><a onclick=\"$.adicionarnoroteiro('" + imovel.IdImovel + "');\"><img src='../img_site/bot_adicionar.gif' style='border:0px;' alt='Adicionar no Roteiro'/></a></span>";
    else htmlOrg += "<span id=\"spanID_" + imovel.IdImovel + "\"><a onclick=\"$.removerdoroteiro('" + imovel.IdImovel + "');\">Remover</a></span>";

    htmlPopup = "<div style=\"width:260px\">" + img + "<b>" + imovel.Titulo + "</b><br/>" + imovel.Descricao +
                "<br/><br/><a href='detalhes.aspx?idImov=" + imovel.IdImovel + "' target='_blank'><img src='../img_site/bot_detalhes.gif' style='border:0px;' alt='Veja Detalhes deste Imóvel'/></a>";

    if (!checkImovel) htmlPopup = htmlPopup + "<span id=\"spanID_" + imovel.IdImovel + "\"><a onclick=\"$.adicionarnoroteiro('" + imovel.IdImovel + "');\"><img src='../img_site/bot_adicionar.gif' style='border:0px;' alt='Adicionar no Roteiro'/></a></span></div>";
    else htmlPopup = htmlPopup + "<span id=\"spanID_" + imovel.IdImovel + "\"><a onclick=\"$.removerdoroteiro('" + imovel.IdImovel + "');\">Remover</a></span></div>";

    $("#listadeimoveisorganica").append("<div>" + htmlOrg + "<hr/></div>");

    return htmlPopup;
}
$.adicionaimovel = function(imovel) {
    var coord = imovel.LatLong.Latitude + "," + imovel.LatLong.Longitude;
    var iconeImovel = null;
    var htmlPopup = "";

    if (imovel.QuantidadeImovel == 1) htmlPopup = $.formataimovel(imovel);
    else if (imovel.QuantidadeImovel > 1) {
        var altura = "";
        if (imovel.QuantidadeImovel > 3) altura = "height:180px"; //altura = "height:" + (60) * imovel.QuantidadeImovel + "px";
        //else altura = "height:180px";
        var ls = $.carregaimoveisporlalongimovel(coord);
        var listahtml = "";
        if (ls != null) {
            if (ls.length > 0) {
                var img = "";
                for (var i = 0; i < ls.length; i++) {
                    if (ls[i].Galeria != null) if (ls[i].Galeria.Fotos.length > 0) img = "<img style=\"width:50px; height:50px; float:left; padding-right:10px\" border=\"0px\" src=\"admin/" + ls[i].Galeria.Fotos[0].Caminhos.Quadrado + "\"><br/>";
                    else img = "";
                    listahtml += "<div id='ID_" + ls[i].IdImovel + "'>" + img + "<b>" + ls[i].Titulo +
                            "</b><br/>" + ls[i].Descricao +
                            "<br/><br/><a href='detalhes.aspx?idImov=" + ls[i].IdImovel + "' target='_blank'><img src='../img_site/bot_detalhes.gif' style='border:0px;' alt='Veja Detalhes deste Imóvel'/></a>"; //<a onclick=\"$.adicionarnoroteiro('" + imovel.IdImovel + "');\">
                    if (!$.checaimovelnoroteiro(ls[i].IdImovel)) listahtml += "<span id=\"spanID_" + ls[i].IdImovel + "\"><a onclick=\"$.adicionarnoroteiro('" + ls[i].IdImovel + "');\"><img src='../img_site/bot_adicionar.gif' style='border:0px;' alt='Adicionar no Roteiro'/></a></span>";
                    else listahtml += "<span id=\"spanID_" + ls[i].IdImovel + "\"><a onclick=\"$.removerdoroteiro('" + ls[i].IdImovel + "');\"><img src='../img_site/bot_remover.gif' style='border:0px;' alt='Remover'/></a></span>";
                    listahtml += "<hr/></div>";
                }
                $('#listadeimoveisorganica').append(listahtml);
            }
        }
        htmlPopup = "<b><div id='ID" + $.somentenumeros(coord) + "' style='width:260px;" + altura + "'>" + listahtml + "</div></b>";
    }

    if (imovel.Finalidade == 'Predial') {
        iconeImovel = new GIcon(G_DEFAULT_ICON, "img_site/predios_p.png");
        iconeImovel.shadow = "img_site/predios_p.png";
    } else if (imovel.Finalidade == 'Comercial') {
        iconeImovel = new GIcon(G_DEFAULT_ICON, "img_site/comerciais_p.png");
        iconeImovel.shadow = "img_site/comerciais_p.png";
    } else if (imovel.Finalidade == 'Residencial') {
        iconeImovel = new GIcon(G_DEFAULT_ICON, "img_site/casas_p.png");
        iconeImovel.shadow = "img_site/casas_p.png";
    } else if (imovel.Finalidade == 'Terrenos') {
        iconeImovel = new GIcon(G_DEFAULT_ICON, "img_site/terrenos_p.png");
        iconeImovel.shadow = "img_site/terrenos_p.png";
    } else if (imovel.Finalidade == 'Rural') {
        iconeImovel = new GIcon(G_DEFAULT_ICON, "img_site/rural_p.png");
        iconeImovel.shadow = "img_site/rural_p.png";
    }

    iconeImovel.iconSize = new GSize(40, 40);
    iconeImovel.shadowSize = new GSize(40, 40);
    iconeImovel.infoWindowAnchor = new GPoint(20, 1);
    iconeImovel.imageMap = [9, 9, 29, 9, 29, 29, 9, 29];
    $.adicionaponto(divMap, [imovel.LatLong.Longitude, imovel.LatLong.Latitude], htmlPopup, false, false, false, iconeImovel);

    return imovel.Bairro.IdBairro;
}
$.carregaimoveisporlalongimovel = function(coord) {
    var retorno = null;
    $.exec("wsImoveis.asmx/CarregaImoveisLatLongImovel", "{coordImovel: '" + coord + "'}", false, function(response) {
        retorno = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
    });
    return retorno;
}
$.carregaimoveisporlatlongbairro = function(coord) {
    $.exec("wsImoveis.asmx/CarregaImoveisLatLongBairro", "{coordBairro: '" + coord + "'}", false, function(response) {
        var ls = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
        if (ls.length > 0) {
            $.selectionabairro(ls[0].Bairro.IdBairro);
            $.filtrarimoveis();
            $.montasliders(true);
        } else {
            coord = $.limpalalong(coord);
            var latLong = coord.split(',');
            $.loadmapa(divMap, latLong[1] + "," + latLong[0], 15);
            var bairro = $.procurabairrocoord(coord);
            $.selectionabairro(bairro.IdBairro);
        }
    });
}
$.carregaimoveis = function() {
    var idBairro = $('#dpBairros').val();
    if (idBairro == '') {
        idBairro = -1;
        var idImob = $.query.get('idImob');
        if (idImob == '') $.carregabairros();
        else $.carregabairrosimob(true);
    } else {
        $.exec("wsImoveis.asmx/CarregaImoveis", "{idBairro: '" + idBairro + "'}", false, function(response) {
            $.filtrarimoveis(false);
        });
    }
}
$.marcarbairro = function(idBairro, isCenter) {
    $.exec("wsImoveis.asmx/ProcurarBairro", "{idBairro: '" + idBairro + "'}", false, function(response) {
        var bairro = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
        $.centralizamapalatlong(divMap, bairro.LatLong.Latitude, bairro.LatLong.Longitude, bairro.LatLong.Zoom);
    });
}
