var timeout = 500;

for( var i = 0; i < 100; i++ )
{
    eval("var timeoutli" + i + " = false;");
}

// this fonction apply the CSS style and the event
function initMenu()
{
		var tab_select = document.getElementsByTagName('select');
		for(var i=0;i<tab_select.length;i++)
		{
			tab_select[i].style['visibility'] = 'visible';
		}
    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        // get some element
        var menu = document.getElementById('menu'); // the root element
        var lis = menu.getElementsByTagName('li'); // all the li
        
        // change the class name of the menu, 
        // it's usefull for compatibility with old browser
        menu.className='menu';
        
        // i am searching for ul element in li element
        for ( var i=0; i<lis.length; i++ )
        {
            // is there a ul element ?
            if ( lis.item(i).getElementsByTagName('ul').length > 0 )
            {        
                // improve IE key navigation
                if ( browser.isIE )
                {
                    addAnEvent(lis.item(i),'keyup',show);
                }
                // link events to list item
                addAnEvent(lis.item(i),'mouseover',show);
                addAnEvent(lis.item(i),'mouseout',timeoutHide);
                addAnEvent(lis.item(i),'blur',timeoutHide);
                addAnEvent(lis.item(i),'focus',show);
                
                // add an id to list item
                lis.item(i).setAttribute( 'id', "li"+i );
                lis.item(i).getElementsByTagName('a')[0].className = 'menuSub';
            }
        }
    }
}

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 first ul element of the current element
function timeoutHide()
{
    // start the timeout
    eval( "timeout" + this.id + " = window.setTimeout('hideUlUnder( \"" + this.id + "\" )', " + timeout + " );");
}

// hide the ul elements under the element identified by id
function hideUlUnder( id )
{   
    document.getElementById(id).getElementsByTagName('ul')[0].style['visibility'] = 'hidden'; 
    document.getElementById(id).getElementsByTagName('ul')[0].style['display'] = 'none';
		//On test pour voir si un sous menu est encore visible, auquel cas on ne réaffiche pas les listes déroulantes
    var menu = document.getElementById('menu'); // the root element
		var tab_li = menu.getElementsByTagName('ul');
		var afficheSelect = 1;
		for(var i=0;i<tab_li.length;i++)
		{
    	if(tab_li[i].style['visibility'] == 'visible') afficheSelect = 0;
    	if(tab_li[i].style['display'] == '') afficheSelect = 0;
		}
		//On affiche de nouveau les listes déroulantes
		if(afficheSelect == 1)
		{
			var tab_select = document.getElementsByTagName('select');
			for(var i=0;i<tab_select.length;i++)
			{
				tab_select[i].style['visibility'] = 'visible';
			}
		}
//    document.getElementById(id).getElementsByTagName('ul')[0].style['border'] = '1px #00ff00 solid';
}

// show the first ul element found under this element
function show()
{
    // show the sub menu
		var tab_select = document.getElementsByTagName('select');
		for(var i=0;i<tab_select.length;i++)
		{
			tab_select[i].style['visibility'] = 'hidden';
		}
    this.getElementsByTagName('ul')[0].style['visibility'] = 'visible';
    this.getElementsByTagName('ul')[0].style['display'] = 'block';
//    this.getElementsByTagName('ul')[0].style['border'] = '1px #ff0000 solid';
// clear the timeout
    eval ( "clearTimeout( timeout"+ this.id +");" );
    hideAllOthersUls( this );
}

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

// hide all the ul wich are in the li element
function hideUlUnderLi( li )
{
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ )
    {
//        uls.item(i).style['visibility'] = 'hidden';
        uls.item(i).style['display'] = 'none';
//        uls.item(i).style['border'] = '1px #0000ff solid';
    }
} 
