/*
	JavaScript for http://www-personal.umd.umich.edu/~aknoyes/
	by Andrew Noyes
*/

function addLoadEvent(func) {	// Generic onload event queue
	var oldOnload = window.onload;
	if (typeof window.onload !== 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			if (oldOnload) {
				oldOnload();
			}
			func();
		};
	}
}

function domReady(func) {	// Calls func as soon as DOM is ready
	var timer, tempNode;
	
	if (document.uniqueID && document.expando) {	// Internet Explorer test
		/*
			Internet Explorer DOM check relies on a proprietary method
			for DOM elements called doScroll(). This method, by definition,
			is available as soon as the DOM is loaded, and the element isn't
			required to be inserted into the node tree to call the method.
		*/
		tempNode = document.createElement("ANDREW:ready");	// Dummy DOM node
		
		time = setTimeout(function () {
			try {
				if (document.readyState != "complete") {
					timer = setTimeout(arguments.callee, 0);	// Puts test back into call stack
					return;
				}
				
				tempNode.doScroll("left");	// If document isn't loaded, exception is thrown
				func();
				tempNode = null;
			} catch (e) {
				timer = setTimeout(arguments.callee, 0);
			}
		}, 0);
	} else if (/WebKit/i.test(navigator.userAgent)) {	// WebKit test
		timer = setInterval(function () {	// Checks DOM status in intervals
			if (/loaded|complete/.test(document.readyState)) {	// Checks of the DOM has been loaded
				clearInterval(timer);	// Clears interval
				func();
			}
		}, 10);	
	} else if (document.addEventListener) {	// For Mozilla browsers
		/*  This check must come after WebKit. Webkit
			supports document.addEventListener, but doesn't
			support "DOMContentLoaded".
		*/
		document.addEventListener("DOMContentLoaded", func, false);
	} else {	// Falls back on traditional onload
		addLoadEvent(func);
	}
}

function externalLinks() {	// Sets mailto: and http/s: links to open in new window
	// Object detection
	if (!document.getElementsByTagName) return;

	var i, il, links, href, extension, protocol, rel;
	
	links = document.getElementsByTagName("a");
	for (i = 0, il = links.length; i < il; i++) {
		href = links[i].getAttribute("href", 2);	// 2 ensures IE returns actual attrib text, not absolute link
		protocol = href.substring(0, href.indexOf(":"));
		if (protocol == "mailto") {
			links[i].className = "email";
		} else if (protocol == "http" || protocol == "https") {
			links[i].className = "external";
			links[i].target = "_blank";
		}
	}
}
domReady(externalLinks);