var map;
var geocoder;
var infoWindow;
var bounds;

/***
 * initMap
 *    Initializes the Google map.
 ***/
function initMap()
{
   if (undefined == window.markers || markers.constructor != Array) {
      document.getElementById('map_canvas').style.display = 'none';
   } else {
   
      var options = {
         center: new google.maps.LatLng(34, -110),
         disableDefaultUI: true,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
         navigationControl: true,
         navigationControlOptions: {
               style: google.maps.NavigationControlStyle.SMALL
            },
         scrollwheel: false,
         zoom: 10
      };
      
      map = new google.maps.Map(document.getElementById('map_canvas'), options);
      infoWindow = new google.maps.InfoWindow();
      bounds = new google.maps.LatLngBounds();

      for (var i = 0; i < markers.length; i++) {
         addMarker(markers[i]);
      }
      
      google.maps.event.addListener(map, 'click', function() {
            infoWindow.close();
         });
   }
} // end initMap



/***
 * addMarker
 *    Adds a marker to the map at a specified location, potentially geocoding
 *    the location, if necessary.
 *    
 * @param data an object with the following properties:
 *       info - ID of a container that contains the content for the info window
 *       address - the physical address of the location
 *       lat - the latitude of the location; if set to -1000, will be ignored
 *       lng - the longitude of the location; if set to -1000, will be ignored
 ***/ 
function addMarker(data)
{
   var latlng;
   if (undefined != data.lat && undefined != data.lng
      && data.lat != -1000 && data.lng != -1000) {
      latlng = new google.maps.LatLng(data.lat, data.lng);
      createMarker(latlng, data);
   } else if (undefined != data.address) {
      if (null == geocoder) {
         geocoder = new google.maps.Geocoder();
      }
      geocoder.geocode({
            address: data.address
         },
         function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
               latlng = results[0].geometry.location;
               createMarker(latlng, data);
            }
         });
   }
} // end addMarker



/***
 * createMarker
 *    Creates a marker at a specified location.
 *    
 * @param latlng an initialized google.maps.LatLng that contains the marker's
 *               coordinates
 * @param data an object with the following properties:
 *       info - ID of a container that contains the content for the info window
 *       address - the physical address of the location
 *       lat - the latitude of the location; if set to -1000, will be ignored
 *       lng - the longitude of the location; if set to -1000, will be ignored    
 ***/
function createMarker(latlng, data)
{
   var marker;
   marker = new google.maps.Marker({
         map: map,
         position: latlng
      });
   
   if (undefined != data.info && null != document.getElementById(data.info)) {
      google.maps.event.addListener(marker, 'mouseover', function() {
            infoWindow.setOptions({
                  content: document.getElementById(data.info).innerHTML
               });
            infoWindow.open(map, marker);
         });
   }

   bounds.extend(latlng);
   map.fitBounds(bounds);
} // end createMarker