// the timeout for the menu
var timeout = 1000;

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

// hide the first ul element of the current element
function timeoutHide(obj)
{
//hideUlUnder(this.id);

//eval("timeoutA"+obj.id+" = window.setTimeout('document.getElementById(\""+obj.id+"\").style.backgroundColor=\"#73D0FF\"' ,100);");
//obj.style.backgroundColor="#990134";

    // start the timeout
    eval( "timeout" + obj.id + " = window.setTimeout('hideUlUnder( \"" + obj.id + "\" )', " + timeout + " );");
}

// hide the ul elements under the element identified by id
function hideUlUnder( id )
{
    var uls=document.getElementById(id).getElementsByTagName('ul');
    if (uls.length>=1)
      uls[0].style['visibility'] = 'hidden';
}


// show the first ul element found under this element
function show(obj)
{
//obj.style.backgroundColor="#666666";
//if (eval("typeof(timeoutA"+obj.id+")") != "undefined") eval ( "clearTimeout( timeoutA"+ obj.id +");" );

    // show the sub menu
    var uls=obj.getElementsByTagName('ul');
    if (uls.length>=1) {


// see whether we should move the menu
var ul=uls[0];
var botWin =  document.body.clientHeight;
var topPos = nwGetY(ul);
var botPos = topPos + ul.offsetHeight;
if (botPos > botWin) {
  var newPos = ul.offsetTop - (botPos - botWin);
  ul.style.top = newPos;
}



      uls[0].style['visibility'] = 'visible';
    }
    // clear the timeout
if (eval("typeof(timeout"+obj.id+")") != "undefined")
    eval ( "clearTimeout( timeout"+ obj.id +");" );
    hideAllOthersUls( obj );
}

// 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';
    }
} 

// get Y position of the object on a page
function nwGetY(obj) {
  return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+nwGetY(obj.offsetParent) );
}











