var constructGoogleMap = function(spec) {
  var that = {};

  var default_location = new GLatLng(41.87, -87.62);

  var map = new GMap2(document.getElementById(spec.map_div));
  map.enableScrollWheelZoom();


  var bounds = new GLatLngBounds();
  map.setCenter(default_location);
  map.setZoom(3);

  // Adds a location to the map with specified latitude, longitude and GMarkerPlus options
  var addLocation = function(latitude, longitude, options) {
    if (options === undefined) {
      options = {}
    }

    var loc = new GLatLng(latitude, longitude);
  
    marker = new Webitects.GMarkerPlus(loc);
    if (!(options.marker_content === undefined)) {
      marker.bindInfoWindowHtml(options.marker_content);
    }

    bounds.extend(loc);
    map.addOverlay(marker);
  }

  var addOverlay = function(marker)
  {
    return map.addOverlay(marker);
  };

  var clear = function() {
    map.clearOverlays();
    map.setCenter(default_location);
  }

  var recenter = function() {
    var zoom = map.getBoundsZoomLevel(bounds);

    // decrement zoom to make sure all markers comfortably fit on it

    // zoom 3 comfortably covers the continental US, so no need to decrease further.
    if (zoom > 3) {
      zoom--;
    }

    map.setCenter(bounds.getCenter(), zoom);
  }

  var addControl = function(controller) {
    map.addControl(controller);
  }

  that.addLocation = addLocation;
  that.clear = clear;
  that.recenter = recenter;
  that.addControl = addControl;
  that.addOverlay = addOverlay;

  return that;
};
