
function createMarkerWithWindow(point_in, html_in)
{
	// Create the marker
	var marker = new GMarker(point_in);
	
	// Store the html in the marker (hack hack hack?)
	marker.window_html = html_in;
	
	// Give it click behavior
	GEvent.addListener(marker, "click", function() {
		this.openInfoWindowHtml(this.window_html);
	});
	
	return marker;
}

function google_maps_init()
{
	if (GBrowserIsCompatible()) {
		if (g_mapHandler) {
			// Create the map (don't forget to setCenter and add controls)
			g_map = new GMap2($("GST_gmap"));
			
			// Custom do-the-rest function
			g_mapHandler(g_map);
		}
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

function getDirections()
{
	if (typeof g_google_maps_address != "undefined" && typeof g_google_maps_address_id != "undefined") {
		if (g_previousDirections != null) {
			g_previousDirections.clear();
		}
		
		// Use the GST_gmap_directions div as the target
		var dir = new GDirections(g_map, $("GST_gmap_directions"));
		
		if (!g_directionsErrorHandler) {
			g_directionsErrorHandler = function() {
				var result = this.getStatus();
				alert("Google Maps was unable to find the given address.");
				//alert(result.code + "\n" + result.request);
			};
		}
		GEvent.addListener(dir, "error", g_directionsErrorHandler);
		
		// Combine the form fields into one address string
		var from_street = $(g_google_maps_address_id + "_street").value;
		var from_city = $(g_google_maps_address_id + "_city").value;
		var from_state = $(g_google_maps_address_id + "_state").value;
		var from_postal_code = $(g_google_maps_address_id + "_postal_code").value;
		var dirfrom = from_street + ", " + from_city + ", " + from_state.toUpperCase() + " " + from_postal_code;
		
		// Convert the from address and to address into a directions string google maps understands
		dirstring = "from: " + dirfrom + " to: " + g_google_maps_directions_destination_address;
		
		//alert("dirstring = " + dirstring);
		
		// Get the directions
		dir.load(dirstring, {getSteps:true});
		
		// Store that there has been a directions request (for clearing the map before later requests)
		g_previousDirections = dir;
	} else {
		alert("There is no origin address available for driving directions.");
	}
}

var g_map;
var g_geocoder;
var g_previousDirections = null;
var g_mapHandler = null; // The actual do-something code for the page
var g_directionsErrorHandler = null; // Respond to errors from a GDirections load request

// Require that Prototype is available
if (typeof Prototype == "undefined") {
	alert("Google Maps, as included by the GST, requires the Prototype javascript framework.\n\nPlease check that it is included.");
} else {
	document.observe("dom:loaded", google_maps_init);
}
