var activeMenuItem = new Array();

function isUlInArray(inputObj, ulObj) {
	while (inputObj && inputObj.id != 'listMenu') {
		if (inputObj == ulObj) return true;
		inputObj = inputObj.parentNode;
	}
	return false;
}

function showHideSub(e, inputObj) {
	if (!inputObj) inputObj = this;
	var parentObj = inputObj.parentNode;
	var ul = parentObj.getElementsByTagName('UL')[0];
	if (activeMenuItem.length > 0) {
		for (var no = 0; no < activeMenuItem.length; no++) {
			if (!isUlInArray(ul, activeMenuItem[0]) && !isUlInArray(activeMenuItem[0], ul)) {
				activeMenuItem[no].style.display = 'none';
				activeMenuItem.splice(no, 1);
				no--;
			}
		}
	}
	if (ul.offsetHeight == 0) {
		ul.style.display = 'block';
		activeMenuItem.push(ul);
	}
	else {
		ul.style.display = 'none';
	}
}

function showHidePath(inputObj) {
	var startTag = inputObj;
	showHideSub(false, inputObj);
	inputObj = inputObj.parentNode;
	var ancFirstLevel;
	while (inputObj) {
		ancFirstLevel = inputObj.getElementsByTagName('A')[0]
		inputObj = inputObj.parentNode;
		if (inputObj.tagName == 'LI') showHideSub(false, inputObj.getElementsByTagName('A')[0]);
		if (inputObj.id == 'listMenu') inputObj = false;
	}
	if (ancFirstLevel) {
		ancFirstLevel.className = 'activeMenuLink';
		ancFirstLevel.parentNode.className = 'activeMenuItem';
	}
}

function initMenu() {
	var obj = document.getElementById('listMenu');
	var linkCounter = 0;
	var aTags = obj.getElementsByTagName('A');
	var activeMenuItem = false;
	var activeMenuLink = false;
	var thisLocationArray = location.href.split(/\//);
	var fileNameThis

	//id bepalen als deze er is, anders wordt de bestandsnaam gebruikt

	if (location.href.indexOf('/wcs/') > 0) { //vriendelijke urls, dus langere filenaam pakken om te matchen (ivm niet unieke filenames).
		fileNameThis = '/' + thisLocationArray[thisLocationArray.length - 4] + '/' + thisLocationArray[thisLocationArray.length - 3] + '/' + thisLocationArray[thisLocationArray.length - 2] + '/' + thisLocationArray[thisLocationArray.length - 1];
	}
	else {
		fileNameThis = thisLocationArray[thisLocationArray.length - 1];

		if (fileNameThis.indexOf('?') >= 0) {
			fileNameThis = fileNameThis.substr(fileNameThis.indexOf('?') + 1);
			if (fileNameThis.indexOf('id') >= 0) {
				fileNameThis = fileNameThis.substr(fileNameThis.indexOf('id'));
				if (fileNameThis.indexOf('&') >= 0) {
					fileNameThis = fileNameThis.substr(0, fileNameThis.indexOf('&'));
				}
			}
		}
	}

	if (fileNameThis.indexOf('#') > 0) {
		fileNameThis = fileNameThis.substr(0, fileNameThis.indexOf('#'));
	}

	//alle anchor tags doorlopen
	for (var no = 0; no < aTags.length; no++) {
		var parent = aTags[no].parentNode; //de tag voor de A
		var subs = parent.getElementsByTagName('ul');
		if (subs.length > 0) {
			aTags[no].onclick = showHideSub;
			linkCounter++;
			aTags[no].id = 'aLink' + linkCounter;
		}

		//bepaal of er een actief menu item is a.d.h.v. een id of een bestandsnaam zonder id
		var strHref = aTags[no].href + '&';
		if (strHref.indexOf(fileNameThis + '&') >= 0 && aTags[no].href.charAt(aTags[no].href.length - 1) != '#' && ((fileNameThis.indexOf('id=') >= 0 && aTags[no].href.indexOf('id=') >= 0) || (fileNameThis.indexOf('id=') == -1 && aTags[no].href.indexOf('id=') == -1))) {

			if (aTags[no].parentNode.parentNode) {
				var parentObj = aTags[no].parentNode.parentNode.parentNode;
				var a = parentObj.getElementsByTagName('a')[0];

				if (!activeMenuLink) { // 
					//bepaal of het actieve items ook subitems bevat en laat deze eventueel zien door activeMenuItem op te hogen
					if (aTags[no].parentNode.getElementsByTagName('UL').length > 0) {
						aTags[no].id = 'aLink' + linkCounter + 1;
						activeMenuItem = 'aLink' + linkCounter + 1;
					}
			//		else { /* als er geen subitems zijn in het eerste niveau moet niet alsnog de eerste subitems worden getoond... */
					else if (a.id) {
						if (aTags[no].parentNode.id != 'base') {
							activeMenuItem = a.id;
						}
					}
					activeMenuLink = aTags[no];
				}
			}
		}
	}

	if (activeMenuLink) {
		activeMenuLink.className = 'activeMenuLink';
		activeMenuLink.parentNode.className = 'activeMenuItem';
	}
	if (activeMenuItem) {
		if (document.getElementById(activeMenuItem)) showHidePath(document.getElementById(activeMenuItem));
	}
}