﻿/**
 * @class Location logic
 * depends on jQuery, jQuery-cookie, and sunweb static "logic" object
 */
var LocationMain = function($) {

	var config = {
		allDestinationsLinkElement: "div.country-blocks-holder a.all-destinations"
	};

	/**
	* @namespace Private methods and properties
	*/
	var priv = {

		locationPriceRibbonElement: 'div.white-box-block div.price-box',

		offerBlockListElement: 'ul.aside-list li.item, div.aside-box div.special-selected-item',

		/**
		* @private
		*/
		countryId: null,

		/**
		* @private
		*/
		regionId: null,

		/**
		* @private
		*/
		randomBoxId: null,

		/**
		* @private
		*/
		locationType: null,

		/**
		* maximum number of tips to show initially
		* @private
		*/
		maxTips: 10,

		/**
		* holds the name for the current sub-page (tab)
		* @private
		*/
		currentTab: null,

		openLightbox: function(id, options) {
			var lightbox = Lightbox.CreateCached(id, options);
			lightbox.Show();
		},

		locationPageReady: function() {

			//determine the current tab
			priv.setCurrentTab();

			priv.regionId = $("#regionId").val();
			priv.randomBoxId = $("#randomBoxId").attr("name");
			priv.locationType = $("#locationType").val().toLowerCase();

			priv.bindBlockListElement();
			priv.bindResultListEvents();

			$(priv.locationPriceRibbonElement).bind("click", function() {
				location.href = $('span.lmonth', $(this).parent()).text();
			});

			priv.resizeMapBox();
			priv.hideTips();

			if (priv.currentTab == "weather") {
				if (typeof (WeatherMain) != "undefined" && WeatherMain) {
					WeatherMain.AddEvenOddClasses();
				}
			}

			if (priv.locationType == 'city') {
				priv.bindAccommodationHover();
			}
		},

		bindBlockListElement: function() {
			$(priv.offerBlockListElement).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			}).bind("click", function() {
				var offerList = $(this).closest('ul.aside-list');

				if ($(offerList).length) {
					var searchQuery = $(offerList).next('div.more').find('input').val();
					//save searchquery in searchquery cookie
					var cookieName = 'searchquery';
					$.cookie(Resource.GetText('cookie-prefix') + cookieName, searchQuery, { raw: true, path: '/' });
				}

				//redirect to the correct page
				var url;

				if ($(this).find('a').size() > 0) {
					url = $(this).find('a').attr("href");
				}

				document.location.href = url;

				//prevent event bubbling
				return false;
			});
		},

		bindResultListEvents: function() {
			$("#result-list>li").hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});
			
			// Set attribute 'rel' with value of item index to maintain search query in detail page
			$("#result-list>li span.acco-name a").each(function(i) {
				$(this).attr('rel', i);
			});
		},

		bindAccommodationHover: function() {
			var path = Resource.GetText("path_prefix");
			var ttOptions = {
				trigger: "hover",
				autoClose: true,
				source: "ajax",
				type: "GET",
				loadingText: "",
				loadingImg: "",
				url: path + "/_html/popups/acco-hover-block.aspx",
				onShowCallback: Main.showHoverCallback
			};

			$('#sidebar div.sorted-list-block ul li').each(function() {
				var $this = $(this).find('a');
				ttOptions["data"] = "id=" + $this.attr("rel") + "&theme=accommodation";
				ttOptions["locationId"] = $this.attr("rel");
				$(this).jHelperTip(ttOptions);

				$(this).bind('click', function(e) {
					var accoList = $(this).closest('ul');

					if ($(accoList).length) {
						var searchQuery = $(accoList).next('input').val();
						//save searchquery in searchquery cookie
						var cookieName = 'searchquery';
						$.cookie(Resource.GetText('cookie-prefix') + cookieName, searchQuery, { raw: true, path: '/' });
					}
					
					location.href = $this.attr('href');
					return false;
				});
			});
		},

		/**
		* Void, fills the property currentTab with the correct value
		* @private
		*/
		setCurrentTab: function() {
			priv.currentTab = $.query.get('view').toLowerCase();
			if (priv.currentTab.length < 1) {
				priv.currentTab = $('body').attr('class');
			}
			Log.Debug("LocationMain: currentTab is '" + priv.currentTab + "'");
		},

		resizeMapBox: function() {
			var maxHeight = 0;
			if($('#static-map-container')[0] != undefined){
				maxHeight = $('#static-map-container').height() - 10;
			}else{
				maxHeight = $("[id*=locationImage]").height() -10;
			}

			var resourcetxt = "";
			if(priv.locationType == "country"){
				resourcetxt = Resource.GetText("more_locations_show_country");
			}else{
				resourcetxt = Resource.GetText("more_locations_show_region");
			}
			if (maxHeight > 0) {
				if ($('div.map-box').height() > maxHeight) {
					$('div.map-box ul.locations').append('<li class="more-locations-show"><a class="more-locations-show">' + resourcetxt + '&nbsp;<img src="' + Resource.GetText('path_prefix') + '/images/arrow-02.gif" heigth="6" width="5" alt="" style="display:inline" /></a></li>');
					$('div.map-box div.more-locations').append($('<ul></ul>'));
				}

				while ($('div.map-box').height() > maxHeight) {
					Log.Debug("LocationMain: resizing region box");
					var length = $('div.map-box ul.locations li').length;
					if (length >= 2)
					{
						var removedElement = $('div.map-box ul.locations li:eq(' + (length - 2) + ')').remove();
						$('div.map-box div.more-locations ul').prepend(removedElement);
					}
					else { break; }
				}

				$('a.more-locations-show').bind('click', function(e) {
					$('div.more-locations').toggle();
				});

				$('div.map-box .c').bind('mouseleave', function(e) {
					if ($('div.more-locations').css('display') == 'block') {
						$('div.more-locations').toggle();
					}
				});

				$('a.more-locations-close').bind('click', function(e) {
					$('div.more-locations').hide();
				});
			}
		},

		hideTips: function() {
			Main.hideItems(priv.maxTips, "tips", "#tip-count");
		},

		destinationsPageReady: function() {
			$("#content div.country-block .price").bind("click", function(e) {
				var url = $(this).closest('.img-block').find('a.image-block').attr('href');
				location.href = url;
			});


			$('div.destination-block h2').hover(function() {
				$(this).addClass('destination-hover');
			}, function() {
				$(this).removeClass('destination-hover');
			});

			$(config.allDestinationsLinkElement).bind("click", function() {
				LocationMain.showDestinationsPopup($(this).attr('href'));
				return false;
			});
		},

		initCountryMap: function() {
			//test if we have a map
			if (!$("#map").get(0)) {
				return;
			}
			//define the areacontainer containing all possible countries with id, image and map area
			var countryMapAreas = [
				new Country({ id: "20", activeImage: "images/hovermaps/destinations/italy.png", image: "images/hovermaps/destinations/italy-hover.png", area: [345, 316, 349, 308, 349, 306, 331, 302, 328, 291, 331, 283, 331, 278, 324, 275, 321, 266, 333, 261, 334, 256, 327, 245, 333, 239, 347, 236, 354, 238, 364, 230, 364, 224, 368, 218, 372, 227, 379, 234, 384, 241, 386, 236, 392, 227, 395, 218, 400, 225, 406, 222, 414, 227, 415, 212, 423, 214, 424, 204, 429, 204, 438, 206, 440, 200, 466, 195, 470, 204, 504, 214, 500, 222, 504, 227, 506, 239, 502, 243, 495, 241, 494, 245, 474, 254, 474, 266, 479, 272, 474, 279, 472, 279, 483, 310, 510, 326, 547, 379, 461, 379, 451, 373, 443, 370, 445, 368, 433, 359, 433, 354, 425, 352, 426, 340, 416, 313, 409, 309, 390, 299, 379, 296, 372, 299, 361, 313, 343, 318] }),
				new Country({ id: "1", activeImage: "images/hovermaps/destinations/andora.png", image: "images/hovermaps/destinations/andora-hover.png", area: [173, 353, 175, 351, 185, 354, 184, 358, 174, 360, 172, 358, 173, 355, 173, 353] }),
				new Country({ id: "8", activeImage: "images/hovermaps/destinations/germany.png", image: "images/hovermaps/destinations/germany-hover.png", area: [340, 1, 336, 5, 337, 10, 318, 11, 320, 28, 312, 44, 315, 52, 323, 68, 316, 76, 324, 88, 322, 99, 331, 113, 366, 123, 351, 156, 350, 173, 372, 174, 372, 167, 380, 169, 402, 178, 408, 176, 415, 183, 422, 177, 435, 177, 438, 179, 445, 180, 467, 171, 470, 167, 481, 167, 488, 175, 479, 152, 492, 142, 497, 133, 503, 133, 503, 127, 478, 105, 468, 92, 469, 83, 461, 68, 465, 74, 476, 65, 482, 64, 512, 44, 511, 40, 516, 38, 522, 44, 527, 31, 524, 23, 516, 11, 517, 1, 338, 0] }),
				new Country({ id: "13", activeImage: "images/hovermaps/destinations/france.png", image: "images/hovermaps/destinations/france-hover.png", area: [393, 360, 395, 348, 399, 379, 374, 378, 372, 374, 377, 365, 387, 359, 393, 359, 343, 318, 320, 339, 299, 341, 265, 328, 247, 322, 218, 337, 218, 360, 204, 364, 185, 358, 184, 353, 175, 350, 173, 353, 151, 339, 156, 346, 123, 340, 97, 324, 88, 310, 101, 294, 120, 230, 114, 216, 120, 220, 117, 204, 95, 178, 98, 162, 93, 155, 54, 130, 47, 133, 43, 121, 45, 114, 43, 111, 46, 103, 62, 100, 72, 104, 75, 99, 83, 99, 95, 111, 125, 115, 118, 73, 135, 74, 136, 87, 163, 94, 172, 89, 168, 85, 180, 76, 206, 64, 211, 40, 233, 35, 243, 52, 247, 50, 250, 56, 257, 60, 258, 65, 267, 67, 270, 78, 279, 79, 286, 74, 284, 80, 302, 100, 308, 99, 323, 102, 331, 114, 364, 123, 347, 157, 348, 173, 340, 179, 334, 176, 330, 183, 332, 185, 318, 198, 306, 215, 308, 218, 304, 226, 308, 228, 313, 226, 322, 218, 327, 222, 331, 234, 326, 239, 333, 257, 321, 265, 325, 276, 329, 278, 326, 288, 329, 301, 342, 305, 347, 305, 343, 317] }),
				new Country({ id: "2", activeImage: "images/hovermaps/destinations/austria.png", image: "images/hovermaps/destinations/austria-hover.png", area: [505, 212, 472, 205, 466, 193, 441, 197, 436, 203, 424, 201, 423, 196, 413, 199, 404, 194, 401, 184, 405, 175, 410, 178, 417, 186, 422, 180, 423, 176, 432, 178, 443, 181, 466, 171, 467, 169, 482, 171, 488, 178, 486, 160, 481, 152, 494, 142, 498, 131, 502, 135, 506, 127, 512, 132, 525, 132, 531, 124, 533, 115, 555, 118, 563, 123, 583, 126, 588, 146, 589, 157, 581, 160, 577, 159, 574, 163, 580, 167, 577, 185, 570, 192, 563, 199, 547, 203, 531, 215, 506, 211] }),
				new Country({ id: "5", activeImage: "images/hovermaps/destinations/switzerland.png", image: "images/hovermaps/destinations/switzerland-hover.png", area: [332, 236, 329, 221, 324, 217, 313, 220, 307, 224, 309, 218, 310, 212, 332, 184, 335, 176, 336, 178, 348, 174, 372, 174, 372, 169, 375, 167, 381, 171, 398, 176, 401, 185, 396, 190, 396, 194, 402, 194, 405, 198, 418, 200, 420, 197, 421, 211, 417, 209, 413, 222, 408, 219, 397, 221, 393, 213, 389, 223, 384, 237, 381, 233, 371, 221, 368, 216, 363, 224, 354, 235, 347, 233, 334, 237, 333, 235] })
			];
			//initiate the map
			countryMap = new CountryMap({
				element: $("#map").get(0),
				mapImage: "images/hovermaps/destinations/map.png",
				dynamicCountries: dynamicCountryList,
				staticCountries: countryMapAreas,
				logger: Log,
				useActiveImages: true,
				activeCountryOpacity: 1,
				hoverCountryOpacity: 0.5,
				emptyImage: "images/" + Resource.GetText("site_identifier") + "/hovermaps/destinations/overlay.gif"
			});

			//bind the links to the map
			$("div.map-overview-holder div.white-box a").hover(
				function(evt) {
					countryMap.SelectCountryById($(this).attr("rel"))
				},
				function(evt) {
					countryMap.DeSelectCountryById($(this).attr("rel"))
				}
			);
		}
	};

	/**
	* @scope LocationMain
	*/
	return {

		showDestinationsPopup: function(url) {
			var id = 'pDestinations';
			var config = {
				contentUrl: url + " #result-destination-popup",
				container: document.getElementById(id),
				width: '275px'
			};
			priv.openLightbox(url, config);
		},

		/**
		* should be triggered on page ready
		*/
		OnReady: function() {
			priv.countryId = $("#countryId").val();

			switch ($('body').attr('class')) {
				case 'destination-location':
					priv.locationPageReady();
					break;
				case 'destination-overview':
					priv.destinationsPageReady();
					break;
			}

			//copy the ups-texts to the lightbox list
			$("#usp-texts-lightbox").html($("#usp-texts").html());

			priv.initCountryMap();
		}
	};
} (jQuery);

