//Globals!
var map;
var route;
var yuzenIcon;
var yuzenPos;
var yuzenAddress = 
{
  street: '8451 SE 68th St, Suite 104',
  city: 'Mercer Island',
  state: 'Wa',
  zip: '98040',
  country: 'USA',
  infowindow: 'default',
  infowindowtext: '<span style="font: 12px Verdana, Arial, Helvetica, sans-serif; color: #000;"><strong>Address:</strong><br />8451 SE 68th St, Suite 104<br />Mercer Island, Wa 98040 USA</span>',
  full: '8451 SE 68th St, Suite 104, Mercer Island, Wa, 98040, USA',
  isdefault: true
};

function initialize() 
{
  if(GBrowserIsCompatible()) 
  {
    if(!document.getElementById('wagt_map_2')) return false;
    map = new GMap2(document.getElementById('wagt_map_2'));
	route = new GDirections(map, document.getElementById("routeInfo"));
	
    map.enableScrollWheelZoom();
    map.enableDoubleClickZoom();
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

	GEvent.addListener(route, "addoverlay", onRouteLoad);
 	GEvent.addListener(route, "error", handleErrors);

	setDirections("Seattle");

  } 
}

function setDirections(fromAddress) 
{
	route.load("from: " + fromAddress + " to: " + yuzenAddress.full, { "locale": "en_US" });
}


function onRouteLoad(){ 
	var poly = route.getPolyline();
	
	var baseUrl = "http://maps.google.com/staticmap?";
	
	var params = [];
	var markersArray = [];
	markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
	markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
	params.push("markers=" + markersArray.join("|"));
	
	var polyParams = "rgba:0x0000FF80,weight:5|";
	var polyLatLngs = [];
	for (var j = 0; j < poly.getVertexCount(); j++) 
	{
	polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
	}
	params.push("path=" + polyParams + polyLatLngs.join("|"));
	params.push("size=300x300");
	params.push("key=ABQIAAAAkZshcY44E6KNBkndyXcOaRT7-nMae9EmQ295DXJb-m7M8zotyBSxGRzlAgolHLGDdPmSbxOvjugvzQ");
	
	baseUrl += params.join("&");
}


function handleErrors()
{
   if (route.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + route.getStatus().code);
   else if (route.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + route.getStatus().code);
   else if (route.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + route.getStatus().code);
   else if (route.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + route.getStatus().code);
   else if (route.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + route.getStatus().code);
   else alert("An unknown error occurred.");
}


