//Dynamic Menu Script Provided by Andrew Penry
//for use on Webonizer Websites
//Learn more about Andrew at http://www.thepenry.net

// the timeout for the menu

var atp_timeout = 200;

// not very clean but simple
// the function can be run in the HTML for faster display
//window.onload=atp_initMenu;

// create timeout variables for list items
// to avoid some warnings in IE


// this function apply the CSS style and the event
function atp_initMenu()
{
	for( var i = 0; i <document.getElementById('atp_menu').getElementsByTagName('li').length; i++ )
	{
	    eval("atp_timeoutli" + i + " = false;");
	}

    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( atp_browser.isDOM1
//    && !( atp_browser.isMac && atp_browser.isIE )
    && !( atp_browser.isOpera && atp_browser.versionMajor < 7 )
    && !( atp_browser.isIE && atp_browser.versionMajor < 5 ) )
    {
        // get some element
        var menu = document.getElementById('atp_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='atp_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 ( atp_browser.isIE )
                {
                    atp_addAnEvent(lis.item(i),'keyup',atp_show);
                }
                // link events to list item
                atp_addAnEvent(lis.item(i),'mouseover',atp_show);
                atp_addAnEvent(lis.item(i),'mouseout',atp_timeoutHide);
                atp_addAnEvent(lis.item(i),'blur',atp_timeoutHide);
                atp_addAnEvent(lis.item(i),'focus',atp_show);

                // add an id to list item
                lis.item(i).setAttribute( 'id', "li"+i );
                lis.item(i).getElementsByTagName('a')[0].className = 'atp_menuExpandable';

				// add an id to the first child ul
				lis.item(i).getElementsByTagName('ul')[0].setAttribute( 'id', "li"+i+"ul" );
			}
        }
    }
}

function atp_addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( atp_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 atp_timeoutHide()
{
    // start the timeout
    eval( "atp_timeout" + this.id + " = window.setTimeout('atp_hideUlUnder( \"" + this.id + "\" )', " + atp_timeout + " );");
}

// hide the ul elements under the element identified by id
function atp_hideUlUnder( id )
{
    document.getElementById(id).getElementsByTagName('ul')[0].style['visibility'] = 'hidden';
}

// show the first ul element found under this element
function atp_show()
{
    // show the sub menu
    document.getElementById(this.id+"ul").style['visibility'] = 'visible';
    // clear the timeout
    eval ( "clearTimeout( atp_timeout"+ this.id +");" );
    atp_hideAllOthersUls( this );
}

// hide all ul on the same level of  this list item
function atp_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 )
        {
            atp_hideUlUnderLi( ul.childNodes[i] );
        }
    }
}

// hide all the ul wich are in the li element
function atp_hideUlUnderLi( li )
{
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ )
    {
        uls.item(i).style['visibility'] = 'hidden';
    }
}