﻿var Hotels = null;
var index = 0;
var numRows = 0;
var numPerPage = 15;
var page = 1;
var template01 = null;
var DefaultURL;
$(document).ready(function() {
    DefaultURL = GetBaseUrlWS() + '/WSHotel.asmx';
});


var OrderType = null;
var amountPriceMin = null;
var amountPriceMax = null;
var templatePagCont = null;
starfilterchecked = new Array();

function StarFilterChecked(star, value) {
    this.star = star;
    this.value = value;
}

function FormatDecimal(valorCampo) {
    if (valorCampo != null) {
        valorCampo = valorCampo.toString();

        valorCampo = valorCampo.replace('.', ',');

        var inteiro = valorCampo.split(',')[0];

        var dec = '00'
        if (valorCampo.split(',').length > 1) {
            dec = valorCampo.split(',')[1];
        }

        if (dec.length > 0) {
            if (dec.length == 1) {
                dec = dec + '0'
            }
            else if (dec.length > 2) {
                dec = dec.substring(0, 2)
            }
        }
        return inteiro + ',' + dec;
    }
    else { return ""; }
}


function GetAllHotels(reloadFilter) {

    var strstarfilterchecked = "";
    var separador = "";
    $(starfilterchecked).each(function(index) {
        if (!starfilterchecked[index].value) {
            strstarfilterchecked += separador + starfilterchecked[index].star;
            separador = "|";
        }
    });

    //debugger;

    $.ajax({
        type: "POST",
        url: DefaultURL + "/GetAllHotels",
        data: "{index:" + index +
                    ",numPerPage:" + numPerPage +
                    ",orderType:'" + OrderType +
                    "',amountPriceMin:" + amountPriceMin +
                    ",amountPriceMax:" + amountPriceMax +
                    ",starFilterChecked:'" + strstarfilterchecked + "'" +
                    "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            if (!response.d.Error) {
                Hotels = response.d.Hotel;
                numRows = response.d.Quantity;

                if (Hotels.length == 0) {
                    if (reloadFilter) {
                        $("#msgFilterEmpty").hide();
                        $("#msgError").hide();
                        $("#msgEmpty").show();
                        $("#semResultadoBusca").show();
                        $("#divMaster").hide();
                        $("#hotelResultSearch").hide();
                        $("#loadingHotel").hide();
                        $("#loadingBlock").hide();
                    }
                    else {
                        $("#loadingHotel").hide();
                        $("#loadingBlock").hide();
                        $("#msgEmpty").hide();
                        $("#msgError").hide();
                        $("#msgFilterEmpty").show();
                        $("#hotelResultSearch").hide();
                    }
                }
                else {
                    $("#loadingHotel").hide();
                    $("#loadingBlock").hide();
                    $("#divMaster").show();
                    $("#msgEmpty").hide();
                    $("#msgError").hide();
                    $("#msgFilterEmpty").hide();
                    $("#hotelResultSearch").show();
                    $("#tituloResultados").show("slow");
                    $("#semResultadoBusca").hide();
                }

                GenerateListHotel();

                if (reloadFilter) {
                    SetFilters();
                }
            }
            else {
                $("#loadingHotel").hide();
                $("#loadingBlock").hide();
                $("#msgEmpty").show();
                $("#msgFilterEmpty").hide();
                $("#msgError").hide();
                $("#hotelResultSearch").hide();
                $("#semResultadoBusca").show();
                $("#divMaster").hide();
            }

            LoadingDataHide();
        },
        failure: function(msg) {

        },
        error: function(msg) {
            $("#msgEmpty").show();
            $("#msgFilterEmpty").hide();
            $("#msgError").hide();
            $("#hotelResultSearch").hide();
            $("#semResultadoBusca").show();
            $("#divMaster").hide();
            $("#loadingHotel").hide();
            $("#loadingBlock").hide();
        }
    });
}

function SendToDetail(hotelId) {
    $.ajax({
        type: "POST",
        url: DefaultURL + "/SendToDetail",
        data: "{hotelId:" + hotelId + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            RedirectToDetail(response.d);
        },
        failure: function(msg) {

        },

        error: function(msg) {
            $("#msgEmpty").show();
        }
    });
}

function GenerateListHotel() {
    CalculatePages();
    PublishGrid();
    PreLoadImage();
}

function CalculatePages() {
    numPages = Math.ceil(numRows / numPerPage);
    try {
        SetPagerState();
        GenerateNumPage();
    } catch (ex) { }
}


function PublishGridLine(i, hotel) {
    var values = {
        Index: i,
        urllogo: (hotel.UrlLogo != "" ? "srcreal='" + hotel.UrlLogo + "'" : "srcreal='" + GetBaseUrlIMG() + "/semImagem.png'"),
        hotelname: hotel.HotelName,
        currencycode: hotel.Currencycode,
        //amountprice: hotel.AmountPrice.format("c").replace("¤",""),
        amountprice: hotel.AmountPrice.localeFormat("N"),
        boardbase: hotel.BoardBase,
        boardbaseid: hotel.BoardBaseId,
        star: "class='estrelas hotel-" + hotel.Star + "-estrelas'",
        hotelid: hotel.HotelId,
        localization: hotel.Localization,
        address: hotel.Address,
        paystay: (hotel.PayStay > 0 ? "class='noite-extra-" + hotel.PayStay + "'" : ''),
        bestprice: (!hotel.BestPrice ? '' : 'class=melhor-preco'),
        functionselect: 'href="javascript:SendToDetail(' + hotel.HotelId + ');"'
    };
    
    $('#destination').empty();
    $('#destination').append(hotel.Destination);
    $('#destination2').empty();
    $('#destination2').append(hotel.Destination);
    $('#checkin').empty();
    $('#checkin').append(hotel.Checkin);
    $('#checkout').empty();
    $('#checkout').append(hotel.Checkout);
    $('#outputhotel').append($.tmpl(template01 + "<BR>", values));
}

function PublishGrid() {
    $('#outputhotel').empty();
    for (var i = 0; i <= Hotels.length - 1; i++) {
        PublishGridLine(i, Hotels[i]);
    }
    if (Hotels.length > 0) {
        $("#hotelResultSearch").show("slow");
    }
}

function StartApp() {
    templatePagCont = $('#divPagCont').html();
    $('#loadingBlock').show();
    $('#loadingBlock').height(document.body.offsetHeight);
    $('#loadingBlock').width(document.body.offsetWidth);
    SetTemplate();
    GetAllHotels(true);
}

function SetTemplate() {
    template01 = $('#outputhotel').html();
    $('#outputhotel').empty();
}

$(document).ready(function() {
    $('#loadingBlock').height(document.body.offsetHeight);
    $('#loadingBlock').width(document.body.offsetWidth);
    StartApp();
});
