var map = null;
var geocoder = null;

function initializeMap() {
  if (GBrowserIsCompatible()) {
    map      = new GMap2($('map_canvas'));
    map.setCenter(new GLatLng(37.4419, -122.1419), 5);
    geocoder = new GClientGeocoder();
  }  
}


function showMap(address, city, state, zip) {
  if (geocoder) {
    geocoder.getLocations(
      address+", "+city+", "+state+" "+zip,
      function(response) {
        if (!response || response.Status.code != 200) {
          // Fall back to city, state zip if we can't find the exact address
          geocoder.getLocations(
            city+", "+state+" "+zip,
            function(response) {
              if (!response || response.Status.code != 200) {
                alert(zip + " not found. " + response.Status.code);
              } else {
                centerAndMark(response.Placemark[0]);
              }
            }
          );
        } else {
          centerAndMark(response.Placemark[0]);
        }
      }
    );
  } else {
    alert('no geocoder available');
  }
}

function centerAndMark(place) {
  point = new GLatLng(place.Point.coordinates[1],
                      place.Point.coordinates[0]);
  map.setCenter(point, 10);
  var marker = new GMarker(point);
  map.addOverlay(marker);
}