/*
* Extended GMaps API 
*/

if (GMap2 == undefined)
{
    this.debug('Error: You must include the Google Maps API external javascript in your page with the correct key.');
}

GMap2.prototype.max_auto_zoom = 16;
GMap2.prototype.default_zoom = 13;
GMap2.prototype.zoom_level = GMap2.default_zoom;
GMap2.prototype.locations = [];
GMap2.prototype.enable_debug = false;

GMap2.prototype.createMarker = function (point, info_html, icon_settings)
{
  if (icon_settings != null)
  {
    icon = new GIcon();
    icon.iconAnchor = G_DEFAULT_ICON.iconAnchor;
    
    for (s in icon_settings)
    {
        icon[s] = icon_settings[s];
    }
  }
  else
  {
    icon = new GIcon(G_DEFAULT_ICON);
  }
  
  var marker = new GMarker(point, icon);
  var tabs = new Array();
    
  for (var i=0; i<info_html.length; i++)
  {
     tabs.push(new GInfoWindowTab(info_html[i].label, info_html[i].html)); 
  }

  GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowTabsHtml(tabs);
  });

  return marker;
}

GMap2.prototype.addLocations = function (locations, auto_zoom)
{
  
  if (auto_zoom == null) auto_zoom = true;
  
  var center = this.getCenter();
  if (center == null)
  {
    this.debug("Error: Cannot add locations without a set center."); 
    return false;
  }
  var bounds = this.getBounds();

  
  for (var i=0; i<locations.length; i++)
  {
    errors = new Array();
    if (locations[i].coordinates.lat == null) errors.push("Latitude is empty for location #");
    if (locations[i].coordinates.lng == null) errors.push("Longitude is empty for location #");
    if (errors.length > 0)
    {
        for (e=0; e<errors.length; e++)
        {
            this.debug(errors[e]);
        }
        return false;
    }
    
    loc = new GLatLng(locations[i].coordinates.lat,locations[i].coordinates.lng);

    if (auto_zoom == true)
    {
        bounds.extend(loc);
    }
    
    locations[i].marker = this.createMarker(loc, locations[i].info_html, locations[i].icon);
    
    this.addOverlay(locations[i].marker);
  }
  
  if (auto_zoom == true)
  {
    zoom = this.getBoundsZoomLevel(bounds);
     
    if (zoom > this.defaultZoom) zoom = this.defaultZoom;
    this.setCenter(bounds.getCenter(), zoom);
  }

}

// override setCenter to use max_auto_zoom
/*
GMap2.prototype._setCenter = GMap2.prototype.setCenter;
GMap2.prototype.setCenter = function(center, zoom, type)
{
  if (zoom == null) zoom = this.max_auto_zoom;
  this._setCenter(center, zoom, type);
}
*/

//debug function
GMap2.prototype.debug = function(txt)
{
    if (this.enable_debug)
    {
        GLog.write(txt);
    }
}
