// xMenu2
// Copyright (c) 2002,2003 Michael Foster (mike@cross-browser.com)
// This code is distributed under the terms of the LGPL (gnu.org)

var menu1;
window.onload = function() {
  menu1 = new xMenu2(
    false, true, false,					// absolute, horizontal, floating,
    0, 0, 3,							// menuX, menuY, menuZ
    0, [95,110,100,110,125,110], 31,	// lblOffset, lblWidthsArray, lblHeight,
    [95,110,100,110,125,110],			// boxWidthsArray,
    '#FFF', '#FFF',				// activeColor, inactiveColor,
    'transparent', 'transparent',				// activeBkgnd, inactiveBkgnd
    '#135d08'							// boxBkgnd
  );
  
  scrollListener(); // initial slide
  xAddEventListener(document, "mousemove", menuHideListener, false);
  xAddEventListener(window, "resize", resizeListener, false);
  xAddEventListener(window, "scroll", scrollListener, false);
}
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 xMenu2(
  absolute, horizontal, floating, menuX, menuY, menuZ, lblOffset,
  lblWidthsArray, lblHeight, boxWidthsArray,
  activeColor, inactiveColor,
  activeBkgnd, inactiveBkgnd,
  boxBkgnd
) {
  // properties
  this.n = ++xTotalMenus;
  this.abs = absolute;
  this.hz = horizontal;
  this.flt = floating;
  this.x = menuX;
  this.y = menuY;
  this.z = menuZ;
  this.lblW = lblWidthsArray;
  this.lblH = lblHeight;
  this.lblOfs = lblOffset;
  this.boxW = boxWidthsArray;
  this.ac = activeColor;
  this.ic = inactiveColor;
  this.ab = activeBkgnd;
  this.ib = inactiveBkgnd;
  this.bb = boxBkgnd;
  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);
    if (this.hz) {
      y = 0;
      x = this.lblOfs;
    }
    else {
      y = this.lblOfs;
      x = 0;
    }
    if (arguments.length > 1) {
      this.x = menuX;
      this.y = menuY;
    }
    if (this.abs) {
      if (arguments.length > 1) {
        if (this.flt) xMoveTo(mnu, xScrollLeft() + menuX, xScrollTop() + menuY);
        else xMoveTo(mnu, menuX, menuY);
      }
    }
    lbl = xGetElementById('label'+this.n+""+i);
    while (lbl) {
      xResizeTo(lbl, this.lblW[i-1], this.lblH);
      xMoveTo(lbl, x, y);
      xColor(lbl, this.ic);
      xBackground(lbl, this.ib);
      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[i-1]);
        var bx, by;
        if (this.hz) { // horizontal
		
          if (xPageX(lbl) + this.boxW[i-1] > xScrollLeft() + xClientWidth()) { bx = x - (this.boxW[i-1] - this.lblW[i-1]); }
          else { bx = x - (this.boxW[i-1] - this.lblW[i-1]); }
	      by = y + this.lblH; 
 
        }
        else { // vertical
          if (xPageX(lbl) + this.lblW[i-1] + this.boxW[i-1] > xScrollLeft() + xClientWidth()) { bx = x - this.boxW[i-1]; }
          else { bx = x + this.lblW[i-1]; }
          if (xPageY(lbl) + xHeight(lbl.box) > xScrollTop() + xClientHeight()) { by = y + this.lblH - xHeight(lbl.box); }
          else { by = y; }
        }
        xMoveTo(lbl.box, bx, by);
        lbl.box.lbl = lbl;
        xZIndex(lbl, i);
        xZIndex(lbl.box, i);
        xBackground(lbl.box, this.bb);
        xHide(lbl.box);
      }
      if (this.hz) x += this.lblW[i-1];
      else y += this.lblH;
      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) return;
  var menu = lbl.menu;
  if (!menu) return;
  if (menu.active) {
    if (menu.active == lbl) return;
    xHide(menu.active.box);
    xColor(menu.active, menu.ic);
    xBackground(menu.active, menu.ib);
    if (menu.active.box.lbl.className) menu.active.box.lbl.className = 'mLabel'; // experiment
  }
  if (xActiveMenu && xActiveMenu != menu) { menuHide(xActiveMenu); }
  if (menu.hz && xNN4) { // hack!
    xBackground('menu'+menu.n,null);
    xResizeTo('menu'+menu.n,xClientWidth(),xClientHeight());
  }
  if (lbl.className) lbl.className = 'mLabelOver'; // experiment
  xShow(lbl.box);
  xColor(lbl, menu.ac);
  xBackground(lbl, menu.ab);
  menu.active = lbl;
  xActiveMenu = menu;
}
var tmr;
function menuHideListener(e) {
  var evt = new xEvent(e);
  var ele = evt.target;
  while (ele && !ele.lbl && !ele.box) { ele = xParent(ele); }
  if (xActiveMenu && xActiveMenu.active && !ele && !tmr) tmr = setTimeout('menuHide()', 500);  // experiment
  else if (ele && tmr) {clearTimeout(tmr); tmr = null;}  // experiment
}
function menuHide(menu) {
  
  if (!menu) menu = xActiveMenu;  // experiment
  if (!menu || !menu.active || !menu.active.box) return;
  
  xHide(menu.active.box);
  xColor(menu.active, menu.ic);
  xBackground(menu.active, menu.ib);
  if (menu.active.box.lbl.className) menu.active.box.lbl.className = 'mLabel'; // experiment
  menu.active = null;
  xActiveMenu = null;
  if (menu.hz && xNN4) { // hack!
    xResizeTo('menu'+menu.n,xClientWidth(),menu.lblH + 2);
    xBackground('menu'+menu.n, menu.ib);
  }
}

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;
}

