// Google Map Code for ASPCA
function setUp() { 
  if (! document.getElementById("mapDiv")) {
    alert("No mapdiv found");
    return; 
  }
  var map = new GMap2(document.getElementById("mapDiv")); 
  map.addControl(new GLargeMapControl()); 
  map.addControl(new GOverviewMapControl()); 
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(40.010787,-95.344849),4);
  loadData(map);
}

// Define some "constants" 
var TITLE = 0;
var URL = TITLE + 1;
var LOCATION = URL + 1;
var LATITUDE = LOCATION + 1;
var LONGITUDE = LATITUDE + 1;
var DESCRIPTION = LONGITUDE + 1;
var EVENTDATE = DESCRIPTION + 1;
var STARTTIME = EVENTDATE + 1;
var ENDTIME = STARTTIME + 1;
var WEBSITE = ENDTIME + 1;
var HOSTEDBY = WEBSITE + 1;
var LOCATIONNAME = HOSTEDBY + 1;

// This requires markers to be defined somewhere...

// Define a class to represent a shelter 
function Shelter(data) {
  var fields = data.getElementsByTagName("SPAN");

  this.title = fields[TITLE].innerHTML;
  this.url = fields[URL].innerHTML;
  this.location = fields[LOCATION].innerHTML;
  this.latitude = fields[LATITUDE].innerHTML;
  this.longitude = fields[LONGITUDE].innerHTML;
  this.description = fields[DESCRIPTION].innerHTML;
  this.eventdate = fields[EVENTDATE].innerHTML;
  this.starttime = fields[STARTTIME].innerHTML;
  this.endtime = fields[ENDTIME].innerHTML;
  this.website = fields[WEBSITE].innerHTML;
  this.hostedby = fields[HOSTEDBY].innerHTML;
  this.locationname = fields[LOCATIONNAME].innerHTML;



  this.getTitle = function () { return this.title; }
  this.getURL = function () { return this.url; }
  this.getLocation = function () { return this.location; }
  this.getLatitude = function () { return this.latitude; }
  this.getLongitude = function () { return this.longitude; }
  this.getDescription = function () { return this.description; }
  this.getEventDate = function () { return this.eventdate; }
  this.getWebSiteURL = function () { return this.website; }
  this.getEndTime = function () { return this.endtime; }
  this.getStartTime = function () { return this.starttime; }
  this.getHostedBy = function () { return this.hostedby; }
  this.getLocationName = function () {return this.hostedby; }
  
  
}



function loadData(map) {
  // Find the DIV containing the data
  var dataDiv = document.getElementById("mapData");
  if (dataDiv == null) { return; }

  // Get a list of all of the inner DIVs each of which contains one
  // data record
  var records = dataDiv.getElementsByTagName("DIV");
  if (records.length == 0) { return; }

  // Default Google Icons
  var blueIcon = new GIcon();
  blueIcon.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
  blueIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
  blueIcon.iconSize = new GSize(12, 20);
  blueIcon.shadowSize = new GSize(22, 20);
  blueIcon.iconAnchor =  new GPoint(19, 7);
  blueIcon.infoWindowAnchor =  new GPoint(15, 1);

  // Bounds to hold all the markers
  var bounds = new GLatLngBounds();

  markers = new Array(records.length);

  // Iterate over each record
  for (var r = 0; r < records.length; r++) {
    // Get the fields for this record
    var shelter = new Shelter(records[r]);

    // we need to format some HTML for the popup
    var html = getInfoWindowHTML(shelter);

    // Create a Google Maps point
    var point = new GLatLng(shelter.getLatitude(), shelter.getLongitude());
    // add point to bounding rectangle
    bounds.extend(point);

    var icon = blueIcon;

    // create the marker using the appropriate marker icon
    markers[r] = createMarker(point, icon, html);
    // add the new marker to the map
    map.addOverlay(markers[r]);
  }
  // zoom the map to fit the data
  map.setZoom(map.getBoundsZoomLevel(bounds));

  // center the map on the data
  map.setCenter(bounds.getCenter());
}

function createMarker(point, icon, html) { 
  var marker = new GMarker(point, icon); 
  // If there is something to show in the popup... 
  if (html != null) { 
    GEvent.addListener(marker, "click", 
                    function() { 
                     marker.openInfoWindowHtml(html); 
                   }); 
  }
  return marker; 
} 

