LCS.GoogleMap = {
	storeId : null,
	map : null,
	geocoder : null,
	baseIcon : null,
	isLocating : false,
	
	mapPoints : new Array(),
	
	Init : function() {	
		Event.observe(window, "unload", GUnload);
	
		this.map = document.getElementById("map");
		
		if (GBrowserIsCompatible() && this.map) {
			this.map = new GMap2(this.map);
			this.map.addMapType(G_PHYSICAL_MAP);
			this.map.addControl(new GLargeMapControl());
			this.map.enableContinuousZoom();									
		
			var mapControl = new GHierarchicalMapTypeControl();
			mapControl.clearRelationships();
			mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, "Labels", false);
			this.map.addControl(mapControl);			
			
			this.geocoder = new GClientGeocoder();
			
			// Create a base icon for all of our markers that specifies the
			// shadow, icon dimensions, etc.
			this.baseIcon = new GIcon(G_DEFAULT_ICON);
			this.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			this.baseIcon.iconSize = new GSize(20, 34);
			this.baseIcon.shadowSize = new GSize(37, 34);
			this.baseIcon.iconAnchor = new GPoint(9, 34);
			this.baseIcon.infoWindowAnchor = new GPoint(9, 2);									
			
			// Initialize Map.
			centerMontreal = new GLatLng(45.54545, -73.63908);
			this.map.setCenter(centerMontreal, 11);
			
			// Initialize Store Marker
			this.renderMarker();

			// Set the correct Center from StoreID
			if(this.storeId) {
				var storeMarker = this.findMarkerFromId(this.storeId);
				if (storeMarker) {
					this.map.setCenter(storeMarker.GLatLng, 12);	
					GEvent.trigger(storeMarker.marker, "click");
				}					
			}	
		}
	},
	
	renderMarker : function() {
		this.map.clearOverlays();

		for (i=0; i<this.mapPoints.length; i++) {				
			this.mapPoints[i].marker = this.addMarkerNo(this.mapPoints[i].GLatLng, i+1);

			storeContent = $('store_' + this.mapPoints[i].Id);
			if(storeContent) storeContent = storeContent.select('div.store-content');
			if(storeContent) {
				storeContent = storeContent[0].cloneNode(true);
				this.mapPoints[i].marker.bindInfoWindow(storeContent,{maxWidth:210});
			}
		}	
	},
	
	addMarkerNo : function(point, no) {
		var marker = null;

		var iconNo = new GIcon(this.baseIcon);
		iconNo.image = "/lcs/web/images/map/no/marker-" + no + ".png";

		// Set up our GMarkerOptions object
		markerOptions = { icon:iconNo };
		marker = new GMarker(point, markerOptions);
		
		this.map.addOverlay(marker);
		return marker;
	},

	locateOnMap : function(id) {
		//Find marker from ID and open info.
		
		var storeMarker = this.findMarkerFromId(id);						
		if(this.map.getZoom() < 12) {
			this.map.setZoom(12);	
		}		
		this.map.panTo(storeMarker.GLatLng);
		GEvent.trigger(storeMarker.marker, "click");
	},
	
	findMarkerFromId : function(id) {						
		marker = null;
		for (i=0; i<this.mapPoints.length; i++) {				
			if(this.mapPoints[i].Id == id)
				marker = this.mapPoints[i];
		}				
		return marker;
	},
	
	findAddress : function(address) {
		if (this.geocoder) {
			this.geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						alert("Postal code \"" + address + "\" is not found.");
					} else {
						LCS.GoogleMap.findClosestMarker(point);
					}
				}
			);
    	}
	},
	
	findClosestMarker : function(point) {	
		// Find distance
		for (i=0; i<this.mapPoints.length; i++) {
			this.mapPoints[i].Distance = Math.round(this.mapPoints[i].GLatLng.distanceFrom(point));											
		}	
		
		// Re-Order Object Array
		this.mapPoints.sort(function(a, b) {
		    var x = parseInt(a.Distance);
		    var y = parseInt(b.Distance);		    
		    return (x - y);
		});
					
		// Re-Order List (left of the map)
		LCS.Pager.moveToPage(1);
		storeList = document.getElementById('ulResults');
		
		for (i=0; i<this.mapPoints.length; i++) {
			id = this.mapPoints[i].Id;			
			store = document.getElementById('store_' + id);			
			storeList.appendChild(store);
			var img = document.getElementById('store_no-' + id);
			
			if(img.style.filter) {				
				img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/lcs/web/images/map/no/marker-" + (i+1) + ".png', sizingMethod='scale')";
			} else {
				img.src = "/lcs/web/images/map/no/marker-" + (i+1) + ".png";
			}				
		}		

		// Reset Map				
		this.renderMarker();			
		
		if(this.map.getZoom() < 12) {
			this.map.setZoom(12);	
		} 		
		this.map.panTo(this.mapPoints[0].GLatLng);
		GEvent.trigger(this.mapPoints[0].marker, "click");
	},
	
   findStore : function() {
		var form1 = document.forms['findStoreForm'];
		address = form1.codePostal1.value + " " + form1.codePostal2.value;
		this.findAddress(address);
	}
}