// xMenu2
// Copyright (c) 2002,2003 Michael Foster (mike@cross-browser.com)
// Copyright (c) 2003 Sebastien Georget (http://purl.org/NET/Georget/Sebastien)
// This code is distributed under the terms of the LGPL (gnu.org)
// CHANGES :
//   10/9/2003 - Sebastien Georget - Modifications for CPS

var menu1, inClass, outClass, oldLbl;
function resizeListener(e) {
  if (xOp5or6 || xNN4) location.replace(location.href);
  else {
    menu1.paint();
  }
}
function scrollListener(e) {
  var i,y;
  for (i=0; i < xFloatingMenus.length; ++i) {
    if (xFloatingMenus[i]) {
      xSlideTo('menu' + xFloatingMenus[i].n, xScrollLeft() + xFloatingMenus[i].x, xScrollTop() + xFloatingMenus[i].y, 500);
    }
  }
}

//// menu implementation

var xFloatingMenus = new Array(), xTotalMenus=0, xActiveMenu=null;

function xMenub(boxWidth, inClassParam, outClassParam) {
  // properties
  inClass= inClassParam;
  outClass = outClassParam;
  this.n = ++xTotalMenus;
  this.subs = xGetElementById('dyn_sub_hierarchies')
  this.boxW = xWidth(this.subs);
  this.active = null;
  // methods
  this.paint = function(menuX, menuY) {
    var i=1, x, y, mnu, lbl, box;
    mnu = xGetElementById('menu'+this.n);
    if (!mnu) return;
    xZIndex(mnu, this.z);

    lbl = xGetElementById('label'+this.n+""+i);
    while (lbl) {
      this.lblW = xWidth(lbl);
      this.lblH = xHeight(lbl);
      this.y = xPageY(lbl);
      xShow(lbl);
      lbl.menu = this;
      if (arguments.length==3) xAddEventListener(lbl, 'mouseover', menuShowListener, false);
      lbl.box = xGetElementById('box'+this.n+""+i);
      if (lbl.box) {
        xWidth(lbl.box, this.boxW);
        var bx, by;
        if (xPageX(lbl) + this.boxW > xScrollLeft() + xClientWidth()) { bx = x - (this.boxW - this.lblW); }
        else { bx = x; }
        if (xPageY(lbl) + this.lblH + xHeight(lbl.box) > xScrollTop() + xClientHeight()) { by = y - xHeight(lbl.box); }
        else { by = y + this.lblH; }
        xMoveTo(lbl.box, xPageX(this.subs), xPageY(this.subs));
        lbl.box.lbl = lbl;
        xZIndex(lbl, i);
        xZIndex(lbl.box, i);
        xHide(lbl.box);
      }
      if (lbl && lbl.childNodes.item(1).href == document.URL) {
        xShow(lbl.box);
        lbl.menu.active = lbl
      }
      lbl = xGetElementById('label'+this.n+""+(++i)); // for next iteration
    }
    xShow(mnu);
  }
  // constructor code
  this.paint(this.x, this.y, 'init');
  if (this.flt) xFloatingMenus[this.n-1] = this;
}
function menuShowListener(e) {
  var evt = new xEvent(e);
  var lbl = evt.target;
  while (lbl && !lbl.menu) {
    lbl = xParent(lbl);
  }
  if (lbl.className == outClass) {
    lbl.className = inClass;
//    alert('lbl.id: ' + lbl.id + ', inClass: ' + inClass + 'outClass: ' + outClass);
    if (oldLbl) { oldLbl.className = outClass; }
    oldLbl = lbl;
  }
  if (!lbl) return;
  var menu = lbl.menu;
  if (!menu) return;
  if (menu.active) {
    if (menu.active == lbl) return;
    xHide(menu.active.box);
  }
  //if (xActiveMenu && xActiveMenu != menu) { menuHide(xActiveMenu); }
  if (xNN4) { // hack!
    xResizeTo('menu'+menu.n,xClientWidth(),xClientHeight());
  }
  xShow(lbl.box);
  menu.active = lbl;
  xActiveMenu = menu;
}

function menuHide(menu) {
  if (!menu) menu = xActiveMenu;  // experiment
  if (!menu || !menu.active || !menu.active.box) return;
  
  xHide(menu.active.box);
  menu.active = null;
  xActiveMenu = null;
  if (xNN4) { // hack!
    xResizeTo('menu'+menu.n,xClientWidth(),menu.lblH + 2);
  }
}

function xName(e) {
  if (!e) return e;
  else if (e.id && e.id != "") return e.id;
  else if (e.nodeName && e.nodeName != "") return e.nodeName;
  else if (e.tagName && e.tagName != "") return e.tagName;
  else return e;
}




