﻿var _previousText;
var _currentPage;
var _hasHotel = true;
var _destinationBoxPostitionSet = false;

$(function() {
    if ($("#NoOfHotels").html() != "0" && $("#ulPaging").children("li").length > 0) {
        $("#CurrentPageTop").html(1).removeClass("hide");
        if ($("#ulPaging").children("li").length > 1)
            $("#NextPage").removeClass("hide");
        $("#ListView img").lazyload();
    }
    else { _hasHotel = false; }
    $("#Loading").dialog({ autoOpen: false, modal: true, buttons: "none",
        draggable: false, closeOnEscape: false, resizable: false, height: 100,
        open: function() { $("select").addClass("invisible"); },
        close: function() { $("select").removeClass("invisible"); }
    }).parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
    $("#Loading").dialog("close");
    if (_hasHotel) {
        $(".hotelbox a").live("click", function() { $("#Loading").dialog("open"); });
        $("#ulPaging li a").live("click", function() { $(this).parent().trigger("click") });
        $("#ulPaging li").live("click", function() {
            _currentPage = $.trim($(this).find("a").html());
            $("#Type").val("paging");
            $("#ulPaging li[class='current']").removeAttr("class");
            var currentPage = $(this).find("a").html();
            if (currentPage != $("#CurrentPage").val()) {
                $("#CurrentPage").val(currentPage);
                $("#FilterForm").submit();
            }
            return false;
        });
    }
    var minPrice = parseInt($("#FromPrice").html());
    $("#MinPrice").val(minPrice);
    var maxPrice = parseInt($("#ToPrice").html());
    $("#MaxPrice").val(maxPrice);
    if (_hasHotel) {
        $("#PriceRange").slider({
            range: true,
            min: minPrice,
            max: maxPrice,
            values: [minPrice, maxPrice],
            slide: function(event, ui) { ChangePrice(event, ui, false); },
            stop: function(event, ui) {
                ChangePrice(event, ui, true);
            }
        });
    }
    $("#ChangeSearch").click(function() { ShowSearchForm(); });
    $("#HotelName").keyup(function(e) { setTimeout("SearchHotelName()", 500); });
    $("a.hoteldetails").click(function() { HotelDetails(this); });
    $("#FilterForm input[type='checkbox']").click(function() {
        $("#Type").val("filtering");
        $("#CurrentPage").val(1);
        $("#FilterForm").submit();
    });

    $("#FilterForm").submit(function() {
        if (_hasHotel) {
            $("#Loading").dialog('open');
            var form = $("#FilterForm");
            var serializedForm = form.serialize();
            var action;
            if (form.attr("action").indexOf("Destinations") != -1) {
                action = $("#MapView").css("display") == "none" ? "/Destinations/FilterHotels" :
                    "/Destinations/FilterHotelsForMap";
            }
            else {
                action = $("#MapView").css("display") == "none" ? "/DisplayHotels/FilterHotels" :
                    "/DisplayHotels/FilterHotelsForMap";
            }
            $.post(action, serializedForm,
                function(response) {
                    if ($("#MapView").css("display") == "none") {
                        $("#ListView").html(response);
                        $("#PageSize").val($("#HdPageSize").val());
                        var type = $("#Type").val();
                        if (type == "paging") {
                            $("#ulPaging li:eq(" + (parseInt(_currentPage) - 1) + ")").addClass("current");
                        }
                        else {
                            $("#ulPaging li:first").addClass("current");
                            $("#CurrentPage").val(1);
                        }
                        var noOfPages = $("#ulPaging").children("li").length;
                        $("#CurrentPageTop").html($("#CurrentPage").val());
                        if (noOfPages == 0) {
                            $("#PrevPage,#NextPage").addClass("hide");
                            $("#CurrentPageTop").html("");
                        }
                        else {
                            var currentPage = parseInt($("#CurrentPage").val());
                            if (noOfPages == 1) {
                                $("#PrevPage").addClass("hide");
                                $("#NextPage").addClass("hide");
                            }
                            else {
                                if (currentPage == noOfPages) {
                                    $("#NextPage").addClass("hide");
                                    $("#PrevPage").removeClass("hide");
                                }
                                else if (currentPage == 1) {
                                    $("#NextPage").removeClass("hide");
                                    $("#PrevPage").addClass("hide");
                                }
                                else {
                                    $("#NextPage").removeClass("hide");
                                    $("#PrevPage").removeClass("hide");
                                }
                                if ($("#SearchForMore").length > 0 && currentPage == noOfPages) {
                                    $("#NextToMore").removeClass("hide");
                                }
                                else {
                                    $("#NextToMore").addClass("hide");
                                }
                            }
                        }
                        var displayFrom;
                        var displayTo;
                        var noOfHotels = parseInt($("#hdNoOfHotels").val());
                        var pageSize = parseInt($("#PageSize").val());
                        $("#HotelFound").html(noOfHotels);
                        if ($("#hdNoOfHotels").val() == "0") {
                            displayFrom = 0;
                            displayTo = 0;
                        }
                        else if (currentPage == 1) {
                            displayFrom = 1;
                            displayTo = noOfHotels > pageSize ? pageSize : noOfHotels;
                        }
                        else {
                            displayFrom = (currentPage - 1) * pageSize + 1;
                            displayTo = currentPage * (pageSize + 1) > noOfHotels ? noOfHotels : currentPage * pageSize;
                        }
                        $("#NoOfHotels").html(displayFrom + "<span class='margin-left-5 margin-right-5 inline-block'>to</span>" + displayTo);
                        if (_hasHotel && $("#hdNoOfHotels").val() == "0") {
                            $("#ListView p").append("<h4><a href='javascript:void(0);' onclick='ClearAll()'>Click here to view all available hotels</a></h4>");
                        }
                        $("#ListView img").lazyload({ threshold: 0 });
                        $("#Loading").dialog("close");
                    }
                    else {
                        RemoveMarkersFromMap("Hotel");
                        _otherHotels = [];
                        AddMarkersToMap(response, "Hotel");
                    }
                    $("html").scrollTop(0);
                });
        }
        return false;
    });
    $("#Currency").change(function() { ChangeCurrency(this); });
    $("#SortBy").change(function() { ChangeSorting(this); });
    $("#ulPaging li:first").addClass("current");
    $("#ClearAll").click(function() { ClearAll(); });
});
function ChangePrice(event, ui, submit) {
    $("#MinPrice").val(ui.values[0]);
    $("#MaxPrice").val(ui.values[1]);
    $("#FromPrice").html(ui.values[0]);
    $("#ToPrice").html(ui.values[1]);
    $("#Type").val("filtering");
    if(submit && _hasHotel)
        $("#FilterForm").submit();
}
function ShowSearchForm() {
    var searchBox = $("#SearchBoxContainer");
    if (searchBox.css("display") == "none") searchBox.slideDown();
    else searchBox.slideUp();
    if (!_destinationBoxPostitionSet)
        SetDestinationBoxPosition();
}
function SetDestinationBoxPosition() {
    _destinationBoxPostitionSet = true;
    var left = $("#DestinationName").position().left;
    var top = $("#DestinationName").position().top;
    $("#DestinationsContainer").css({ left: left, top: top + $("#DestinationName").height() + (parseInt($("#DestinationName").css("padding")) * 2) + 5 });
}
function SearchHotelName() {
    var prefix = $("#HotelName").val();
    if (_previousText != prefix) {
        _previousText = prefix;
        $("#Type").val("filtering");
        $("#FilterForm").submit();
        $("#HotelName").focus();
    }
}
function HotelDetails(a) {
    $(a).parents("#HotelImage").trigger();
}
function ChangeCurrency(ddl) {
    $("#PayingCurrency").val($(ddl).val());
    $("#SearchForm").submit();
}
function ChangeSorting(ddl) {  
    $("#Sorting").val($(ddl).val());
    $("#Type").val("filtering");
    $("#FilterForm").submit();
}
function ClearAll() {
    $("#HotelName").val("");
    $("#FilterValueAdd input[type='hidden']").val("false");
    $("#FilterValueAdd input[type='checkbox']").removeAttr("checked");
    var minPrice = $("#PriceRange").slider("option", "min");
    var maxPrice = $("#PriceRange").slider("option", "max");
    $("#PriceRange").slider("option", "values", [2, minPrice, maxPrice]);
    $("#MinPrice").val(minPrice);
    $("#MaxPrice").val(maxPrice);
    $("#FromPrice").html(minPrice);
    $("#ToPrice").html(maxPrice);
    $("#FilterStarRating input[type='hidden']").val("false");
    $("#FilterStarRating input[type='checkbox']").removeAttr("checked");
    $("#FilterLocation input[type='hidden']").val("false");
    $("#FilterLocation input[type='checkbox']").removeAttr("checked");
    $("#CurrentPage").val(1);
    $("#Type").val("filtering");
    if ($("#HotelChain").length > 0) {
        $("#HotelChain").val("");
    }
    $("#FilterForm").submit();
}
function ShowMoreLocationInfo(hotelId, a, cityName) {
    $.get("/DisplayHotels/ReturnMoreLocationInfo?hotelId=" + hotelId +"&city= " + cityName, function(response) {
        var moreLocationInfoContainer = $(a).parent().parent().find(".MoreLocationInfo");
        moreLocationInfoContainer.html(response).show();
    });
}
function NavigatePage(type) {
    if (type == "next") {
        $("#ulPaging li.current").next("li").trigger("click");
    }
    else {
        $("#ulPaging li.current").prev("li").trigger("click");
    }
}
function SwitchView(view) {
    if (view == "list") {
        $("#SwitchToList").hide();
        $("#SwitchToMap").show();
        $("#MapView").hide();
        $("#ListView").show();
        $("#SortByContainer").show();
    }
    else {
        $("#SwitchToList").show();
        $("#SwitchToMap").hide();
        $("#ListView").hide();
        $("#MapView").show();
        $("#SortByContainer").hide();
        LoadMap();
    }
    $("#FilterForm").submit();
}
function SelectChange() {
    $("#HdPageSize").val($("#PageSize").val());
    $("#Type").val("filtering");
    $("#CurrentPage").val(1);
    $("#FilterForm").submit();
}