
// Complete the map given the map, the point and the html to display
function completeVenueMap(map, point, mapInfoHtml) {
  map.setCenter(point, 15);
  var marker = createMarker(point, mapInfoHtml);
  map.addOverlay(marker);
  marker.openInfoWindowHtml(mapInfoHtml);
}

// Complete the map given the map, the point and the html to display
function completeVenueForAllVenuesMap(map, point, html) {
  map.setCenter(point, 7);
  var marker = createMarker(point, html);
  map.addOverlay(marker);
}

// Create marker given point and the html to display when clicked
function createMarker(point, mapInfoHtml) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(mapInfoHtml);
  });
  return marker;
}

// Find point using geocoder
function geocodeAddress(address, map, point, mapInfoHtml) {
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert("Address: " + address + " could not be Geocoded.");
        return;
      } else {
        completeVenueMap(map, point, mapInfoHtml);
      }
    }
  );
}

// Find point using geocoder - for the all venues map
function geocodeAddressForAllVenuesMap(address, map, point, html) {
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert("Address: " + address + " could not be Geocoded.");
        return;
      } else {
        completeVenueForAllVenuesMap(map, point, html);
      }
    }
  );
}

// Load venue map given either a lat, lng set or simply an address string
function loadVenueMap(lat, lng, address, mapInfoHtml) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    if (lat != undefined && lng != undefined) {
      var point = new GLatLng(lat, lng);
      completeVenueMap(map, point, mapInfoHtml);
    } else {
      geocodeAddress(address, map, point, mapInfoHtml);
    }
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser.");
  }
}

function loadAllVenuesMap(latArr, lngArr, addressArr, htmlArr) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    var i = 0;
    for (i = 0; i < latArr.length; i++) {
      if (latArr[i] != undefined && lngArr[i] != undefined) {
        var point = new GLatLng(latArr[i], lngArr[i]);
        var marker = createMarker(point, htmlArr[i]);
        map.setCenter(point, 8);
        map.addOverlay(marker);
      }
      else {
        geocodeAddressForAllVenuesMap(addressArr[i], map, point, htmlArr[i]);
      }
    }
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser.");
  }
}

window.onunload = GUnload;
