/*
 * Copyright (c) 2007-2009 Mikael Relbe
 *
 * With thankful contribution from Elias Lovén Larsson.
 */

function menu()
{
}

menu.showSubMenu = function(forAnchorObj, doNotToggle)
{
	if (!forAnchorObj.id) {
		alert('Error: menu.showSubMenu');
		return false;
	}

	var subMenuId = 'submenu_' + forAnchorObj.id

	if (!document.getElementById(subMenuId)) {
		alert('Error on page, missing sub menu ' + subMenuId);
		return false;
	}
	if (document.getElementById(subMenuId).style.display == 'block' && !doNotToggle)
		document.getElementById(subMenuId).style.display = 'none'; // Hide sub menu
	else
		document.getElementById(subMenuId).style.display = 'block'; //Show sub menu
	// Return True to allow the anchor to reload when the menu URL differs from the viewed page
	return forAnchorObj.href != document.URL;
}

menu.hideSubMenus = function()
{
	var myDivs = document.getElementsByTagName('div'); //Get all DIV elements
	for (var i = 0; i < myDivs.length; i++)
	{
		if (myDivs[i].className == 'subMenu') myDivs[i].style.display = 'none';
	}
}

menu.removeMark = function (anchorObject)
{
	// Remove a mark
	// Check if marked
	if (anchorObject.className.indexOf('menuItemMarked') != -1)
		// Remove mark
		anchorObject.className = anchorObject.className.replace(/\s*menuItemMarked/, '');
}

menu.markNone = function()
{
	// Remove all marks
	var myAss = document.getElementsByTagName('a'); //Get all A elements (anchors)
	for (var i = 0; i < myAss.length; i++) menu.removeMark(myAss[i]);
}

menu.mark = function(url)
{
	//alert('menu.mark(' + url + ')');
	// This function is called upon by each page on this site when loaded.
	// All menu items, anchors, that refers to the provided URL are marked and
	// its parent sub menues are opened up to show the user the menu location
	// of the presented document
	var myUrl = url;
	var marked;

	// Remove all info after #-sign in the URL
	if (myUrl.indexOf('#') > 0) myUrl = myUrl.slice(0, myUrl.indexOf('#'));

	marked = menu.mark_Private(myUrl);

	if (!marked) {
		// Remove all info after ?-sign in the URL and try to mark
		if (myUrl.indexOf('?') > 0) {
			myUrl = myUrl.slice(0, myUrl.indexOf('?'));
			marked = menu.mark_Private(myUrl);
		}
	}

	if (!marked) {
		// Remove all info after /index.* in the URL and try to mark
		if (myUrl.indexOf('/index.') > 0) {
			myUrl = myUrl.slice(0, myUrl.indexOf('/index.'));
			marked = menu.mark_Private(myUrl);
		}
	}
}

menu.mark_Private = function(url)
{
	//alert('menu.mark_Private(' + url + ')');
	// This function is used by menu.mark.
	// All menu items, anchors, that refers to the provided URL are marked and
	// its parent sub menues are opened up to show the user the menu location
	// of the presented document
	//
	// Returns True when menu marked, False otherwise
	//
	var myAss = document.getElementsByTagName('a'); //Get all A elements (anchors)
	var marked = false;
	for (var i = 0; i < myAss.length; i++)
	{
		// Check if anchor is a menu item
		if (myAss[i].className.indexOf('menuItem' != -1))
		{
			if (myAss[i].href != url) // anchors href is NOT the URL
				menu.removeMark(myAss[i]);
			else
			{
				// Mark the found menu item, and expand menues around it when not already marked
				if (myAss[i].className.indexOf('suppressMark') == -1 && // marking not suppressed
					myAss[i].className.indexOf('menuItemMarked') == -1) // not already marked
				{
					myAss[i].className = myAss[i].className + ' menuItemMarked'; // Mark the menu item
					// Show the parent sub menues containing this menu item
					var p = myAss[i].parentNode; // Get the parent node to the anchor
					while (p) // Loop through each parent node, start with the first one
					{
						if (p.className == 'subMenu') p.style.display = 'block'; // Show the submenu
						p = p.parentNode; // The next parent
					}
					// If the menu item itself is a sub menu, open it
					if (myAss[i].className.indexOf('hasSubMenu') != -1)
						menu.showSubMenu(myAss[i], true);
				}
				marked = true;
			}
		}
	}
	return marked;
}

menu.markItem = function(itemId)
{
	// This function is only needed at anchors referring to external sites,
	// since external web pages cannot make a callback to menu.mark which
	// is performed by each page on the relbe.se site.
	menu.markNone();
	// Mark this provided anchor as being selected
	itemId.className = itemId.className + ' menuItemMarked';
}


// Mark active menu item
menu.mark(document.URL);
