//Google Map Creator

var map = null;
var geocoder = null;
function createGmap(mapaddress,mapcanvas,mapwidth,mapheight) 
	{
	//var mapaddress = address;
	//var mapcanvas = canvas;
	//var type = type;
	//var width = width;
	//var height = height;
	
	if (GBrowserIsCompatible()) 
			{
			if (!mapwidth)
			    {mapwidth=232;}
			if (!mapheight)
			    {mapheight=260;}

			map = new GMap2(document.getElementById(mapcanvas),{size:new GSize(mapwidth,mapheight)});
			geocoder = new GClientGeocoder();
			showAddress(mapaddress);
		
			//SMALL MAP CONTROLS
			var mapTypeControl = new GMapTypeControl();
			//var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
			//var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			//map.addControl(mapTypeControl, topRight);
			map.addControl(new GSmallMapControl());

			}
	}

function createDirectionsMap (mapaddress,mapcanvas,mapwidth,mapheight)
	{
	map = new GMap2(document.getElementById(mapcanvas),{size:new GSize(500,360)});
	geocoder = new GClientGeocoder();
	showAddress(mapaddress);

	//LARGE MAP CONTROLS
	var customUI = map.getDefaultUI();
	//Remove MapType.G_HYBRID_MAP
	//customUI.maptypes.hybrid = false;
	map.setUI(customUI);
	gdir = new GDirections(map, document.getElementById("directions"));
	//GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);

	}
	
function showAddress(address) 
	{
	if (geocoder) 
		{
		geocoder.getLatLng(address,
					function(point) 
						{
						if (!point) 
							{
							//alert(address + " not found");
							} 
						else 
							{
							map.setCenter(point, 11);
							var marker = new GMarker(point);
							map.addOverlay(marker);
							//marker.openInfoWindowHtml(address);
							}
						}
					);
		}
	} //end show address

function setDirections(fromAddress, toAddress, locale) 
	{
	document.getElementById('directions').innerHTML = '';
	
      directionsPanel = document.getElementById("directions");
      directions = new GDirections(map, directionsPanel);
      directions.load("from: " + fromAddress + " to: " + toAddress,{ "locale": locale });	
      document.getElementById('directions').style.display = 'inline';

	
	
	}

function hideDirections() 
	{
	document.getElementById('directions').innerHTML = '';
	document.getElementById('directions').style.display = 'none';
	}

function handleErrors()
	{
	if (gdir.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: " + gdir.getStatus().code);

	else if (gdir.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: " + gdir.getStatus().code);

	else if (gdir.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: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	  
	else if (gdir.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: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	  alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
	else alert("An unknown error occurred.");
	}

function replaceAddress(address,phone)
	 {
	 document.getElementById('ToLabel').innerHTML = address;
	 document.getElementById('toAddress').value = address;
	 document.getElementById('phone').innerHTML = phone;
	 }
	


