// JavaScript Document
var map;
var address;
var geocoder;
var directionsPanel;
var directions;


function initialize() {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(41.76865, -70.52343), 13);
 map.addControl(new GLargeMapControl);
  geocoder = new GClientGeocoder();
 var point = new GLatLng(41.76865, -70.52343);
var over = map.addOverlay(new GMarker(point));
  GEvent.addListener(map, "click", getAddress(over, point));
    directionsPanel = document.getElementById("my_directions");

} 

function setDirections(startpt)
{   directions = new GDirections(map, directionsPanel);
 directions.load("from: " + startpt + " to: Freeman Lane, Sagamore, MA");
}
function getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng;
    geocoder.getLocations(latlng, showAddress);
  }
}

function showAddress(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(
        '<b>Sagamore Inn<br/>' + 
        '<b>Rt 6A on the Sagamore/Sandwich line.<br>');
  }
  
}
