GoogleMap = new Class({
  'initialize': function(strMapElement, strDirectionsElement) {
    this.objMap = new GMap2($(strMapElement));
    this.objDirectionsPanel = $(strDirectionsElement);
    this.objGeocoder = new GClientGeocoder();
    this.arrPoints = [];
  },
  'setToAddress': function(strAddress, strName) {
    this.arrPoints = [];
    this.objGeocoder.getLocations(strAddress, this.addToMap.bind(this));
  },
  'addToMap': function(objGeocode) {
    if(!objGeocode) {
      alert(strAddress + ' was not found!');
    } else {
      if(strName) {
        objGeocode.name = strName;
      }
      objPlacemarks = objGeocode.Placemark[0];
      objPoint = new GLatLng(objPlacemarks.Point.coordinates[1],
          objPlacemarks.Point.coordinates[0]);
      this.arrPoints[1] = objGeocode.Placemark[0].address;
      this.objMap.setCenter(objPoint, 13);
      objMarker = new GMarker(objPoint);
      this.objMap.addOverlay(objMarker);
    }
  },
  'setFromAddress': function(strAddress, strName) {
    if(this.arrPoints) {
      this.objGeocoder.getLocations(strAddress, this.showRoute.bind(this));
    }
  },
  'showRoute': function(objGeocode) {
    if(!objGeocode) {
      alert(strAddress + ' was not found!');
    } else {
      this.objMap.clearOverlays();
      this.objDirectionsPanel.empty();
      objPoint = objGeocode.Placemark[0].address;
      this.arrPoints[0] = objPoint;
      objDirections = new GDirections(this.objMap, this.objDirectionsPanel);
      objDirections.loadFromWaypoints(this.arrPoints);
    }
  }
});

var objGoogleMap;

window.addEvent('domready', function() {
  objGoogleMap = new GoogleMap('map', 'directions');
  strAddress = '2729 78th AVE SE Mercer Island, WA 98040';
  strName = 'Mercer Island Eye Works';
  objGoogleMap.setToAddress(strAddress, strName);
});