/* ======================================================================================= 
   Copyright (c) 2009 Stephen D. Williams. All rights reserved.

   The original version of this javascript and the associated (x)html is available at
	 http://iagenweb.org/state/tools/hover.html and may be modified in any way to fit your
   requirements.
	 
   Hover over a link to construct an email or http/https link in a CSS hover popup.
	 Prevents automated spammers and spiders (including search engines) from crawling the
	 link.  Also see the 'hoverlink' CSS class.
   ======================================================================================= */

// Add the initialization event when the page is loaded
addEvent(window, 'load', initHovermail);

function addEvent(obj, eventType,fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(eventType, fn, useCapture);
		return true;
	} else {
		if (obj.attachEvent) {
			var r = obj.attachEvent("on"+eventType, fn);
			return r;
		}
	}
}

// Initialization event upon page load
function initHovermail() {
	var elems = document.getElementsByTagName('span');
	for (var i=0; i<elems.length; i++) { 
		if (elems[i].className == 'hoverlink') {
			// Initialize link text
			//
			var e = elems[i].getElementsByTagName('a')[1]; // Get 2nd <A> (the hover email link)
			if (e.target == 'mail') { var text = "Click here to send an email";}
			else { var text = "Click here to visit the link"; }
			text = document.createTextNode(text);
			e.replaceChild(text,e.firstChild);

			// Make mouseover event for each email hover <A> (anchor) link
			//
			e.onmouseover = function () {
				if (this.target == 'mail') {
					// Construct full email address for href link
					var value = this.target + "to:" + this.name + "@" + this.rel + "." + this.id + "?subject=" + this.title;
				} else {
					// Construct full email address for href link
					var value = this.target + this.name + this.rel + this.id;
					
				}
				this.href = value;
			}

		}
	}
}
