// JavaScript Document

var link_mapa;

function comoChegar(endereco) {

    $('#chegada').val(endereco);
    como();
}

function end() {
    desabilita_all();
    $('#end').css("display", "inline");
    $('#spaceEstado').hide();
    $('#spaceCidade').hide();
    $('#spacePais').find('select').val(0);


}
function ccep() {
    desabilita_all();
    $('#ccep').css("display", "inline");
}
function reg() {
    desabilita_all();
    $('#reg').css("display", "inline");
}
function como() {
    $('#resultado').html("");
    $('#resultados').hide();
    desabilita_all();
    $('#como').css("display", "inline");
}
function desabilita_all() {
    $('#resultado').html("");
    $('#resultados').hide();

    $('#end').css("display", "none");
    $('#ccep').css("display", "none");
    $('#reg').css("display", "none");
    $('#como').css("display", "none");
}

$(document).ready(function () {

    $('#carrega').hide();
    end();
    


    $('#ccep #enviar').click(function () {
        if ($('#ccep #pais').val() != 0) {
            $('#carrega').show();
            $('#resultados').hide();
            $('#resultado').html("");
            $.get("/Service/BuscaCep/", { cep: $('#cep').val(), pais: $('#ccep #pais').val() }, function (j) {
                $('#resultado').html(j);
                $('#resultados').show();
                $('#carrega').hide();
            });
        }
    });

    $('#reg #enviar').click(function () {
        $('#carrega').show();
        $('#resultados').hide();
        $('#resultado').html("");
        $.get("/Service/BuscaRegiao/", { regiao: $('#regiao').val() }, function (j) {
            $('#resultado').html(j);
            $('#resultados').show();
            $('#carrega').hide();
        });
    });

    $('#como #enviar').click(function () {
        var newWindow = window.open('http://maps.google.com/maps?saddr=' + $('#saida').val()  + '&daddr=' + $('#chegada').val(), '_blank');
        newWindow.focus();
        return false;
    });

    $("#pais").change(function () {
        if ($(this).val() != 0) {
            $('#spaceEstado').html(" ");
            $('#cidade').hide();
            $('#carrega').show();
            $.get("/Service/Estado/", { pais: $(this).val(), ajax: 'true' }, function (j) {
                $('#spaceEstado').html(j);
                $('#spaceEstado').show();
                $('#estado').show();
                $('#carrega').hide();
            });
        }
    });

    $("#estado").live('change', function () {
        if ($(this).val() != 0) {
            $('#carrega').show();
            $('#spaceCidade').html(" ");

            $.get("/Service/Cidade/", { pais: $('#pais').val(), uf: $(this).val(), ajax: 'true' }, function (j) {
                $('#spaceCidade').html(j);
                $('#spaceCidade').show();
                $('#cidade').show();
                $('#carrega').hide();
            });
        }
    });

    $("#cidade").live('change', function () {
        if ($(this).val() != 0) {
            $('#carrega').show();
            $('#resultados').hide();
            $('#resultado').html("");
            $.get("/Service/Igreja/", { cidade: $('#cidade').val() }, function (j) {
                $('#resultado').html(j);
                $('#resultados').show();
            });
            $('#carrega').hide();
        }
    });

});
