/*  MCPS Unobtrusive GMaps JavaScript Library, version .06 (7/24/2007)
 *  (c) 2007 Montgomery County Public Schools
 *  by Matthew Barger & Zac Gordon
 *
 *  The MCPS Unobtrusive GMaps JavaScript Library is an easy way to embed
 *  a Google Map on the MCPS web server.  The library is designed to run on
 *  either static or dynamic HTML pages using only structured and semantic  
 *  markup.  Geolocation and descriptive data is pulled directly from the  
 *  HTML (via hCard) and loaded into a GMap instance.  The library supports
 *  more than 1 hCard per page.
 * 
 *  Requirements:  
 *    - Geolocation and descriptive data needed to be in both the 
 *      hcard and geo microformats (http://microformats.org/).
 *
 *    - Prototype 1.6+ (http://prototypejs.org/).  We use getElementsByClassName
 *      and Template.
 *
 *    - A valid Google Maps API Key (http://google.com/apis/maps/).
 *
/*--------------------------------------------------------------------------*/

/* Unobtrusive JavaScript Helper Functions -------------------------------- */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function addUnLoadEvent(func) {
	var oldunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldunload();
			func();
		}
	}
}

/* Find hCards on the Page ------------------------------------------------ */
function findHcards() {
  var hcards = $$('.vcard');
  return hcards;
}

/* Create hCard JS Objects from hCard Markup ------------------------------ */
function fillHcardObject(hCardRef) {
	var hCard = {
	  addressName: null,
	  addressStreet: null,
	  addressCity: null,
	  addressState: null,
	  addressZip: null,
	  latitude: null,
	  longitude: null,
	  template: '<strong>#{addressName}</strong><br />#{addressStreet}<br />#{addressCity}, #{addressState} #{addressZip}'
	};
	
	hCard.addressName = hCardRef.getElementsByClassName('fn')[0].firstChild.nodeValue;
	hCard.addressStreet = hCardRef.getElementsByClassName('street-address')[0].firstChild.nodeValue;
	hCard.addressCity = hCardRef.getElementsByClassName('locality')[0].firstChild.nodeValue;
	hCard.addressState = hCardRef.getElementsByClassName('region')[0].firstChild.nodeValue;
	hCard.addressZip = hCardRef.getElementsByClassName('postal-code')[0].firstChild.nodeValue;
	hCard.latitude = parseFloat(hCardRef.getElementsByClassName('latitude')[0].firstChild.nodeValue);
	hCard.longitude = parseFloat(hCardRef.getElementsByClassName('longitude')[0].firstChild.nodeValue);
	
	return hCard;
	
}

/* Create Marker & InfoWindow from hCard JS Object ------------------------ */
function createMarker(hcard) {
 	var marker = new GMarker(new GLatLng(hcard.latitude, hcard.longitude));
	var t = new Template(hcard.template);
	var c = t.evaluate(hcard)
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(c);
 	});
	return marker;
}

/* Find Map Container Node ------------------------------------------------ */

function findMapContainer() {
	// Look for a node with an ID = 'map' or an ID ending with a 'MapMapMap'
	// The second lookup is useful for dynamic pages built by ASP.net where
	// the IDs of elements are partially managed by a compiler
	
	var divs = document.getElementsByTagName("div");
	var pattern = /map$/;
	for (var index = 0; index < divs.length; ++index) {
		if (pattern.test(divs[index].id)) {
			return divs[index];
		}
	}
}

/* Main Function: Build and Load Map -------------------------------------- */
var map;
function loadMap() {
	if (GBrowserIsCompatible()) {
		var hcardRefs = findHcards();
		var hcards = [];
		var markers = [];
		for (var index = 0; index < hcardRefs.length; ++index) {
			hcards.push(fillHcardObject(hcardRefs[index]));
		}
		
		for (var i = 0; i < hcards.length; i++) {
			markers.push(createMarker(hcards[i]));
		}
		
		map = new GMap2(findMapContainer());
		//var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(hcards[0].latitude, hcards[0].longitude), 12);
		/* map.setMapType(G_MAP_TYPE); */
		map.addControl(new GLargeMapControl());
		for (var i = 0; i < markers.length; i++) {
			map.addOverlay(markers[i]);
		}
		
	}
}

/* Initialize Load & UnLoad Events ---------------------------------------- */
addLoadEvent(loadMap);
addUnLoadEvent(GUnload);