﻿/**
 * static object that handles page logic
 * @class 
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var HomeMain = function($) {

	/**
	* @namespace Private methods and variables
	*/
	var priv = {

		promotionBlockListElement: 'div.block-list ul li.item, ul.hotels li.item',

		top10ListElement: '#content div.top10 ul li a',

		//
		/**
		* @namespace Private methods and variables
		*/
		initAdRotator: function() {
			//Enable the adrotator functionality
			try {
				var ulBanner = new BannerPool({
					container: $("#content ul.gallery").get(0),
					useThumbnails: true,
					debuglogger: function(msg) { Log.Debug(msg); }
				}
                );
				//make the elements clickable
				$("#content ul.gallery li").bind("click",
                    function(evt) {
                    	var newloc = $(this).find("a").attr("href");

                    	//deterimine if we need to open a new window
                    	if (newloc && $(this).find("a").attr("target") != "") {
                    		return;
                    	}
                    	else if (newloc) {
                    		document.location.href = newloc;
                    	}
                    }
                );
			}
			catch (e) {
				Log.Debug("HomeMain : error initializing the adrotator: " + e.toString());
			}
		},

		bindPromoEvents: function() {
			$(priv.promotionBlockListElement).hover(
                function() { $(this).addClass("hover"); },
                function() { $(this).removeClass("hover"); }
            );
		},

		bindSearchBoxEvents: function() {
			// Search button
			$('#homesearch').bind("focus",
                function() {
                	this.value = '';
                }
            ).bind("keydown",
                function(evt) {
                	if (evt.keyCode == 13) {
                		priv.inputSubmit();
                	}
                }
            );

			$('#homesearchbutton').bind("click",
                function() {
                	priv.inputSubmit();
                }
            );

			// Add hover class to submit button. Fixes hover state for IE6.
			$('#searchbar .submit').hover(
                function() { $(this).addClass("submit-hover"); },
                function() { $(this).removeClass("submit-hover"); }
            );
		},

		top10Tooltip: function() {
			//enable tooltips top 10 destinations            
			$(priv.top10ListElement).each(function() {
				$(this).jHelperTip({
					trigger: "hover",
					source: "container",
					dC: "#tip" + $(this).attr('rel'),
					autoClose: true,
					topOff: 20,
					leftOff: -30,
					initializeOnce: false
				});
			});
		},

		initCountryMap: function() {
			if (typeof (Country) == "undefined") {
				return;
			}
			
			//define the areacontainer containing all possible countries with id, image and map area
			var countryMapAreas = [
				new Country({ id: "2", image: "images/hovermaps/homepage/austria-hover.png", area: [295, 102, 284, 106, 280, 104, 277, 105, 273, 100, 275, 96, 279, 99, 281, 96, 285, 97, 296, 94, 301, 96, 300, 87, 312, 82, 316, 82, 320, 78, 332, 80, 335, 87, 333, 92, 329, 100, 329, 101, 322, 105, 313, 108, 304, 107, 296, 105, 294, 102, 285, 103, 285, 105, 283, 105] }),
				new Country({ id: "3", image: "images/hovermaps/homepage/belgium-hover.png", area: [218, 53, 230, 62, 232, 65, 236, 63, 235, 67, 242, 71, 243, 64, 246, 62, 243, 55, 241, 52, 235, 48, 229, 50, 224, 48, 217, 50] }),
				new Country({ id: "17", image: "images/hovermaps/homepage/croatia-hover.png", area: [344, 148, 322, 136, 314, 125, 311, 128, 309, 121, 320, 120, 323, 120, 322, 116, 326, 115, 326, 111, 331, 106, 341, 112, 348, 114, 353, 115, 356, 119, 355, 123, 352, 122, 334, 122, 330, 124, 327, 123, 328, 127, 345, 149] }),
				new Country({ id: "13", image: "images/hovermaps/homepage/france-hover.png", area: [214, 157, 200, 154, 196, 152, 185, 151, 172, 140, 182, 114, 182, 106, 175, 95, 175, 91, 158, 79, 159, 72, 172, 73, 174, 75, 184, 75, 184, 63, 187, 64, 191, 68, 200, 68, 201, 65, 211, 61, 213, 52, 220, 51, 244, 72, 247, 72, 261, 79, 256, 98, 252, 98, 243, 113, 249, 111, 250, 114, 251, 122, 250, 125, 248, 125, 251, 136, 256, 138, 256, 142, 267, 157, 272, 155, 273, 159, 270, 172, 267, 167, 264, 157, 270, 155, 256, 142, 246, 149, 239, 150, 224, 144, 215, 149, 214, 156] }),
				new Country({ id: "8", image: "images/hovermaps/homepage/germany-hover.png", area: [253, 26, 259, 22, 263, 25, 267, 20, 267, 12, 267, 8, 272, 9, 279, 13, 284, 13, 284, 18, 286, 18, 298, 13, 306, 22, 313, 54, 308, 54, 292, 63, 296, 73, 306, 81, 299, 88, 299, 96, 294, 95, 283, 98, 280, 97, 276, 99, 270, 97, 262, 94, 259, 95, 256, 95, 261, 79, 249, 77, 248, 71, 248, 68, 244, 65, 246, 61, 243, 55, 246, 48, 247, 44, 250, 44, 252, 40, 251, 35, 254, 31, 254, 29, 253, 26] }),
				new Country({ id: "20", image: "images/hovermaps/homepage/italy-hover.png", area: [255,141, 257,138, 250,134, 250,130, 249,125, 252,123, 252,116, 260,115, 263,111, 269,116, 272,110, 277,111, 279,111, 279,108, 279,108, 281,105, 284,105, 293,103, 297,108, 307,109, 306,119, 297,124, 297,128, 296,131, 300,139, 306,142, 321,162, 332,163, 333,168, 353,178, 357,186, 355,185, 350,181, 345,181, 340,190, 344,192, 346,199, 340,201, 338,208, 333,213, 331,211, 329,219, 330,225, 326,228, 302,215, 307,213, 308,213, 313,213, 316,213, 333,208, 334,206, 337,200, 332,187, 328,187, 323,184, 323,181, 317,180, 315,178, 312,174, 273,200, 270,200, 265,201, 260,196, 262,186, 261,179, 264,178, 271,175, 274,182, 274,199, 315,179, 311,174, 308,174, 282,153, 277,141, 267,136, 256,142] }),
				new Country({ id: "25763", image: "images/hovermaps/homepage/luxemburg-hover.png", area: [243, 72, 243, 65, 247, 70, 246, 73, 243, 73] }),
				new Country({ id: "25", image: "images/hovermaps/homepage/netherlands-hover.png", area: [225, 48, 235, 35, 239, 26, 250, 25, 253, 24, 253, 27, 254, 30, 254, 35, 252, 36, 252, 38, 250, 44, 246, 44, 246, 50, 244, 54, 244, 56, 242, 52, 235, 48, 230, 50, 225, 48] }),
				new Country({ id: "12", image: "images/hovermaps/homepage/spain-hover.png", area: [119, 223, 113, 213, 112, 208, 107, 205, 104, 205, 104, 199, 111, 194, 109, 190, 114, 184, 111, 174, 114, 176, 120, 159, 127, 155, 123, 149, 106, 143, 108, 135, 104, 130, 114, 128, 118, 123, 125, 128, 149, 135, 154, 135, 161, 136, 171, 139, 185, 150, 191, 151, 194, 150, 202, 155, 213, 157, 214, 162, 201, 169, 191, 172, 177, 190, 180, 198, 190, 197, 193, 195, 195, 196, 204, 191, 209, 187, 212, 190, 218, 187, 219, 186, 220, 188, 219, 189, 217, 187, 211, 189, 213, 191, 209, 194, 203, 192, 195, 196, 192, 199, 190, 198, 181, 198, 174, 202, 169, 212, 164, 211, 155, 219, 153, 219, 148, 219, 135, 217, 125, 218, 118, 223] })
			];
			//initiate the map
			countryMap = new CountryMap({
				element: $("#map").get(0),
				mapImage: "images/hovermaps/homepage/map.png",
				dynamicCountries: dynamicCountryList, //defined in the aspx
				staticCountries: countryMapAreas,
				logger: Log
			});

			//bind the links to the map
			$("#country-links a").hover(
				function(evt) {
					countryMap.SelectCountryById($(this).attr("rel"))
				},
				function(evt) {
					countryMap.DeSelectCountryById($(this).attr("rel"))
				}
			);
		}
	};

	/** @scope Main */
	return {

		/**
		* Initializes the logic for the current page
		* to be called on $(document).ready
		*/
		OnReady: function() {
			priv.bindPromoEvents();
			priv.bindSearchBoxEvents();
			priv.top10Tooltip();
			priv.initAdRotator();
			priv.initCountryMap();
		}
	};
} (jQuery);
