/*-------------------------------------------------------------------------+
 | Hub Systems JavaScript Library                                          |
 |                                                                         |
 | Includes many standard javascript funtions, and many assorted functions |
 | from various sources.                                                   |
 +-------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------+
 | addLoadEvent: Add event handler to body when window loads               |
 +-------------------------------------------------------------------------*/

function addLoadEvent(func) {
	var oldonload = window.onload;

	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/*-------------------------------------------------------------------------+
 | initElementHovers: Init hovers for IE                                   |
 +-------------------------------------------------------------------------*/

function initElementHovers(tag, tagclass, subtag) {
	if (!document.all || !document.getElementById)
		return;

	var tags = document.getElementsByTagName(tag);

	for (i = 0; i < tags.length; i++) {
		var theTag = tags[i];

		if (theTag.className.indexOf(tagclass) <= -1)
			continue;

		if (subtag == "") {
			theTag.onmouseover = function() {
				this.className += " hover";
			};
			theTag.onmouseout = function() {
				this.className.replace(" hover", "");
			};
		} else {
			var subtags = theTag.getElementsByTagName(subtag);

			for (j = 0; j < subtags.length; j++) {
				var theSubtag = subtags[j];

				theSubtag.onmouseover = function() {
					this.className += " hover";
				};
				theSubtag.onmouseout = function() {
					this.className = this.className.replace(" hover", "");
				};
			}
		}
	}
}

/*-------------------------------------------------------------------------+
 | addEvent: Add an event to an object                                     |
 +-------------------------------------------------------------------------*/

function addEvent(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent("on"+type, obj[type+fn]);
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function() {
			var i, item;
			for (i = listEvents.length - 1; i >= 0; i = i - 1) {
				item = listEvents[i];
				if (item[0].removeEventListener)
					item[0].removeEventListener(item[1], item[2], item[3]);
				if (item[1].substring(0, 2) != "on")
					item[1] = "on" + item[1];
				if (item[0].detachEvent)
					item[0].detachEvent(item[1], item[2]);
				item[0][item[1]] = null;
			}
		}
	};
}();

addEvent(window,'unload',EventCache.flush);

/*-------------------------------------------------------------------------+
 | toggleObjectByID: Toggle the visibility of an object by ID              |
 +-------------------------------------------------------------------------*/
function displayObjectByElement(el) {
	if (el.style.display == 'none') {
		el.style.display = '';
	}
}

function displayObjectById(obj) {
	var	el = document.getElementById(obj);

	displayObjectByElement(el);
}

function hideObjectByElement(el) {
	if (el.style.display != 'none') {
		el.style.display = 'none';
	}
}

function hideObjectById(obj) {
	var	el = document.getElementById(obj);

	hideObjectByElement(el);
}

function toggleObjectByElement(el) {
	if (el.style.display != 'none') {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

function toggleObjectById(obj) {
	var	el = document.getElementById(obj);

	toggleObjectByElement(el);
}

function isHiddenObjectByElement(el) {
	if (el.style.display != 'none') {
		return false;
	} else {
		return true;
	}
}

function isHiddenObjectById(obj) {
	var	el = document.getElementById(obj);

	return isHiddenObjectByElement(el);
}

/*-------------------------------------------------------------------------+
 | getElementsByClass: Grab all elements of a specific class name          |
 +-------------------------------------------------------------------------*/

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/*-------------------------------------------------------------------------+
 | hubGetCookie(), hubSetCookie(), hubDeleteCookie(): cookie functions     |
 +-------------------------------------------------------------------------*/

function hubGetCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;

	if ((!start) && (name != document.cookie.substring(0, name.length)))
		return '';

	if (start == -1)
		return '';

	var end = document.cookie.indexOf(';', len);
	if (end == -1)
		end = document.cookie.length;

	return unescape(document.cookie.substring(len, end));
}

function hubSetCookie(name, value, expires, path, domain, secure) {
	var today = new Date();

	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + '=' + escape(value) +
		((expires) ? ';expires=' + expires_date.toGMTString() : '') +
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}

function hubDeleteCookie(name, path, domain) {
	if (hubGetCookie(name) && hubGetCookie(name) != '')
		document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +
			((domain) ? ';domain=' + domain : '') +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


/*-------------------------------------------------------------------------+
 | inArray: An prototype function to find an element in an array           |
 +-------------------------------------------------------------------------*/

Array.prototype.inArray = function (value) {
	var i;
	for (i = this.length - 1; i >= 0; i--) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/*-------------------------------------------------------------------------+
 | insertAfter: Add a node after the current node                          |
 +-------------------------------------------------------------------------*/

function insertAfter(parent, node, referenceNode) {
	return (referenceNode.nextSibling) ? 
		parent.insertBefore(node, referenceNode.nextSibling) :
		parent.appendChild(node);
}

/*-------------------------------------------------------------------------+
 | linkFader: Support for fading link colours                              |
 +-------------------------------------------------------------------------*/

/******************************************************************
*	Script name: Link fader (http://design64.net/js/linkfader.html)
*	Version: 1.0
*	Date: 12.05.02
*	Usage: Freeware - You may modify this script as you wish,
*		as long as you don't remove this header comment.
*
*	Script by: Fayez Zaheer (viol8r on #webdesign [irc.zanet.org.za])
*	Email: fayez@design64.net
*	Web site: http://design64.net
* 	Original idea: http://anarchos.xs.mw/fade.phtml
*       Script featured on JavaScript Kit (http://www.javascriptkit.com)
******************************************************************/

var linkFadeColour = "FF0000";
var linkFadeInRate = 6;
var linkFadeOutRate = 10;
var linkFadeSpeed = 10;
var linkFadeClass = "fade";

var x = 0, oc, linkFader, ocs = new Array();

function linkFadeConvertRGB(z)
{
	var newfcS = "", splitter = "";
	splitter = z.split(",");
	splitter[0] = parseInt(splitter[0].substring(4, splitter[0].length));
	splitter[1] = parseInt(splitter[1]);
	splitter[2] = parseInt(splitter[2].substring(0, splitter[2].length-1));
	for (var q = 0; q < 3; q++)
	{
		splitter[q] = splitter[q].toString(16);
		if (splitter[q].length == 1) splitter[q] = "0" + splitter[q];
		newfcS += splitter[q];
	}
	return newfcS;
}

function linkFadeCurrentColour(index)
{
	var temp, cc;
	if (navigator.userAgent.indexOf("Opera") != -1) cc = document.links[index].style.color
	else if (document.all) cc = document.links[index].currentStyle.color
	else if (document.getElementById) cc = document.defaultView.getComputedStyle(document.links[index], '').getPropertyValue("color");

	if (cc.length == 4 && cc.substring(0, 1) == "#")
	{
		temp = "";
		for (var a = 0; a < 3; a++)
			temp += cc.substring(a+1, a+2) + cc.substring(a+1, a+2);
		cc = temp;
	}
	else if (cc.indexOf("rgb") != -1) cc = linkFadeConvertRGB(cc)
	else if (cc.length == 7) cc = cc.substring(1, 7)
	else cc = linkFadeColour;

	return cc;
}


function linkFadeConvert2Dec(hex)
{	
	var rgb = new Array();
	for (var u = 0; u < 3; u++)
		rgb[u] = parseInt(hex.substring(u*2, u*2+2), 16);
	return rgb;
}

function linkFadeNewRGB(f, n, d)
{
	var change;
	if (d == 1) change = linkFadeInRate
	else change = linkFadeOutRate;
	for (var g = 0; g < 3; g++)
	{
		if (n[g] > f[g] && n[g] - change >= 0) n[g] -= change;
		if (n[g] < f[g] && n[g] + change <= 255) n[g] += change;
	}
	return n;
}

function linkFade(index, d)
{
	var fc, nc, temp = new Array(), finished = false;
	nc = linkFadeConvert2Dec(linkFadeCurrentColour(index));
	if (d == 1) fc = linkFadeConvert2Dec(linkFadeColour)
	else fc = linkFadeConvert2Dec(ocs[x]);
	temp = linkFadeConvert2Dec(linkFadeCurrentColour(index));
	nc = linkFadeNewRGB(fc, nc, d);
	if ((nc[0] == temp[0]) && (nc[1] == temp[1]) && (nc[2] == temp[2]))
		finished = true;
	if (!finished) document.links[x].style.color = "rgb(" + nc[0] + "," + nc[1] + "," + nc[2] + ")"
	else clearInterval(linkFader);
}

function linkFadeFindLink(over)
{
	if (document.layers) return;
	if (linkFader)
	{
		clearInterval(linkFader);
		document.links[x].style.color = "#" + ocs[x];
	}
	if (over.id && !this.id) this.id = over.id;
	x = 0;
	while (!(this.id == document.links[x].id) && (x < document.links.length))
		x++;
	if (this.id == document.links[x].id)
	{
		oc = linkFadeCurrentColour(x);
		linkFader = setInterval("linkFade(" + x  + ", 1)", linkFadeSpeed);
	}
}

function linkFadeClearFade()
{
	if (document.layers) return;
	if (linkFader) clearInterval(linkFader);
	linkFader = setInterval("linkFade(" + x + ", 0)", linkFadeSpeed);
}

function linkFadeInit()
{
	var oldfn;

	if (!document.all && !document.getElementById)
		return;

	for (var i = 0; i < document.links.length; i++)
	{
		if (document.links[i].className != linkFadeClass)
			continue;

		ocs[i] = linkFadeCurrentColour(i);
		document.links[i].id = "link" + i;

		if (typeof document.links[i].onmouseover != "function")
			document.links[i].onmouseover = linkFadeFindLink;
		else {
			var oldfn = document.links[i].onmouseover;
			var link = document.links[i];

			document.links[i].onmouseover = function() {
				oldfn();
				linkFadeFindLink(link);
			}
		}

		if (typeof document.links[i].onmouseout != "function")
			document.links[i].onmouseout = linkFadeClearFade;
		else {
			var oldfn = document.links[i].onmouseout;
			var link = document.links[i];

			document.links[i].onmouseout = function() {
				oldfn();
				linkFadeClearFade(link);
			}
		}
	}
}

function activateTab(name, which, howmany) {
	for (i = 1; i <= howmany; i++) {
		var ov = document.getElementById(name + i + 'tab');

		if (i == which)
		{
			if (ov.style.display == 'none')
				ov.style.display = '';
		}
		else
		{
			if (ov.style.display != 'none')
				ov.style.display = 'none';
		}

		ov = document.getElementById(name + i + 'label');
		if (i == which)
		{
			ov.style.background = '#993333';
			ov.style.color = '#ffffff';
		}
		else
		{
			ov.style.background = '#f3f3f3';
			ov.style.color = '#666666';
		}
	}
}


addLoadEvent(linkFadeInit);

