/* Liest den Link (a) der für die JavaScript-Navi zuständig ist zu einem Übergeordneten Menüelement */
function getPopperLink(parentN) {
	var tmp = parentN.firstChild;
	var popper = null;
	if (!tmp) {
		return popper;
	}
	do {
		if (tmp != null && tmp.nodeName == 'A' &&
				(tmp.className == 'open' ||
				tmp.className == 'close' ||
				tmp.className == 'popper')) {
			popper = tmp;
		}
		tmp = tmp.nextSibling;
	} while (popper == null && tmp != null);
	return popper;
}

function popSubCategory(popperLink) {
	var li = popperLink.parentNode;
	var ul = findNextSub(popperLink,'UL',null);
	if (ul) {
		if (li.className && (li.className.match(/ open/) || li.className.match(/^open/))) {
			popperLink.innerHTML='<span>[+]</span>';
			li.className=li.className.replace(/ open/, '');
			li.className=li.className.replace(/^open/, '');
		} else {
			popperLink.innerHTML='<span>[-]</span>';
			li.className+=' open';
		}
	}
}

/* Parst die verschachtelte Liste zu den Unterkategorien */
function parseList(ul) {
	var node = findFirstSub(ul,'LI',null);
	do {
		if (node && node.className.match(/hasSubs/)) {
			var popperLink = getPopperLink(node);
			if (popperLink) {
				popperLink.onclick = function() {
					//POP IT
					popSubCategory(this);
					return false;
				}
				popperLink.onfocus = function() {
					this.blur();
				}
			}
			var subList = findFirstSub(node,'UL',null);
			if (subList) {
				parseList(subList);
			}
		}
		node=findNextSub(node,'LI',null);
	} while (node!=null);
}

/* Versteckt die Unterkategorien zu einer Hauptkategorie (H3 = Brother)
   Ist init true, wird kein Cookie gesetzt
*/
function hideSubs(brother,isInit) {
	var brotherType = brother.nodeName;
	getPopperLink(brother).className='open';
	var node = brother.nextSibling;
	do {
		if (node.nodeName != brotherType && node.className!=null && !(node.className.match(/noClose/)) ) {
			node.className+=' hidden';
		}
		node = node.nextSibling;
	} while (node != null && node.nodeName != brotherType);
}

/* Zeigt die Unterkategorien zu einer Hauptkategorie (H3 = Brother)
   Ist init true, wird kein Cookie gesetzt, aber die Subkategorien geparst
*/
function showSubs(brother,isInit) {
	var brotherType = brother.nodeName;
	getPopperLink(brother).className='close';
	var node = brother.nextSibling;
	do {
		if (node.nodeName != brotherType && node.className!=null && !(node.className.match(/noClose/))) {
			node.className=node.className.replace(/ hidden/, '');
			node.className=node.className.replace(/^hidden/, '');
		}
		node = node.nextSibling;
	} while (node != null && node.nodeName != brotherType);
}



function initLeftNaviPopper() {
	var rootNode = document.getElementById('navi');
	var node = rootNode.firstChild;
	do {
		if (node.nodeName == 'H3' && !node.className.match(/noClose/)) {
			var popper = getPopperLink(node);
			if (popper != null) {
				popper.onclick = function() {
					if (this.className == 'close') {
						hideSubs(this.parentNode,false);
						this.className = 'open';
					} else if (this.className == 'open') {
						showSubs(this.parentNode,false);
						this.className = 'close';
					}
					return false;
				}
				popper.onfocus = function() {
					this.blur();
				}
				var maybeList = findNextSub(node,'UL',null);
				if (maybeList) {
					do {
						if (maybeList && maybeList.nodeName=='UL' && maybeList.className.match(/category/)) {
							parseList(maybeList);
						}
						maybeList=findNextSub(maybeList,'UL',null);
					} while (maybeList != null);
				}
			}
		}

		node = node.nextSibling;
	} while (node != null);
}

/* Trägt die Navi-Initialisierung bei den onload-Funktionen ein.  */
addInitFunction(initLeftNaviPopper);