// Show submenu
function ShowSubmenu( pId, eMenuItem )
{
   eSubmenu = document.getElementById( 'Submenu' + pId );
   eMenuItem = document.getElementById( 'MenuItem' + pId );
   
   if ( eSubmenu != null )
   {
      // IE hack, whee!
      if ( eMenuItem != null && window.event && document.compatMode == 'BackCompat' )
         eSubmenu.style.marginLeft = '-' + ( eMenuItem.clientWidth + 2 ) + 'px';

      if ( eMenuItem.className != 'Hover' )
      {
         eMenuItem.originalClassName = eMenuItem.className;
         eMenuItem.className = 'Hover';
      }

      eSubmenu.className = 'Submenu';

   }
}

// Hide submenu
function HideSubmenu( pId )
{
   eSubmenu = document.getElementById( 'Submenu' + pId );
   eMenuItem = document.getElementById( 'MenuItem' + pId );
   
   if ( eSubmenu != null )
   {
      eSubmenu.className = 'Hidden';
      eSubmenu.style.marginLeft = '';
      eMenuItem.className = eMenuItem.originalClassName;
   }
}


function ToggleShowHide( eMenuItem )
{
   var eListItemElement = eMenuItem.parentNode;
   
   // Loop through list item element children.
   for ( var i=0; i<eListItemElement.childNodes.length; i++ )
   {
      var theChild = eListItemElement.childNodes[ i ];
      if ( theChild.tagName == "UL" )
      {
         if ( theChild.className == "Hidden" )
         {
            theChild.className = theChild.oldClassName;
            eMenuItem.innerHTML = '<img src="System/XPlorer/Images/Minus.png" alt="-">';
         }
         else
         {
            theChild.oldClassName = theChild.className;
            theChild.className = 'Hidden';
            eMenuItem.innerHTML = '<img src="System/XPlorer/Images/Plus.png" alt="+">';
         }
      }
   }
}

// Alert object with all it's properties
function AlertObject( object )
{
	var s='';
	for ( var key in object )
		s += key + ': ' + object[key] + '\n';
		
	alert( s );
}

window.onload = function() {
    var cListItems = document.getElementById("LeftmenuItems").getElementsByTagName("li");

    // Loop through list items
    for (var i = 0; i < cListItems.length; i++) {
        // Check whether item is an LI (firefox)
        if (cListItems[i].tagName == "LI") {
            var eListItem = cListItems[i];

            var mSublistFound = false;

            // Loop through LI childNodes, try to find a UL tag
            for (var j = 0; j < eListItem.childNodes.length; j++) {
                if (eListItem.childNodes[j].tagName == "UL") {
                    // Woohoo! We found a list !!!!1!!1! :D
                    mSublistFound = true;

                    break;
                }
            }

            // If no UL found...
            if (mSublistFound) {
                eListItem.getElementsByTagName("div")[0].innerHTML = '<img src="System/XPlorer/Images/Minus.png" alt="-">';

                // Give boldness to next sibling.
                if (eListItem.getElementsByTagName("a")[0].className != 'NoLink') {
                    eListItem.getElementsByTagName("a")[0].style.fontWeight = 'Bold';
                }

                // Collapse empty submenus.
                if (eListItem.getElementsByTagName("a")[0].className == 'NoLink') {

                    var mSelectedFound = false;

                    // Check if there's no selected subelement.
                    for (var j = 0; j < eListItem.getElementsByTagName("ul")[0].getElementsByTagName("a").length; j++) {
                        if (eListItem.getElementsByTagName("ul")[0].getElementsByTagName("a")[j].className == "Selected") {
                            mSelectedFound = true;
                            break;
                        }
                    }

                    // Collapse submenu when no selected subelement is found, otherwise give the menuitem boldness.
                    if (!mSelectedFound) {
                        ToggleShowHide(eListItem.getElementsByTagName("div")[0]);
                    } else {
                        eListItem.getElementsByTagName("a")[0].style.fontWeight = 'Bold';
                    }

                }

                // Improve indentation.
                if (eListItem.getElementsByTagName("ul")[0].className == "LeftmenuItemsSubSub" ||
                    eListItem.getElementsByTagName("ul")[0].className == "Hidden")
                {
                    eListItem.getElementsByTagName("div")[0].style.paddingLeft = '13px';
                }

            }
        }
    }
}