
$(function() {

    if (typeof window.SH == "undefined" || !window.SH) {
        window.SH = {};
    }
    if (typeof SH.Util == "undefined" || !SH.Util) {
        SH.Util = {};
    }
    if (typeof SH.Search == "undefined" || !SH.Search) {
        SH.Search = {};
    }

    SH.createCalender("#CheckInDate", '/_assets/img/common/btn_cal.gif');
});


SH.Search = function() {

    var _destL2 = null;
    var _search = "";
    var _searchInlineBook = null;
    var _hotelform = null;

    var init = function(search, destL2) {

        _search = search;
        _destL2 = destL2;
    
        var hotelForm = getHotelForm();

        $("#HotelRoomsPax .plus_container a").click(function(e) {
            e.preventDefault();
            SH.addRoom("#HotelRooms", "Hotel", "Room", 1);
            return false;
        });
        $("#HotelRoomsPax .minus_container a").click(function() {
            SH.removeRoom($(this).parent().parent(), "#HotelRooms", "Hotel", "Room");
            return false;
        });

        if (_search == "MainSearch") {
            hotelForm.Submit.click(function() {
                postForm("hotel");
            });
        }
        else {
            hotelForm.Submit.click(function() {
                var sc = getHotelFormValues();
                SH.Results.ajaxResults(sc);
                return false;
            });

            if ($("#search_rb_inline_holder").length > 0) {
                _searchInlineBook = $("#search_rb_inline_holder").html();
                $("#search_rb_inline_holder").remove();
            }
        }
    };

    var getHotelFormValues = function() {

        var hotelForm = SH.Search.getHotelForm();

        var hotel = SH.Util.isNull(hotelForm.Hotel) ? "" : hotelForm.Hotel.val();
        if (!SH.Util.isNull(hotelForm.DestL2)) {
            hotel = hotelForm.DestL2;
        }
        var sc = {
            Hotel: hotel,
            CheckInDate: hotelForm.CheckInDate.val(),
            PlusMinus: hotelForm.PlusMinus.val(),
            Duration: hotelForm.Duration.val(),
            RoomType: hotelForm.RoomType.val(),
            Board: hotelForm.Board.val(),
            Price: hotelForm.Price.val(),
            PromoCode: hotelForm.PromoCode.val(),
            Rooms: SH.getRooms("#HotelRooms", "Hotel")
        };
        return sc;
    };

    var postForm = function(type) {

        $("#__VIEWSTATE").attr("disabled", "disabled");
        $("form")[0].action = "/searchresults";
        $("form").attr("name", "search");
        $("form")[0].submit();
    }

    var getHotelForm = function() {

        if (SH.Util.isNull(_hotelform)) {
            _hotelform = {
                DestL2: _destL2,
                Hotel: $("#Hotel").length > 0 ? $("#Hotel") : null,
                CheckInDate: $("#CheckInDate"),
                PlusMinus: $("#HotelPlusMinus"),
                Duration: $("#HotelDuration"),
                Rooms: $("#HotelRooms"),
                RoomType: $("#HotelRoomType"),
                Board: $("#HotelBoard"),
                Price: $("#HotelPrice"),
                PromoCode: $("#HotelPromoCode"),
                Submit: $("#Hotel_Submit")
            };
        }
        return _hotelform;
    };

    var getSearchPanel = function() {
        return _searchInlineBook;
    };

    var getDestL2 = function() {
        return _destL2;
    };

    var getSearchType = function() {
        return _search;
    };

    return {
        init: init,
        getHotelForm: getHotelForm,        
        getSearchPanel: getSearchPanel,
        getSearchType: getSearchType,
        getDestL2: getDestL2
    };
} ();

var Product = makeClass();

Product.prototype = {
    _data: {}
};

Product.prototype.init = function(json) {

    var instance = this;
    instance._data = json;
    instance.InitControls();
};

Product.prototype.InitControls = function() {

    var instance = this;
    var data = instance._data;

    var checkAvail = $('#' + data.divId + ' .result_details .result_cta .cta_top a');

    checkAvail.click(function(e) {

        e.preventDefault();

        $("#search_rb_inline_form").remove();
        $("#search_rb_inline_results").remove();
        var rapidBook = SH.Search.getSearchPanel();
        
        var bookingForm = $(rapidBook);
        var booking = $('#' + data.divId + ' .result_details ');

        booking.append(bookingForm);

        $("#inline_hotel_name").html(data.name);
        SH.createCalender("#Search_CheckInDate", '/_assets/img/common/btn_cal.gif');
        $("#Search_HotelRoomsPax .plus_container a").click(function() {
            SH.addRoom("#Search_HotelRooms", "Search_Hotel", "Room", 1);
            return false;
        });
        $("#Search_HotelRoomsPax .minus_container a").click(function() {
            SH.removeRoom($(this).parent().parent(), "#HotelRooms", "Search_Hotel", "Room");
            return false;
        });
        $("#Search_Hotel_Submit").click(function(e) {
            e.preventDefault();
            instance.Search();
        });
    });
};


Product.prototype.Search = function() {

    var instance = this;
    var data = instance._data;

    var resultsHolder = "#search_rb_inline_results";
    $(resultsHolder).html($("#search_results_loader").html());
    $(resultsHolder + " .search_results_progress").addClass("search_results_progress_inline");

    var sc = {
        DestL2: SH.Search.getDestL2(),
        Product: data.code,
        CheckInDate: $("#Search_CheckInDate").val(),
        Duration: $("#Search_HotelDuration").val(),
        PlusMinus: $("#Search_PlusMinus").val(),
        Rooms: SH.getRooms("#Search_HotelRooms", "Search_Hotel")
    };

    var searchType = SH.Search.getSearchType();

    var url = "/ajax_results.aspx?mode=inline&search=" + searchType + "&criteria=" + JSON.stringify(sc);
    $.getJSON(url, function(data) {
        if (!data.IsError) {
            $(resultsHolder).html(data.results_html);
        }
        else {
            $(resultsHolder).html("");
        }
    });
};

// makeClass - By John Resig (MIT Licensed)
function makeClass() {
    return function(args) {
        if (this instanceof arguments.callee) {
            if (typeof this.init == "function")
                this.init.apply(this, args.callee ? args : arguments);
        } else
            return new arguments.callee(arguments);
    };
} 
    
   
    
   
    
    
 
    
     
 
    
    