addLoadListener(loadMap);

	function loadMap() {
		if (GBrowserIsCompatible()) {
			
			var address = "";
			
			// 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 (!isNotDefined(qs[ "mapHead" ]))
								mapHead = (qs[ "mapHead" ]);
					if (!isNotDefined(qs[ "mapText" ]))
								mapText = (qs[ "mapText" ]);
					if (!isNotDefined(qs[ "mapText" ]))
								mapText = (qs[ "mapText" ]);
					if (!isNotDefined(qs[ "address" ]))
								address = (qs[ "address" ]);
			}
			
			// 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)
			//if an address has been provided, we do the geocode now
			if ( qs[ "draggable" ] == "true" && isNumeric(qs[ "longitude" ]) && isNumeric(qs[ "latitude" ]) )  {
				//center map, add marker and insert map controls
				doCenter(latitude,longitude, zoomLevel);
				addMarker(latitude, longitude);
				addControl();
			} else if ( address.length > 0 ) {
				showAddress(address);
			} else	{		
				//center map, add marker and insert map controls
				doCenter(latitude,longitude, zoomLevel);
				addMarker(latitude, longitude);
				addControl();
			}
		}
	}
	
	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(",");
		latitude = coordsplit[0];
		longitude = coordsplit[1];
		doCenter(latitude,longitude, zoomLevel);
		addMarker(latitude, longitude);
		addControl();
	}
	
	
	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);
		_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);
			map.addOverlay(marker);
            
			GEvent.addListener(marker, "mouseover", function() {
                
              // create two tabs, one for the hotel and one for the streetview
                var div = document.createElement('div');
                div.id = 'panorama';
                div.style.width = '300px';
                div.style.height = '160px';
                div.style.overflow = 'hidden';
                var tabList = [
                    new GInfoWindowTab("Info", markerText),
                    new GInfoWindowTab("Street View", div)
                ];
                
                if($("panorama") && $("panorama").up() && $("panorama").up().previous() && $("panorama").up().previous().previous())
                    $("panorama").up().previous().previous().hide();
                marker.openInfoWindowTabsHtml(tabList);
                setTimeout(function(){
                    $("panorama").up().previous().previous().hide();
                    var myPano = new GStreetviewPanorama(document.getElementById("panorama"), {latlng:point});
                    var timeout = setTimeout(function(){
                        $("panorama").up().previous().previous().show().addClassName('show');
                    }, 2000);
                    GEvent.addListener(myPano, "error", function(){
                        clearTimeout(timeout);
                        $("panorama").up().previous().previous().remove();
                    });
                },500);
			});	
          
                    
			return marker;
	}
	
	function refreshMapWithNewPoint( point ) {
			var url = '/show-map/?';
			var qs = requestQuerystring();
			url = url + 'address=' + escape(qs[ "address" ]) + '&mapHead=' + escape(qs[ "mapHead" ]) + '&mapText=' + escape(qs[ "mapText" ]) + '&width=' + escape(qs[ "width" ]) + '&height=' + escape(qs[ "height" ]);
			url = url + '&draggable=true&longitude=' + point.x + '&latitude=' + point.y;
			document.location.href = url;
	}	
	
	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);
	}
	
	function popUp(url){
		window.open(url,"","width=600,height=500,scrollbars=1")
	}

