﻿/**
 * static object that handles page logic
 * @class 
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var Main = function($) {

    /**
    * @namespace Private methods and variables
    */
    var priv = {
        /**
        * Sets the debug level
        * @private
        */
        debugSeverity: 0, //level of error logging

        bindPromoEvents: function() {
            $('div.column3 div.promotion-blocks').children("div.module2").click(function() {
                //redirect to the correct page
                var url = $(this).find("span.promo-acco-name a").attr("href");

                document.location.href = url;

                //prevent event bubbling
                return false;
            });
        }
    };

    /** @scope Main */
    return {

        /**
        * Initializes the logic for the current page
        * to be called on $(document).ready
        */
        OnReady: function() {
            //overal js timer
            var overallTime = new Timer();

            //enable/disable debugging
            Log.SetDebugging(Resource.GetText("js-debug-enabled") == 'true');
            Log.SetLevel(priv.debugSeverity);

            if (typeof (Utils) != "undefined" && Utils) {
                Utils.OnReady();
            }

            //load PersonalItems logic
            var personalItemsTimer = new Timer();
            //load and show the personal item links
            PersonalItems.Load("alreadyviewed");
            PersonalItems.Load("favorites");
            PersonalItems.ShowItemLinks();
            Log.Info("Main: PersonalItems script time was: " + (personalItemsTimer.Stop()) + " ms", -1);

            // load the travelers
            if (typeof (Occupancy) != "undefined") {
                var occupancyTime = new Timer();
                Occupancy.OnReady();
                Log.Info("Main: Occupancy Javascript load time was: " + (occupancyTime.Stop()) + " ms", -1);
            }

			//load CarRentals
			if (typeof (CarRental) != "undefined") {
				CarRental.OnReady();
			}
			
            // load the resource info
            if (typeof (ResourceInfo) != "undefined") {
                var resourceInfoTime = new Timer();
                ResourceInfo.OnReady();
                Log.Info("Main: ResourceInfo Javascript load time was: " + (resourceInfoTime.Stop()) + " ms", -1);
            }

            // logic for result list (list of accommodations, like on search page)
            if (typeof (ResultList) != "undefined") {
                var resultListTime = new Timer();
                ResultList.OnReady();
                Log.Info("Main: ResultList Javascript load time was: " + (resultListTime.Stop()) + " ms", -1);
            }

            //load homepage specific logic
            if (typeof (HomeMain) != "undefined" && HomeMain) {
                var homeMainTime = new Timer();
                HomeMain.OnReady();
                Log.Info("Main: HomeMain Javascript load time was: " + (homeMainTime.Stop()) + "ms ", -1);
            }

            //load accommodation specific logic
            if (typeof (AccoMain) != "undefined" && AccoMain) {
                var accoMainTime = new Timer();
                AccoMain.OnReady();
                Log.Info("Main: AccoMain Javascript load time was: " + (accoMainTime.Stop()) + "ms ", -1);
            }

            //load search specific logic
            if (typeof (SearchMain) != "undefined" && SearchMain) {
                var searchMainTime = new Timer();
                SearchMain.OnReady();
                Log.Info("Main: SearchMain Javascript load time was: " + (searchMainTime.Stop()) + "ms ", -1);
            }

			//load participant logic
			if (typeof (ParticipantConfig) != "undefined" && ParticipantConfig) {
				var participantTime = new Timer();
				ParticipantConfig.OnReady();
				Log.Info("Main: ParticipantConfig Javascript load time was: " + (participantTime.Stop()) + "ms ", -1);
			}
			
			//load confirmation logic
			if (typeof (Confirmation) != "undefined" && Confirmation) {
				var confirmationTime = new Timer();
				Confirmation.OnReady();
				Log.Info("Main: Confirmation Javascript load time was: " + (confirmationTime.Stop()) + "ms ", -1);
			}
			
            //load reservation logic            
            if (typeof (Reservation) != "undefined" && Reservation) {
                var reservationTime = new Timer();
                Reservation.OnReady();
                Log.Info("Main: Reservation Javascript load time was: " + (reservationTime.Stop()) + "ms ", -1);
            }

            //load destination/location specific logic
            if (typeof (LocationMain) != "undefined" && LocationMain) {
                var locationMainTime = new Timer();
                LocationMain.OnReady();
                Log.Info("Main: LocationMain Javascript load time was: " + (locationMainTime.Stop()) + "ms ", -1);
            }

            // logic for static pages
            if (typeof (StaticMain) != "undefined" && StaticMain) {
                var staticMainTime = new Timer();
                StaticMain.OnReady();
                Log.Info("Main: StaticMain Javascript load time was: " + (staticMainTime.Stop()) + "ms ", -1);
            }

            //Binds the events for the "direct to" control
            if (typeof (DirectTo) != "undefined" && DirectTo) {
                var directToTime = new Timer();
                var directTo = new DirectTo({ 'container': $('div.directto>select').get(0), 'searchpagePath': [Resource.GetText('searchpage')], 'offerspagePath': [Resource.GetText('offerspage')] });
                Log.Info("Main: DirectTo Javascript init time was: " + (directToTime.Stop()) + "ms ", -1);
            }

            if (Resource.GetText("show_ribbon_tag") == "true") {
                var userOpinionTime = new Timer();

                var pathPrefix = Resource.GetText('path_prefix');
                var userOpinion = new UserOpinion({
                    handler: pathPrefix + "/js/ajax/sendreaction.ashx",
                    popupContent: pathPrefix + "/documents/html/popups/send_reaction.htm"
                });

                Log.Info("Main: UserOpinion Javascript init time was: " + (userOpinionTime.Stop()) + "ms ", -1);
            }

            // Bind event click for topright promo
            priv.bindPromoEvents();

            //end overallTimer
            Log.Info("Main: Overall Javascript load time was: " + (overallTime.Stop()) + "ms ", -1);
        },

        ChangeCountriesVisibility: function() {
            $countryLinks = $('#country-links');
            if ($countryLinks.is(':hidden')) {
                $countryLinks.show();
            }
            else {
                $countryLinks.hide();
            }
        }
    };
} (jQuery);


// The supplied function is executed when dom is ready. In this case,
// we execute the Main.OnReady function, which will execute all functions
// necessary for the page to work correctly
$(document).ready(function($){
    Main.OnReady();
});

