﻿/**
 * 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"); }
            ).bind("click",
                function() {
                	//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;
                }
            );
		},

		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"); }
            );

			// autocomplete functionality
			$('#homesearch').autocomplete(Resource.GetText('path_prefix') + "/js/ajax/get_searchsuggestions.ashx", {
				selectFirst: false,
				delay: 10,
				width: 200
			}).result(function(event, item) {
				priv.inputSubmit();
			});
		},

		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: "20", activeImage: "images/hovermaps/homepage/italy.png", image: "images/hovermaps/homepage/italy-hover.png", area: [174, 173, 177, 169, 176, 167, 172, 168, 167, 165, 165, 161, 167, 156, 163, 149, 168, 146, 170, 143, 165, 136, 176, 133, 178, 134, 184, 129, 187, 124, 188, 127, 195, 134, 199, 128, 201, 124, 205, 126, 210, 127, 210, 121, 213, 121, 215, 116, 222, 118, 222, 115, 237, 112, 243, 118, 255, 122, 254, 126, 256, 127, 257, 135, 239, 143, 245, 152, 240, 154, 246, 169, 259, 178, 279, 207, 295, 206, 297, 209, 295, 213, 301, 218, 247, 218, 216, 190, 208, 170, 192, 163, 176, 174] }),
				new Country({ id: "1", activeImage: "images/hovermaps/homepage/andora.png", image: "images/hovermaps/homepage/andora-hover.png", area: [88, 196, 88, 191, 93, 192, 92, 194, 88, 195] }),
				new Country({ id: "8", activeImage: "images/hovermaps/homepage/germany.png", image: "images/hovermaps/homepage/germany-hover.png", area: [174, 1, 170, 6, 172, 12, 167, 18, 161, 19, 163, 27, 158, 34, 163, 47, 160, 51, 165, 59, 163, 66, 185, 76, 178, 100, 190, 99, 204, 103, 206, 102, 211, 108, 214, 102, 222, 105, 237, 97, 247, 101, 243, 89, 256, 78, 238, 64, 234, 49, 261, 35, 265, 35, 265, 24, 262, 5, 256, 2, 254, 1, 175, 1] }),
				new Country({ id: "13", activeImage: "images/hovermaps/homepage/france.png", image: "images/hovermaps/homepage/france-hover.png", area: [117, 31, 106, 33, 104, 49, 86, 56, 89, 60, 81, 61, 70, 58, 69, 52, 61, 51, 63, 70, 48, 71, 43, 64, 23, 66, 22, 70, 26, 71, 22, 76, 46, 92, 62, 132, 44, 171, 65, 185, 78, 187, 88, 190, 91, 190, 95, 192, 95, 195, 111, 196, 113, 183, 125, 177, 151, 187, 174, 174, 192, 198, 189, 202, 191, 217, 199, 217, 202, 206, 199, 190, 198, 196, 193, 197, 174, 175, 176, 167, 166, 165, 165, 158, 167, 156, 161, 147, 167, 143, 165, 136, 166, 133, 163, 124, 155, 128, 155, 124, 167, 106, 169, 101, 172, 103, 174, 101, 175, 91, 183, 76, 164, 68, 163, 66, 154, 64, 142, 53, 136, 54, 135, 49, 117, 32] }),
				new Country({ id: "2", activeImage: "images/hovermaps/homepage/austria.png", image: "images/hovermaps/homepage/austria-hover.png", area: [258, 78, 268, 80, 270, 73, 295, 77, 300, 90, 299, 94, 293, 96, 290, 112, 269, 122, 240, 118, 236, 111, 231, 113, 222, 113, 220, 117, 215, 113, 210, 115, 204, 110, 205, 103, 213, 108, 215, 104, 222, 105, 238, 99, 246, 102, 247, 100, 244, 91, 252, 84, 254, 81, 256, 78] }),
				new Country({ id: "5", activeImage: "images/hovermaps/homepage/switzerland.png", image: "images/hovermaps/homepage/switzerland-hover.png", area: [157, 126, 156, 123, 170, 103, 173, 104, 177, 102, 190, 101, 190, 98, 202, 104, 202, 112, 206, 114, 214, 115, 214, 120, 210, 119, 209, 126, 203, 124, 200, 123, 195, 133, 187, 124, 180, 132, 167, 133, 166, 124, 156, 127] })
			];
			//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,
				useActiveImages: true,
				activeCountryOpacity: 1,
				hoverCountryOpacity: 0.5
			});

			//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);
