LoadManager.registerloadfunction(initmenus);

function initmenus() {
 initmenu('pageblock',document.getElementById('currentpagelink').getAttribute("value"),"white");
 initmenu('folderblock',document.getElementById('currentfolderlink').getAttribute("value"),"#ffffcf");
 inittabs();
 initopenmenu('foldermenublock');
}

function inittabs() {
 var theSpecElement=document.getElementById("tabselection"),theTabElement;
 if (!theSpecElement) return;
 theTabElement=document.getElementById(theSpecElement.value);
 if (theTabElement) {
  theTabElement.className="selectedtab";
 }
}

function initopenmenu(blockname) {
 var theBlockElement=document.getElementById(blockname);
 if (!theBlockElement) return;
 var thelist=theBlockElement.getElementsByTagName('li');
 var theLength=thelist.length,theSublist,theElement,theParent,theChild;
 for (var i=0;i<theLength;i++) {
  theElement=thelist[i];
  theSublist=theElement.getElementsByTagName('ul');
  var theNewNode = document.createElement('div');
  if (theSublist.length>0) {
    theNewNode.style.backgroundImage='url('+MenuPackRepository+'arrowdown.gif)';
  } else {
    theNewNode.style.backgroundImage='url('+MenuPackRepository+'disc.gif)';
  }
  theNewNode.className='icon';
  theElement.insertBefore(theNewNode,theElement.firstChild);
 }
}

function initmenu(blockname,hrefspec,theColor) {
 var theBlock=document.getElementById(blockname),thelist;
 var theLength,theSublist,theElement,theParent,theChild;
 if (!theBlock) return;
 // Step 1. Create an icon for each menu item
 thelist=theBlock.getElementsByTagName('li');
 theLength=thelist.length;
 for (var i=0;i<theLength;i++) {
  theElement=thelist[i];
  theSublist=theElement.getElementsByTagName('ul'); //get list of sub-menus
  if (!(theElement.firstChild.tagName=='DIV')) { //place an icon at the left position of menu item
   var theNewNode = document.createElement('div');
   if (theSublist.length>0) { //for a submenu place plus icon
    theNewNode.style.backgroundImage='url('+MenuPackRepository+'plus.gif)';
    theNewNode.setAttribute('dlmenustatus',"closed");
    theNewNode.style.cursor='pointer';
    theNewNode.onclick=togglesubmenu;
   } else { // if no submenu place a leaf icon
    theNewNode.style.backgroundImage='url('+MenuPackRepository+'disc.gif)';
    theNewNode.setAttribute('dlmenustatus',"leaf");
   }
   theNewNode.className='icon';
   theElement.insertBefore(theNewNode,theElement.firstChild);
  }
 }
 // Step 2. Find and highlight the currently selected menu item
 thelist=document.getElementById(blockname).getElementsByTagName('a');
 theLength=thelist.length
 for (var i=(theLength-1);i>=0;i--) {
  theElement=thelist[i];
  if (theElement.href==hrefspec) { //selected menu item is found
   //mark selected item
   theElement.className = "selected";
   theElement.style.backgroundColor=theColor;
   theParent=theElement.parentNode;
   // find the enclosing UL to open it
   while (!(theParent.tagName=="LI")) {
    theParent=theParent.parentNode;
   }
   for (var i=0;i<theParent.childNodes.length;i++) {
    theChild=theParent.childNodes[i];
    if (theChild.tagName=='UL') {
     theChild.style.display='block';
     theParent.firstChild.style.backgroundImage='url('+MenuPackRepository+'arrowdown.gif)';
     theParent.firstChild.setAttribute('dlmenustatus',"selectionnode");
     theParent.firstChild.style.cursor='default';
	 break;
    }
   }
   // open any ancestor UL's
   var theTestElement=theElement;
   while (!((theTestElement.id) && (theTestElement.id==blockname))) {
    theTestElement=theTestElement.parentNode;
    if (theTestElement.tagName=='UL') {
     theTestElement.style.display='block';
     theParent=theTestElement.parentNode;
     if (theParent.tagName=="LI") {
      theParent.firstChild.style.backgroundImage='url('+MenuPackRepository+'arrowdown.gif)';
      theParent.firstChild.setAttribute('dlmenustatus',"selectionnode");
      theParent.firstChild.style.cursor='default';
     }
    }
   }
   break;
  }
 }
}

function togglesubmenu(evt) {
 var theElement=this;
 var theParent=theElement.parentNode,theChild;
 var theDisplayValue;
 if (theElement.getAttribute('dlmenustatus')=='open') {
  theElement.setAttribute('dlmenustatus','closed');
  theElement.style.backgroundImage='url('+MenuPackRepository+'plus.gif)';
  theDisplayValue='none';
 } else if (theElement.getAttribute('dlmenustatus')=='closed'){
  theElement.setAttribute('dlmenustatus','open');
  theElement.style.backgroundImage='url('+MenuPackRepository+'minus.gif)';
  theDisplayValue='block';
 } else return; // eg. if the node dlmenustatus="selectionnode"
 for (var i=0;i<theParent.childNodes.length;i++) {
  theChild=theParent.childNodes[i];
  if (theChild.tagName=='UL') {
   theChild.style.display=theDisplayValue;
  }
 }
}

