﻿
function setup() {

    //BookingBox : rooms/children 
    //Enable FancyBoxRoomPlacement 
    // $("#Room").change(function () {
    //     $('<a href="#FancyBoxRoomPlacement">Open roomplacement in fancybox..</a>').fancybox({
    //         overlayShow: true
    //     }).click();
    //  });


    $("#content").click(function () {
        $(".locationSuggest").autocomplete("close");
    });


    //$(".bedtypes").html($("#bedTypeOptions").html());
    $(".children").html($("#childrenOptions").html());
    $(".adults").html($("#adultOptions").html());
    //$(".kidsBedChoiseText").html($("#kidsBedChoiseText").html());

    $(".orderRoomBtn").live("click", function () {
        var romCode = $(this).nextAll(".MYroomCode").val();
        var romName = $(this).nextAll(".MyRoomName").val();
        var choiseText1 = $("#RoomChoiseText1").html();
        var choiseText2 = $("#RoomChoiseText2").html();
        $('html, body').animate({ scrollTop: 0 }, 'slow', function () {
            $("#booking").addClass("eyeFocus");
            $("#roomStatus").html("<div class='booking-hotel-status-sucess'><h4>" + choiseText1 + "<strong>" + romName + " .</strong>" + choiseText2 + "</h4></div>");
            $("#roomCodeInput").val(romName);
        });
    });




    $("#RoomCount").change(function () {
        var selected = $(this).val();
        $(".roomContainer").hide();
        var i = 1;
        for (i = 1; i <= selected; i++) {
            $("#roomcontainer" + i).show();
        }
        return false;
    });

    //Commented out:
    $(".childrenXXX").change(function () {
        var numChildren = $(this).val();
        if (numChildren > 0) {
            $(this).parent().next(".childPlacementContainer").show();
            $(this).parent().next(".childPlacementContainer").find(".childrenrow").hide();
            var i = 1;
            for (i = 1; i <= numChildren; i++) {
                $(this).parent().next(".childPlacementContainer").find(".children" + i).show();
            }
        } else {
            $(this).parent().next(".childPlacementContainer").hide();
        }
    });

    //end



    $(".locationSuggest").click(function () {
        $(".locationSuggest").removeClass("errorBG");
        $(this).focus();
        $(this).select();
    });

    $(".openLocationSuggest").click(function () {
        $(".locationSuggest").autocomplete('search', 'First Hotel');
        return false;
    });


    $("#booking .big-btn, #booking-frontpage .big-btn").click(function () {
        //standard paramaters:
        var url = $(".BookingURL").attr("href");
        var hotelCode = $(".hotelCode").val();
        var datefrom = $("#startdate").val();
        var dateto = $("#enddate").val();
        var rooms = $("#RoomCount :selected").val();
        var personstring = "";
        //find adults and children pr. room:
        var i = 1;
        var x = 0;
        var children_count = 0;
        for (i = 1; i <= rooms; i++) {
            personstring += "&adults_room_" + i + "=" + $("#adults_room_" + i).val();
            personstring += "&children_total_room_" + i + "=" + $("#children_room_" + i).val();
            /* bedchoise logic:
            children_count = $("#children_room_" + i).val();
            var removeKids = 0;
            for (x = 1; x <= children_count; x++) {
            bedtype = $("#roomcontainer" + i).find(".bedtype_child_" + x).val();
            if (bedtype.indexOf("Parents") >= 0) {
            removeKids++;
            }
            }
            personstring += "&children_room_" + i + "=" + (children_count - removeKids);
            personstring += "&children_total_room_" + i + "=" + children_count;
            */
        }




        var standardparams = "?HotelCode=" + hotelCode + "&dateFrom=" + datefrom + "&dateTo=" + dateto + "&numOfRooms=" + rooms + "" + personstring;
        var offerparams = "";
        var roomparams = "";
        var valuecodeparams = "";

        if ($(".valuecode").length > 0) {
            var valuecode = $(".valuecode").val();
            if (valuecode != '') {
                valuecodeparams = "&valuecode=" + valuecode;
            }
        }

        if ($("#roomCodeInput").length > 0) {
            roomCode = $("#roomCodeInput").val();
            if (roomCode != '') {
                roomparams = "&roomCode=" + roomCode;
            }
        }

        if ($("#offerCode").length > 0) {
            offerCode = $("#offerCode").val();
            offerPageLink = $("#offerPageLink").val();
            offerparams = "&offerCode=" + offerCode + "&OfferPageLink=" + offerPageLink;
        }

        var requestparams = standardparams + "" + offerparams + "" + roomparams + "" + valuecodeparams;

        if (hotelCode.indexOf("_NA") >= 0) {
            $(".locationSuggest").addClass("errorBG");
        }
        else {
            //alert(requestparams);
            //$.blockUI({ message: '<img src="/Media/img/ajax-loader.gif"/>' });
            document.location = url + requestparams;
        }

        return false;
    });



    //PRICECALENDAR

    $("#country").change(function () {
        var selected = "#" + $(this).val();
        $(".cityOptionContainer").hide();
        $(selected).show();
    });

    $("#lowFareCalendar .months a").live("click", function () {
        $("#lowFareCalendar .months li").removeClass("selected");
        $(this).parent().addClass("selected");
        var showID = $(this).attr("class");
        $("#pricecalendarTable tbody").hide();
        $("#" + showID).fadeIn();
        return false;
    });


    $("#lowFareCalendar .priceCalenderGoToBooking").live("click", function () {
        var url = $(".BookingURL").attr("href");
        var hotelcode = "";
        if ($(".hotelCode").length > 0) {
            hotelcode = "&hotelCode=" + $(".hotelCode").val();
        }
        var CellParams = "?" + $(this).attr("rel");
        var roomsandpeople = "&numOfRooms=1&adults_room_1=1&children_total_room_1=0";

        document.location = url + CellParams + hotelcode + roomsandpeople;
        return false;
    });

}

$(document).ready(function () {
    setup();
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        setup();
    });
});
