/* WRITE THESE FUNCTIONS */

/**
* Crossbrowser event handling functions.
*
* A set of functions to easily attach and detach event handlers to HTML elements.
* These functions work around the shortcomings of the traditional method ( element.onevent = function; )
* where only 1 handler could be attached for a certain event on the object, and mimic the DOM level 2
* event methods addEventListener and removeEventListener for browsers that do not support these
* methods (e.g. Internet Explorer) without resorting to propriety methods such as attachEvent and detachEvent
* that have a whole set of their own shortcomings.
* Created as an entry for the 'contest' at quirksmode.org: http://www.quirksmode.org/blog/archives/2005/09/addevent_recodi.html
*
* @author Tino Zijdel ( crisp@xs4all.nl )
* @version 1.0
* @date 2005-09-09
*/


/**
* addEvent
*
* Generic function to attach event listeners to HTML elements.
* This function does NOT use attachEvent but creates an own stack of function references
* in the DOM space of the element. This prevents closures and therefor possible memory leaks.
* Also because of the way the function references are stored they will get executed in the
* same order as they where attached - matching the behavior of addEventListener.
*
* @param obj The object to which the event should be attached.
* @param evType The eventtype, eg. 'click', 'mousemove' etcetera.
* @param fn The function to be executed when the event fires.
* @param useCapture (optional) Whether to use event capturing, or event bubbling (default).
*/
function addEvent(obj, evType, fn, useCapture)
{
	//-- Default to event bubbling
	if (!useCapture) useCapture = false;

	//-- DOM level 2 method
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, useCapture);
	}
	else
	{
		//-- event capturing not supported
		if (useCapture)
		{
			alert('This browser does not support event capturing!');
		}
		else
		{
			var evTypeRef = '__' + evType;

			//-- create function stack in the DOM space of the element; seperate stacks for each event type
			if (!obj[evTypeRef])
			{
				//-- create the stack if it doesn't exist yet
				obj[evTypeRef] = [];

				//-- if there is an inline event defined store it in the stack
				var orgEvent = obj['on'+evType];
				if (orgEvent) obj[evTypeRef][0] = orgEvent;

				//-- attach helper function using the DOM level 0 method
				obj['on'+evType] = IEEventHandler;
			}
			else
			{
				//-- check if handler is not already attached, don't attach the same function twice to match behavior of addEventListener
				for (var ref in obj[evTypeRef])
				{
					if (obj[evTypeRef][ref] === fn) return;
				}
			}

			//-- add reference to the function to the stack
			obj[evTypeRef][obj[evTypeRef].length] = fn;
		}
	}
}

/**
* removeEvent
*
* Generic function to remove previously attached event listeners.
*
* @param obj The object to which the event listener was attached.
* @param evType The eventtype, eg. 'click', 'mousemove' etcetera.
* @param fn The listener function.
* @param useCapture (optional) Whether event capturing, or event bubbling (default) was used.
*/
function removeEvent(obj, evType, fn, useCapture)
{
	//-- Default to event bubbling
	if (!useCapture) useCapture = false;

	//-- DOM level 2 method
	if (obj.removeEventListener)
	{
		obj.removeEventListener(evType, fn, useCapture);
	}
	else
	{
		var evTypeRef = '__' + evType;

		//-- Check if there is a stack of function references for this event type on the object
		if (obj[evTypeRef])
		{
			//-- iterate through the stack
			for (var ref in obj[evTypeRef])
			{
				//-- if function reference is found, remove it
				if (obj[evTypeRef][ref] === fn)
				{
					try
					{
						delete obj[evTypeRef][ref];
					}
					catch(e)
					{
						obj[evTypeRef][ref] = null;
					}

					return;
				}
			}
		}
	}
}

/**
* IEEventHandler
* 
* IE helper function to execute the attached handlers for events.
* Because of the way this helperfunction is attached to the object (using the DOM level 0 method)
* the 'this' keyword will correctely point to the element that the handler was defined on.
*
* @param e (optional) Event object, defaults to window.event object when not passed as argument (IE).
*/
function IEEventHandler(e)
{
	e = e || window.event;
	var evTypeRef = '__' + e.type, obj;

	//-- check if there is a custom function stack defined for this event type on the object
	if (this[evTypeRef])
	{
		//-- iterate through the stack and execute each function in the scope of the object by using function.call
		var evTypeRefLen = this[evTypeRef].length
		for(var ref = 0; ref < evTypeRefLen; ref++)
		{
		//for (var ref in this[evTypeRef])
		//{
			if (Function.call)
			{
				this[evTypeRef][ref].call(this, e);
			}
			else
			{
				//-- IE 5.0 doesn't support call or apply, so use this
				this.__fn = this[evTypeRef][ref];
				this.__fn(e);
				this.__fn = null;
			}
		}
	}
}

/* DON'T TOUCH THESE FUNCTIONS */

function removeBorders()
{
	var x = document.getElementById('navigation').getElementsByTagName('li');
	for (var i=0;i<x.length;i++)
	{
		removeEvent(x[i],"mouseover",showBorder);
		removeEvent(x[i],"mouseout",hideBorder);
	}
}

function showSubNav(e)
{
	this.className += ' menu_hover';
	this.className = this.className.replace('  ', ' ');	
	setHover(this);
}

function hideSubNav(e)
{
	this.className = this.className.replace(/menu_hover/g,'');
	//noBubble(e);
}

function showBorder(e)
{
	this.className += ' current';
	this.className = this.className.replace('  ', ' ');
}

function hideBorder(e)
{
	this.className = this.className.replace(/current/g,'');
	//noBubble(e);
}

function noBubble(e)
{
	if (e && e.stopPropagation)
		e.stopPropagation();
	else
		window.event.cancelBubble = true;
}

function initWinMode(id) {   
	var el = document.getElementById(id);
	if (el) {
	    if (mmPagesPath.length > 0) {
	        var x = el.getElementsByTagName('li');
	        var selPageId = mmPagesPath[mmPagesPath.length - 1];
	        for (var i = 0; i < x.length; i++) {
	            var o = x[i];
	            if (o.getAttribute('pageId') == selPageId) {
	                o.className += ' menu_selected';
	                break;
	            }
	        }
	    }
	}
	enableMenu(id);
}

function initWebMode(id)
{
	var el = document.getElementById(id);
	if(el) {
		var x = el.getElementsByTagName('li');
		for (var i=0;i<x.length;i++)
		{
			addEvent(x[i], 'mouseover', showSubNav);
			addEvent(x[i], 'mouseout', hideSubNav);
			addEvent(x[i], 'mouseover', showBorder);
			addEvent(x[i], 'mouseout', hideBorder);
		}
	}
		
	enableMenu(id);
}

function enableMenu(id){		
    var el = document.getElementById(id);
	if(el){
		setHover(el);
		var cLi = el.getElementsByTagName('li');
		for (var i = 0; i < cLi.length; i++) {
		    if (inPagePath(cLi[i])) {
		        cLi[i].className += ' selected';
		        var an = getAnchor(cLi[i]);
		        if (an) {
		            an.className += ' selected';
		        }
		    }
		}
	}
}

function inPagePath(listItem) {
    var pageID = listItem.getAttribute('pageId');
    if (pageID) {
        for (var i = 0; i < mmPagesPath.length; i++) {
            if (mmPagesPath[i] == pageID) {
                return true;
            }
        }
    }
    return false;
}

function getAnchor(parentEl){
	var childs=parentEl.childNodes;
	var i=childs.length;
	while(i--){if(childs[i].tagName && childs[i].tagName.toLowerCase()=='a'){return childs[i];break;}}
	return null;
}


// Created a long, long time ago by Tanny O'Haley
// Update history.
// 24 Oct 2006 - Added to http://tanny.ica.com
// 14 Jul 2006 - Modified the way the iframe is added to the DOM. I no longer use 
//			innerHTML but use DOM methods.
// 15 Sep 2006 - Added a check to see if the target element onmouseout is contained
//			in the onmouseout element. If it is then I dont' remove the
//			sfhover class. I made use of the Microsoft proprietary
//			obj.contains() method.
//		 Added check to make sure that the sfhover class is not already in
//			the li element.
//
sfHover = function() {
	// Support the standard nav without a class of nav.
	var el = document.getElementById("nav");
	if(!/\bnav\b/.test(el.className) && el.tagName == "UL")
		setHover(el);

	// Find all unordered lists.
	var ieNavs = document.getElementsByTagName('ul');
	for(i=0; i<ieNavs.length; i++) {
		var ul = ieNavs[i];
		// If they have a class of nav add the menu hover.
		if(/\bnav\b/.test(ul.className))
			setHover(ul);
	}

}

function setHover(nav) {
	var ieULs = nav.getElementsByTagName('ul');
	if (navigator.appVersion.substr(22,3)!="5.0") {
		// IE script to cover <select> elements with <iframe>s
		for (j=0; j<ieULs.length; j++) {
			//alert(ieULs[j].childNodes[0].outerHTML);
			var ieMat;
			if (ieULs[j].childNodes[0].className=='shimframe')
				ieMat = ieULs[j].childNodes[0];
			else
			{
				ieMat=document.createElement('iframe');
				/*if(document.location.protocol == "https:")
					ieMat.src="://";
				else */if(window.opera != "undefined")
					ieMat.src="";
				else
					ieMat.src="javascript:false";
				ieMat.scrolling="no";
				ieMat.frameBorder="0";
				ieMat.style.zIndex="-1";
				ieMat.style.position="absolute";
				ieMat.style.left = "0";
				ieMat.style.top = "0";
				ieMat.className = 'shimframe';
				ieMat.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);";
				ieULs[j].insertBefore(ieMat, ieULs[j].childNodes[0]);
				ieULs[j].style.zIndex="101";
			}
			ieMat.style.width=(ieULs[j].clientWidth + 3)+"px";
			ieMat.style.height=(ieULs[j].clientHeight + 1)+"px";
		}
	} else {
		// IE 5.0 doesn't support iframes so hide the select statements on hover and show on mouse out.
		// IE script to change class on mouseover
		var ieLIs = document.getElementById('nav').getElementsByTagName('li');
		for (var i=0; i<ieLIs.length; i++) if (ieLIs[i]) {
			addEvent(ieLIs[i], 'mouseover', hideSelects);
			addEvent(ieLIs[i], 'onmouseout', showSelects);
		}
	}
}

// If IE 5.0 hide and show the select statements.
function hideSelects(){
	var oSelects=document.getElementsByTagName("select");
	for(var i=0;i<oSelects.length;i++)
		oSelects[i].className+=" hide";
}

function showSelects(){
	var oSelects=document.getElementsByTagName("select");
	for(var i=0;i<oSelects.length;i++)
		oSelects[i].className=oSelects[i].className.replace(" hide","");
}