// This part belongs in a separate JS file eventually

// 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 MYMOFFERED = LONGITUDE + 1;
var MYMOFFEREDLABEL = MYMOFFERED + 1;
var PHONE = MYMOFFEREDLABEL + 1;
var EMAIL = PHONE + 1;
var WEBSITE = EMAIL + 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.mymoffered = fields[MYMOFFERED].innerHTML;
  this.mymofferedlabel = fields[MYMOFFEREDLABEL].innerHTML;
  this.phone = fields[PHONE].innerHTML;
  this.email = fields[EMAIL].innerHTML;
  this.website = fields[WEBSITE].innerHTML;

  if (typeof(_Shelter_prototype_called) == 'undefined') {
    _Shelter_prototype_called = true;
    Shelter.prototype.getTitle = getTitle;
    Shelter.prototype.getURL = getURL;
    Shelter.prototype.getLocation = getLocation;
    Shelter.prototype.getLatitude = getLatitude;
    Shelter.prototype.getLongitude = getLongitude;
    Shelter.prototype.getMyOffered = getMyOffered;
    Shelter.prototype.getMyOfferedLabel = getMyOfferedLabel;
    Shelter.prototype.getWebSiteURL = getWebSiteURL;
    Shelter.prototype.getEmail = getEmail;
    Shelter.prototype.getPhone = getPhone;
  }
}

function getTitle() { return this.title; }
function getURL() { return this.url; }
function getLocation() { return this.location; }
function getLatitude() { return this.latitude; }
function getLongitude() { return this.longitude; }
function getMyOffered() { return this.myoffered; }
function getMyOfferedLabel() { return this.myofferedlabel; }
function getPhone() { return this.phone; }
function getEmail() { return this.email; }
function getWebSiteURL() { return this.website; }

function getInfoWindowHTML(obj) {
  var html = "";
  if (obj.getWebSiteURL() != "") {
    if (obj.getWebSiteURL().indexOf("http") != -1 ) {
      html = '<span style="font-family: arial; font-size: 10pt;"><a href="' + obj.getWebSiteURL() + '">' + obj.getTitle() + '</a><br>';
    } else {
      html = '<span style="font-family: arial; font-size: 10pt;"><a href="http://' + obj.getWebSiteURL() + '">' + obj.getTitle() + '</a><br>' 
    }
  } else {
    html = '<span style="font-family: arial; font-size: 10pt;">' + obj.getTitle() + '<br>';
  }
  return html + 'Location: ' + obj.getLocation() + '<br>Phone: ' + obj.getPhone() + '<br>Email: ' + obj.getEmail() + '<br><a href="http://maps.google.com/maps?saddr=' + obj.getLocation() + '">Get Directions</a></span>';
}

function getInfoWindowHTML_OLD(shelter) {
  return '<a href="' + shelter.getWebSiteURL() + '">' + shelter.getTitle() + '</a><br>Location:' + shelter.getLocation() + '<br>Phone:' + shelter.getPhone() + '<br>Email:' + shelter.getEmail() + '</span>';
}

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; 
} 

// End of separate file code
