/*
Original author: Batiste Bieler (doSimple)
Rewritten by: Rene-Gilles Deberdt (Nao/noisen.com)
Please visit http://www.dosimple.ch for a complete license agreement
Copyright (C) 2004 Batiste Bieler, 2008 R.-G. Deberdt
*/

var timeout = 150, last_opened_submenu = '';

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 && is_ie5up) ) )
	{
		var menu = document.getElementById('menu');
		var lis = menu.getElementsByTagName('li');

		// change the class name of the menu,
		// for compatibility with old browsers
		menu.className = 'menu';
		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 == 'menu' ? 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 != 'menu')
				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 on the same level of  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('menu');
	for ( var i=0, len = lis.childNodes.length; i<len; i++ )
		if ( lis.childNodes[i].nodeName == 'LI' )
			hideUlUnderLi( lis.childNodes[i] );
}

// hide all the ul which are 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';
}

