﻿//Métodos para setagem dos filtros **************************************************
function SetFilters() {

    GetStarFilter();
    GetAmountPriceFilter();
}

function SetOrder() {
    OrderType = $("#ddlOrderType").val();
    GetAllHotels(false);
}

function GetAmountPriceFilter() {

    $.ajax({
        type: "POST",
        url: DefaultURL + "/AmountPriceFilter",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d != null) {

                var strTitle = "<h3>" + configHotel.jsFlightFilterPrice + "</h3>";
                var strHtml = "";
                Currency = response.d.Currency;
                if (response.d.AmountPriceMin != null) {

                    strHtml = "<div id='divFilterAmount'>" +
                                "<span id='divFilterAmountMin' />" +
                                "<span id='divFilterAmountText' />" +
                                "<span id='divFilterAmountMax' />" +
                            "</div>" +
                            "<div class='barra-preco'>" +
                                "<div id='slider' class='slider-range'></div>" +
                            "</div>";
                }

                if (strHtml != "") {
                    $("#divAmountPrice").html(strTitle + strHtml);

                    $(document).ready(function () {
                        var AmountPriceFilterMin = Math.round(Number(response.d.AmountPriceMin.replace(",", ".")));
                        var AmountPriceFilterMax = Math.round(Number(response.d.AmountPriceMax.replace(",", ".")));


                        if ((Number(response.d.AmountPriceMin.substring((response.d.AmountPriceMin.indexOf(',') + 1), response.d.AmountPriceMin.lenght))) >= 50)
                        { AmountPriceFilterMin-- }

                        if ((Number(response.d.AmountPriceMax.substring((response.d.AmountPriceMax.indexOf(',') + 1), response.d.AmountPriceMax.lenght))) < 50)
                        { AmountPriceFilterMax++ }


                        $("#slider").slider({
                            range: true,
                            min: AmountPriceFilterMin,
                            max: AmountPriceFilterMax,
                            values: [AmountPriceFilterMin, AmountPriceFilterMax],
                            slide: function (event, ui) {
                                //$("#divFilterAmountMin").html(Currency + " " + $("#slider").slider("values", 0) + ',00');
                                //$("#divFilterAmountMax").html(Currency + " " + $("#slider").slider("values", 1) + ',00');
                                $("#divFilterAmountMin").html(Currency + " " + $("#slider").slider("values", 0).localeFormat("N"));
                                $("#divFilterAmountMax").html(Currency + " " + $("#slider").slider("values", 1).localeFormat("N"));
                            },
                            stop: function (event, ui) {
                                LoadingDataShow();
                                page = 1;
                                index = 0;
                                amountPriceMin = $("#slider").slider("values", 0);
                                amountPriceMax = $("#slider").slider("values", 1);
                                GetAllHotels(false);
                            }
                        });
                        //$("#divFilterAmountMin").html(Currency + " " + $("#slider").slider("values", 0) + ',00');
                        $("#divFilterAmountMin").html(Currency + " " + $("#slider").slider("values", 0).localeFormat("N"));
                        //$("#divFilterAmountText").html(response.d.Separator);
                        $("#divFilterAmountText").html(configHotel.lblUntil);
                        //alert(response.d.Separator);
                        //$("#divFilterAmountMax").html(Currency + " " + $("#slider").slider("values", 1) + ',00');
                        $("#divFilterAmountMax").html(Currency + " " + $("#slider").slider("values", 1).localeFormat("N"));
                        LoadingDataShow();
                    });
                }
                LoadingDataHide();
            }
        },
        error: function (msg) {

        }
    });
}

function LoadingDataShow() {
    //	$('.filtro-box-resultado :input').attr('disabled', true);
    /*$("#msgEmpty").hide();
    $("#msgFilterEmpty").hide();
*/
    $('#loadingData').height(document.body.offsetHeight);
    $('#loadingData').width(document.body.offsetWidth);

    $('#loadingData').show();
    if ($('#loadingHotel').css('display') != "block") {
        $('.CarregandoPequeno').show();
    }
}

function LoadingDataHide() {
    $('#loadingData').hide();
    $('.CarregandoPequeno').hide();
}

function GetStarFilter() {
    starfilterchecked = new Array();
    $.ajax({
        type: "POST",
        url: DefaultURL + "/StarFilter",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            if (response.d != null) {
                var strTitle = "<h3>" + response.d.Title + "</h3>";
                var strHtml = "";

                starfilterchecked = new Array();

                $(response.d.Star).each(function(index) {

                    strHtml += "<label class='estrelasFiltro hotel-" + response.d.Star[index] + "-estrelas'>" +
                                "<input id='CheckBox" + index + "' " +
                                    "type='checkbox' checked='checked' name='CheckBox" + index + "' " +
                                    "onclick='javascript:LoadingDataShow();StarFilter(\"" + response.d.Star[index] + "\",this.checked);GetAllHotels(false);' " +
                                    "/>" +
                            "</label>";
                            starfilterchecked.push(new Star(response.d.Star[index], true));

                });

                if (strHtml != "") {
                	$("#divStar").html(strTitle + strHtml);
                    LoadingDataShow();
               }
               LoadingDataHide();
            }
        },
        error: function(msg) {
            
        }
    });
}

function StarFilter(star, value) {
    LoadingDataShow();
    page = 1;
    index = 0;
    $(starfilterchecked).each(function(index) {
        if (starfilterchecked[index].star == star) {
            starfilterchecked[index].value = value;
        }
    });
}

function Star(star, value) {
    this.star = star;
    this.value = value;
}

function ClearFilter() {

    amountPriceMin = null;
    amountPriceMax = null;
    GetStarFilter();
    GetAmountPriceFilter();
    GetAllHotels(false);
    return false;
}
