var regionMapProperties = $H({
								17: $H({'zoom': 500,
									   'posY': -97,
									   'posX': -466,
									   'defaultImage': 'images/HomeMap2/europeDefault.gif',
									   'prefixImage': 'images/HomeMap2/europe',
									   'imageOver': 'images/HomeMap2/europeOver'
									 }),
								22: $H({'zoom': 160,
									   'posY': -9,
									   'posX': -156,
									   'defaultImage': 'images/HomeMap2/asiaDefault.gif',
									   'prefixImage': 'images/HomeMap2/asia',
									   'imageOver': 'images/HomeMap2/asiaOver'
									}),
								16: $H({'zoom': 160,
									   'posY': -58,
									   'posX': -75,
									   'defaultImage': 'images/HomeMap2/africaDefault.gif',
									   'prefixImage': 'images/HomeMap2/africa',
									   'imageOver': 'images/HomeMap2/africaOver'
									}),
								21: $H({'zoom': 1000,
									   'posY': -605,
									   'posX': -360,
									   'defaultImage': 'images/HomeMap2/caribbeanDefault.gif',
									   'prefixImage': 'images/HomeMap2/caribbean',
									   'imageOver': 'images/HomeMap2/caribbeanOver'
									})
							});

var HomeMap = Class.create({
		initialize: function(imgMap, imgMapOver, hmpCenterMapOver,
							 hmpCenterMapClick, regionMaps, divHotelsList, divDGList,
							 regions, countriesByRegion, dgByRegion, hotelsInPanel, topDGPanel, divInitialLegend, divResultsLegend, divResultsGlobeLegend , backPnlResults,appPrefix) {
			this.imgMap = $(imgMap);
			this.imgMapOver = $(imgMapOver);
			this.hmpCenterMapOver = $(hmpCenterMapOver);
			this.hmpCenterMapClick = $(hmpCenterMapClick);
			this.hotelsInPanel = $(hotelsInPanel);
			this.topDGPanel = $(topDGPanel);
			this.appPrefix = appPrefix;

			this.divHotelsList = $(divHotelsList);
			this.divDGList = $(divDGList);
			this.divInitialLegend = $(divInitialLegend);
			this.divResultsLegend = $(divResultsLegend);
			this.divResultsGlobeLegend = $(divResultsGlobeLegend);
			this.backPnlResults = backPnlResults;
			
			this.selectedRegion = 0; // world
			this.currentContinent        = this.appPrefix+'images/HomeMap2/worldDefault.gif';
			
			this.highlightClassName = 'hmpHighlight';

			this.regions = regions;
			// Merge the regions parameter with the properties of our map
			var p = this;
			var newRegions = $H();
			this.regions.each(function(x) {
				x.value = x.value.merge(regionMapProperties.get(x.key));
				x.value = x.value.merge(regionMaps.get(x.key));
				newRegions.set(x.key, x.value);
			}, p);
			
			this.regions = newRegions;
			
			this.countriesByRegion = countriesByRegion;
			
			this.dgByRegion = dgByRegion;
			
			this.populateHotelsIn();
		},
		populateHotelsIn: function() {
			// Clear list
			var ul = this.divHotelsList.firstDescendant();
			
			if (ul != null) {
				this.divResultsLegend.show();
				this.divInitialLegend.hide();
				this.divResultsGlobeLegend.hide();
				ul.remove();
			}
			else {
				this.divResultsLegend.hide();
				this.divInitialLegend.show();
				this.divResultsGlobeLegend.hide();
			}
			
			ul = this.divDGList.firstDescendant();
			if (ul != null) {
				ul.remove();
			}
			// Get subregions
			var p = this;
			var a = this.regions.findAll(function(x) {return x.value.get('parent') == p.selectedRegion }, p);
			if (a.length == 0) {
				a = this.regions.findAll(function(x) {return x.key == p.selectedRegion }, p);
			}
			ul = new Element('ul');
	
			if (this.selectedRegion != 0) {
				if (this.regions.get(this.selectedRegion).get('hasZoom')) {
					this.divResultsGlobeLegend.show();
					this.divResultsLegend.hide();
				}
				// Add back button
				var li = new Element('li');
				var lnk = new Element('a', {'href': '#', 'style': 'font-style: italic; color: #f15a22; font-size:10px;'}).update(this.backPnlResults);
				lnk.observe('click', function(e) {
					this.back();
					Event.stop(e);
				}.bind(this));
				li.appendChild(lnk);
				ul.appendChild(li);
			}
			for (var i = 0; i < a.length; i++) {
				if (this.selectedRegion == 0) {
					var li = new Element('li');
					var lnk = new Element('a', {'class': 'hmpLinks', 'href': '#'}).update(a[i].value.get('name'));				
					lnk.observe('click', function(e) {
						var data = $A(arguments);
						this.selectContinent(data[1],null,null);
						Event.stop(e);
					}.bindAsEventListener(this,a[i].key));
					li['RegionID'] = a[i].key;
					li['IsRegion'] = true;
					li.appendChild(lnk);
					ul.appendChild(li);
				}
				else {
					var style = {'class': 'hmpSubregionTitle'};
					var li = new Element('li', style).update(a[i].value.get('name'));
					li['RegionID'] = a[i].key;
					li['IsRegion'] = true;
					ul.appendChild(li);
				}
				
				if (this.selectedRegion != 0 && this.countriesByRegion.get(a[i].key) != null) {
					this.countriesByRegion.get(a[i].key).each(function(c) {
						var li = new Element('li');
						var lnk = new Element('a', {'class': 'hmpLinks', 'href': 'searchresults.aspx?countryid=' + c.get('id')}).update(c.get('name'));
						li.appendChild(lnk);
						li['RegionID'] = a[i].key;
						li['IsRegion'] = false;
						ul.appendChild(li);
					});
				}
			}
			this.divHotelsList.appendChild(ul);
			
			// Populate DGs
			ul = new Element('ul');
			var prevLi = null;
			for (var i = 0; i < this.dgByRegion.length; i++) {
				if (this.selectedRegion != 0 || this.dgByRegion[i].get('top')== 1) {				
					if ((this.selectedRegion == 0) ||
						this.selectedRegion == this.dgByRegion[i].get('regionId') ||
						this.selectedRegion == this.regions.get(this.dgByRegion[i].get('regionId')).get('parent')) {
						if((prevLi == null) || (prevLi['dgId'] != this.dgByRegion[i].get('dgId')))
						{
							var li = new Element('li');
							var lnk = new Element('a', {'class': 'hmpLinks', 'href': 'destinationprofile.aspx?id=' + this.dgByRegion[i].get('dgId')}).update(this.dgByRegion[i].get('name'));
							li['RegionID'] = [this.dgByRegion[i].get('regionId')];
							li['ParentRegionID'] = [this.regions.get(this.dgByRegion[i].get('regionId')).get('parent')];						
							li['IsRegion'] = true;
							li['dgId'] = this.dgByRegion[i].get('dgId');
							prevLi = li;
							li.appendChild(lnk);
							ul.appendChild(li);
						}else if(prevLi['dgId'] == this.dgByRegion[i].get('dgId'))
						{
							prevLi['RegionID'].push(this.dgByRegion[i].get('regionId'));
							li['ParentRegionID'].push(this.regions.get(this.dgByRegion[i].get('regionId')).get('parent'));
						}
					
					}
				}
			}
			this.divDGList.appendChild(ul);
			
			Scroll.showScrollbar(this.hotelsInPanel);
			Scroll.showScrollbar(this.topDGPanel);
		},
		rollOverContinent: function(continent) {
			this.imgMapOver.src = this.appPrefix+'images/HomeMap2/worldOver' + continent + '.gif';

			if (this.regions.get(continent) != null && this.selectedRegion == 0) {
				this.divHotelsList.select("ul li").each(function(x) {
					if (x['IsRegion']) {
						if (x['RegionID'] == continent) {
							x.addClassName(this.highlightClassName);
						} else {
							x.removeClassName(this.highlightClassName);
						}
					}
				}, this);
			}
			if (this.regions.get(continent) != null && this.selectedRegion == 0) {
				this.divDGList.select("ul li").each(function(x) {
					if (x['IsRegion']) {
						//if (x['RegionID'] == continent || x['ParentRegionID'] == continent) {
						if (x['RegionID'].indexOf(continent) != -1 || x['ParentRegionID'].indexOf(continent) != -1) {
							x.addClassName(this.highlightClassName);
						} else {
							x.removeClassName(this.highlightClassName);
						}
					}
				}, this);
			}
		},
		rollOutContinent: function(continent) {
			//this.imgMap.src = this.currentContinent;
			this.imgMapOver.src = this.appPrefix+'images/blank.gif';

			this.divHotelsList.select("ul li").each(function(x) {
				if (x['IsRegion']) {
					x.removeClassName(this.highlightClassName);
				}
			}, this);
			this.divDGList.select("ul li").each(function(x) {
				if (x['IsRegion']) {
					x.removeClassName(this.highlightClassName);
				}
			}, this);
		},
		switchContinent:function(continent) {
			this.currentRegion = this.appPrefix + this.regions.get(continent).get('defaultImage');
			this.hmpCenterMapOver.hide();
			new Effect.Parallel(
				[ new Effect.Scale(this.imgMap, this.regions.get(continent).get('zoom'),
					{scaleFromCenter: false}),
				new Effect.MoveBy(this.imgMap,this.regions.get(continent).get('posY'),this.regions.get(continent).get('posX'),
					{})
				],
				{duration: 0.2
				}
			);
			new Effect.Parallel(
				[ 
				new Effect.Fade(this.hmpCenterMapClick, {to:1}),
				new Effect.Appear(this.regions.get(continent).get('divOver')),
				new Effect.Appear(this.regions.get(continent).get('divClick'), {to:1})
				],
				{duration: 0.2, queue: 'end'
				}
			);
		  },
		selectContinent: function(continent, obj, eve)
		{
			this.selectedRegion = continent;
			if (this.regions.get(this.selectedRegion).get('hasZoom')) {
				this.currentContinent = this.appPrefix+'images/HomeMap2/worldDefault.gif';
				this.imgMap.src = this.currentContinent;
				this.switchContinent(continent);
			} else {
				this.currentContinent = this.appPrefix+'images/HomeMap2/world' + continent + '.gif';
				this.imgMap.src = this.currentContinent;
			}
			this.populateHotelsIn();
			/*
			this.txtCurrentContinent.value    = continent;
            this.txtCurrentCommandArgument.value =this.commandArgument;
			if (this.hasZoom(continent))
			{
				this.switchContinent(continent);
			} else {
				this.currentContinent = 'images/HomeMap/world' + continent + '.gif';
				this.currentRegion		          = continent;
				this.txtCurrentSubregion.value    = '-1';
				this.showWaitingPanel();
				Anthem_InvokeControlMethod(this.mapClientId,'MapChangeSelection',[],this.showResultPanel.bind(this));
			}
			*/
		},
		back: function() {
			if (this.regions.get(this.selectedRegion).get('parent') != 0) {
				this.selectedRegion = this.regions.get(this.selectedRegion).get('parent');
				this.currentRegion = this.appPrefix + this.regions.get(this.selectedRegion).get('defaultImage');
				this.regions.get(this.selectedRegion).get('imgMap').src = this.currentRegion;
				this.populateHotelsIn();
			} else
			if (!this.regions.get(this.selectedRegion).get('hasZoom')) {
				this.selectedRegion = 0;
				this.currentContinent = this.appPrefix+'images/HomeMap2/worldDefault.gif';
				this.imgMap.src = this.currentContinent;
				this.rollOutContinent(0);
				this.populateHotelsIn();
			} else {
				this.backToWorld(null, null, this.selectedRegion);
			}
		},
		rollOverZoomcontinet:function(continent,region)
		{
			this.regions.get(continent).get('imgMapOver').src = this.appPrefix + this.regions.get(continent).get('imageOver') + region + '.gif';
			
			if (this.countriesByRegion.get(region) != null && this.regions.get(this.selectedRegion).get('parent') == 0) {
				this.divHotelsList.select("ul li").each(function(x) {
					if (!x['IsRegion']) {
						if (x['RegionID'] == region) {
							x.addClassName(this.highlightClassName);
						} else {
							x.removeClassName(this.highlightClassName);
						}
					}
				}, this);
			}
			
			if (this.countriesByRegion.get(region) != null  && this.regions.get(this.selectedRegion).get('parent') == 0) {
				this.divDGList.select("ul li").each(function(x) {
					if (x['IsRegion']) {
						if (x['RegionID'].indexOf(region) != -1) {
							x.addClassName(this.highlightClassName);
						} else {
							x.removeClassName(this.highlightClassName);
						}
					}
				}, this);
			}

		},
		rollOverZoomcontinetBack:function(continent)
		{
			this.regions.get(continent).get('imgMapOver').src = this.appPrefix+'images/HomeMap2/europeOver0.gif';
		},
		
		rollOutZommContinent:function(continent, region)
		{
			this.regions.get(continent).get('imgMap').src	  = this.currentRegion;
			this.regions.get(continent).get('imgMapOver').src = this.appPrefix+'images/blank.gif';
			
			this.divHotelsList.select("ul li").each(function(x) {
				if (!x['IsRegion']) {
					x.removeClassName(this.highlightClassName);
				}
			}, this);
			
			this.divDGList.select("ul li").each(function(x) {
				if (x['IsRegion']) {
					x.removeClassName(this.highlightClassName);
				}
			}, this);
		},

		 backToWorld:function(obj, eve,continent)
		 {
		    this.selectedRegion = 0;
			this.currentContinent = this.appPrefix+'images/HomeMap2/worldDefault.gif';
			this.currentRegion    = this.appPrefix + this.regions.get(continent).get('defaultImage');
			this.rollOverZoomcontinet(continent);
			this.rollOutZommContinent(continent);
			this.regions.get(continent).get('divOver').hide();
			new Effect.Parallel(
				[ 
				new Effect.Appear(this.hmpCenterMapClick),
				new Effect.Fade(this.regions.get(continent).get('divClick'))
				],
				{duration: 0.0
				}
			);
			new Effect.Parallel(
				[
				new Effect.Scale(this.imgMap, 10000 / this.regions.get(continent).get('zoom')),
				new Effect.MoveBy(this.imgMap, -this.regions.get(continent).get('posY'), -this.regions.get(continent).get('posX'))
				],
				{duration: 0.2, queue: 'end',
				afterFinish: function(eff) {
						this.hmpCenterMapOver.show();
						this.populateHotelsIn();
					}.bind(this)
				}
				
			);

		 },
		 selectZoomRegion:function(continent,region, obj, eve)
		 {
		 	this.currentRegion = this.appPrefix + this.regions.get(continent).get('prefixImage') + region + '.gif';
		 	this.regions.get(continent).get('imgMap').src	  = this.currentRegion;
		 	this.selectedRegion = region;
		 	this.populateHotelsIn();
			//this.txtCurrentSubregion.value = region;
			//this.showWaitingPanel();
			//Anthem_InvokeControlMethod(this.mapClientId,'MapChangeSelection',[],this.showResultPanel.bind(this));
		 }
	});
