﻿var j$ = jQuery.noConflict();

var _textoGA = new Object();
_textoGA["chat"] = "chat/header";

j$(document).ready(function()
{

    //visualizarEmpresa();

    // se a resolução do usuário for menor ou igual a 1024 não exibir banners laterais
    if (screen.width <= 1024)
    {
        j$(".banners-laterais-fixo").hide();
        j$(".banners-laterais-top").hide();
        j$(".banners-laterais-bottom").hide();

        j$("#divOpine").hide();
    }

    var cboEstado = encontrarElemento('cboEstado');

    cboEstado.onchange = function()
    {
        switch (cboEstado.value)
        {
            case "5": // bahia
                GravarCookieEstado(cboEstado.value);
                location.href = "http://www2.lopes.com.br/SetCookie.aspx?codEstado=5";
                break;
            case "7": // brasilia
                window.open("http://royal.lopes.com.br");
                break;
            case "ceara":
                window.open("http://immobilis.lopes.com.br");
                break;
            case "espirito-santo":
                window.open("http://actual.lopes.com.br");
                break;
            case "9": // goias
                GravarCookieEstado(cboEstado.value);
                location.href = "http://www2.lopes.com.br/SetCookie.aspx?codEstado=10";
                break;
            case "11": // minas-gerais
                GravarCookieEstado(cboEstado.value);
                location.href = "http://www2.lopes.com.br/SetCookie.aspx?codEstado=14";
                break;
            case "14": // para
                GravarCookieEstado(cboEstado.value);
                location.href = "http://www2.lopes.com.br/SetCookie.aspx?codEstado=15";
                break;
            case "parana":
                window.open("http://sul.lopes.com.br/site/content/home/index.php?state=pr");
                break;
            case "pernambuco":
                window.open("http://sergiomiranda.lopes.com.br");
                break;
            case "19": // rio-de-janeiro
                //GravarCookieEstado(cboEstado.value);
                //location.href = "http://www2.lopes.com.br/SetCookie.aspx?codEstado=20";
                document.location.href = "/home.aspx?estado=19";
                break;
            case "rio-grande-do-sul":
                window.open("http://sul.lopes.com.br/site/content/home/index.php?state=rs");
                break;
            case "santa-catarina":
                window.open("http://sul.lopes.com.br/site/content/home/index.php?state=sc");
                break;
            case "26": // sao-paulo
            case "capital": // sao-paulo
                //document.location.href = "/";
                document.location.href = "/home.aspx?estado=26";
                break;
            case "campinas":
                window.open("http://bauer.lopes.com.br");
                break;
        }
    }
});

//=================================================================================
// INÍCIO ABAS HEADER HOME

j$(document).ready(function(){
		
	j$("div#tab-home-buscaLivre").hide();
	//j$("div#tab-home-buscaMapa").hide();
	
	j$("div.box-abas-buscas a").click(function(){				
		j$("div#tab-home-buscaSimples").hide();
		j$("div#tab-home-buscaLivre").hide();
		//j$("div#tab-home-buscaMapa").hide();
		
		j$("div.box-abas-buscas > a#aba-buscaSimples").css("background-position", "top");
		j$("div.box-abas-buscas > a#aba-buscaLivre").css("background-position", "top");
		//j$("div.box-abas-buscas > a#aba-buscaMapa").css("background-position", "top");
		//j$("div.box-abas-buscas > a#aba-buscaAvancada").css("background-position", "top");
		
		j$("div#tab-home-" + j$(this).attr("id").split("-")[1] ).show();
		j$(this).css("background-position", "bottom");
	});
	
//	j$('#frmBuscaLivre').onsubmit = function(){alert('aaaaaaaaaa')};
	
//	j$('#frmBuscaLivre').submit(function(){
//	
//	    alert('oi')
//	


//	document.forms[0].onkeypress = function()
//	{
//		if (event.keyCode == 13)
//		{
//			alert('a');
//			//document.frm.btnAcao.click();
//		}
//	}

    j$('#linkConsultorHeader').click(function(){
        AbrirConsultorOnline('header', null, null, null, null, null, null);
    });

    try
    {
        j$('#btnBuscarSimples').click(function(){
            document.getElementById('hdnAcao').value = 'buscarHeader';
            document.forms[0].submit();
            return false;
        });

        j$('#btnBuscarLivre').click(function(){
            document.location.href = '/imoveis/busca-imoveis-livre.aspx?termo=' + encodeURI(j$('#txtBuscaLivre').val());
            return false;
        });

        var cboEstadosImoveis = encontrarElemento('cboEstadosImoveis');
        cboEstadosImoveis.onchange = function() { CarregarCombo('cboCidadesImoveis', this, null, 'Indiferente') };

        var cboCidadesImoveis = encontrarElemento('cboCidadesImoveis');
        cboCidadesImoveis.onchange = function() { LimparRegioesBairros() };

        // Modal Bairros
        j$("#bairros").jqm();

        document.getElementById('linkBairro').onclick = function() { AbrirBairrosImoveis(); };
    }
    catch(ex){}
    

});

// FINAL ABAS HEADER HOME
//=================================================================================



function AjustarComboRegioesImoveis()
{
    var cboCidadesImoveis = encontrarElemento('cboCidadesImoveis');
    var cboRegioesImoveis = document.getElementById('cboRegioesImoveis');

    // Se for Cidade de São Paulo...
    if (cboCidadesImoveis.value == 336)
        cboRegioesImoveis.disabled = false;
    else
        cboRegioesImoveis.disabled = true;
}


function checkKey(e)
{
    if (checkChr(e) == 13) // enter
    {
        if (window.event) //IE
        {
            window.event.returnValue = false;
            window.event.cancelBubble = true;
        }
        else //Firefox
        {
            e.preventDefault();
        }
        
        j$('#btnBuscarLivre').click();
    }
}

function GravarCookieEstado(estado)
{
    j$.ajax
    ({
        type: "POST",
        url: "/geral/interfaceHTTP.aspx?acao=gravar-cookie-estado",
        data: "&estado=" + estado
    });
}

function AbrirBairrosImoveis()
{
    var cboCidadesImoveis = encontrarElemento('cboCidadesImoveis');

    if (cboCidadesImoveis.selectedIndex == 0)
    {
        alert("Selecione uma cidade!");
        cboCidadesImoveis.focus();
        return;
    }

    _textoAjax = "Aguarde...";

    j$.ajax
    ({
        type: "POST",
        url: "/imoveis/fragmentos/bairros-imoveis.aspx",
        data: j$("form").serialize(),
        success: function(retorno)
        {
            j$('#bairros').jqmShow();
            AlinharElementoTopo('#bairros', 10);
            j$('#bairros').html(retorno);
            j$('#bairros').jqmAddClose('#linkFechar');
            j$('#linkSelecionarBairros').click(function() { SelecionarBairros() });
            j$('#chkMarcarTodos').click(function() { MarcarTodosBairros(this) });
            j$('input[@name=chkRegiao]').click(function() { MarcarBairrosRegiao(this.id) });
            j$('input[@name=chkBairro]').click(function() { VerificarBairrosRegiao(this.id) });

            try
            {
                document.getElementById('chkBairro0').focus();
            }
            catch (ex) { };
        }
    });
}

function LimparRegioesBairros()
{
    var hdnCboCidadesImoveis = encontrarElemento("hdnCboCidadesImoveis");

    if (IsNullOrEmpty(hdnCboCidadesImoveis.value))
    {
        document.getElementById('hdnRegioes').value = "";
        document.getElementById('hdnBairros').value = "";
    }

    try
    {
        ReiniciarPaginacao(true);
    }
    catch (ex) { };
}

function SelecionarBairros()
{
    var hdnRegioes = document.getElementById('hdnRegioes');
    var hdnBairros = document.getElementById('hdnBairros');
    var hdnBuscouBairros = document.getElementById('hdnBuscouBairros');

    // Bairros
    var itens = document.getElementsByName("chkBairro");
    var total = itens.length;
    var conta = 0;

    var bairrosSelecionados = "";

    for (var i = 0; i < total; i++)
    {
        if (itens[i].checked)
            bairrosSelecionados = bairrosSelecionados + itens[i].value + ",";
    }

    bairrosSelecionados = bairrosSelecionados.substr(0, bairrosSelecionados.length - 1);

    hdnBairros.value = bairrosSelecionados;

    // Regioes
    itens = document.getElementsByName("chkRegiao");
    total = itens.length;
    conta = 0;

    var regioesSelecionadas = "";

    for (var i = 0; i < total; i++)
    {
        if (itens[i].checked)
            regioesSelecionadas = regioesSelecionadas + itens[i].value + ",";
    }

    regioesSelecionadas = regioesSelecionadas.substr(0, regioesSelecionadas.length - 1);

    hdnRegioes.value = regioesSelecionadas;

    try
    {
        hdnBuscouBairros.value = "S";
        ReiniciarPaginacao(false); //BuscarImoveis('lista');
    }
    catch (ex) { };

    j$('#bairros').jqmHide();
}

function MarcarTodosBairros(chkMarcarTodos)
{
    var checkar = chkMarcarTodos.checked;

    var itens = document.getElementsByName("chkBairro");
    var total = itens.length;

    for (var i = 0; i < total; i++)
    {
        itens[i].checked = checkar;
    }

    itens = document.getElementsByName("chkRegiao");
    total = itens.length;

    for (var i = 0; i < total; i++)
    {
        itens[i].checked = checkar;
    }
}

function MarcarBairrosRegiao(id)
{
    var index = Right(id.toString(), 1);
    var check;

    if (document.getElementById(id).checked == true)
        check = "checked";
    else
        check = "";

    j$(".ulZona" + index + " input[@type=checkbox]").attr("checked", check);
}

function VerificarBairrosRegiao(id)
{
    var index;
    var chkBairro = j$('#' + id);
    var chkRegiao;

    index = Right(chkBairro.attr("class"), 1);
    chkRegiao = j$('#chkRegiao' + index);


    // Se está marcando, verifica se estao todos marcados
    if (chkBairro.attr('checked') == true)
    {
        var selecionados = 0;

        // Para cada checkbox dentro da Regiao...
        j$(".ulZona" + index + " input[@type=checkbox]").each(function()
        {
            // Se algum estiver desmarcado...
            if (j$(this).attr("checked") == false)
            {
                // Desmarca Regiao
                chkRegiao.attr("checked", false);
                selecionados = -1;
                return;
            }
        });

        if (selecionados > -1)
            chkRegiao.attr("checked", true);
    }
    else
    {
        // Desmarca Regiao
        chkRegiao.attr("checked", false);
    }
}