﻿var nlsbairros = "lsbairros_";
var nlsbairrosimob = "lsbairrosimob_";
var ndadosbairros = "dadosbairro_";
var iconeBairros = new GIcon(G_DEFAULT_ICON, "img_site/bairros_p.png");
iconeBairros.iconSize = new GSize(60, 60);
iconeBairros.shadow = "img_site/bairros_p.png";
iconeBairros.shadowSize = new GSize(60, 60);
iconeBairros.infoWindowAnchor = new GPoint(30, 15);
iconeBairros.imageMap = [9, 9, 49, 9, 49, 49, 9, 49];

$.listabairrosselect = function(categ) {
    var id = $('#dpCidades' + categ).val();
    var ls = $.jCache.getItem(nlsbairros + id);
    if (ls == null) {
        $.exec("wsCep.asmx/CarregaBairros", "{idCidade: '" + id + "'}", false, function(dados) {
            ls = (typeof dados.d) == 'string' ? eval('(' + dados.d + ')') : dados.d;
            $.jCache.setItem(nlsbairros + id, ls);
        });
    }
    var combo = $('#dpBairros' + categ);
    combo.attr('disabled', true).removeOption(/./);
    combo.attr('disabled', false).addOption('', ' -- Bairros -- ');
    if (ls != null) if (ls.length > 0) for (var i = 0; i < ls.length; i++) combo.addOption(ls[i].IdBairro, ls[i].NomeBairro, false);
}
$.carregabairros = function(loadmapa) {
    if (loadmapa == null) loadmapa = true;

    var idCidade = $('#dpCidades').val();
    if (idCidade == '') idCidade = -1;
    else {
        var ls = $.jCache.getItem(nlsbairros + idCidade);
        if (ls == null) {
            $.exec("wsCep.asmx/CarregaBairros", "{idCidade: '" + idCidade + "'}", false, function(dados) {
                ls = (typeof dados.d) == 'string' ? eval('(' + dados.d + ')') : dados.d;
                $.jCache.setItem(nlsbairros + idCidade, ls);
            });
        }
        var combo = $('#dpBairros');
        combo.attr('disabled', true).removeOption(/./);
        combo.attr('disabled', false).addOption('', ' -- Bairros -- ');
        var cidade = null;
        if (ls != null) {
            if (ls.length > 0) {
                cidade = ls[0].Cidade;
                if (loadmapa && (cidade != null)) $.loadmapa(divMap, cidade.LatLong.Longitude + "," + cidade.LatLong.Latitude, cidade.LatLong.Zoom);
                for (var i = 0; i < ls.length; i++) {
                    combo.addOption(ls[i].IdBairro, ls[i].NomeBairro, false);
                    $.adicionaponto(divMap, [ls[i].LatLong.Longitude, ls[i].LatLong.Latitude],
                    "<b>Bairro: " + ls[i].NomeBairro + "</b>", false, false, false, iconeBairros,
                    function(coord) {
                        $.carregaimoveisporlatlongbairro("" + coord);
                    });
                }
            } else $.centralizacidade(null, idCidade);
        } else $.centralizacidade(null, idCidade);
        //$('#menu_imoveis').show();
    }
}
$.carregabairrosimob = function(loadmapa) {
    if (loadmapa == null) loadmapa = true;
    var ls = $.jCache.getItem(nlsbairrosimob);
    if (ls == null) {
        $.exec("wsCep.asmx/CarregaBairrosImob", null, false, function(dados) {
            ls = (typeof dados.d) == 'string' ? eval('(' + dados.d + ')') : dados.d;
            $.jCache.setItem(nlsbairrosimob, ls);
        });
    }
    var combo = $('#dpBairros');
    combo.attr('disabled', true).removeOption(/./);
    combo.attr('disabled', false).addOption('', ' -- Bairros -- ');
    if (ls != null) {
        if (ls.length > 0) {
            $.carregaimoveiscidade();
            var cidade = ls[0].Cidade;
            if (loadmapa) $.loadmapa(divMap, cidade.LatLong.Longitude + "," + cidade.LatLong.Latitude, cidade.LatLong.Zoom);
            for (var i = 0; i < ls.length; i++) {
                combo.addOption(ls[i].IdBairro, ls[i].NomeBairro, false);
                $.adicionaponto(divMap, [ls[i].LatLong.Longitude, ls[i].LatLong.Latitude],
                    "<b>Bairro: " + ls[i].NomeBairro + "</b>", false, false, false, iconeBairros,
                    function(coord) {
                        $.carregaimoveisporlatlongbairro("" + coord);
                    });
            }
        } else $.centralizacidade(null, $('#dpCidades').val());
    } else $.centralizacidade(null, $('#dpCidades').val());
    //$('#menu_imoveis').show();
}
$.selectionabairro = function(idBairro) {
    $('#dpBairros').val(idBairro);
}
$.centralizabairro = function(idBairro) {
    var bairro = $.jCache.getItem(ndadosbairros + idBairro);
    if (bairro == null) {
        $.exec("wsCep.asmx/ProcurarBairro", "{idBairro: '" + idBairro + "'}", false, function(response) {
            bairro = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $.jCache.setItem(ndadosbairros + idBairro, bairro);
        });
    }
    $.centralizamapalatlong(divMap, bairro.LatLong.Latitude, bairro.LatLong.Longitude, bairro.LatLong.Zoom);
}
$.procurabairrocoord = function(coord) {
    var retorno = $.jCache.getItem(ndadosbairros + coord);
    if (retorno == null) {
        $.exec("wsCep.asmx/ProcurarBairroCoord", "{coord: '" + coord + "'}", false, function(response) {
            retorno = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $.jCache.setItem(ndadosbairros + coord, retorno);
        });
    }
    return retorno;
}
$.procurabairro = function(idBairro) {
    var retorno = $.jCache.getItem(ndadosbairros + idBairro);
    if (retorno == null) {
        $.exec("wsCep.asmx/ProcurarBairro", "{idBairro: '" + idBairro + "'}", false, function(response) {
            retorno = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $.jCache.setItem(ndadosbairros + idBairro, retorno);
        });
    }
    return retorno;
}
