// xMenu6 r3, Copyright 2006-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xMenu6(sUlId, sMainUlClass, sSubUlClass, sLblLiClass, sItmLiClass, sPlusImg, sMinusImg, sImgClass, sItmPadLeft, bLblIsItm) // Object Prototype
{
  // Public Property
  this.id = sUlId;
  // Private Event Listener
  function click(e)
  {
    if (this.xmChildUL) { // 'this' points to the A element clicked
      var s, uls = this.xmChildUL.style;
      if (uls.display != 'block') { // close sub-menu
        s = sMinusImg;
        uls.display = 'block';
        xWalkUL(this.xmParentUL, this.xmChildUL,
          function(p,li,c,d) {
            if (c && c != d && c.style.display != 'none') {
              if (sPlusImg) {
                var a = xFirstChild(li,'a');
                xFirstChild(a,'img').src = sPlusImg;
              }
              c.style.display = 'none';
            }
            return true;
          }
        );
      }
      else { // open sub-menu
        s = sPlusImg;
        uls.display = 'none';
      }
      if (sPlusImg) {
        xFirstChild(this,'img').src = s;
      }
      e = e || window.event;
      var t = e.target || e.srcElement;
      if (t.nodeName.toLowerCase() != 'img' && bLblIsItm) {
        return true; // click was on a label and bLblIsItm is true
      }
      return false; // click was on a label and bLblIsItm is false
    }
    return true; // click was on an item
  }
  // Constructor Code
  var ul = xGetElementById(sUlId);
  ul.className = sMainUlClass;
  xWalkUL(ul, null,
    function(p,li,c) {
      var liCls = sItmLiClass;
      var a = xFirstChild(li,'a');
      if (a) {
        var m = 'Click to toggle sub-menu';
        if (c) { // this LI is a label which precedes the submenu c
          if (sPlusImg) {
            // insert the image as the firstChild of the A element
            var i = document.createElement('img');
            i.title = m;
            a.insertBefore(i, a.firstChild);
            i.src = sPlusImg;
            i.className = sImgClass;
          }
          liCls = sLblLiClass;
          c.className = sSubUlClass;
          c.style.display = 'none';
          a.title = bLblIsItm ? 'Click to follow link' : m;
          a.xmParentUL = p;
          a.xmChildUL = c;
          a.onclick = click;
        }
        else if (sPlusImg) { // this LI is not a label but is an item
          // if we are inserting images in label As then give A items some left padding
          a.style.paddingLeft = sItmPadLeft;
        }
      }
      li.className = liCls;
      return true;
    }
  );
} // end xMenu6 prototype
// xMenu6 Public Methods
xMenu6.prototype.unload = function()
{
  xWalkUL(xGetElementById(this.id), null,
    function(p,li,c) {
      var a = xFirstChild(li,'a');
      if (a && c) { a.xmParentUL = a.xmChildUL = a.onclick = null; }
      return true;
    }
  );
};
/* not yet finished
xMenu6.prototype.open = function(s)
{
  var i, n, a = s.split(',');
  var ul = xGetElementById(this.id);
  n = parseInt(a[0]);
  for (i = 0; i < n; ++i) {

  }
};
*/
