﻿/**
 * 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: 420 }, false, Main.occupancySelected);
				Log.Info("Main: Occupancy Javascript load time was: " + (occupancyTime.Stop()) + " ms", -1);
			}

			// 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({ resultListElements: '#content div.list-holder div.i-box-holder' });
				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 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);
			}
			//load destination/locationmap specific logic
			if (typeof (Destination) != "undefined" && Destination) {
				var destinationTime = new Timer();
				Destination.OnReady();
				Log.Info("Main: Destination Javascript load time was: " + (destinationTime.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 faq pages
			if (typeof (Faq) != "undefined" && Faq) {
				var faqTime = new Timer();
				Faq.OnReady();
				Log.Info("Main: StaticMain Javascript load time was: " + (faqTime.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 ($('td.hotspot').length) {
				$('td.hotspot').bind('click', function() {
					location.href = $(this).find('a').attr('href');
				});
			}

			if (typeof (bookingLogic) != "undefined") {
				bookingLogic.OnReady();
			}

			priv.reservation = $('.acco-ribbon-reservation').length > 0;


			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);
		},

		bindViewedFavoritesEvents: function() {
			$('#viewed-favorites a.clear-favorites').click(function() {
				PersonalItems.RemoveAll('favorites');
				PersonalItems.ShowItemLinks();
				$(this).parent().hide();
				$(this).parent().parent().addClass('inactive');
			});

			$('#viewed-favorites a.clear-viewed').click(function() {
				PersonalItems.RemoveAll('alreadyviewed');
				PersonalItems.ShowItemLinks();
				$(this).parent().hide();
				$(this).parent().parent().addClass('inactive');
			});
		},

		/**
		* Generic way to open a new lightbox: the contents of the container identified by the supplied id
		* are loaded in a lightbox. Also, the lightbox is cached and thus only has to be initialized once.
		* @param {String} id The id of the container to show in the lightbox
		* @param {Object} options (Optional) Several options can be supplied to override default behavior of just
		* loading the contents of the container. When supplied, these options will be used to initialize the lightbox.
		* @private
		*/
		openLightbox: function(id, options) {
			var lightbox = Lightbox.CreateCached(id, options);
			lightbox.Show();
		},
		
		occupancySelected: function() {
			var occupancyCount = Occupancy.getOccupancy();
			if (occupancyCount > 0) {
				var occupancyText = (occupancyCount == 1 ? Resource.GetText('travelers_person') :
					Resource.GetText('travelers_persons')).replace('{0}', occupancyCount);
				$('#prices-occ span.occupancy-text').html(occupancyText);
				$('#prices-occ span.occupancy-text').removeClass('no-occupancy');
			}
		}
	};
} (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();
});

