function initGoogleMap( mapDiv, mapItems ) {

  if ( GBrowserIsCompatible() ) {

    var map = new GMap2( mapDiv );

		map.addControl( new GSmallMapControl() );
		map.addControl( new GMapTypeControl() );

    var bounds;

    mapItems.each (
      function( mapItem, index ) {
        if ( mapItem.latitude && mapItem.longitude ) {
          if ( index == 0 ) {
            bounds = new GLatLngBounds( new GLatLng( mapItem.latitude, mapItem.longitude ), new GLatLng( mapItem.latitude, mapItem.longitude ) );
            map.setCenter( bounds.getCenter() ); // required to give the map some context before adding markers
          } else {
            bounds.extend( new GLatLng( mapItem.latitude, mapItem.longitude ) )
          }
          addMarker( map, mapItem );
        }
      }
    );
		
    map.setCenter( bounds.getCenter() );

    if ( mapItems.length == 1 ) {
      map.setZoom(12);
    } else {
      var boundszoom = map.getBoundsZoomLevel( bounds, map.getSize() );
      map.setZoom( boundszoom );
    }

  }

}


function addMarker(map, mapItem) {

  var mapDivId = map.getContainer().id;
  var tooltipId = mapDivId + '_tooltip_' + mapItem.id;
  var markerId = mapDivId + '_' + mapItem.id;

  // Create the marker. I'm setting the id so that I can grab 
  // the relevant Dom element and attach a tooltip to it 
  // later.
  var marker = new GMarker( new GLatLng( mapItem.latitude, mapItem.longitude ), { id: markerId } );

  if (mapItem.url != null) {
    GEvent.addListener( marker, 'click', function() { window.location = mapItem.url; } );
  }

  map.addOverlay( marker );

  // Add the tooltip.  Using 'mtgt_' plus the id I set earlier
  // probably isn't a good idea (it isn't part of the published
  // API so Google could change it at any time...)
  var tooltip = new MapTooltip( 'mtgt_' + markerId, tooltipId );
}


// Make sure we tidy up
Event.observe( window, "unload", GUnload );
