function loadStoreMap()
{
	var staticURL = "/static/storelocator";
	if (GBrowserIsCompatible())
	{
		function addMarker(point, store)
		{		
			if (!store)
			{
				var marker = new GMarker(point,locationIcon);			
			}
			else
			{
				var marker = new GMarker(point,storeIcon);
				GEvent.addListener(marker, "click", function()
				{
					marker.openInfoWindowHtml(createInfoWindow(store));
				});			
			}					
			return marker;
		}
		
		function createInfoWindow(store)
		{
			var template = document.getElementById('infoWindowTemplate').innerHTML;			
			for (var key in store)
			{			
				var rxChar = "\\$"+key;
				var rx = new RegExp(rxChar,"ig");
				template = template.replace(rx, store[key]);				
			}
			return template;
		}
		
		var map = new GMap2(document.getElementById("storeMap"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(0,0),0);
		
		if (stores == "") {
			map.setCenter(new GLatLng(40.33761230687415, -3.71337890625), 0);
			map.setZoom(5);
		} else {

		
			var bounds = new GLatLngBounds();
		
			var storeIcon = new GIcon();
			storeIcon.image = staticURL + "/media/layout/intersport2.png";
			storeIcon.shadow = staticURL + "/media/layout/intersport2_shadow.png";
			//storeIcon.iconSize = new GSize(34, 24);
			storeIcon.iconSize = new GSize(21, 27);
			storeIcon.shadowSize = new GSize(47, 24);
			storeIcon.iconAnchor = new GPoint(17, 12);
			storeIcon.infoWindowAnchor = new GPoint(17, 12);
			
			var locationIcon = new GIcon();
			locationIcon.image = staticURL + "/media/layout/pin.png";
			locationIcon.shadow = staticURL + "/media/layout/pin_shadow.png";
			locationIcon.iconSize = new GSize(12, 20);
			locationIcon.shadowSize = new GSize(12, 12);
			locationIcon.iconAnchor = new GPoint(6, 20);
			locationIcon.infoWindowAnchor = new GPoint(6, 1);		
				
			if (currentLocation.length == 2)
			{
				var point = new GLatLng(currentLocation[0],currentLocation[1]);
				map.addOverlay(addMarker(point));
				bounds.extend(point);
			}
			
			//console.log(stores);
			
			for (var i=0;i<stores.length;i++)
			{
					if (stores[i].longitude && stores[i].latitude)
					{
						longitude = stores[i].longitude;
						latitude = stores[i].latitude;
						if (longitude.length > 0 && latitude.length > 0)
						{
							var point = new GLatLng(latitude,longitude);
							map.addOverlay(addMarker(point, stores[i]));
							bounds.extend(point);
						}
					}
			}
			
			if (stores.length == 1)
			{
				map.setZoom(11);	
			}
			else
			{
				map.setZoom(map.getBoundsZoomLevel(bounds));
			}
			
			var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
			var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
			map.setCenter(new GLatLng(clat,clng));
		}
	}
}

function unloadStoreMap()
{
	GUnload();
}

window.onload = loadStoreMap;
window.onunload = unloadStoreMap;