/****** BEGIN LICENSE BLOCK *****
 * Copyright (c) 2005 Harmen Christophe and contributors. All rights reserved.
 * 
 * Menu script is free software; you can redistribute it and/or
 *   modify under the terms of the Creative Commons - Attribution-ShareAlike 2.0
 * <http://creativecommons.org/licenses/by-sa/2.0/>
 * You are free:
 *     * to copy, distribute, display, and perform the work
 *     * to make derivative works
 *     * to make commercial use of the work
 * 
 * Under the following conditions:
 * _Attribution_. You must attribute the work in the manner specified by the
 *   author or licensor.
 * _Share Alike_. If you alter, transform, or build upon this work, you may
 *   distribute the resulting work only under a license identical to this one.
 *     * For any reuse or distribution, you must make clear to others 
 *      the license terms of this work.
 *     * Any of these conditions can be waived if you get permission from 
 *      the copyright holder.
 * 
 * Your fair use and other rights are in no way affected by the above.
 * 
 * This is a human-readable summary of the Legal Code (the full license). 
 * <http://creativecommons.org/licenses/by-sa/2.0/legalcode>
 ***** END LICENSE BLOCK ******/
function trim(s) {
	return s.replace(/(^\s*)|(\s*$)/g,"");
}
function hasClassName(oEl,sHasClass) {
    return ((" " + oEl.className + " ").indexOf(" " + sHasClass + " ")!=-1);
}
function addClassName(oEl,sNewClass) {
    if (!hasClassName(oEl,sNewClass)) oEl.className = trim(oEl.className + " " + sNewClass);
}
function removeClassName(oEl,sRemoveClass) {
    oEl.className = trim((" " + oEl.className + " ").replace(" " + sRemoveClass + " "," "));
}
function isChildNodeOf(childNode,parentNode) {
	var bIsChildNodeOf = false;
	function _isChildNodeOf(childNode,parentNode) {
		while (parentNode) {
			if (parentNode==childNode) {
				bIsChildNodeOf = true;
				return;
			} else _isChildNodeOf(childNode,parentNode.firstChild);
			parentNode = parentNode.nextSibling;
		}
	}
	_isChildNodeOf(childNode,parentNode.firstChild);
	return bIsChildNodeOf;
}
function addEventLst(EventTarget,type,listener,useCapture) {
	useCapture = typeof(useCapture)=="boolean"?useCapture:false;
	if (EventTarget.addEventListener)
		EventTarget.addEventListener(type,listener, useCapture);
	else if ((EventTarget==window) && document.addEventListener)
		document.addEventListener(type,listener, useCapture);
	else if (EventTarget.attachEvent)
		EventTarget.attachEvent("on"+type,listener);
	else
		eval("EventTarget.on"+type+" = "+listener);
}
function removeEventLst(EventTarget,type,listener,useCapture) {
	useCapture = typeof(useCapture)=="boolean"?useCapture:false;
	if (EventTarget.removeEventListener)
		EventTarget.removeEventListener(type,listener, useCapture);
	else if ((EventTarget==window) && document.removeEventListener)
		document.removeEventListener(type,listener, useCapture);
	else if (EventTarget.detachEvent)
		EventTarget.detachEvent("on"+type,listener);
	else
		eval("EventTarget.on"+type+" = null");
}
addEventLst(window,"load",initMenus);
function initMenus() {
	if (!document.createElement && !document.createTextNode && !document.getElementById && !document.getElementsByTagName) return;
	var nChild = document.getElementById("externalRessources").firstChild;
	while (nChild) {
    	if (hasClassName(nChild,"externalTheme")) {
			loadMenus();
			break;
		}
    	nChild = nChild.nextSibling;
	}
}
function loadMenus() {
	var nExternalRessources, nA;
	addClassName(document.body,"dynamik");
	nExternalRessources = document.getElementById("externalRessources");
	if (nExternalRessources.addEventListener) {
		addEventLst(nExternalRessources,"mouseover",montrerMenu,true);
		addEventLst(nExternalRessources,"focus",montrerMenu,true);
		addEventLst(nExternalRessources,"DOMFocusIn",montrerMenu,true);
		addEventLst(nExternalRessources,"mouseout",cacherMenus,true);
		addEventLst(nExternalRessources,"blur",cacherMenus,true);
		addEventLst(nExternalRessources,"DOMFocusOut",cacherMenus,true);
	} else {
		var nChild=nExternalRessources.firstChild;
		while (nChild) {
			if (hasClassName(nChild,"externalTheme")) {
				addEventLst(nChild,"mouseover",montrerMenu);
				addEventLst(nChild,"mouseout",cacherMenus);
				for (var j=0; nA = nChild.getElementsByTagName("a")[j]; j++) {
					addEventLst(nA,"focus",montrerMenu);
					addEventLst(nA,"blur",cacherMenus);
				}
			}
			nChild=nChild.nextSibling;
		}
	}
}

function montrerMenu(e) {
	var oNode;
	if ((typeof(e)!="undefined") && (e.target)) {
		oNode = e.target;
	} else if (window.event) {
	    oNode = window.event.srcElement;
	} else {
		oNode = this;
	}
	while ( (oNode.nodeType!=9) && !hasClassName(oNode,"externalRessources")) {
		if (hasClassName(oNode,"externalTheme")) {
			addClassName(oNode,"currentTheme");
			break;
		}
		oNode = oNode.parentNode;
	}
}
function cacherMenus(e) {
	var oNode, nRelatedTarget;
	function _cacherMenus() {
		var nChild=document.getElementById("externalRessources").firstChild;
		while (nChild) {
			if (hasClassName(nChild,"externalTheme")) removeClassName(nChild,"currentTheme");
			nChild=nChild.nextSibling;
		}
	}
	if ((typeof(e)!="undefined") && (e.target)) {
		oNode = e.target;
		nRelatedTarget = e.relatedTarget;
	} else if (window.event) {
		oNode = window.event.srcElement;
		nRelatedTarget = window.event.toElement;
	} else {
		oNode = this;
	}
	while ( (oNode.nodeType!=9) && !hasClassName(oNode,"externalRessources")) {
		if (hasClassName(oNode,"externalTheme")) {
			if (nRelatedTarget && (!isChildNodeOf(nRelatedTarget,oNode))) {
				_cacherMenus();
			} else if (!nRelatedTarget) {
				_cacherMenus();
			}
			break;
		}
		oNode = oNode.parentNode;
	}
}
