﻿/**
 * 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
		msie6: ($.browser.msie && typeof (XMLHttpRequest) == "undefined" && (/MSIE 6\.0/i.test(window.navigator.userAgent)) && !(/MSIE 7\.0/i.test(window.navigator.userAgent)) && !(/MSIE 8\.0/i.test(window.navigator.userAgent))),
		reservation: false,

		bindEvents: function() {
			$('#nr-adults').bind("change", function() {
				var newValue = parseInt($('#nr-adults').val());

				if (newValue >= 5) {
					$('#pTravelers div.nroccs div.extranote').css('display', '');
				}
				else {
					$('#pTravelers div.nroccs div.extranote').css('display', 'none');
				}
			});
		}
	};

	/** @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' || $.query.get("jsdebug") == 'true');
			Log.SetLevel(priv.debugSeverity);

			if (typeof (Utils) != "undefined" && Utils) {
				var utilsTime = new Timer();
				Utils.OnReady();
				Log.Info("Main: Utils Javascript load time was: " + (utilsTime.Stop()) + " ms", -1);
			}

			//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({ width: 325 });
				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({ compareLinkElement: ".items-form .btn-compare", compareMaxCount: 5 });
				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 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 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 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);
			}

			if (typeof (StaticMap) != "undefined" && StaticMap) {
				var staticMapTime = new Timer();
				StaticMap.OnReady();
				Log.Info("Main: StaticMap Javascript load time was: " + (staticMapTime.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);
			}

			//load compare specific logic
			if (typeof (CompareMain) != "undefined" && CompareMain) {
				var compareMainTime = new Timer();
				CompareMain.OnReady();
				Log.Info("Main: CompareMain Javascript load time was: " + (compareMainTime.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 + "/_html/popups/contentpopup.aspx?contentfile=send_reaction&title=" +
						encodeURIComponent(Resource.GetText('send_reaction_title'))
				});

				Log.Info("Main: UserOpinion Javascript init time was: " + (userOpinionTime.Stop()) + "ms ", -1);
			}
			
			priv.reservation = $('form[id$=formBooking]').length > 0;
			priv.basket = $('form[id$=formBasket]').length > 0;

			// Note: Call custom form after all calls if select of checkboxes are present on the page (perfomance)
			var customFormTime = new Timer();
			//&& $("select, checkbox").not("#pTravelers select").length > 0
			if (!priv.msie6 && !priv.reservation && !priv.basket) {
				jQuery('div.directto, div.dateselection').customForm({
					dropMaxHeight: 200,
					dropWidth: true
				});
			}
			Log.Info("Main: Customform Javascript init time was: " + (customFormTime.Stop()) + "ms ", -1);

			priv.bindEvents();

			//end overallTimer
			Log.Info("Main: Overall Javascript load time was: " + (overallTime.Stop()) + "ms ", -1);
		},


		/**
		* Only show the first X items of a list; the rest is hidden and can be toggled by using a 'view more' link
		* @param {Integer} maxItemsCount The maximum number of items to show initially
		* @param {String} setName The class of the list
		* @param {String} (Optional) countSelector The css selector for selecting a place to show the count of remaining items
		* @private
		*/
		hideItems: function(maxItemsCount, setName, countSelector) {
			Log.Info("AccoMain: Called priv." + setName + "Tab");

			//get the items html object
			var listSelector = 'ul.' + setName;
			var $items = $(listSelector + ' li');

			// hide all items that are more than the maximum
			if ($items.length > maxItemsCount) {
				for (var i = maxItemsCount; i < $items.length; i++) {
					$($items[i]).css("display", "none");
				}

				// set the total number of items
				if (typeof (countSelector) != "undefined" && countSelector) {
					$(countSelector).html('(' + ($items.length - maxItemsCount) + ')');
				}

				// bind the show all items event to show all items link
				$('div.show-' + setName + ' a').bind("click", function() {
					$items.show();
					$('div.show-' + setName).hide();
					//TODO correct the shades?
				});

				$('div.show-' + setName).show();
			}
			else {
				$('div.show-' + setName).hide();
			}

			//finally show the ul
			$(listSelector).show();
		},
		
		showHoverCallback: function(id, hoverContent) {
			jHelperTipExtensions.AddRenderedHover(id, hoverContent);
		}
	};
} (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();
});

