/******************************************

	HTML javascript functions
	by Dave Barnwell http://freshsauce.co.uk
	License : Public Domain

	file version 16/09/2009 09:30:45

******************************************/

function nextElement(elm,n) {
	n=n||0;
	elm = E(elm)
	if (!elm) return null;
	do { 
		elm = elm.nextSibling; 
		if (elm && elm.nodeType == 1) n--;
	}	while (elm && n>=0);
	return elm;
}

function previousElement(elm,n) {
	n=n||0;
	elm = E(elm)
	if (!elm) return null;
	do {
		elm = elm.previousSibling; 
		if (elm && elm.nodeType == 1) n--;
	}	while (elm && n>=0);
	return elm;
}

function downElement(elm,n) {
	n=n||0;
	elm = E(elm)
	if (!elm) return null;
	do {
		elm = elm.firstChild; 
		if (elm && elm.nodeType == 1) n--;
	}	while (elm && n>=0);
	return elm;
}

function upElement(elm,n) {
	n=n||0;
	elm = E(elm)
	if (!elm) return null;
	do {
		elm = elm.parentNode; 
		if (elm && elm.nodeType == 1) n--;
	}	while (elm && n>=0);
	return elm;
}

function E(elm) {
	return elm =  (typeof elm == 'string') ? ((document.getElementById) ? document.getElementById(elm) : null) : elm;
}

/******************

	window load and unload helpers
		
*******************/

function addLoadEvent(func) {
	// Allow multiple loaders
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
			window.onload = function() {
				oldonload();
				func();
			}
	}
}

function addUnloadEvent(func) {
	// Allow multiple unloaders
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
			window.onunload = function() {
				oldonunload();
				func();
			}
	}
}


/******************
		CSS Helpers
*******************/

function hasClass(elm,v) {
	elm = E(elm)
	return elm.className.match(new RegExp('(\\s|^)'+v+'(\\s|$)'));
}

function addClass(elm,v) {
	elm = E(elm)
	if (!hasClass(elm,v)) elm.className += (elm.className) ? " "+v : v;
}
 
function removeClass(elm,v) {
	elm = E(elm)
	if (hasClass(elm,v))	elm.className=elm.className.replace(new RegExp('(\\s|^)'+v+'(\\s|$)'),' ');
}

function getElementsByClassName(className,container,tag) {
	var m=[];
	c=container||document; 	// default container to document
	tag=tag||'*';		// default tag to *
	if (c.getElementsByTagName) {
		var elms = c.getElementsByTagName(tag);	// find tags in container
		for (i=0; i<elms.length; i++) {
			if (hasClass(elms[i],className)) m.push(elms[i]); // collect for class
		}
	}
	return m;
}

function hideElm(elm) {
	elm = E(elm)
	elm.style.display = 'none';
}

function showElm(elm) {
	elm = E(elm)
	elm.style.display = '';
}

function toggleElm(elm) {
	elm = E(elm)
	elm.style.display = (elm.style.display != "none") ? "none" : "";
}

function stripeTableRows(className,rowClass) {
	// Find all tables assigned className, then add classses to rows based on names in array rowClass,
	// if rowClass is null it defaults to ['row0','row1']
	if (!document.getElementsByTagName) return false;
	rowClass=rowClass||[,'row0','row1'];	// default 2 classes
	// Get a reference to the menu container
	var elms = getElementsByClassName(className,document,'TABLE');
	if (!elms) return false;
	for(var t=0;t<elms.length;t++){ 
		var rows = elms[t].getElementsByTagName('TR');
		for(var i=0; i<rows.length; i++) {
			rows[i].className=rowClass[i%rowClass.length];
		}
	}
	return true;
}

function higlightExternalLinks() {
	// add class 'external' to all external links
	var localhost = false;
	if (window.location) {
		localhost = window.location.hostname;
	}
	if (!localhost) return false;	// if we cant get the local host exit
	if (document.getElementsByTagName && document.getElementById) {
		var content = document.getElementById('detailcol');
		if (content) {
			var links = content.getElementsByTagName('a'); // only links inside 'detailcol'
			for (i=0; i<links.length; i++) {
				if (links[i].hostname != localhost && links[i].hostname != '' && !(downElement(links[i]) && downElement(links[i]).nodeName == "IMG")) {
					addClass(links[i],'external');
					links[i].target="_blank"; // set to open in new window
				}
			}
		}
	}
}

addLoadEvent(higlightExternalLinks);