// JavaScript Document
/* A More Accessible Map */
var conf_idleTime    = 3500; //3 seconds
var conf_popupLeft   = "365px"; //should be 365
var conf_popupTop    = "100px";

var mapMaker = {
	element: false,	DLs:     false,	DTs:     false,	on:      false,	idleTimeout: false,
	init: function(){
		var i=0;
		var ii=0;
		var currentLocation = 0;
		mapMaker.DLs = document.getElementsByTagName('dl');
		mapMaker.DTs = document.getElementsByTagName('dt');
		if( mapMaker.on == false ){// only loop thru items once
			while (mapMaker.DLs.length > i) { //loop through each DL on page
				if (mapMaker.DLs[i].className == 'map'){ //only affect DLs with a class of 'map'
					mapMaker.DLs[i].className = 'map on';//change map DL class, this way map is text only without javascript enabled
					mapMaker.stripWhitespace(mapMaker.DLs[i]);//remove any white space
					mapMaker.stripWhitespace(mapMaker.DTs[i]);
					while (mapMaker.DTs.length > ii){// loop thru all DT elements
						currentLocation = mapMaker.DTs[ii].firstChild;
						mapMaker.addEvt(currentLocation,'mouseover',mapMaker.showTooltip);//displays tooltip on mouse over
						mapMaker.addEvt(currentLocation,'focus',mapMaker.showTooltip);//display tooltip on focus, for added keyboard accessibility
						ii++;
					};
					ii=0;
				};
				i++;
			};
			mapMaker.on = true;
		};
	},
	showTooltip: function() {
		if(mapMaker.idleTimeout) clearTimeout(mapMaker.idleTimeout);
		if(mapMaker.element) mapMaker.hideTooltip();
		mapMaker.idleTimeout = setTimeout('mapMaker.hideTooltip()',conf_idleTime);
		var evt = this;
		var i = 0;
		var objid = evt.parentNode.nextSibling;  //Find DD to display - based on currently hovered anchor move to parent DT then next sibling DD
		mapMaker.element = objid;	//set for the hideTooltip
		objid.style.top  = conf_popupTop;
		objid.style.left = conf_popupLeft;
		//objid.style.width  = conf_popupWidth;
		//objid.style.height = conf_popupHeight;
	},
	hideTooltip: function() {
		mapMaker.element.style.left = '-9999px';
	},
	addEvt: function(element, type, handler) {
		if (!handler.$$guid) handler.$$guid = mapMaker.addEvt.guid++; // assign each event handler a unique ID
		if (!element.events) element.events = {}; // create a hash table of event types for the element
		var handlers = element.events[type]; // create a hash table of event handlers for each element/event pair
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element["on" + type]) { // store the existing event handler (if there is one)
				handlers[0] = element["on" + type]; 
			};
		};
		handlers[handler.$$guid] = handler; // store the event handler in the hash table
		element["on" + type] = mapMaker.handleEvent; // assign a global event handler to do all the work
	},
	handleEvent: function(event) {
		var returnValue = true;
		event = event || mapMaker.fixEvent(window.event); // grab the event object (IE uses a global event object)
		var handlers = this.events[event.type]; // get a reference to the hash table of event handlers
		for (var i in handlers) { // execute each event handler
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			};
		};
		return returnValue;
	},
	fixEvent: function(event) {// add W3C standard event methods		
		event.preventDefault = mapMaker.fixEvent.preventDefault;
		event.stopPropagation = mapMaker.fixEvent.stopPropagation;
		return event;
	},
	stripWhitespace: function( el ){
		for(var i = 0; i < el.childNodes.length; i++){
			var node = el.childNodes[i];
			if( node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
				node.parentNode.removeChild(node);
			};
		};
	}
};

mapMaker.fixEvent.preventDefault = function() {this.returnValue = false;};
mapMaker.fixEvent.stopPropagation = function() {this.cancelBubble = true;};
mapMaker.addEvt.guid = 1;


//TODO : Make this really work across as many browsers as possible .... conditional includes and whatever else ....
mapMaker.addEvt( window, 'load', mapMaker.init);
