/**********************
 * SUBMENU JAVASCRIPT *
 **********************/

// Checks if submenu items fit to the height of the submenu
function CollapseSubmenusIfNotVisible()
{
    var submenuDiv = document.getElementById('submenu');
    
    // Check if a submenu exists    
    if (submenuDiv == null)
	return;
    
    var submenuItems = submenuDiv.childNodes;
    
    var submenuContainerOffset = submenuDiv.offsetTop;
    var submenuContainerHeight = submenuDiv.offsetHeight;
    var collapse = false;
        
    for (menuitemCnt = 0; menuitemCnt < submenuItems.length; menuitemCnt++)
    {
		var submenu = submenuItems[menuitemCnt];
	    
		if (submenu.nodeName == 'UL')
		{
			//check if a previous menu was collapsed, in that case, collapse the rest
			if(collapse == true)
			{
				HideSubmenu(submenu);
			}
			else
			{
				// Check if item is visible, by checking if the _bottom_ of the
				// submenu falls within the submenucontainer height
				var submenuItemOffset = submenu.offsetTop;
				var submenuItemHeight = submenu.offsetHeight;
		    	
				var submenuBottom =
				submenuItemOffset + submenuItemHeight
					- submenuContainerOffset
					+ 50; // extra buffer for shadow and title overhead
		    	
				if (submenuBottom > submenuContainerHeight)
				{
					HideSubmenu(submenu);
					collapse = true;
				}
			}	
		}
    }    
    
    // Make the (entire) submenu expand and collapse along with the submenuitems
    submenuDiv.style.height = 'auto';
    submenuDiv.style.overflow = '';
}

// Expands a submenu item if it is collapsed,
// collapses a submenuitem if a submenuitem is expanded
function HideOrUnhideSubmenu(submenuitem)
{
    var links = submenuitem.childNodes[1];
    
    if (links.style.display == 'none')
    {
	UnhideSubmenu(submenuitem);
    }
    else
    {
	HideSubmenu(submenuitem);
    }
}

// Collapses a submenu item
function HideSubmenu(submenuitem)
{
    // path to submenu ul: submenuitem
    // path to submenutitle: submenuitem.children(0)
    // path to submenutitle arrow: submenuitem.children(0).children(0)
    // path to links ul: submenuitem.children(1).children(0)
    // path to submenulink li: submenuitem.children(1).children(0).children(x)

    var links = submenuitem.childNodes[1];
    var arrow = submenuitem.childNodes[0].childNodes[0];
    
    links.style.display = 'none';

    arrow.className = 'submenuarrowcollapsed';
}

// Expands a submenu item
function UnhideSubmenu(submenuitem)
{
    // path to submenu ul: submenuitem
    // path to submenutitle: submenuitem.children(0)
    // path to submenutitle arrow: submenuitem.children(0).children(0)
    // path to links ul: submenuitem.children(1).children(0)
    // path to submenulink li: submenuitem.children(1).children(0).children(x)

    var links = submenuitem.childNodes[1];
    var arrow = submenuitem.childNodes[0].childNodes[0];

    links.style.display = 'block';

    arrow.className = 'submenuarrow';
}

// Matches the length of the submenu with that of the content, so that
// submenu items can be collapsed if it makes the submenu exceed the
// length of the content
function CorrectSubmenuLength()
{    
    var midsection = window.document.getElementById('innercontent');
    var submenu = window.document.getElementById('submenu');

    if (midsection != null && submenu != null)
    {
        submenu.style.display = 'none';
        midsection.style.height = 'auto';
	
	submenu.style.overflow = 'hidden';
	
	submenu.style.height = midsection.offsetHeight + 'px';
	submenu.style.display = 'inline';
    }
}

// Fire these functions _after_ the page content had finished loading (and rendering)
addFunction(CorrectSubmenuLength);
addFunction(CollapseSubmenusIfNotVisible);

