window.addEventListener?window.addEventListener("load",loadMap,false):window.attachEvent("onload",loadMap);
window.addEventListener?window.addEventListener("unload",GUnload,false):window.attachEvent("onunload",GUnload);

var marker;
var map;

function createMarker(point,name,html) {
	var marker = new GMarker(point);

	// The info window version with the "to here" form open
	to_html = html + '<div class="hr"><hr /></div>' +
		'<p style="font-size:85%;">Directions: <strong>To here</strong> - <a href="javascript:fromhere()">From here</a></p>' +
	   '<form action="http://maps.google.com/maps" method="get" target="_blank"><p style="font-size:85%;">Start address:<br />' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
	   '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
			   "(" + name + ")" + 
	   '"/></p>';
	// The info window version with the "to here" form open
	from_html = html + '<div class="hr"><hr /></div>' +
		'<p style="font-size:85%;">Directions: <a href="javascript:tohere()">To here</a> - <strong>From here</strong></p>' +
	   '<form action="http://maps.google.com/maps" method="get"" target="_blank"><p style="font-size:85%;">End address:<br />' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br />' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
	   '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
			   "(" + name + ")" + 
	   '"/></p>';
	// The inactive version of the direction info
	html = html + '<div class="hr"><hr /></div>' +
		'<p style="font-size:85%;">Directions: <a href="javascript:tohere()">To here</a> - <a href="javascript:fromhere()">From here</a></p>';
	
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html, {maxWidth:200});
	});
	marker.openInfoWindowHtml(html, {maxWidth:200});

	return marker;
}

// functions that open the directions forms
function tohere() {
	marker.openInfoWindowHtml(to_html, {maxWidth:200});
}
function fromhere() {
	marker.openInfoWindowHtml(from_html, {maxWidth:200});
}
  
  
function loadMap() {
   if (GBrowserIsCompatible()) {
			
		map = new GMap2(document.getElementById("google-map"));
		map.addControl(new GSmallMapControl());
		
		var loc = new GLatLng(gm_lat, gm_lng);
		map.setCenter(loc, gm_zoom);

		marker = createMarker(loc,gm_name,gm_address);

	}
}

