// $Id: subMenu.js,v 1.3 2010/11/10 05:24:09 pwh Exp $


function insertList ( item )

{
   item.next = this.next
   this.next = item
}


function removeList ( id )

{
   var item = null
   var previousItem = this
   var currentItem = this.next

   while ( currentItem ) {

	if ( currentItem.id == id ) {

		previousItem.next = currentItem.next

		item = currentItem
		currentItem = null

	} else {

		previousItem = currentItem
		currentItem = currentItem.next
	}
   }

   return ( item )
}


function findList ( id )

{
   var item = this.next

   while ( item && item.id != id ) item = item.next

   return ( item )
}


function menuListItem ()

{
   this.next = null
   this.id = null
   this.handle = null

   this.insert = insertList
   this.remove = removeList
   this.find = findList
}


var pendingOpen = new menuListItem ()
var openMenus = new menuListItem ()
var pendingClose = new menuListItem ()


function showSubMenu ( id )

{
   var menuItem = pendingClose.remove ( id )

   if ( menuItem ) {

	clearTimeout ( menuItem.handle )
	menuItem.handle = null
	openMenus.insert ( menuItem )

   } else if ( ! pendingOpen.find ( id ) && ! openMenus.find ( id ) ) {

	menuItem = new menuListItem ()

	menuItem.id = id
	menuItem.handle = setTimeout ( "delayedShowSubMenu(\"" + id + "\")",
									500 )
	pendingOpen.insert ( menuItem )
   }
}


function delayedShowSubMenu ( id )

{
   var item = pendingOpen.remove ( id )

   if ( item ) {

	item.handle = null
	openMenus.insert ( item )

	document.getElementById(id).style.display = "block"
   }
}


function hideSubMenu ( id )

{
   var menuItem = pendingOpen.remove ( id )

   if ( menuItem ) clearTimeout ( menuItem.handle )
   else if ( menuItem = openMenus.remove ( id ) ) {

	menuItem.handle = setTimeout ( "delayedHideSubMenu(\"" + id + "\")",
									1000 )
	pendingClose.insert ( menuItem )
   }
}


function delayedHideSubMenu ( id )

{
   var item = pendingClose.remove ( id )

   if ( item ) document.getElementById(id).style.display = "none"
}

