// xmenu5.js, part of X, a Cross-Browser.com Javascript Library
// Copyright (C) 2004,2005 Michael Foster - Distributed under the terms of the GNU LGPL - OSI Certified
// File Rev: 5


/***
  xMenu5
  Only one branch per level can be open.
  Assumes btnClass element to be a direct child of an LI,
  tho it doesn't have to be the first child.
  Assumes the btn's corresponding UL to be a sibling,
  tho it doesn't have to be the next sibling.
***/

function xMenu5(idUL, btnClass, idAutoOpen) // object prototype
{
  // Constructor

  var i, ul, btns, mnu = xGetElementById(idUL);
  btns = xGetElementsByClassName(btnClass, mnu, 'DIV');
  for (i = 0; i < btns.length; ++i) {
    ul = xNextSib(btns[i], 'UL');
    btns[i].xClpsTgt = ul;
    btns[i].onclick = btn_onClick;
    set_display(btns[i], 0);
  }
  if (idAutoOpen) {
    var navSpans = xGetElementsByTagName("SPAN", mnu);
    
    var i;
    for (i = 0; i < navSpans.length; i++) { 
		if (navSpans[i].innerHTML.toLowerCase() == idAutoOpen) {    
			break;
		}
	}
	
	if (i < navSpans.length) {
	    var e = navSpans[i].parentNode.parentNode.getElementsByTagName("DIV")[0];
    
        while (e && e != mnu) {
            if (e.xClpsTgt) set_display(e, 1);
            while (e && e != mnu && e.nodeName != 'LI') e = e.parentNode;
            e = e.parentNode; // UL
            while (e && !e.xClpsTgt) e = xPrevSib(e);
        }
    }
}

  // Private
  
  function btn_onClick()
  {
    var thisLi, fc, pUl;
    if (this.xClpsTgt.style.display == 'none') {
      set_display(this, 1);
      // get this label's parent LI
      var li = this.parentNode;
      thisLi = li;
      pUl = li.parentNode; // get this LI's parent UL
      li = xFirstChild(pUl); // get the UL's first LI child
      // close all labels' ULs on this level except for thisLI's label
      while (li) {
        if (li != thisLi) {
          fc = xFirstChild(li);
          if (fc && fc.xClpsTgt) {
            set_display(fc, 0);
          }
        }
        li = xNextSib(li);
      }
    }  
    else {
      set_display(this, 0);
    }
  }

  function set_display(ele, bBlock)
  {
    if (bBlock) {
      ele.xClpsTgt.style.display = 'block';
      ele.innerHTML = '-';
    }
    else {
      ele.xClpsTgt.style.display = 'none';
      ele.innerHTML = '+';
    }
  }

  // Public

  this.onUnload = function()
  {
    for (i = 0; i < btns.length; ++i) {
      btns[i].xClpsTgt = null;
      btns[i].onclick = null;
    }
  }
} // end xMenu5 prototype


/***
  xMenu5 Procedural.
  This was my first implementation of this menu.
  It is a procedural implementation, instead of object-oriented.
  This code is not used in this demo.

function xMenu5_onLoad(id)
{
  var i, lbl, mnu = xGetElementById(id);
  var ul = xGetElementsByTagName('UL', mnu);
  for (i = 0; i < ul.length; ++i) {
    ul[i].style.display = 'none';
    lbl = xPrevSib(ul[i]);
    lbl.xClpsTgt = ul[i];
    lbl.onclick = xMenu5_lbl_onClick;
  }
}
function xMenu5_lbl_onClick()
{
  if (this.xClpsTgt.style.display == 'none') {
    this.xClpsTgt.style.display = 'block';
  }  
  else {
    this.xClpsTgt.style.display = 'none';
  }
}
function xMenu5_onUnload(id)
{
  var i, lbl, mnu = xGetElementById(id);
  var ul = xGetElementsByTagName('UL', mnu);
  for (i = 0; i < ul.length; ++i) {
    lbl = xPrevSib(ul[i]);
    lbl.xClpsTgt = null;
    lbl.onclick = null;
  }
}
***/
