var mapaMaker = {
	offsetX: -16, // tooltip X offset
	offsetY: 16,  // tooltip Y offset
	element: false,
	DLs:     false,
	DTs:     false,
	on:      false,
	/* constructor - sets events */
	init: function(){
		var i=0;
		var ii=0;
		var currentLocation = 0;
		mapaMaker.DLs = document.getElementsByTagName('dl');
		mapaMaker.DTs = document.getElementsByTagName('dt');
		// only loop thru items once
		if( mapaMaker.on == false ){
			//loop through each DL on page
			while (mapaMaker.DLs.length > i) {
				//only affect DLs with a class of 'mapa'
				if (mapaMaker.DLs[i].className == 'mapa'){
					//change mapa DL class, this way mapa is text only without javascript enabled
					mapaMaker.DLs[i].className = 'mapa on';
					//strip whitespace
					mapaMaker.stripWhitespace(mapaMaker.DLs[i]);
					mapaMaker.stripWhitespace(mapaMaker.DTs[i]);
					// loop thru all DT elements
					while (mapaMaker.DTs.length > ii){
						//current Location
						currentLocation = mapaMaker.DTs[ii].firstChild;
						// add events to links
						mapaMaker.addEvt(currentLocation,'mouseover',mapaMaker.showTooltip);//displays tooltip on mouse over
						mapaMaker.addEvt(currentLocation,'mouseout',mapaMaker.hideTooltip);//hide tooltip on mouse out
						mapaMaker.addEvt(currentLocation,'focus',mapaMaker.showTooltip);//display tooltip on focus, for added keyboard accessibility
						mapaMaker.addEvt(currentLocation,'blur',mapaMaker.hideTooltip);//hide tooltip on focus, for added keyboard accessibility
						ii++;
					};
					ii=0;
				};
				i++;
			};
			mapaMaker.on = true;
		};
	},
	/* SHOW TOOLTIP */
	showTooltip: function() {
		var evt = this;
		var i = 0;
		//Find DD to display - based on currently hovered anchor move to parent DT then next sibling DD
		var objid = evt.parentNode.nextSibling;
		mapaMaker.element = objid;//set for the hideTooltip
		//get width and height of background mapa
		var mapaWidth  = objid.parentNode.offsetWidth;
		var mapaHeight = objid.parentNode.offsetHeight;
		//get width and height of the DD
		var toopTipWidth = objid.offsetWidth;
		var toopTipHeight = objid.offsetHeight;
		//figure out where tooltip should be places based on point location
		var newX = evt.offsetLeft + mapaMaker.offsetX;
		var newY = evt.offsetTop + mapaMaker.offsetY;
		//check if tooltip fits mapa width 
		if ((newX + toopTipWidth) > mapaWidth) {
			objid.style.left = newX-toopTipWidth-24 + 'px';
		} else {
			objid.style.left = newX + 'px';
		};
		//check if tooltip fits mapa height 
		if ((newY + toopTipHeight) > mapaHeight) {
			objid.style.top = newY-toopTipHeight-14 + 'px';
		} else {
			objid.style.top = newY + 'px';
		};
	},
	/* HIDE TOOLTIP */
	hideTooltip: function() {
		mapaMaker.element.style.left = '-9999px';
	},
	addEvt: function(element, type, handler) {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = mapaMaker.addEvt.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			};
		};
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = mapaMaker.handleEvent;
	},
	handleEvent: function(event) {
		var returnValue = true;
		// grab the event object (IE uses a global event object)
		event = event || mapaMaker.fixEvent(window.event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			};
		};
		return returnValue;
	},
	fixEvent: function(event) {
		// add W3C standard event methods
		event.preventDefault = mapaMaker.fixEvent.preventDefault;
		event.stopPropagation = mapaMaker.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);
			};
		};
	}
};
mapaMaker.fixEvent.preventDefault = function() {this.returnValue = false;};
mapaMaker.fixEvent.stopPropagation = function() {this.cancelBubble = true;};
mapaMaker.addEvt.guid = 1;


/* LOAD SCRIPT */
	/* for Mozilla */
		if (document.addEventListener) {
			document.addEventListener("DOMContentLoaded", mapaMaker.init, null);
		};
		
	/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
			document.write("<script defer src=ie_onload.js><"+"/script>");
		/*@end @*/
		
	/* for other browsers */
		mapaMaker.addEvt( window, 'load', mapaMaker.init);