var map = false;
var directions = false;
function createMarker(point,html) {
				var marker = new GMarker(point);
				GEvent.addListener(marker, "click", function() {
				  marker.openInfoWindowHtml(html);
				});
				return marker;
			  }
			  
function setupmap() {
   if (GBrowserIsCompatible()) {     
      point = new GLatLng(44.309179,-69.790146);      
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(point, 15);
      var marker = createMarker(point,'150 Capitol Street, Suite 4<br /> Augusta, ME 04330<br />207.620.8266');
      map.addOverlay(marker);
   }
}

function getdirections() {
   // First we'll get the start and end address from our
   // form.
   var fmaddr = document.getElementById("fromaddress").value;
   var toaddr = document.getElementById("toaddress").value;
   
   // Next we'll setup a GDirections object. This is what
   // we interact with to generate the directions.
   if (!directions) {
      directions = new GDirections(map);
   }
   
   // Now we'll need to setup a listener to react when
   //  the directions are drawn on the map.
   GEvent.addListener(directions, "addoverlay", function() {
      // First we'll gather up some information about
      // the directions.
      var route = directions.getRoute(0);
      
      // This variable will help us track the boundaries
      // for all the points along our path.
      var bounds = new GLatLngBounds();
      
      // Here's a variable that we'll dump our html
      // results into.
      var disp = "";
      
      // We can start off with a header that's got our
      // distance and an approximate driving duration.
      disp = disp + "<h3>Directions</h3>"+directions.getDistance().html+" about "+directions.getDuration().html+"<br />";

      // We'll put our turn by turn directions into a table.
      disp = disp + "<table>";
      
      // Start looping through the turn by turn elements.
      for(i=0;i< route.getNumSteps();i++) {
         // We work with the turn by turn directions in
         // steps... One at a time.
         var stp = route.getStep(i);
         
         // We'll extend our boundaries for each step
         // so that the entire path is on our map at
         // the end.
         bounds.extend(stp.getLatLng());
         
         // I'm doing alternating colors here to make
         // it a little more readable.
         var clr = (i%2)?"#efefef":"#dfdfdf";
         
         // Here we build our table row. First we do
         // the description of the step, then we
         // display the distance.
         disp = disp + "<tr valign='top'><td style='background:"+clr+";'>" + stp.getDescriptionHtml() + "</td>";
         disp = disp + "<td style='background:"+clr+";'>" + stp.getDistance().html+"</td></tr>";
      }
      // Now we close out our table. and display
      // our results.
      disp = disp + "</table>";
      document.getElementById('dircontainer').innerHTML = disp;
      
      // Now we'll change the map view port so that
      // it shows all the points in our directions.
      map.setZoom(map.getBoundsZoomLevel(bounds));
      map.setCenter(bounds.getCenter());
   });
   
   // This listener will react when either of our
   // addresses is unmapable.
   GEvent.addListener(directions, "error", function() {
      alert("Could not generate a route for the current start and end addresses");
   });
   
   // We finally arrive at the moment of truth. This
   // single line kicks off the whole process.
   directions.load("from: "+fmaddr+" to: "+toaddr, { preserveViewport: true, getSteps: true });
}

function teardownmap() {
   // This command uploads the Google Map API.
   GUnload();
}
