var redicon = new GIcon();
redicon.image = "/wiki/pub/googlemaps/rinksmap/mm_20_red.png";
redicon.shadow = "/wiki/pub/googlemaps/rinksmap/mm_20_shadow.png";
redicon.iconSize = new GSize(12, 20);
redicon.shadowSize = new GSize(22, 20);
redicon.iconAnchor = new GPoint(6, 20);
redicon.infoWindowAnchor = new GPoint(5, 5);

var map;
var rinks = [];

LoadManager.registerloadfunction(pageload);

function pageload() {
 map = new GMap2(document.getElementById("rinksmap")); 
// map.setCenter(new GLatLng(43.719505,-79.417419), 11); 
 map.setCenter(new GLatLng(43.70263,-79.433899), 10); 
 map.addControl(new GSmallMapControl());
 map.addControl(new GMapTypeControl());
 GDownloadUrl("/wiki/pub/googlemaps/rinksmap.xml", 
 function(data, responseCode){
    var xml = GXml.parse(data);
    var items = xml.documentElement.getElementsByTagName("rink");
    var theListString='<ul class="narrow">';
    var theLength=items.length;
    for (var i = 0; i < theLength; i++) {
      var theObject={},theItem,theElement;
      theItem=items[i];
      if (theItem.firstChild) {
		theElement=theItem.firstChild;
		if (theElement.nodeType==1) { //ELEMENT_NODE
		  theObject[theElement.tagName.toLowerCase()]=theElement.firstChild.nodeValue;
		}
		while (theElement.nextSibling) {
			theElement=theElement.nextSibling;
			if (theElement.nodeType==1) { //ELEMENT_NODE
			  theObject[theElement.tagName.toLowerCase()]=theElement.firstChild.nodeValue;
			}
		}
		if (theObject.longitude&&theObject.latitude) {
			theObject.id="id"+i;
			rinks[theObject.id]=theObject;
			theObject.marker = createmarker(theObject);
			map.addOverlay(theObject.marker);
			theListString+="<li><a href='javascript:showmarkerwindow(\""+theObject.id+"\")'>"+theObject.name+"</a></li>";
		}
	}
    }
    theListString+="</ul>";
    var theListElement=document.getElementById("rinkslist");
    if (theListElement) theListElement.innerHTML=theListString;
});
}
function createmarker(theObject) {
  var theMarker;
  var point = new GLatLng(parseFloat(theObject.latitude), parseFloat(theObject.longitude));
  theMarker=new GMarker(point,{icon:redicon});
  GEvent.addListener(theMarker, "click", function() {
    var theString='<div style="width:'+(theObject.image?'300px':'200px')+';font-size:84%">';
    theString+=theObject.image?"<img style='float:right;' src='"+theObject.image+"'/>":'';
    theString+=theObject.name?"<h6 class='compact'>" + theObject.name + "</h6>":'';
    theString+=theObject.location?"<i>Location:</i> "+theObject.location+"<br>":'';
//    theString+=theObject.contact?"<i>Contact:</i> "+theObject.contact+"<br>":'';
    theString+=theObject.info?"See <a href='"+theObject.info+"'>website pages</a>. &nbsp;":'';
//    theString+=theObject.website?"<a target='_blank' href='http://"+theObject.website+"'>website</a> &nbsp;":'';
//    theString+=theObject.email?"<a href='mailto:"+theObject.email+"'>email</a> &nbsp;":'';
    theString+=theObject.phone?"["+theObject.phone+"]<br>":'';
    theString+=theObject.ward?theObject.ward+"<br>":'';
    theString+=(theObject.councillor&&theObject.councillorinfo)?"<i>Councillor:</i> <a href='http://cityrinks.ca/wiki/wiki.php?n="+theObject.councillorinfo+"'>"+theObject.councillor+"</a><br>":'';
    theString+=theObject.map?"See <a target='_blank' href='"+theObject.map+"'>satellite map</a>":'';
    theString+="<div style='clear:both'>&nbsp;</div></div>";
    theMarker.openInfoWindowHtml(theString);
  });
  return theMarker;
}

function showmarkerwindow(theIndex) {
 GEvent.trigger(rinks[theIndex].marker,'click');
}

function pageunload() {
  GUnload();
}