var HOSTNAME = document.location.href.match(/^(http:\/\/)?([^\/]+)/i)[0];
var GOOGLEMAP = null;
var USER_MARKER = null;
var icon = new GIcon();
//icon.image = "http://www.localcarbongame.org.uk/assets/images/googleMapMarker.png";
icon.image = HOSTNAME+"/assets/images/googleMapMarker.gif";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_10_shadow.png";
icon.iconSize = new GSize(7, 7);
icon.shadowSize = new GSize(22, 10);
icon.iconAnchor = new GPoint(6, 10);
icon.infoWindowAnchor = new GPoint(5, 1);


ONLOAD_STACK.push(
function() {
	if (GBrowserIsCompatible()) {
		GOOGLEMAP = new GMap2(document.getElementById("map"));


		GOOGLEMAP.setCenter(new GLatLng(49.425560, 9.415590), 3);
		GOOGLEMAP.setMapType(G_SATELLITE_MAP);
		GOOGLEMAP.disableDragging();

		var loc = new String(window.location);
		if(loc.match('admin')!=null)
		{
			try{
				// If the user coordinates are set then add them to the map
				var userLat = document.getElementById('mapCoordinates').value.split(',')[0];
				var userLong = document.getElementById('mapCoordinates').value.split(',')[1];
				if(userLat!=null && userLong!=null)
				{
					var point = new GLatLng(userLat, userLong);
				}
				else
				{
					var point = GOOGLEMAP.getCenter();
				}
			}catch(e){
			}
			carbonTrading_addUserMarker(GOOGLEMAP,point);
		}
	}
});
function carbonTrading_addMarker(lat,long,schoolName)
{
	var m = new GMarker(new GLatLng(lat, long), icon);
	if(schoolName)
	{
		var html = document.createElement('div');
		html.innerHTML = '<font style="font-size:2em;">'+schoolName+'</font>';
		GEvent.addListener(m, "mouseover", function() {
			GOOGLEMAP.openInfoWindow(m.getPoint(),html,{maxWidth:50});
		});
		/*
		GEvent.addListener(GOOGLEMAP.getInfoWindow(), "mouseout", function() {
			GOOGLEMAP.closeInfoWindow();
		});
		*/
	}
	GOOGLEMAP.addOverlay(m);
}
function carbonTrading_addUserMarker(map,point)
{
	USER_MARKER = new GMarker(point,{draggable:true});

	GEvent.addListener(USER_MARKER, "dragstart", function() {
		map.closeInfoWindow();
	});

	GEvent.addListener(USER_MARKER, "dragend", function() {
		document.getElementById('mapCoordinates').value = USER_MARKER.getPoint().y+','+USER_MARKER.getPoint().x;
	});
	map.addOverlay(USER_MARKER);
}


var loc = new String(window.location);
if(loc.match('admin')==null)
{
	document.write('<script type="text/javascript" src="/assets/scripts/googleLocationCache.js"></script>');
}