﻿/**
 * @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><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: "4", image: "images/hovermaps/destinations/bulgaria.png", area: [600, 94, 606, 100, 630, 100, 640, 89, 659, 89, 654, 104, 653, 110, 659, 114, 652, 115, 643, 123, 643, 126, 628, 130, 625, 128, 611, 133, 609, 125, 603, 121, 607, 110, 600, 103] }),
				new Country({ id: "6", image: "images/hovermaps/destinations/cyprus.png", area: [727, 192, 749, 179, 743, 193, 734, 198, 728, 195] }),
				new Country({ id: "24219", image: "images/hovermaps/destinations/dubai.png", area: [521, 338, 550, 338, 577, 307, 581, 318, 579, 325, 576, 335, 569, 356, 534, 357, 519, 340] }),
				new Country({ id: "11", image: "images/hovermaps/destinations/egypt.png", area: [651, 263, 696, 272, 720, 263, 742, 269, 749, 269, 749, 379, 651, 378, 651, 292, 647, 284, 651, 274, 650, 268, 651, 263] }),
				new Country({ id: "15", image: "images/hovermaps/destinations/gambia.png", area: [341, 324, 385, 323, 391, 315, 406, 314, 419, 321, 432, 328, 451, 324, 455, 333, 439, 339, 431, 338, 407, 329, 400, 325, 396, 331, 373, 334, 372, 341, 335, 341, 334, 345, 330, 334, 341, 323] }),
				new Country({ id: "16", image: "images/hovermaps/destinations/greece.png", area: [642, 123, 647, 124, 645, 134, 644, 147, 651, 167, 634, 136, 628, 145, 620, 153, 611, 150, 620, 161, 633, 171, 645, 180, 632, 182, 626, 190, 627, 201, 634, 212, 663, 210, 663, 214, 650, 217, 632, 215, 632, 211, 624, 201, 609, 199, 600, 184, 587, 164, 593, 145, 608, 139, 623, 130, 639, 129, 643, 126] }),
				new Country({ id: "20", image: "images/hovermaps/destinations/italy.png", area: [454, 112, 454, 108, 448, 107, 448, 100, 445, 95, 451, 92, 449, 85, 457, 84, 462, 78, 467, 86, 471, 76, 478, 79, 483, 73, 495, 69, 511, 75, 512, 86, 501, 91, 500, 95, 501, 97, 499, 99, 513, 116, 521, 131, 527, 136, 540, 137, 539, 141, 566, 156, 567, 161, 565, 162, 552, 154, 547, 166, 553, 169, 555, 174, 550, 178, 550, 181, 544, 190, 536, 190, 534, 198, 537, 204, 534, 208, 529, 208, 524, 204, 521, 204, 506, 197, 506, 191, 514, 190, 523, 191, 534, 188, 541, 182, 543, 176, 537, 163, 530, 160, 527, 155, 524, 155, 513, 148, 510, 148, 470, 148, 476, 160, 474, 177, 469, 177, 466, 181, 460, 176, 462, 168, 458, 154, 470, 148, 510, 148, 491, 133, 483, 125, 478, 111, 465, 106, 454, 113] }),
				new Country({ id: "24", image: "images/hovermaps/destinations/malta.png", area: [526, 215, 532, 215, 533, 223, 524, 223] }),
				new Country({ id: "27", image: "images/hovermaps/destinations/portugal.png", area: [288, 114, 296, 113, 294, 117, 303, 119, 308, 117, 311, 117, 312, 122, 314, 123, 305, 131, 300, 147, 295, 147, 298, 156, 293, 163, 296, 169, 288, 176, 288, 179, 283, 181, 274, 178, 271, 178, 278, 159, 274, 160, 271, 154, 288, 127, 288, 114] }),
				new Country({ id: "12", image: "images/hovermaps/destinations/spain.png", area: [288, 99, 299, 98, 298, 94, 307, 93, 309, 98, 323, 101, 344, 106, 362, 112, 374, 119, 384, 121, 407, 128, 408, 136, 396, 144, 382, 147, 382, 149, 368, 165, 397, 166, 405, 162, 416, 161, 417, 165, 412, 163, 406, 163, 409, 167, 405, 171, 398, 167, 388, 174, 385, 177, 383, 174, 386, 172, 396, 167, 368, 165, 370, 172, 373, 175, 362, 188, 359, 191, 353, 191, 349, 193, 344, 200, 318, 196, 304, 203, 299, 198, 292, 183, 220, 289, 220, 294, 217, 296, 216, 303, 208, 305, 195, 308, 192, 310, 190, 307, 180, 307, 172, 307, 163, 298, 167, 296, 176, 303, 185, 298, 192, 304, 211, 303, 214, 294, 219, 291, 292, 182, 287, 182, 286, 176, 296, 169, 293, 164, 298, 158, 294, 149, 300, 149, 306, 132, 313, 126, 311, 125, 310, 120, 306, 118, 303, 120, 295, 118, 297, 114, 288, 114, 290, 109, 286, 99] }),
				new Country({ id: "28", image: "images/hovermaps/destinations/tunisia.png", area: [465, 199, 477, 193, 485, 194, 487, 200, 493, 198, 489, 206, 490, 212, 494, 218, 494, 221, 483, 232, 500, 245, 500, 253, 485, 267, 484, 276, 481, 282, 478, 279, 472, 260, 458, 243, 456, 234, 465, 221, 465, 199] }),
				new Country({ id: "29", image: "images/hovermaps/destinations/turkey.png", area: [644, 121, 651, 116, 657, 115, 660, 120, 692, 119, 709, 102, 729, 93, 748, 98, 749, 163, 741, 172, 728, 180, 704, 175, 705, 181, 692, 186, 676, 185, 651, 169, 649, 151, 650, 141, 646, 135] }),
				new Country({ id: "17", image: "images/hovermaps/destinations/croatia.png", area: [513, 90, 521, 87, 528, 88, 530, 83, 529, 79, 537, 73, 551, 82, 556, 82, 561, 79, 566, 88, 564, 92, 561, 93, 553, 90, 539, 91, 537, 93, 534, 91, 532, 96, 536, 101, 538, 104, 550, 114, 553, 121, 549, 121, 536, 114, 528, 107, 523, 101, 516, 97, 515, 98, 511, 90] }),
				new Country({ id: "23", image: "images/hovermaps/destinations/morocco.png", area: [306, 211, 311, 212, 317, 219, 338, 219, 348, 222, 353, 247, 358, 257, 341, 258, 328, 263, 329, 272, 314, 280, 287, 281, 286, 292, 275, 299, 274, 311, 225, 313, 250, 298, 265, 281, 264, 261, 269, 250, 286, 240, 298, 228] })
			];
			//initiate the map
			countryMap = new CountryMap({
				element: $("#map").get(0),
				mapImage: "images/" + Resource.GetText('site_identifier') + "/hovermaps/destinations/map.png",
				dynamicCountries: dynamicCountryList,
				staticCountries: countryMapAreas,
				logger: Log
			});

			//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);

