var selectedItem;
var useEffects = true;

/*
 * Initiates the menu.
 */
function initMenu() {
	selectedItem = document.getElementById('selectedItem');
	collapse(document.getElementById('leftMenu'));
	
	var parent = getParent(selectedItem, 'li');
	parent.className = 'current';
	while ( parent = getParent(parent, 'li') ) {
		parent.className = 'expanded';
	}
	
	var currLoc = location.href;
	
	if (homePage == 'true'  && (loginMode == 'true' || (currLoc.indexOf('?debugstate') > -1)))
	{
		$("topMenu-id").hide();
		$("leftMenu").style.top = "64px"
	}
	else
	{
		$("topMenu-id").show();
		$("leftMenu").style.top = "158px"
	}
	
	initialize();
}

/*
 * Collapses all elements which are not or do not contain the currently selected menu item
 * It collapses them using the Effect.SlideUp, so that they can be slid down when it is asked
 */
function collapse(el) {
	var ul = el.getElementsByTagName('ul')[0];
	if (ul) {
		var lis = ul.getElementsByTagName('li');
		for (var i = 0; i < lis.length; i++)
			collapse(lis[i]);
	}
	if (el.className == 'collapsed')
		if (useEffects) new Effect.SlideUp(el.getElementsByTagName('ul')[0], {duration:0});
}

/*
 * Submits the form of which the given selectElement is a child, given the currently selected item has a value.
 * selectElement (<select>): the element which needs to have a value in order for its parent form to be submitted.
 */
function submitForm(selectElement) {
	if (selectElement.options && selectElement.options[selectElement.selectedIndex] && selectElement.options[selectElement.selectedIndex].value !='') {
		selectElement.form.submit();
	}
}

/*
 * Returns the first element (in hierarchy) with the given tagName.
 * el(element): the element of which its parents element should be returned.
 * tagName(str): the tagname of the parent element that should be searched for.
 * returns the first parentNode with the given tagName, or false if it does not exist.
 */
function getParent(el, tagName) {
	var parent = el.parentNode;
	if (parent && parent.id != 'leftMenu') {
		if (parent.tagName.toLowerCase() != tagName.toLowerCase())
			return getParent(parent, tagName);
		else
			return parent;
	}
	else
		return false;
}

/*
 * Checks if a given element contains the currently selected item.
 * el(element): the item that does or does not contain selectedItem.
 * returns if the givens element contains the selected element (true) or not (false).
 */
function containsCurrent(el) {
	parentLi = selectedItem;
	while( parentLi = getParent(parentLi, 'li') ) {
		if (parentLi == el)
			return true;
	}
	return false;
}

/*
 * Expands/collapses a menu item by setting its parents class accordingly.
 * item(<a>): the menu-item being clicked on.
 */
var sliders = 0;
var SLIDE_UP = 1;
var SLIDE_DOWN = 2;

function toggleMenu(item) {
	
	if (sliders > 0)
		return false;
	
	var container = getParent(item, 'li');
	var duration = useEffects ? 0.4 : 0;
	
	// Was the function triggered by clicking on an already expanded item?
  if (container.className == 'expanded') {
		slide(container.getElementsByTagName('ul')[0], duration, SLIDE_UP, containsCurrent(container) ? 'current collapsed' : 'collapsed');
		setTimeout( "collapseChildren('"+container.id+"', true);", duration * 1000);
	}
	else {
		parentNode = container.parentNode;
		for (var i = 0; i < parentNode.childNodes.length; i++) {
			var tagName = parentNode.childNodes[i].tagName;
			if (tagName && parentNode.childNodes[i] != container)
				if (tagName.toLowerCase() == 'li' && parentNode.childNodes[i].className == 'expanded') {
					slide(parentNode.childNodes[i].getElementsByTagName('ul')[0], duration, SLIDE_UP, containsCurrent(parentNode.childNodes[i]) ? 'current collapsed' : 'collapsed');
					setTimeout( "collapseChildren('"+parentNode.childNodes[i].id+"', true);", duration * 1000);
				}
		}
		
		container.className = 'expanded';
		slide(container.getElementsByTagName('ul')[0], duration, SLIDE_DOWN, null);
	}
	
	return false;
}

/*
 * Changes the class of the specified element to the specified class string
 */
function changeClass(el, classN) {
	el = document.getElementById(el);
	el.className = classN;
}

/*
 * Collapses all children of specified element
 */
function collapseChildren(el, doNotCollapse) {
	var ul = $(el).getElementsByTagName('ul')[0];
	if (ul) {
		var lis = ul.getElementsByTagName('li');
		for (var i = 0; i < lis.length; i++)
			collapseChildren(lis[i], false);
	}
	if (!doNotCollapse && el.className == 'expanded') {
  	el.className = containsCurrent(el) ? 'current collapsed' : 'collapsed';
  	if (useEffects) new Effect.SlideUp(el.getElementsByTagName('ul')[0], {duration:0});
  }
}

/*
 * Slides the given element within the given duration towards the given direction
 */
function slide(element, duration, direction, newClass) {
	if (useEffects) {
		sliders++;
		if (direction == SLIDE_UP)
			new Effect.SlideUp(element, {duration:duration});
		else
			new Effect.SlideDown(element, {duration:duration});
		if (newClass)
			setTimeout( "changeClass('"+getParent(element, 'li').id+"', '"+newClass+"');", duration * 1000);
		setTimeout( "sliders--;", duration * 1000);
	}
}
