(function () {

	WidgetMap = {
		
		//Flash file
		flash_path: '/img/destinations/map.swf',
		flash_width: 450,
		flash_height: 460,
		
		//Map place-holder ID
		container_id: 'mapObj',
	
		//Create container if needed
		container_create: false,
	
		//Selected country code
		code: null,
		
		//List of all countries (abbriviation as key, full title as value)
		countries: {},
		
		//Wait max for 20 x 50 = 1000 milisec before giving up on 'mapHighlight'
		timeout_itt: 20,
		
		/**
		 * Callback for flash, redirects to another URL
		 * @param {String} code Country code
		 */
		flashCallback: function (code) {
			if (code in this.countries && this.countries[code]) {
				var country = this.countries[code];
				var pathname = window.location.pathname;
				
				//for landing page, set URL to /destinations?CountryName
				if ( this.container_create )
					pathname += 'destinations';
				
				window.location = pathname + '?' + country;
			}
		},
		
		/**
		 * Highlight country on the map
		 * 
		 * @param {String} code Country code
		 */
		mapHighlight: function (code) {
			
			if (code.length == 0)
				return;
			
			var fl = swfobject.getObjectById(this.container_id), err;
			
			if (fl && fl.mapShowLand) {
				try {
					fl.mapShowLand(code);
					return;
				} catch (e) {
					err = e;
				}
			}
			
			if (this.timeout_itt > 0) {
				this.timeout_itt--;
				setTimeout(function () {
					WidgetMap.mapHighlight(code);
				}, 50);
			} else {
				this.timeout_itt = 100;
			}
		},
		
		/**
		 * Creates container and appends to document.body
		 * Used on landing page
		 */
		createContainer: function () {
			var div = document.createElement('DIV');
				div.className = 'map';
			
			var div_inner = document.createElement('DIV');	
				div_inner.id = this.container_id;
				
			div.appendChild(div_inner);
			document.body.appendChild(div);
		},
		
		/**
		 * Create flash, bind listeners
		 * 
		 * @constructor
		 */
		init: function () {
			if (!document.getElementById(this.container_id)) {
				if (this.container_create) {
					this.createContainer();
				} else {
					return;
				}
			}
			
			//Register flash callback function
			window.mapCallback = $.proxy(this.flashCallback, this);
			
			//Fix for "Google Translate" adding position 'relative' to body, which
			//causes flash flicker in Firefox 
			document.body.style.position = 'relative';
		
			
			swfobject.registerObject(this.container_id, "9.0.0", "/img/destinations/expressInstall.swf");
			
			var flashvars = {};
			var attributes = {};
			var params = {
				salign: "tl",
				allowscriptaccess: "sameDomain",
				wmode: "transparent"
			};

			swfobject.embedSWF(this.flash_path, this.container_id, this.flash_width, this.flash_height, "9.0.0", "/img/destinations/expressInstall.swf", flashvars, params, attributes);
			swfobject.addLoadEvent($.proxy(function () {
				this.mapHighlight(this.code);
			}, this));
			
			$(document.body).click($.proxy(function () {
				this.mapHighlight(this.code);
			}, this));
		}
	};
	
	$($.proxy(WidgetMap.init, WidgetMap));
	
})();