﻿/* ************************************************************************************ */
/* BUSCA AVANCADA - CARACTERISTICAS */
/* ************************************************************************************ */
$(document).ready(function () {

    idade_max = 100000000000;
    idade_min = 0;

    garagem_max = 100000000000;
    garagem_min = 0;

    suites_max = 100000000000;
    suites_min = 0;

    valor_max = 100000000000;
    valor_min = 0;

    area_max = 100000000000;
    area_min = 0;

    $('#radio_sem_range_idade').click(function () {
        if ($(this).is(':checked') == true) {
            idade_max = 100000000000;
            idade_min = 0;
            $("#selected_idade").html('Sem limite');
            $("#amount_idade").text('Sem limite');
        }
    });

    $('#radio_sem_range_garagens').click(function () {
        if ($(this).is(':checked') == true) {
            garagem_max = 100000000000;
            garagem_min = 0;
            $("#amount_vagas").text('Sem limite');
            $("#selected_garagem").html('Sem limite');
        }
    });

    $('#radio_sem_range_suites').click(function () {
        if ($(this).is(':checked') == true) {
            suites_max = 100000000000;
            suites_min = 0;
            $("#amount_suite").text('Sem limite');
            $("#selected_suites").html('Sem limite');
        }
    });

    $('#radio_sem_range_valor').click(function () {
        if ($(this).is(':checked') == true) {
            valor_max = 100000000000;
            valor_min = 0;
            $("#amount_valor").text('Sem limite');
            $("#selected_valor").html('Sem limite');
        }
    });

    $('#radio_sem_range_area').click(function () {
        if ($(this).is(':checked') == true) {
            area_max = 100000000000;
            area_min = 0;
            $("#amount_area").text('Sem limite');
            $("#selected_area").html('Sem limite');
        }
    });


    $('.buttons-caracteristicas').click(function () {

        var offset = $(this).offset();
        var showBox = '#box_' + $(this).attr('id');

        $(".boxes_caracteristicas:not(" + showBox + "):visible").hide();
        $(showBox).toggle();

    });

    $('.bt_ok_caracteristicas').click(function () {
        $(this).parent().parent().hide();
    });


    //SLIDER IDADE
    $(function () {
        $("#slider-range_idade").slider({
            range: true,
            min: 0,
            max: 100,
            values: [0, 100],
            step: 10,
            slide: function (event, ui) {
                $("#amount_idade").text(ui.values[0] + " até " + ui.values[1] + " anos");
                $("#selected_idade").html(ui.values[0] + " até " + ui.values[1]);

                idade_min = ui.values[0];
                idade_max = ui.values[1];

                $('#radio_sem_range_idade').attr('checked', false);
            }
        });

        $("#selected_idade").html('Sem limite');
        $("#amount_idade").text('Sem limite');

        idade_min = 0;
        idade_max = 100000000000;

    });


    //SLIDER VALOR
    $(function () {
        $("#slider-range_valor").slider({
            range: true,
            min: 20000,
            max: 9000000,
            values: [20000, 9000000],
            step: 20000,
            slide: function (event, ui) {
                $("#amount_valor").text("R$ " + float2moeda(ui.values[0]) + " até R$ " + float2moeda(ui.values[1]));
                $("#selected_valor").html(float2moeda(ui.values[0]) + "<br />até<br />" + float2moeda(ui.values[1]));

                valor_min = ui.values[0];
                valor_max = ui.values[1];

                $('#radio_sem_range_valor').attr('checked', false);
            }
        });
        $("#amount_valor").text('Sem limite');
        $("#selected_valor").html('Sem limite');

        valor_min = 0;
        valor_max = 100000000000;
    });

    $("#transacao_radios input").change(function () {
        var transacao = $(this).attr('value');

        if (transacao == "1") {
            transacao_avc = "1";

            $("#content_box_filter_tipo .content_box_filter_list").empty();
            $("#content_box_filter_estado .content_box_filter_list").empty();
            $("#content_box_filter_cidade .content_box_filter_list").empty();
            $("#content_box_filter_bairro .content_box_filter_list").empty();

            $(".content_box_filter_button_fechar").click();

            v_tipoDesc = ""
            v_tipo = ""

            v_estadoDesc = ""
            v_estado = ""

            v_cidadeDesc = ""
            v_cidade = ""

            v_bairroDesc = ""
            v_bairro = ""

            AtivarDesativarBt();

            $(function () {
                $("#slider-range_valor").slider({
                    range: true,
                    min: 20000,
                    max: 9000000,
                    values: [20000, 9000000],
                    step: 20000,
                    slide: function (event, ui) {
                        $("#amount_valor").text("R$ " + float2moeda(ui.values[0]) + " até R$ " + float2moeda(ui.values[1]));
                        $("#selected_valor").html(float2moeda(ui.values[0]) + "<br />até<br />" + float2moeda(ui.values[1]));

                        valor_min = ui.values[0];
                        valor_max = ui.values[1];

                        $('#radio_sem_range_valor').attr('checked', false);

                    }
                });
                $("#amount_valor").text('Sem limite');
                $("#selected_valor").html('Sem limite');

                valor_min = 0;
                valor_max = 100000000000;

            });
        }
        else if (transacao == "2") {

            transacao_avc = "2";

            $("#content_box_filter_tipo .content_box_filter_list").empty();
            $("#content_box_filter_estado .content_box_filter_list").empty();
            $("#content_box_filter_cidade .content_box_filter_list").empty();
            $("#content_box_filter_bairro .content_box_filter_list").empty();

            $(".content_box_filter_button_fechar").click();

            v_tipoDesc = ""
            v_tipo = ""

            v_estadoDesc = ""
            v_estado = ""

            v_cidadeDesc = ""
            v_cidade = ""

            v_bairroDesc = ""
            v_bairro = ""

            AtivarDesativarBt();

            $(function () {
                $("#slider-range_valor").slider({
                    range: true,
                    min: 450,
                    max: 100000,
                    values: [450, 100000],
                    step: 100,
                    slide: function (event, ui) {
                        $("#amount_valor").text("R$ " + float2moeda(ui.values[0]) + " até R$ " + float2moeda(ui.values[1]));
                        $("#selected_valor").html(float2moeda(ui.values[0]) + "<br />até<br />" + float2moeda(ui.values[1]));

                        valor_min = ui.values[0];
                        valor_max = ui.values[1];

                        $('#radio_sem_range_valor').attr('checked', false);
                    }
                });
                $("#amount_valor").text('Sem limite');
                $("#selected_valor").html('Sem limite');

                valor_min = 0;
                valor_max = 100000000000;

            });
        }

    });

    //SLIDER ÁREA
    $(function () {
        $("#slider-range_area").slider({
            range: true,
            min: 5,
            max: 9000,
            values: [5, 9000],
            step: 25,
            slide: function (event, ui) {
                $("#amount_area").text(ui.values[0] + " m² até " + ui.values[1] + " m²");
                $("#selected_area").html(ui.values[0] + "m² até " + ui.values[1] + "m²");

                area_min = ui.values[0];
                area_max = ui.values[1];

                $('#radio_sem_range_area').attr('checked', false);

            }
        });
        $("#amount_area").text('Sem limite');
        $("#selected_area").html('Sem limite');

        area_min = 0;
        area_max = 100000000000;

    });

    //SLIDER SUITES 
    $(function () {
        $("#slider-range_suite").slider({
            range: true,
            min: 0,
            max: 5,
            values: [0, 5],
            step: 1,
            slide: function (event, ui) {
                $("#amount_suite").text(ui.values[0] + " até " + ui.values[1] + " suites");
                $("#selected_suites").html(ui.values[0] + " até " + ui.values[1]);

                suites_min = ui.values[0];
                suites_max = ui.values[1];

                $('#radio_sem_range_suites').attr('checked', false);

            }
        });
        $("#amount_suite").text('Sem limite');
        $("#selected_suites").html('Sem limite');

        suites_min = 0;
        suites_max = 100000000000;

    });

    //SLIDER VAGAS 
    $(function () {
        $("#slider-range_vagas").slider({
            range: true,
            min: 0,
            max: 20,
            values: [0, 20],
            step: 1,
            slide: function (event, ui) {
                $("#amount_vagas").text(ui.values[0] + " até " + ui.values[1] + " vagas");
                $("#selected_garagem").html(ui.values[0] + " até " + ui.values[1]);

                garagem_min = ui.values[0];
                garagem_max = ui.values[1];

                $('#radio_sem_range_garagens').attr('checked', false);

            }
        });
        $("#amount_vagas").text('Sem limite');
        $("#selected_garagem").html('Sem limite');

        garagem_min = 0;
        garagem_max = 100000000000;

    });


    //    //BOTÕES AVANCAR BUSCA AVANÇADA

    //    $("#bt_avc_idade").click(function () {

    //        $("#valor").click();

    //    });

    //    $("#bt_avc_valor").click(function () {

    //        $("#area").click();

    //    });

    //    $("#bt_avc_area").click(function () {

    //        $("#suites").click();

    //    });

    //    $("#bt_avc_suites").click(function () {

    //        $("#garagens").click();

    //    });

    //    $("#bt_avc_garagens").click(function () {

    //        $("#box_garagens").hide();

    //    });


    //BOTÃO PESQUISAR
    $("#bt_buscar_pp").click(function () {

        aux_erro = "";

        if (v_tipo == "") {
            aux_erro = aux_erro + "o tipo de imóvel, "
        }
        if (v_estado == "") {
            aux_erro = aux_erro + "o estado, "
        }
        if (v_cidade == "") {
            aux_erro = aux_erro + "a cidade "
        }
        if (v_bairro == "") {
            if (aux_erro != "") {
                aux_erro = aux_erro + "e o bairro."
            }
            else {
                aux_erro = aux_erro + " o bairro."
            }
        }

        if (aux_erro != "") {

            //aux_erro = aux_erro.substr(0, aux_erro.length - 2);
            alert("Você precisa informar " + aux_erro);
        }
        else {
            GeraCookiesAvanc();
        }

    });




});

function float2moeda(num) {

    x = 0;

    if (num < 0) {
        num = Math.abs(num);
        x = 1;
    } if (isNaN(num)) num = "0";
    cents = Math.floor((num * 100 + 0.5) % 100);

    num = Math.floor((num * 100 + 0.5) / 100).toString();

    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.'
               + num.substring(num.length - (4 * i + 3)); ret = num + ',' + cents; if (x == 1) ret = ' - ' + ret; return ret;
}
