var gdir, fromAddress, toAddress;

function initialize() {   
  if (GBrowserIsCompatible()) {   
    //settings   
    var companyMarkerImage= "location/images/gmapslogo.gif";   
    var companyLatLng     = new GLatLng(41.902784,-70.982419);   
    var companyMarkerSize = new GSize(55, 55); //width, height
    var directions;
    var directionsPanel;   
       
    toAddress = "155 Millennium Circle Lakeville, MA 02347";   
  
    var defaultZoomLevel  = 16;   
    //end settings   
  
    //setup elements   
    var map = new google.maps.Map2(document.getElementById("map"));
    directionsPanel = document.getElementById("directionz");   
    gdir  = new GDirections(map, directionsPanel);
      
      
    map.setMapType(G_NORMAL_MAP);
    map.addControl(new GLargeMapControl3D()); 
    map.addControl(new GMapTypeControl());
     
    //error handler   
    GEvent.addListener(gdir, "error", handleErrors);   
  
    //set company marker   
    var companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize);   
  
    //set map center   
    map.setCenter(companyLatLng, defaultZoomLevel);   
    map.addOverlay(companyMarker); 
    
         
  }   
}  

function createMarker(latlng, imageURL, imageSize)   
{   
  
    var marker      = new GIcon(G_DEFAULT_ICON, imageURL);   
    marker.iconSize = imageSize;  
  
    return new GMarker(latlng, { icon: marker });   
  
} 

function overlayDirections()   
{   
    fromAddress = document.getElementById("street").value   
    gdir.load("from: " + fromAddress + " to: 41.90295,-70.982012");   
}  

  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_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.");
  }
