﻿var _destinationBoxPositionSet = false;
var _hotelIdFromSearchResult;

$(function() {
    if ($("#StaticHotelData").val() == "True") LoadMap();
    $("#imageGallery img").click(function() { ViewImage(this); });
    $("#Search").click(function(e) {
        var isValid = ValidateSearch(e);
        if (isValid) {
            if ($("#DestinationType").val() != "hotel" && $("#DestinationType").val() != "Hotel") {
                var url = "/SearchHotels/Searching?DestinationName=" + escape($("#DestinationName").val()) + "&DestinationType=" + $("#DestinationType").val() + "&DestinationId=&HotelId=&CheckInDate=" + $("#CheckInDate").val() + "&CheckOutDate=" + $("#CheckOutDate").val() + "&PayingCurrency=" + $("#PayingCurrency").val() + "&NoOfRooms=" + $("#NoOfRms").val() + "&Room1NoOfAdults=" + $("#Rm1NoOfAdts").val() + "&Room1NoOfChildren=" + $("#Rm1NoOfChdren").val() + "&Room1Child1Age=" + $("#Rm1Chd1Age").val() + "&Room1Child2Age=" + $("#Rm1Chd2Age").val() + "&Room2NoOfAdults=" + $("#Rm2NoOfAdts").val() + "&Room2NoOfChildren=" + $("#Rm2NoOfChdren").val() + "&Room2Child1Age=" + $("#Rm2Chd1Age").val() + "&Room2Child2Age=" + $("#Rm2Chd2Age").val() + "&Room3NoOfAdults=" + $("#Rm3NoOfAdts").val() + "&Room3NoOfChildren=" + $("#Rm3NoOfChdren").val() + "&Room3Child1Age=" + $("#Rm3Chd1Age").val() + "&Room3Child2Age=" + $("#Rm3Chd2Age").val();
                location.href = url;
            }
            else if ($("#HotelName").val().replace("&amp;", "&") != $("#DestinationName").val()) {
                var url = "/SearchHotels/Searching?DestinationName=" + escape($("#DestinationName").val()) + "&DestinationType=hotel&DestinationId=&HotelId=&CheckInDate=" + $("#CheckInDate").val() + "&CheckOutDate=" + $("#CheckOutDate").val() + "&PayingCurrency=" + $("#PayingCurrency").val() + "&NoOfRooms=" + $("#NoOfRms").val() + "&Room1NoOfAdults=" + $("#Rm1NoOfAdts").val() + "&Room1NoOfChildren=" + $("#Rm1NoOfChdren").val() + "&Room1Child1Age=" + $("#Rm1Chd1Age").val() + "&Room1Child2Age=" + $("#Rm1Chd2Age").val() + "&Room2NoOfAdults=" + $("#Rm2NoOfAdts").val() + "&Room2NoOfChildren=" + $("#Rm2NoOfChdren").val() + "&Room2Child1Age=" + $("#Rm2Chd1Age").val() + "&Room2Child2Age=" + $("#Rm2Chd2Age").val() + "&Room3NoOfAdults=" + $("#Rm3NoOfAdts").val() + "&Room3NoOfChildren=" + $("#Rm3NoOfChdren").val() + "&Room3Child1Age=" + $("#Rm3Chd1Age").val() + "&Room3Child2Age=" + $("#Rm3Chd2Age").val();
                location.href = url;
            }
            else {
                $("#SearchForm").submit();
            }
        }
        return false;
    });
    $("#SearchForm").submit(function() {
        $("#Search").attr("disabled", "disabled");
        $("#Coupon").hide();
        var form = $("#SearchForm");
        var action = form.attr("action");
        var serializedForm = form.serialize();
        $("#roomsLi").trigger("click");
        $("#roomTypes").hide();
        $("#availableRooms").html("<p class='searching'>Searching for hotel rates, please wait.....<img src='/images/loading_small.gif' title='Searching for hotel rates' alt='Searching' class='margin-left-15' /></p>");
        var checkIn = $("#CheckInDate").val();
        var checkIn_month = GetMonth(parseInt(checkIn.substring(3, 5), 10) - 1, 10);
        var checkIn_day = parseInt(checkIn.substring(0, 2), 10);
        var checkIn_year = parseInt(checkIn.substring(6, 10), 10);
        var checkOut = $("#CheckOutDate").val();
        var checkOut_month = GetMonth(parseInt(checkOut.substring(3, 5), 10) - 1, 10);
        var checkOut_day = parseInt(checkOut.substring(0, 2), 10)
        var checkOut_year = parseInt(checkOut.substring(6, 10), 10);
        var CheckInDate = $("#CheckInDate").datepicker('getDate');
        var CheckOutDate = $("#CheckOutDate").datepicker('getDate');
        var noOfNights = (CheckOutDate - CheckInDate) / 1000 / 60 / 60 / 24;
        $("#Header-CheckIn").html(checkIn_day + "-" + checkIn_month + "-" + checkIn_year);
        $("#Header-CheckOut").html(checkOut_day + "-" + checkOut_month + "-" + checkOut_year);
        $("#Header-NoOfNights").html(noOfNights);
        $("#Header-NoOfRooms").html($("#NoOfRms").val());
        $.post(action, serializedForm,
        function(response) {
            $("#Search").removeAttr("disabled");
            $("#availableRooms").html(response);
            if ($("#CouponError").val() != "") $("#InvalidCoupon").html($("#CouponError").val());
            if ($("#FromPrice").length > 0) {
                $("#fromLi").html("<strong>" + $("#FromPrice").val() + "</strong>");
                if ($("#SearchBoxContainer").css("display") != "none") $("#SearchBoxContainer").slideUp();
                $("#Coupon").show();
            }
            else {
                $("#fromLi").html("");
                $("#NoHotelViewAll").attr("href", "/HotelDetails/ViewAllHotelsAvailable?hotelid=" + $("#HotelId").val());
                if ($("#SearchBoxContainer").css("display") == "none") $("#SearchBoxContainer").slideDown();
                $("#Coupon").hide();
            }
            $("#availableRooms").slideDown();
        });
        return false;
    });
    if ($.trim($("#availableRooms").html()) != "") {
        $("#roomTypes").hide();
        $("#availableRooms").slideDown();
    }

    $("#ChangeSearch").click(function() { ShowRefineSearch(); });
    $("#tabsHeader div").click(function() { SwitchTab(this); });
    $("#ViewDestinationPhotos").click(function() { ViewDestinationPhotos(); });
    var url = window.location.href;
    if (url.indexOf("#=") != -1) {
        var tab = url.substring(url.indexOf("#=") + 2).toLowerCase();
        SwitchTab("#" + tab + "Li");
    }
    else SwitchTab("#overviewLi");
    if ($("#NoRoom").length > 0) {
        ShowRefineSearch();
        SwitchTab("#roomsLi");
    }
    $("#UpdateSearch").dialog({ autoOpen: false, modal: true, buttons: "none",
        draggable: false, closeOnEscape: false, resizable: false, height: 100, width: 400,
        open: function() { $("select").addClass("invisible"); },
        close: function() { $("select").removeClass("invisible"); }
    }).parents(".ui-dialog").find(".ui-dialog-titlebar").remove();
});
function ViewImage(img) {
    $("#largeImage").html("<img id='hotelImage' src='/images/loading_small.gif' alt='Hotel Image' />");
    $("#hotelImage").attr("src", $(img).attr("rel"));
}
function ShowRmInfo(a) {
    var roomType = $(a).attr("title");
    if(roomType.indexOf("-") != -1) roomType = roomType.substring(0, roomType.indexOf("-"));
    var container = $(a).next("div");
    if(container.html() == ""){
        var amenities = $("div[title^='"+ roomType +"']");
        container.html(amenities.html());
    }
    if(container.css("display") == "none") container.slideDown();
    else container.slideUp();
}
function ShowRoomAmenities(a) {
    var ul = $(a).parent().parent().find("ul");
    if (ul.css("display") == "none") ul.slideDown();
    else ul.slideUp();
}
function ShowCancellationPolicy(a) {
    var p = $(a).next("p");
    if (p.css("display") == "none") p.slideDown();
    else p.slideUp();
}
function ShowRefineSearch() {
    var searchBox = $("#SearchBoxContainer");
    if (searchBox.css("display") == "none") searchBox.slideDown();
    else searchBox.slideUp();
    if (!_destinationBoxPositionSet)
        SetDestinationBoxPosition();
}
function SwitchTab(div) {
    if ($("#StaticHotelData").val() == "False") {
        $("div.selectedTab").removeClass("selectedTab");
        var tab = $(div).attr("href");
        $(".tabContainer:visible").hide();
        $(tab).show();
        if (tab == "#mapTab")
            LoadMap();
        else if ((tab == "#roomsTab" && $.trim($("#availableRooms").html()) == "") || tab.toLowerCase() == "#hotdeals") {
            var searchBox = $("#SearchBoxContainer");
            if (searchBox.css("display") == "none") searchBox.slideDown();
        }
        if ($(div).attr("id") == "fromLi") {
            $("#roomsLi").addClass("selectedTab");
        }
        else {
            $(div).addClass("selectedTab");
        }
    }
}
function ShowHotDeals(a) {
    var href = $(a).attr("href");
    if (window.location.href.indexOf(href) != -1)
        SwitchTab("#roomsLi");
}
function ViewDestinationPhotos() {
    $("#ViewDestinationPhotos").hide();
    $("#DestinationPhotosContainer").slideDown();
    var latitude = parseFloat($.trim($("#Latitude").val()));
    var longitude = parseFloat($.trim($("#Longitude").val()));
    var minX = latitude - 0.5;
    var minY = longitude - 0.5;
    var maxX = latitude + 0.5;
    var maxY = longitude + 0.5;
    $.getJSON("/HotelDetails/ReturnDestinationPhotos?latitude=" + latitude + "&longitude=" + longitude, function(response, status) {
        GetResponse(response);
    });
    function GetResponse(response) {
        var data = eval('(' + response + ')').photos;
        for (var i = 0; i <= data.length - 1; i++) {
            $("#DestinationPhotoList").append("<li><a href='http://panoramio.com/photo/" + data[i]["photo_id"] + " target='_blank'><img src='" + data[i]["photo_file_url"] + "' title='" + data[i]["photo_title"] + "' alt='" + data[i]["photo_title"] + "' /></a></li>");
        }
        if (data.length == 0)
            $("#DestinationPhotosContainer").append("<strong>No photo found</strong>");
        $("#Loading").hide();
    }
}
function ShowPriceBreakdown(a) {
    var divPriceBreakdown = $(a).parent().next("div.pricebreakdown");
    if (divPriceBreakdown.css("display") == "none")
        divPriceBreakdown.slideDown();
    else
        divPriceBreakdown.slideUp();
}
function SetDestinationBoxPosition() {
    _destinationBoxPositionSet = true;
    var left = $("#DestinationName").position().left;
    var top = $("#DestinationName").position().top;
    top = 235;
    $("#DestinationsContainer").css({ left: left, top: top + $("#DestinationName").height() + (parseInt($("#DestinationName").css("padding")) * 2) + 5 });
}
function GetMonth(month) {
    switch (month) {
        case 0:
            return "Jan";
        case 1:
            return "Feb";
        case 2:
            return "Mar";
        case 3:
            return "Apr";
        case 4:
            return "May";
        case 5:
            return "Jun";
        case 6:
            return "Jul";
        case 7:
            return "Aug";
        case 8:
            return "Sep";
        case 9:
            return "Oct";
        case 10:
            return "Nov";
        case 11:
            return "Dec";
        default:
            return "Jan";
    }
}
function ApplyCoupon() {
    var couponCode = $.trim($("#TbCouponCode").val());
    var invalidCoupon = $("#InvalidCoupon");
    if (couponCode != "" && (couponCode.length < 5 || couponCode.length > 10)) 
        invalidCoupon.html("Invalid coupon code");
    else {
        invalidCoupon.html("");
        $("#UserCouponCode").val(couponCode);
        $("#Search").trigger("click");
    }
}
function ShowCouponInformation() {
    var couponInformation = $("#CouponInformation");
    if (couponInformation.css("display") == "none") couponInformation.slideDown();
    else couponInformation.slideUp();
}
function ValidateSrid(srid) {
    $.get("/HotelDetails/CheckHasValidAvailableRooms?srid=" + srid + "&hotelId=" + $("#HotelId").val(), function(response) {
        if (response == "False") {
            $("#UpdateSearch").dialog("open");
            setTimeout("location.reload(true)", 1000);
        }
        else window.location.href = "/HotelDetails/SelectHotel?srid=" + srid;
    });
}
function ShowExtraPersonMessage(span, type) {
    var position = $(span).position();
    var extraPersonMessage = $("#ExtraPersonMessage");
    if (type == "show") {
        extraPersonMessage.css({ top: position.top - 40, left: position.left + 20 }).fadeIn();
    }
    else {
        extraPersonMessage.fadeOut();
    }
}