addLoadListener(loadMap);

	function loadMap() {
		if (mapId && document.getElementById(mapId) && GBrowserIsCompatible()) {
			
			// if the longitude has been passed in the querystring, we override the hardcoded ones
			// and override is enabled, set values now
			if (allowOveride) {
					
					qs = requestQuerystring();
					
					if (isNumeric(qs[ "longitude" ]))
							longitude = parseFloat(qs[ "longitude" ]);
					if (isNumeric(qs[ "latitude" ]))
							latitude = parseFloat(qs[ "latitude" ]);
					if (isNumeric(qs[ "zoomLevel" ]))
							zoomLevel = parseFloat(qs[ "zoomLevel" ]);
					if (isNumeric(qs[ "width" ]))
							width = parseFloat(qs[ "width" ]);
					if (isNumeric(qs[ "height" ]))
							height = parseFloat(qs[ "height" ]);
					if (!Gundefined(qs[ "mapHead" ]))
								mapHead = (qs[ "mapHead" ]);
					if (!Gundefined(qs[ "mapText" ]))
								mapText = (qs[ "mapText" ]);
			}
			
			// create map object
			mapElement = document.getElementById(mapId);
			mapElement.style.width = width+"px";
			mapElement.style.height = height+"px";
			map = new GMap2(mapElement);
			
			// listener to unload the map object when user leaves the page
			attachEventListener(document.body, "unload", unload, false)
			
			//center map, add marker and insert map controls
			doCenter(latitude,longitude, zoomLevel);
			addMarker(latitude, longitude);
			addControl();
			
		}
	}
	
	function updateCoords() {
		var lon = document.getElementById("lon").value;
		var lat = document.getElementById("lat").value;
		mapHead = document.getElementById("mHead").value;
		mapText = document.getElementById("mText").value;
		doCenter(lat,lon,zoomLevel);
		addMarker(lat, lon);
	}
	
	function addControl() {
		map.addControl(new GSmallMapControl());	
		map.addControl(new GMapTypeControl());
	}
	
	function doCenter(lat,lon,zoom) {
		map.setCenter(new GLatLng(lat, lon), zoom);	
		//document.getElementById("lat").value = lat;
		//document.getElementById("lon").value = lon;
		//document.getElementById("mHead").value = (mapHead);
		//document.getElementById("mText").value = (mapText);
		//document.getElementById("url").value = createUrl(lat,lon);
	}
	
	function center() {
		return map.getCenter().toString();
	}
	
	function addMarker(lat,lon) {
		var point = new GLatLng(lat,lon);
		map.addOverlay(new GMarker(point));	
		map.addOverlay(createMarker(point, "<strong>"+mapHead+"</strong><br />"+mapText.replace(/\n/g, "<br />")));
	}
	
	function createMarker(point, markerText) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "mouseover", function() {
		marker.openInfoWindowHtml(markerText);
	});
	return marker;
	}
	
	function unload() {
		GUnload();	
	}
	
	function requestQuerystring() {
		q_string = location.search.substring( 1 ).split( "&" );
		q_array = new Array();
	   for( i=0; i < q_string.length; i++ ) {
		  pair = q_string[ i ].split( "=" );
		  q_array[ pair[ 0 ] ] = unescape( pair[ 1 ] );
	   } 	
	   return q_array;
	}
	
	function isNumeric(numeral) {
		if (numeral == parseFloat(numeral)){
			return true;	
		}
	}
	
	function htmlEncode(s) {
        var str = new String(s);
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
	}
	
	function Gundefined(s) {
		if (typeof (s) == "Gundefined") {
			return true;
		}
	}
	function createUrl(lat,lon) {
		var windowLocation;
		if (urlBase != "") {
				windowLocation = urlBase;
		} else {
				windowLocation = window.location.toString();
				var locationOfQuerystring = windowLocation.indexOf("?");
				if (locationOfQuerystring > 0) {
					windowLocation = windowLocation.substr(0,locationOfQuerystring)
				}
		}
		return windowLocation + "?latitude=" + escape(lat) + "&longitude=" + escape(lon) + "&mapHead=" + escape(mapHead) + "&mapText=" + escape(mapText);
	}
	
	function popUp(url){
		window.open(url,"","width=600,height=500,scrollbars=1")
	}