
// This part belongs in a separate JS file eventually
// Google Map Code
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(37.807427,-122.432241),8);
  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 OPPHOURS = LONGITUDE + 1; 
var PHONE = OPPHOURS + 1; 
var DIRECTIONS = PHONE + 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.opphours = fields[OPPHOURS].innerHTML;
  this.phone = fields[PHONE].innerHTML;
  this.directions = fields[DIRECTIONS].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.getOpphours = getOpphours;
	Shelter.prototype.getPhone = getPhone;
    Shelter.prototype.getDirections = getDirections;
  }
}

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 getOpphours() { return this.opphours; }
function getPhone() { return this.phone; }
function getDirections() { return this.directions; }


function getInfoWindowHTML(obj) {
  var html = "";
  html += '<p><a href="' + obj.getURL() + '">' + obj.getTitle() + '</a><br>';
	if (obj.getLocation() != '') { html += obj.getLocation()+'<br>';}
	if (obj.getPhone() != '') { html += obj.getPhone()+'<br>'; }
	if (obj.getOpphours() != '') { html += 'Hours: '+obj.getOpphours()+'<br>'; }
	if (obj.getDirections() != '') { html += '<a href="' + obj.getDirections() + '">Get Directions</a></p>'; }
	return html;
}

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(7, 19);
  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
