/* ======================================================================================= 
   Copyright (c) 2008 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 link in a CSS hover popup.
	 Also see the 'hovermail' 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 == 'hovermail') {
			// Initialize link text
			//
			var e = elems[i].getElementsByTagName('a')[1]; // Get 2nd <A> (the hover email link)
				// Construct partial email address text and display in the <A> text link
				// var text = e.name.substr(0,3) + "...@" + e.rel.substr(0,3) + "..." + e.id;
			var text = "Click here to send an email";
			text = document.createTextNode(text);
			e.replaceChild(text,e.firstChild);

			// Make mouseover event for each email hover <A> (anchor) link
			//
			e.onmouseover = function () {
				// Construct full email address for href and text link
				var value = this.name + "@" + this.rel + "." + this.id;
				this.href = "mailto:" + value;
				value = document.createTextNode(value);
				this.replaceChild(value,this.firstChild);
			}

			// Make mouseout event for each email hover <A> (anchor) link
			//
			e.onmouseout = function () {
				// Remove full email address for href
				this.href = "";
				// Reconstruct text display
//				var text = this.name.substr(0,3) + "...@" + this.rel.substr(0,3) + "..." + this.id;
//				text = document.createTextNode(text);
				var text = "Click here to send an email";
				this.replaceChild(text,this.firstChild);
			}
		}
	}
}
