function onGDirectionsLoad()
{
	return true;
}

function getRoute()
{
	//alert(dir.GRoute.getNumSteps());
}

function createMarker(point, html)
{
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function addToMap(response)
{
	// Retrieve the object
	place = response.Placemark[0];
	
	// Retrieve the latitude and longitude
	point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	
	// Center the map on this point
	map.setCenter(point, 13);
	
	// Create a marker
	marker = new GMarker(point);
	
	// Add the marker to map
	map.addOverlay(marker);
	
	// Add address information to marker
	marker.openInfoWindowHtml("<h2 style='margin: 0 0 0.5em 0;'>" + companyname + "</h2><p>" + address + "</p>");
}

function initiate()
{
	if (GBrowserIsCompatible()) { 
		// Display the map, with some controls and set the initial location 
		map = new GMap2(document.getElementById("GST_gmap"));
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
		geocoder.getLocations(address, addToMap);
		
		var objDirectionsForm = document.getElementById("directions_form");
		if (objDirectionsForm) {
			objDirectionsForm.onsubmit = getDirectionsWrapper;
		}
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

function getDirections()
{
	if (last_dir != null) {
		last_dir.clear();
	}

	dir = new GDirections(map, document.getElementById("GST_gmap_directions"));

	GEvent.addListener(dir, "load", onGDirectionsLoad); 
	var fromstreet = document.getElementById("gst_form_element_fromaddress_1").value;
	var fromcity = document.getElementById("gst_form_element_fromaddress_3").value;
	var fromstate = document.getElementById("gst_form_element_fromaddress_4").value;
	var fromzip = document.getElementById("gst_form_element_fromaddress_5").value;
	var dirfrom = fromstreet + ", " + fromcity + ", " + fromstate + ", " + fromzip;
	var dirto = address; //"1111 Northshore Dr NW, Knoxville, TN 37919";
	dirstring = "from: " + dirfrom + " to: " + dirto;
	dir.load(dirstring, {getSteps:true});

	last_dir = dir;
}

function getDirectionsWrapper(e)
{
	alert(e);
	e.cancelBubble = true;
	e.returnValue = false;
	
	getDirections();
	return false;
}

var dir;
var map;
var geocoder;
//var address = "1111 Northshore Dr NW, Knoxville, TN, 37919";
//var companyname = "Accounting Resource Group"
var last_dir = null;

addLoadEvent(initiate);