// include google maps api
include("http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAABk5yzD6w8IJA5OuPDvIRIhRO63NCfHmHLEfOaSTu1exOdBBBqhSVvor9gq7pWsMiDtWiqMo1yBC7gw")

//google maps variables
// map - the google map object
// directionsPanel - the div inw which the directions will go
// gdir - the google map directions object

var map;
var directionsPanel;
var gdir;

// this function clears the value of a text box if the text is the default 'Enter your address' text
// x - text field to be cleared
function setvalue(x)
{
	if(x.value == 'Enter your address')
		x.value = ''
}

// this function includes the given javascript file using javascript write inorder to abstract some of the code
// script_filename - the javascript file to be included
function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}

// this function checks to see if the browser being used is compatible with google maps, loads the map and 
// then finds the directions to the Zedak head quarters using the supplied address. the function then handles any
// errors that may come about with the directions
// address - the user supplied address
function loadmap(address) {
	address += " to 400 Columbus Avenue Valhalla Suite, NY 10595-1335";
    if (GBrowserIsCompatible()) {
      	map = new GMap2(document.getElementById("map"));
      	directionsPanel = document.getElementById("route");
	  	directionsPanel.innerHTML = ""
      	map.setCenter(setpoint(41.096993, -73.777947), 14);
	  	map.addControl(new GLargeMapControl());
	  	gdir = new GDirections(map, directionsPanel);
		GEvent.addListener(gdir, "error", handleErrors);
		gdir.load(address);
	}
}

// this function hides the contact form on the page and finds the directions from the user defined address
// to the Zedak headquarters
// address - the address the user wishes to find directions from 
function getdirections(address)
{
	var x = document.getElementById('content')
	var y = document.getElementById('mapdiv')
	if(address == '' || address == 'Enter your address')
	 	alert('Please enter an address')
	else
	{
		x.style.display = 'none'
		y.style.display = 'block'
		loadmap(address)
	}
}

// error handler taken from google maps api tutorial
// gdir - the google maps direction onject
function handleErrors(gdir){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		{
	     	directionsPanel.innerHTML = "<div class='gmaperror'>We're sorry, we were unable to find your location.</div>"
			markertext = "<img src='images/logo_xsm.gif' class='gmaplogo'/><div class='gmapaddress'><h3>Zedak Corp.</h3>400 Columbus Avenue<br/>Suite 100 S<br />Valhalla, NY 10595-1335<br/><br/><br/></div>"
			createMarker(map,setpoint(41.096993, -73.777947),markertext,1);
		}
	   else
	{
	    	directionsPanel.innerHTML = "<div class='gmaperror'>We're sorry, an error has occurred please try again later.</div>"
			markertext = "<img src='images/logo_xsm.gif' class='gmaplogo'/><div class='gmapaddress'><h3>Zedak Corp.</h3>400 Columbus Avenue<br/>Suite 100 S<br />Valhalla, NY 10595-1335<br/><br/><br/></div>"
			createMarker(map,setpoint(41.096993, -73.777947),markertext,1);
	 } // commented out to only show two types of error, bad address and all other errors 
	   // 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.");
	   
}

// this function sets a marker with a window and sets the window text
// whichmap - says which google map the marker will appear on
// point - sets the point on which the marker will appear
// text - the window text
// open - sets whether or not the window will default to open
function createMarker(whichmap, point, text, open) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(text);
  });
  whichmap.addOverlay(marker)
  if(open == 1)
  	GEvent.trigger(marker,"click");
}

// this function sets a google map point based on user provided information
// lat - the point's latitude
// lon - the point's longitude
function setpoint(lat,lon)
{
	point = new GLatLng(lat, lon);
	return point
}

// this function returns the user from the direction page to the contact form
function showform()
{
	var x = document.getElementById('content')
	var y = document.getElementById('mapdiv')
	
	y.style.display = 'none';
	x.style.display = 'block';
}
