function MenuCollapse(menu,location){

	// Holds a list of all links in the menutree.
	// Each entry is represents a link using an array of length 2.
	// The first value is the HTML<a>Element, the second is
	// a boolean, wheater this link is to be expanded.
	var link = new Array();

	fillLinkArray();

	// Add a Collapser button to each link
	for(var i=0; i<link.length; i++){

		// Create and add collapser button
		var collapser = document.createElement("div");
		link[i][0].parentNode.insertBefore(collapser,link[i][0]);

		// Append function to collapser button
		collapser.onclick = function(){
			var menu = this.parentNode.getElementsByTagName("ul")[0];
			if(menu.style.display == "block"){
				collapse(this);
			}
			else{
				expand(this);
			}
		}

		// Expand or collapse this link accoring to the link array.
		if(link[i][1]){
			expand(collapser);
		}
		else{
			collapse(collapser);
		}
	}

	/*
	 * Traverses the menutree bottom up and checks if a link 
	 * has to be expanded or collapsed. This function is 
	 * rekursive. It will fill up the link array as described
	 * above.
	 */
	function fillLinkArray(branch){
		if(!branch){
			branch = menu
		}

		// Indicates if the parent node of the currently
		// processed node has to be expanded. Holds the
		// parent node to identify it.
		var expandParent = null;

		for(var i=branch.childNodes.length-1; i>=0; i--){
			var expand = false;
			var tag = branch.childNodes[i];
			var tmp = null;

			if(!tag.tagName){
				continue;
			}
			
			switch(tag.tagName.toLowerCase()){
				case "a":
					// After the location link has been found, all its
					// parent nodes have to be expanded.
					if(location == tag.href || tag.parentNode == expandParent){
						expand = true;
						expandParent = tag.parentNode;
					}
					link.push(new Array(tag,expand));
				break;

				case "li":
				case "ul":
					if(tmp = fillLinkArray(tag)){
						expandParent = tmp;
					}
					if(tag == expandParent){
						expandParent = tag.parentNode;
					}
				break;
			}
		}

		return expandParent;
	}


	/*
	 * Collapses the given menu
	 * @param HTML<div>Element
	 */
	function collapse(collapser){
		var menu = collapser.parentNode.getElementsByTagName("ul")[0];
		if(menu){
			menu.style.display = "none";
			collapser.className = "collapsed";
		}
		else{
			collapser.parentNode.removeChild(collapser);
		}
	}


	/*
	 * Expands the given menu
	 * @param HTML<div>Element
	 */
	function expand(collapser){
		var menu = collapser.parentNode.getElementsByTagName("ul")[0];
		if(menu){
			menu.style.display = "block";
			collapser.className = "expand";
		}
		else{
			collapser.parentNode.removeChild(collapser);
		}
	}
}
