addLoadListener(loadMap);
var _marker;
var _pointer;
var _pointerBodyEdited = false;

	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 (!isNotDefined(qs[ "mapHead" ]))
								mapHead = (qs[ "mapHead" ]);
					if (!isNotDefined(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 updateWithAddress(){
			if ( document.getElementById("address").value.length > 0 ) {
				showAddress(document.getElementById("address").value);
			}	else {
				alert("You need to enter an address.");	
			}
	}
	
	function updateCoords() {
		copyAddress();
		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;
		removeMarker();
		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);
		document.getElementById("directions").value = makeDirectionsLink();
	}
	
	function addControl() {
		map.addControl(new GSmallMapControl());	
		map.addControl(new GMapTypeControl());
	}
	
	function copyAddress() {
			if (!_pointerBodyEdited) {
					document.getElementById("mText").value = parseAddress(document.getElementById("address").value);
		}	
	}
	
	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);
		document.getElementById("directions").value = makeDirectionsLink();
	}
	
		
	function showAddress(address) {
	  var geocoder = new GClientGeocoder();
	  geocoder.getLatLng(
		address,
		function(point) {
		  if (!point) {
			alert("The address entered could not be found");
		  } else {
			setCoords(point);
		  }
		}
	  );
	}
	
	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 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 createMarker(point, markerText) {
		removeMarker();
		_marker = new GMarker(point, {draggable: true});
		GEvent.addListener(_marker, "dragend", function() {
				refreshMapWithNewPoint( _marker.getPoint() )
		});
		return _marker;
	}
	
	function refreshMapWithNewPoint( point ) {
			setCoords(point);
	}	
	
	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 isNotDefined(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>'; 
	}
	
	function makeDirectionsLink() {
			destinationAddress = document.getElementById("address").value;
			return '<p>Enter your address to obtain directions to this location: ' +
			'<input type="text" name="googleMapsDirections" id="googleMapsDirections" value="Your Address Here" size="50" onfocus="if (this.value == \'Your Address Here\') this.value =\'\';" />'+
			'<input type="button" value="Go" name="googleMapsDirectionsGo" id="googleMapsDirectionsGo" onclick="javascript:window.open(\'http://maps.google.com/maps?saddr=\' + escape(document.getElementById(\'googleMapsDirections\').value) + \'&daddr=' + escape(destinationAddress) + '\', \'\', \'toolbar=0,resizable=1,scrollbars=1,left=2,top=2,width=790,height=530\'); return false;" />' +
			'</p>';
		
	}
	
	function parseAddress( address ) {
			address = address.replace(/, /g, '\n');	
			address = address.replace(/,/g, '\n');	
			return address;
	}

