var CurrentSubMenu = null;
var CurrentMenu = null;
var CurrentMenuList = [];
var CurrentSubMenuList = [];
var CurrentLevel = 0;
var GapHeight = 2; /* pixels */
var TimerID = null;
var WindowTimeoutSeconds = 6;

function ssm(theContainer,level)
{
	var theDocWidth = document.width;
	
	if( TimerID != null )
	{
		window.clearTimeout(TimerID);
		TimerID = null;
	}
	
	/* Erase old settings if we've moved outside the old tree */
	if( CurrentLevel > 0 )
	{
		if( !EraseOld(theContainer, level) )
		{
			return;
		}
	}
	TimerID = window.setTimeout("EraseAll()", WindowTimeoutSeconds * 1000);
	
	theSubMenuId = theContainer.id + '.menu';
	theSubMenu = document.getElementById(theSubMenuId);
	if( theSubMenu == null || theSubMenu == undefined )
	{
		PushMenuContext(theContainer, theSubMenu);
		return;
	}

	
	/* Render menu relative to parent menu */
	ParentPos = AbsolutePosition([0,0], theContainer);
	
	if( level == 0 )
	{
		theSubMenu.style.left = "" + ParentPos[0] + "px";
		theSubMenu.style.top = "" + (ParentPos[1] + theContainer.offsetHeight + GapHeight) + "px";
		theSubMenu.style.display = "";
	}else{
		theSubMenu.style.left = "" + (ParentPos[0] + theContainer.offsetWidth) + "px";
		theSubMenu.style.top = "" + (ParentPos[1]) + "px";
		theSubMenu.style.display = "";
	}
		
	/* ... but move it if it goes off the page */
	if( ParentPos[0] + theSubMenu.offsetWidth > theDocWidth )
	{
		theSubMenu.style.position = "absolute";
		theSubMenu.style.right = "10px";
		theSubMenu.style.left = "";
	}
	
	theContainer.style.background = '#CCFF99';
	PushMenuContext(theContainer, theSubMenu);
}

/* Update list of currently active menu roots and branches */
function PushMenuContext(theRoot, theLeaves)
{
	CurrentMenuList[CurrentLevel] = theRoot;
	CurrentSubMenuList[CurrentLevel] = theLeaves;
	CurrentLevel++;

}

function AbsolutePosition(CurrPos, anObject)
{
	CurrPos[0] += anObject.offsetLeft;
	CurrPos[1] += anObject.offsetTop;
	
	if( anObject.offsetParent != null && anObject.offsetParent != undefined )
		return( AbsolutePosition(CurrPos, anObject.offsetParent) );
	else
		return( CurrPos );
	
}

function EraseOld(NewRoot, level)
{
	if( NewRoot == CurrentMenuList[level] )
	{
		return(false); /* No updates needed */
	}
		
	while(--CurrentLevel >= level)
	{
		if( NewRoot == CurrentMenuList[CurrentLevel] )
		{
			CurrentLevel++;
			return(true);
		}
		CurrentMenuList[CurrentLevel].style.background = "";
		CurrentMenuList[CurrentLevel] = null;
		
		if( CurrentSubMenuList[CurrentLevel] != null )
		{
			CurrentSubMenuList[CurrentLevel].style.display = "none";
			CurrentSubMenuList[CurrentLevel] = null;
		}
	}
	CurrentLevel = level;
	return(true);
}

/*
 * EraseAll - Close all open menu items
 */
function EraseAll()
{
	var i;

	for(i=CurrentLevel-1; i >= 0; i-- )
	{
		CurrentMenuList[i].style.background = "";
		CurrentMenuList[i] = null;

		if( CurrentSubMenuList[i] != null )
		{
			CurrentSubMenuList[i].style.display = "none";
			CurrentSubMenuList[i] = null;
		}
	}
	CurrentLevel = 0;
}