﻿/**
* @class Result list logic and functionallity
* depends on jQuery, jQuery-cookie, and sunweb static "logic" object
*/
var accoDetails = function($) {

	/**
	* @namespace Private methods and properties
	*/
	var priv = {
		/**
		* 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();
		},

		bindMoreInfoPopups: function() {
			var containerWidth = '432px';

			//Surroundings
			$('#acco-surroundings a.more-info').bind("click", function() {
				var id = 'pAccoSurroundings';
				priv.openLightbox(id, {
					container: $("#" + id).get(0),
					width: containerWidth
				});

				return false;
			});


			//Facilities
			$('#acco-facilities a.more-info').bind("click", function() {
				var id = 'pAccoFacilities';
				priv.openLightbox(id, {
					container: $("#" + id).get(0),
					width: containerWidth
				});

				return false;
			});


			//Roomtypes
			$('#acco-roomtypes a.more-info').bind("click", function() {
				var id = 'pAccoRoomtypes';
				priv.openLightbox(id, {
					container: $("#" + id).get(0),
					width: containerWidth
				});

				return false;
			});

			//Carrental (may or may not be visible)
			//Add 'more-info' class to the a tag. (Adding it in the textresources is not that smart)
			$('#acco-carrental a').addClass(function() {
				return 'more';
			});

			$('#acco-carrental a.more').bind("click", function() {
				var id = 'pAccoCarrental';
				priv.openLightbox(id, {
					container: $("#" + id).get(0),
					contentUrl: $('#acco-carrental a.more').attr('href'),
					title: Resource.GetText('ACCOMMODATION_CARRENTAL'),
					literal: false,
					width: containerWidth
				});

				return false;
			});
		},

		bindWeatherPopup: function() {

				// add container for question popups
				var container = $('<div/>').attr('id', 'lightboxPopup');
				$('body').append(container);

				// make all links with 'moreinfo' class open in lightbox
				$('a.weather-promo-link').each(function() { return priv.lightboxize(this, container); });
		},

		bindWeatherPopup: function() {

				// add container for question popups
				var container = $('<div/>').attr('id', 'lightboxPopup');
				$('body').append(container);

				// make all links with 'moreinfo' class open in lightbox
				$('a.weather-promo-link').each(function() { return priv.lightboxize(this, container); });
		},
        /**
        * Open a lightbox with contents of link URL.
        */
        lightboxize: function(element, container) {
            $(element).click(function() {
                var url = $(this).attr('href');
                var lightbox = Lightbox.CreateCached(
                    url,
                    {contentUrl: url,
                    width: '643px',
                    container: container,
                    literal: false},
                    priv.lightboxedCallback
                    );
                lightbox.Show();
                return false;
				});
            return element;
        },

        lightboxedCallback: function() {
            // does nothing if not photos were loaded
            Carousel.init('#popup .extended-info ul');
            // carousel top part is displayed incorrectly because of some styles clashing. Hiding it for now.
            $('div#photo-holder').hide();
		}
        
	};

	/**
	* @scope accoDetails
	*/
	return {
		/**
		* should be triggered on $(document).ready
		*/
		OnReady: function() {
			$(".offers-block tr.promorow").bind("click",
                function(evt) {
                	//find the link in the element, and use its Href
                	var Href = $(this).find("a").attr("href");
                	if (Href) {
                		document.location.href = Href;
                	}
                	return false;
                }
            );

			//for the promotion offerlist
			$(".offers-block td:last-child").addClass("price");
			$('.offers-block tr.promorow').hover(
                 function(evt) {
                 	$(this).addClass("hover");
                 	return false;
                 },
                function(evt) {
                	$(this).removeClass("hover");
                	return false;
                }
            );

			//bind the more info popups
			priv.bindMoreInfoPopups();
			priv.bindWeatherPopup();
		}
	};
} (jQuery);
