var map = null;
var geocoder = null;
var baseIcon = null;
var bounds = null;

function gmap_init(){
  // Create a google map
  map = new GMap2(document.getElementById("map_canvas"));
  map.setUIToDefault();

  //create a base icon
  baseIcon = new GIcon(G_DEFAULT_ICON);
  baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseIcon.iconSize = new GSize(20, 34);
  baseIcon.shadowSize = new GSize(37, 34);
  baseIcon.iconAnchor = new GPoint(9, 34);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);

  // initialize the geocoder for address location lookups
  geocoder = new GClientGeocoder();
}

function addEvent(a_name, a_address){
  geocoder.getLatLng(a_address,
    function(point) {
      if (point) {
        var marker = createMarker(point, a_name, a_address, "blue", "E");
        map.addOverlay(marker);
        if (bounds==null){
          map.setCenter(point, 15);
          bounds = map.getBounds();
        }else{
          bounds.extend(point);
          map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-1);
        }
      }
    }
  );
}

function addHotel(a_name, a_address){
  geocoder.getLatLng(a_address,
    function(point) {
      if (point) {
        var marker = createMarker(point, a_name, a_address, "red", "A");
        map.addOverlay(marker);
        if (bounds==null){
          map.setCenter(point, 15);
          bounds = map.getBounds();
        }else{
          bounds.extend(point);
          map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-1);
        }
      }
    }
  );
}

function createMarker(a_point, a_name, a_address, a_color, a_letter){
  var letteredIcon = new GIcon(baseIcon);
  letteredIcon.image = "http://events.sportaccom.com/images/gmap/"+a_color+"_Marker"+a_letter+".png";
  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(a_point, markerOptions);

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<b>"+a_name+"</b><br />"+a_address);
  });

  return marker;
}

