﻿/**
 * 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() {
                	
                }
            );
		},

		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() {
			//define the areacontainer containing all possible countries with id, image and map area
			var countryMapAreas = [
				new Country({ id: "4", image: "images/hovermaps/homepage/bulgaria.png", area: [290, 64, 290, 66, 301, 65, 307, 64, 316, 59, 320, 58, 325, 60, 324, 63, 322, 66, 322, 67, 321, 70, 324, 75, 320, 75, 315, 78, 314, 79, 314, 80, 307, 83, 303, 83, 295, 86, 294, 82, 290, 79, 289, 74, 291, 70, 287, 66, 290, 64] }),
				new Country({ id: "6", image: "images/hovermaps/homepage/cyprus.png", area: [369, 121, 372, 121, 371, 119, 373, 119, 378, 117, 379, 116, 379, 118, 379, 121, 378, 121, 378, 122, 374, 125, 370, 125, 368, 123] }),
				new Country({ id: "24219", image: "images/hovermaps/homepage/dubai.png", area: [220, 202, 222, 202, 223, 203, 227, 205, 230, 202, 241, 202, 243, 201, 245, 200, 247, 196, 255, 186, 255, 186, 258, 184, 258, 182, 259, 182, 260, 184, 261, 186, 262, 192, 260, 193, 258, 193, 257, 199, 259, 199, 259, 201, 256, 201, 253, 215, 241, 215, 230, 214, 220, 204, 220, 201] }),
				new Country({ id: "11", image: "images/hovermaps/homepage/egypt.png", area: [319, 230, 319, 178, 319, 173, 318, 169, 320, 165, 331, 168, 348, 173, 362, 167, 379, 171, 379, 229, 319, 230] }),
				new Country({ id: "15", image: "images/hovermaps/homepage/gambia.png", area: [139, 195, 165, 195, 168, 190, 171, 190, 179, 191, 182, 193, 184, 194, 187, 194, 193, 199, 197, 197, 197, 204, 191, 203, 173, 195, 172, 198, 169, 201, 164, 200, 158, 201, 158, 205, 136, 205, 135, 208, 133, 204, 134, 200, 137, 198, 137, 198, 138, 200, 140, 200, 139, 195] }),
				new Country({ id: "16", image: "images/hovermaps/homepage/greece.png", area: [278, 104, 283, 94, 283, 92, 286, 92, 290, 89, 293, 89, 293, 87, 304, 84, 310, 84, 314, 81, 315, 80, 316, 79, 316, 82, 316, 87, 301, 89, 301, 91, 302, 94, 300, 96, 295, 94, 294, 94, 296, 99, 299, 103, 303, 104, 300, 105, 306, 108, 312, 112, 307, 111, 306, 113, 307, 115, 300, 115, 303, 127, 308, 133, 326, 134, 317, 137, 309, 137, 308, 134, 302, 127, 300, 126, 299, 129, 295, 124, 293, 126, 292, 121, 287, 117, 289, 114, 284, 110, 287, 109, 278, 104] }),
				new Country({ id: "20", image: "images/hovermaps/homepage/italy.png", area: [198, 74, 199, 71, 194, 69, 194, 67, 194, 66, 192, 63, 194, 61, 194, 58, 195, 56, 201, 55, 203, 52, 205, 56, 209, 51, 211, 52, 213, 50, 214, 48, 217, 48, 220, 47, 224, 47, 227, 49, 231, 50, 233, 57, 226, 60, 227, 65, 226, 66, 229, 73, 234, 75, 247, 88, 251, 88, 251, 91, 267, 100, 267, 103, 265, 103, 264, 100, 258, 99, 256, 105, 260, 111, 256, 114, 252, 122, 249, 121, 248, 132, 230, 125, 230, 122, 234, 121, 250, 119, 252, 115, 254, 113, 251, 106, 244, 103, 244, 100, 239, 99, 237, 95, 232, 95, 219, 85, 207, 96, 211, 100, 209, 114, 207, 113, 205, 114, 202, 113, 202, 101, 200, 100, 202, 97, 203, 98, 207, 95, 220, 86, 215, 81, 214, 73, 206, 69, 201, 72, 198, 74] }),
				new Country({ id: "24", image: "images/hovermaps/homepage/malta.png", area: [242, 136, 248, 139, 243, 145, 241, 141] }),
				new Country({ id: "27", image: "images/hovermaps/homepage/portugal.png", area: [95, 117, 89, 117, 83, 114, 87, 103, 86, 103, 85, 98, 94, 82, 95, 75, 98, 75, 99, 77, 103, 78, 105, 78, 108, 79, 110, 83, 106, 84, 101, 96, 99, 96, 99, 101, 100, 102, 97, 106, 98, 109, 95, 112, 94, 117] }),
				new Country({ id: "12", image: "images/hovermaps/homepage/spain.png", area: [94, 116, 95, 112, 99, 109, 97, 106, 100, 101, 99, 96, 102, 95, 103, 92, 104, 91, 105, 85, 110, 82, 107, 78, 105, 77, 104, 78, 98, 76, 99, 75, 98, 74, 94, 75, 97, 69, 95, 65, 97, 64, 101, 64, 103, 62, 109, 64, 116, 66, 124, 69, 140, 73, 147, 77, 151, 79, 155, 79, 162, 83, 170, 83, 170, 88, 159, 93, 155, 93, 154, 95, 144, 106, 147, 111, 155, 112, 156, 110, 157, 111, 164, 108, 165, 105, 166, 104, 169, 105, 172, 105, 173, 104, 175, 104, 176, 106, 173, 106, 168, 105, 169, 108, 167, 110, 162, 108, 157, 110, 154, 113, 153, 112, 147, 111, 139, 121, 133, 122, 130, 127, 118, 125, 109, 126, 104, 130, 100, 120, 95, 117, 52, 184, 50, 191, 44, 194, 36, 196, 33, 195, 24, 194, 16, 196, 15, 195, 17, 194, 17, 190, 16, 186, 18, 186, 19, 192, 23, 192, 25, 192, 26, 190, 32, 189, 31, 191, 34, 193, 37, 192, 39, 192, 45, 192, 52, 183, 52, 184, 94, 117] }),
				new Country({ id: "28", image: "images/hovermaps/homepage/tunisia.png", area: [203, 127, 208, 125, 210, 123, 216, 123, 218, 127, 221, 125, 222, 125, 218, 131, 222, 135, 222, 141, 215, 148, 217, 150, 220, 150, 226, 156, 226, 161, 217, 168, 216, 175, 210, 175, 208, 164, 199, 153, 199, 148, 204, 141, 204, 127] }),
				new Country({ id: "29", image: "images/hovermaps/homepage/turkey.png", area: [316, 88, 316, 83, 318, 82, 315, 78, 320, 75, 325, 75, 327, 78, 346, 77, 358, 65, 369, 62, 372, 64, 375, 63, 379, 65, 379, 104, 369, 114, 361, 111, 353, 111, 353, 114, 345, 119, 339, 116, 336, 116, 336, 118, 331, 116, 327, 111, 327, 109, 324, 106, 324, 102, 321, 97, 318, 98, 318, 91, 320, 89, 316, 89, 317, 81, 315, 79, 321, 75, 324, 75] }),
				new Country({ id: "17", image: "images/hovermaps/homepage/croatia.png", area: [235, 65, 235, 59, 240, 57, 244, 57, 247, 50, 250, 49, 255, 52, 261, 53, 263, 53, 267, 61, 263, 59, 259, 60, 250, 60, 247, 61, 248, 65, 255, 74, 259, 78, 253, 77, 245, 71, 243, 69, 239, 65, 238, 62, 235, 64, 234, 59] }),
				new Country({ id: "23", image: "images/hovermaps/homepage/morocco.png", area: [56, 196, 74, 185, 79, 178, 78, 168, 82, 160, 95, 152, 104, 136, 107, 135, 113, 139, 132, 142, 138, 162, 137, 163, 125, 164, 122, 167, 120, 167, 121, 173, 108, 180, 91, 185, 86, 191, 86, 197, 56, 197] })
			];
			//initiate the map
			countryMap = new CountryMap({
				element: $("#map").get(0),
				mapImage: "images/" + Resource.GetText('site_identifier') + "/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);
