// Blog : http://www.magnin-sante.ch/journal/html/menu3/menuhorizontal.htm
// send me a mail :
//      faden@PASDEPOURRIELaltern.org - remove ( PASDEPOURRIEL )

function BrowserDetectLite() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser name
   this.isIE        = ( (ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) ); 
   this.isOpera     = (ua.indexOf('opera') != -1); 
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);

   // platform
   this.isMac   = (ua.indexOf('mac') != -1);
   
}
var browser = new BrowserDetectLite();

function initMenu()
{
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.Opera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        // get some element
        var menu = document.getElementById('menu');
        var lis = menu.getElementsByTagName('li');
		var menu2 = document.getElementById('menu2');
        var lis2 = menu2.getElementsByTagName('li');
        
        // change the style of the menu
        menu.className="menu";
		menu2.className="menu2";
        
        // 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')[0] )
            {
                // improve keyboard navigation with IE
                if ( browser.isIE )
                {
                    addAnEvent(lis.item(i),'keyup',visible);
                }
                
                // apply the method to DOM compliant browsers
                addAnEvent(lis.item(i),'mouseover',visible);
                addAnEvent(lis.item(i),'mouseout',hidden);
                addAnEvent(lis.item(i),'blur',hidden);
                addAnEvent(lis.item(i),'focus',visible);
            }
        }
		
		for ( var i=0; i<lis2.length; i++ )
        {            
            // is there a ul element ?
            if ( lis2.item(i).getElementsByTagName('ul')[0] )
            {
                // improve keyboard navigation with IE
                if ( browser.isIE )
                {
                    addAnEvent(lis2.item(i),'keyup',visible);
                }
                
                // apply the method to DOM compliant browsers
                addAnEvent(lis2.item(i),'mouseover',visible);
                addAnEvent(lis2.item(i),'mouseout',hidden);
                addAnEvent(lis2.item(i),'blur',hidden);
                addAnEvent(lis2.item(i),'focus',visible);
            }
        }
    }
}

function addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //target.attachEvent( 'on'+eventName, functionName ); // dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
}
    
/* hide the first ul element of the current element */
function hidden()
{
    /* setAttribute dont work correctly with IE */
    this.getElementsByTagName('ul')[0].style.display = "none";
}

/* show the first ul element of the current element */
function visible()
{
    this.getElementsByTagName('ul')[0].style.display = "block";
}
    
/* used to improve keyboard navigation with IE */
function hiddenUl( ul )
{
    if ( browser.isIE )
    {
        var uls = ul.getElementsByTagName('ul');
        for ( var i=0; i<uls.length; i++ )
        {
            uls.item(i).style.display = "none";
        }
        ul.style.display = "none";
    }
} 
window.onload=initMenu;

    
