// the timeout for the menu
var timeout = 0.05;
var $globals = new Array();

// this fonction apply the CSS style and the event
function initMenu()
{
	var cpt = 1;
	$('menu').select('li').each( function(el) {
		 if (el.select('ul').length > 0) {
			 Event.observe(el, "mouseenter", show);
			 Event.observe(el, "mouseleave", hideUlUnder);
			 Event.observe(el, "blur", hideUlUnder);
			 Event.observe(el, "focus", show);
			 
			 el.id = "li" + cpt;
			 cpt ++;
			 
			 var cpt2 = 1;
			 el.select('ul').each( function(el2) {
				 el2.id = "ul" + cpt + "-" + cpt2;
				 cpt2 ++;
			 } );
		 }
	} );
}
/*
function addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
    else
    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}*/
    
// hide the ul elements under the element identified by id
function hideUlUnder(event)
{   
	this.select('ul').each( function(el) {
		if ($globals['effect' + el.id])
			$globals['effect' + el.id].cancel();
		$globals['effect' + el.id] = new Effect.Fade(el, { duration: 0.25, delay: timeout });
	});
}

// show the first ul element found under this element
function show(event)
{
	var el = this.select('ul')[0];
	
	if ($globals['effect' + el.id])
		$globals['effect' + el.id].cancel();
	$globals['effect' + el.id] = new Effect.Appear(el, { duration: 0.25 });
	
    var currentNode = this;
    while(currentNode)
    {
            if (currentNode.nodeName == 'LI') {
                //currentNode.getElementsByTagName('a')[0].className = 'linkOver';
            }
            currentNode = currentNode.parentNode;
    }
    hideAllOthersUls(this);
}

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

// hide all the ul wich are in the li element
function hideUlUnderLi( li )
{
    $(li.id).select('a').each( function(el) {
        //el.className = "";
    } );
    $(li.id).select('ul').each( function(el) {
		if ($globals['effect' + el.id])
			$globals['effect' + el.id].cancel();
		$globals['effect' + el.id] = new Effect.Fade(el, { duration: 0.5, delay: timeout });
    } );
} 

