addLoadListener(loadMap);

	function loadMap() {
		if (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" ]))
							zoomLevel = parseFloat(qs[ "width" ]);
					if (isNumeric(qs[ "height" ]))
							zoomLevel = parseFloat(qs[ "height" ]);
					if (!undefined(qs[ "mapHead" ]))
								mapHead = (qs[ "mapHead" ]);
					if (!undefined(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() {
		longitude = document.getElementById("lon").value;
		latitude = document.getElementById("lat").value;
		mapHead = document.getElementById("mHead").value;
		mapText = document.getElementById("mText").value;
		width = document.getElementById("width").value;
		height = document.getElementById("height").value;
		doCenter(latitude,longitude,zoomLevel);
		addMarker(latitude, longitude);
		theURL = createUrl(latitude,longitude);
		document.getElementById("url").value = theURL;
		document.getElementById("iframe").value = makeIframe(theURL);
		document.getElementById("linkcode").value = makeLink(theURL);
	}
	
	function addControl() {
		map.addControl(new GSmallMapControl());	
		map.addControl(new GMapTypeControl());
	}
	
	function doCenter(lat,lon,zoom) {
		mapElement.style.width = width+"px";
		mapElement.style.height = height+"px";
		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("width").value = (width);
		document.getElementById("height").value = (height);
		theURL = createUrl(lat,lon);
		document.getElementById("url").value = theURL;
		document.getElementById("iframe").value = makeIframe(theURL);
		document.getElementById("linkcode").value = makeLink(theURL);
	}
	
	function center() {
		return map.getCenter().toString();
	}
	
	function addMarker(lat,lon) {
		var point = new GLatLng(lat,lon);
		_pointer = new GMarker(point);
		map.addOverlay(_pointer);	
		map.addOverlay(createMarker(point, "<strong>"+mapHead+"</strong><br />"+mapText.replace(/\n/g, "<br />")));
	}
	
	function removeMarker() {
		if ( typeof _pointer != "undefined" )
				map.removeOverlay(_pointer);
		if ( typeof _marker != "undefined" )
				map.removeOverlay(_marker);
	}
	
		function setCoords(coords) {
		coords = coords.toString();
		coords = coords.substring(1,coords.length-1);
		coordsplit = coords.split(",");
		document.getElementById("lat").value = coordsplit[0];
		document.getElementById("lon").value = coordsplit[1];
		updateCoords();
	}
	
	function createMarker(point, markerText) {
			removeMarker();
			_marker = new GMarker(point, {draggable: true});
			GEvent.addListener(_marker, "mouseover", function() {
				_marker.openInfoWindowHtml(markerText);
			});
			GEvent.addListener(_marker, "dragend", function() {
					setCoords(_marker.getPoint());
			});
			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 undefined(s) {
		if (typeof (s) == "undefined") {
			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) + "&width=" + escape(width.toString()) + "&height=" + escape(height.toString());
	}
	
	function popUp(url){
		window.open(url,"","width=600,height=500,scrollbars=1,resizable=1")
	}
	
	function preview(){
		updateCoords();
		var theURL = createUrl(latitude,longitude);
		window.open(theURL,"","toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height);
		document.getElementById("iframe").value = makeIframe(theURL);
		document.getElementById("linkcode").value = makeLink(theURL);
	}
	
	function makeIframe(url) {
		return '<iframe id="locationMap" src="' + url + '" style="width:' + width + 'px; height:' + height + 'px; margin:0; padding:0; border:0" scrolling="no" frameborder="0"></iframe>'; 
	}
	
	function makeLink(url) {
		return '<a href="'+url+'" onclick="window.open(this,\'\',\'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height+'\'); return false;">' +
		'<img src="'+urlBase+'view_map.gif" border="0" /></a>'; 
	}