/*
Original author: Batiste Bieler (http://www.dosimple.ch)
Rewritten by: René-Gilles Deberdt (http://nao.noisen.com)
Released under the LGPL license. (http://www.gnu.org/licenses/lgpl.html)
Copyright (C) 2004 Batiste Bieler, 2008-2010 René-Gilles Deberdt
*/

var timeout = 250, last_opened_submenu = '';

if (typeof ua == 'undefined')
{
	ua = navigator.userAgent.toLowerCase();
	is_opera = ua.indexOf('opera') != -1;
	var is_ie = ua.indexOf('msie') != -1 && !is_opera;
	is_opera5 = ua.indexOf('opera/5') != -1 || ua.indexOf('opera 5') != -1;
	is_opera6 = ua.indexOf('opera/6') != -1 || ua.indexOf('opera 6') != -1;
}

isIE = is_ie && (ua.indexOf('webtv') == -1);

// Create timeout variables for list item.
// Done to avoid some warning with IE
var timeoutli = new Array();
var ieshim = new Array();

function initMenu()
{
	if (document.getElementById && (((ua.indexOf('mac') == -1) || !isIE) || (is_opera && !is_opera5 && !is_opera6) || isIE))
	{
		var menu = document.getElementById('amen');
		menu.style.display = 'block';
		menu.style.visibility = 'visible';
		var lis = menu.getElementsByTagName('li');

		// Change the class name of the menu, for backwards compatibility with old browsers
		menu.className = 'amen';
		if (isIE)
			document.write('<style><!-- .iefs { display: none; z-index: 9; position: absolute; left: 0; top: 0; filter: progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=0); } // --></style>');
		for (var i = 0; i < lis.length; i++)
		{
			// Is there a ul element ?
			if (lis.item(i).getElementsByTagName('ul').length > 0)
			{
				lis.item(i).setAttribute('id', 'li'+i);
				if (isIE)
				{
					addAnEvent(lis.item(i), 'keyup', show);
					document.write('<iframe src="" id="shim' + i + '" class="iefs" frameborder="0" scrolling="no"></iframe>');
					ieshim[i] = document.getElementById('shim' + i);
				}
				addAnEvent(lis.item(i),'mouseover', show);
				addAnEvent(lis.item(i),'mouseout', timeoutHide);
				addAnEvent(lis.item(i),'click', hideAllUls);
				addAnEvent(lis.item(i),'blur', timeoutHide);
				addAnEvent(lis.item(i),'focus', show);
			}
		}
	}
}

function addAnEvent(target, eventName, functionName)
{
	if (target.addEventListener)
		target.addEventListener(eventName, functionName, true); // true is important for Opera 7
	else if (target.attachEvent)
	{
		target['e' + eventName + functionName] = functionName;
		target[eventName + functionName] = function() { target['e' + eventName + functionName](window.event); }
		target.attachEvent('on' + eventName, target[eventName + functionName]);
	}
}

// Hide the first ul element of the current element
function timeoutHide()
{
	eval('timeoutli[' + this.id.substring(2) + '] = window.setTimeout(\'hideUlUnder("' + this.id + '")\', ' + timeout + ');');
	last_opened_submenu = this.id;
}

// Hide the ul elements under the element identified by id
function hideUlUnder(id)
{
	var eid = document.getElementById(id);
	eid.getElementsByTagName('ul')[0].style.visibility = 'hidden';
	var h4s = eid.getElementsByTagName('h4');
	if (h4s.length > 0)
		h4s[0].className = '';
	showShim(false, id);
}

function showShim(showsh, ieid, iemenu)
{
	iem = ieid.substring(2);
	if (typeof ieshim[iem] != 'undefined')
	{
		if (showsh)
		{
			ieshim[iem].style.top = iemenu.offsetTop + iemenu.offsetParent.offsetTop + 'px';
			ieshim[iem].style.left = iemenu.offsetLeft + iemenu.offsetParent.offsetLeft + 'px';
			ieshim[iem].style.width = iemenu.offsetWidth + 'px';
			ieshim[iem].style.height = iemenu.offsetHeight + 'px';
		}
		ieshim[iem].style.display = showsh ? 'block' : 'none';
	}
}

// Show the first ul element found under this element
function show()
{
	// Show the menu item
	var showul = this.getElementsByTagName('ul')[0];
	showul.style.visibility = 'visible';

	// If this is a submenu, show it next to the parent menu item
	showul.style.marginLeft = (this.parentNode.id == 'amen' ? 0 : this.parentNode.clientWidth - 5) + 'px';

	if (isIE)
		showShim(true, this.id, showul);
	var h4s = this.getElementsByTagName('h4');
	if (h4s.length > 0)
		h4s[0].className = 'linkOver';
	else
	{
		var currentNode = this;
		while (currentNode)
		{
			if (currentNode.nodeName == 'LI' && currentNode.parentNode.id != 'amen')
				currentNode.getElementsByTagName('a')[0].className = 'linkOver';
			currentNode = currentNode.parentNode;
		}
	}
	if (last_opened_submenu != '')
	{
		// Needed to turn off highlighting when switching from a submenu-enabled menu item to a solo menu item
		var eid = document.getElementById(this.id);
		var as = eid.getElementsByTagName('a');
		for (var i = 0, j = as.length; i < j; i++)
			as.item(i).className = '';
		last_opened_submenu = '';
	}
	eval('clearTimeout(timeoutli[' + this.id.substring(2) + ']);');
	hideAllOthersUls(this);
}

// Hide all ul's on the same level as this list item
function hideAllOthersUls(currentLi)
{
	var lis = currentLi.parentNode;
	for (var i = 0, len = lis.childNodes.length; i < len; i++)
		if (lis.childNodes[i].nodeName == 'LI' && lis.childNodes[i].id != currentLi.id)
			hideUlUnderLi(lis.childNodes[i]);
}

function hideAllUls()
{
	var lis = document.getElementById('amen');
	for (var i = 0, len = lis.childNodes.length; i < len; i++)
		if (lis.childNodes[i].nodeName == 'LI')
			hideUlUnderLi(lis.childNodes[i]);
}

// Hide all ul's in the li element
function hideUlUnderLi(li)
{
	var h4s = li.getElementsByTagName('h4');
	if (h4s.length > 0)
		h4s[0].className = '';
	else
	{
		var as = li.getElementsByTagName('a');
		for (var i = 0, j = as.length; i < j; i++)
			as.item(i).className = '';
	}
	var uls = li.getElementsByTagName('ul');
	for (var i = 0, j = uls.length; i < j; i++)
		uls.item(i).style.visibility = 'hidden';
}

