if(Array.prototype.push == null)		// enables array.push for old browsers, e.g. IE5. Used by EventCache function below
{
	Array.prototype.push = function()
	{
		for(var i = 0; i < arguments.length; i++)
		{
			this[this.length] = arguments[i];
		};
		return this.length;
	};
};

var EventCache = function()	// fix for IE memory leak problem with event handling
{
	var listEvents = [];
	return {
			listEvents : listEvents,

			add : function(node, sEventName, fHandler, bCapture)
				{
				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]);
						};

					/* From this point on we need the event names to be prefixed with 'on" */
					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;
				};
			}
		};
}();

function addEvent(obj, type, fn)
{
	if(obj.addEventListener)
		{obj.addEventListener(type, fn, false);}
	else
	{
		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);
}

function removeEvent(obj, type, fn)
{
	if ( obj.detachEvent )
		{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		}
	else
		{obj.removeEventListener( type, fn, false );}
}

function getObject(obj, type)		// finding stuff
{
	if(type == 'id')
		{DOMchunk = document.getElementById(obj)}
	if(type == 'tag')
		{DOMchunk = document.getElementsByTagName(obj)}

	return DOMchunk
}

function filter(coll, filter, newArray, type, attname) // filters by id, class or attribute - collection, filter string, new collection, type, attribute name
{
    for(var i=0; i<coll.length; i++)
        {
        var pattern = new RegExp('\\b'+filter+'\\b');

        if (type=='tag')
            {
            if (pattern.test(coll[i].className))
                {newArray[newArray.length] = coll[i]}
            }
        if (type=='id')
            {
            if (pattern.test(coll[i].id))
                {newArray[newArray.length] = coll[i]}
            }
        if (type=='att')
            {
            if (pattern.test(coll[i].getAttribute(attname)))
                {newArray[newArray.length] = coll[i]}
            }
        }
    return newArray;
}

function insertAfter(parent, node, referenceNode)	/* insert an element after a particular node */
{
	parent.insertBefore(node, referenceNode.nextSibling);
}

function createXMLHttpRequest()		// generic asynchronous requests
{
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	return null;
}

// -------------------------------------------------------------------- EVENT LOADING

addEvent(window, 'load', HeLSwitcher);	// loads HeL JS and Opera switches
addEvent(window, 'load', initCollapse);	 // loads collapsing/expanding menu
addEvent(window, 'load', popWindow);		 // loads new window links

// --------------------------------------------------------------------

function HeLSwitcher() /* - used by Happy eL materials to turn Javascripting on and off */
{
	if(document.getElementById('login'))
		{
		JSChecker = getObject('JSSwitch', 'id')
		JSChecker.value = "script_on"
		}
}

function initCollapse()
{
	var headers = getObject('h2', 'tag');
	var menuheaders = [];

	menuheaders = filter(headers, 'menuheader', menuheaders, 'tag')

	for (var i = 0; i < menuheaders.length; i++)
		{

		if(menuheaders[i].nextSibling.tagName == 'UL')
			{menuheaders[i].menu = menuheaders[i].nextSibling;}

		else if(menuheaders[i].nextSibling.nextSibling.tagName == 'UL')
			{menuheaders[i].menu = menuheaders[i].nextSibling.nextSibling;}

		menuheaders[i].onclick = collexp;
		}
}

function collexp()
{
	if(this.menu.className == 'collapse')
		{this.menu.className = 'expand';}
	else
		{this.menu.className = 'collapse'}
}

// --------------------------------------------------------------------

function popWindow()
{
	var allLinks = getObject('a', 'tag')
	var newWinLinks = [];

	var newWinLinks = filter(allLinks, 'newWin', newWinLinks, 'tag')

	for (i=(newWinLinks.length-1); i>=0; i--)
		{
		newWinLinks[i].onclick = newWindow;
		newWinLinks[i].setAttribute('title', 'this link will open in a new window')
		}
}

function newWindow()
{
	var address = this.href

	if (this.className.search('large') != -1)
		{
		this.nme = 'lrgwin'
		this.wdth = '800'
		this.hgt = '700'
		}
	else
		{
		this.nme = 'smwin'
		this.wdth = '600'
		this.hgt = '500'
		}

	window.open(this.href, this.nme, 'width=' + this.wdth + ',height=' + this.hgt + ', toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, copyhistory=yes, resizable=yes')

	return false;
}