/* SCLS Member Libraries Map */
/* This script uses the Google Maps API to produces an interactive map of the SCLS Member Libraries*/

//Map and Gmarkers have to be global variables for the sidebar links to work with the myclick function
var map;

//Declare the array to hold copies of the markers used by the side_bar
var gmarkers = [];

//ID the feed that contains the library map data
var feedUrl = "http://maps.google.com/maps/ms?ie=UTF8&hl=en&oe=UTF8&msa=0&output=georss&msid=109896067737528134055.000472201603402cbb742";
google.load("feeds", "1");

/*if (document.images)
{
  var pic1= new Image(20,34); 
  pic1.src="http://www.scls.info/Scripts/marker.png"; 
  var pic2 = new Image(37,34);
  pic2.src="http://www.scls.info/Scripts/markershadow.png";
}*/

var fastIcon = new GIcon(G_DEFAULT_ICON);
fastIcon.image = 'http://www.scls.info/Scripts/marker.png';
fastIcon.shadow = 'http://www.scls.info/Scripts/markershadow.png';
	
// This function picks up the click and opens the corresponding info window
function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
}

window.onload=function initialize() {

  if (GBrowserIsCompatible()) {
	//document.getElementById("map_link").innerHTML = '<a href="http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=109896067737528134055.000472201603402cbb742&z=8">View this map in a larger window</a> | <a href="http://www.scls.info/about/graphics/systemmap.gif">Non-interactive map (better for older browsers)</a>'; 
	
	// this variable will collect the html which will eventually be placed in the side_bar
    var side_bar_html = "";
	
	//Create the marker objects, make them clickable, and send them back 
	function createMarker(point,label,html) {
		//get the html ready for the info window
		var htmlToPrint = '<b>' + label + '</b><br>' + html + '<br><br><a href="http://maps.google.com/maps?saddr=&daddr=' + label + '%40' + point.toUrlValue() + '" target ="_blank">Get Directions<\/a>';
		
        var marker = new GMarker(point, {icon:fastIcon, title:label});
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(htmlToPrint);
        });
		
		// save the info we need to use later for the side_bar
        gmarkers.push(marker);
        // add a line to the side_bar html
        side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + label + '<\/a><br>';

        return marker;
    }
	
	//Draw the map on the page with the zoom slider
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(43.636075, -89.593506),8);
	
	//Create a manager to handle the markers more efficiently  
	//var mgr = new GMarkerManager(map);
	
	//Markers have to be in an array
	//var sclsMarkers = [];
          
	var feed = new google.feeds.Feed(feedUrl);
	
	feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
	feed.setNumEntries(63);

	feed.load(function(result) {
	   if (!result.error) {
		   for (var i = 0; i < result.feed.entries.length; i++) {
			   //get the georss point tags out of the item array
			  var geoRssPointNode = result.feed.entries[i].xmlNode.getElementsByTagName("georss:point");
			  //WebKit browsers get no results with the original tag name
			  if (geoRssPointNode.length == 0) {
				geoRssPointNode = result.feed.entries[i].xmlNode.getElementsByTagName("point");  
			  }
			  var geoRssPoint = geoRssPointNode[0].firstChild.nodeValue;
			  //split the georss point into lat and lng
			  var pointParts = geoRssPoint.split(' ');
			  // obtain the attribues of each marker
			  var lat = parseFloat(pointParts[0]);
			  var lng = parseFloat(pointParts[1]);
			  var point = new GLatLng(lat,lng);
			  var labelNode = result.feed.entries[i].xmlNode.getElementsByTagName("title");
			  var label = labelNode[0].firstChild.nodeValue;
			  var htmlNode = result.feed.entries[i].xmlNode.getElementsByTagName("description");
			  var html = htmlNode[0].firstChild.nodeValue;
			  var marker = createMarker(point, label, html);
			  map.addOverlay(marker);
			  //sclsMarkers.push(marker);

			}
		}

        // put the assembled side_bar_html contents into the side_bar div
        document.getElementById("side_bar").innerHTML = side_bar_html;
		//Add the markers to the manager and specify the zoom levels at which they should be visible
		//mgr.addMarkers(sclsMarkers, 0, 17);
		//mgr.refresh();
		
      });
  }
}