/*****************************************************************\
* Product:   JavaScript Framework                                 *
* File:      menu.js                                              *
* Author:    Ties Pull ter Gunne                                  *
* Copyright: (c) 1999 Ties Pull ter Gunne                         *
* License:   No Restrictions                                      *
\*****************************************************************/

//=============== class MenuItem ======================

function MenuItem(container,name,label,action,menu) {
  // properties
  this.container = container;
  this.name = name;
  this.label = label;
  this.menu = menu;
  this.action = action;
  this.align = "right";
  this.valign = "top";
  this.menuMarker = "&raquo;";
  this.separator = "\n<HR>";

  if (menu != null) {
    this.menu.activator = this;
  }

}

MenuItem.prototype = new Element;


MenuItem.prototype.stop = function() {
  this.container.stop();
}

MenuItem.prototype.activate = function() {
  this.style.backgroundColor = this.guard.activeBackground;
  this.style.color = this.guard.activeColor;
  var cont = this.guard.container;
  if (cont.activeItem != null) {
    cont.activeItem.elem.onmouseout();
    cont.activeItem = null;
  }
  this.guard.container.activeItem = this.guard;
  if (this.guard.menu != null) {
    if (this.guard.timeout != null) {
      clearTimeout(this.guard.timeout);
      this.guard.timeout = null;
    } else {
      this.guard.timeout = setTimeout( 
        "var elm = document.getElementById('" + this.guard.name + "').guard;" +
        "elm.menu.placeNear(elm);" +
        "elm.timeout = null;",300);
    }
  }
}

MenuItem.prototype.inactivate = function() {
  this.style.backgroundColor = this.guard.backgroundColor;
  this.style.color = this.guard.color;
  this.guard.container.activeItem = null;
  if (this.guard.menu != null) {
    if (this.guard.timeout != null) {
      clearTimeout(this.guard.timeout);
      this.guard.timeout = null;
    } else {
      this.guard.timeout = setTimeout( 
        "var elm = document.getElementById('" + this.guard.name + "').guard;" +
        "elm.menu.hide();" +
        "elm.timeout = null;",300);
    }
  }
}

MenuItem.prototype.click = function(evt) {
  if (this.guard.menu == null) {
    this.guard.container.stop();
  }
  if (this.guard.action != null) {
    location = this.guard.action;
  }
  evt.cancelBubble = true;
}

MenuItem.prototype.init = function() {
  if (this.name != "-") {
    this.elem = document.getElementById(this.name);
    this.elem.guard = this;
    this.baseElem = this.elem;
    var stl = this.elem.style;
    stl.backgroundColor = this.backgroundColor;
    stl.color = this.color;
    stl.cursor = 'pointer';
    this.elem.onmouseover = this.activate;
    this.elem.onmouseout = this.inactivate;
    this.elem.onclick = this.click;
    if (this.menu != null) {
      this.menu.init();
      this.menu.activator = this;
    }
  }
}

MenuItem.prototype.locate = function() {
  if (this.name != "-") {
    document.write("\n<TR ID=\"" + this.name + "\">\n<TD>&nbsp;&nbsp;" + this.label + "&nbsp;&nbsp;\n</TD>\n<TD>");
    if (this.menu != null) {
      document.write("&nbsp;"+this.menuMarker+"&nbsp;");
    } else {
      document.write("&nbsp;&nbsp;&nbsp;");
    }
    document.write("\n</TD>\n</TR>");
  } else {
    document.write("\n<TR>\n<TD ColSpan=2>"+this.separator+"\n</TD>\n</TR>");
  }
}

//=========== class Menu ======================

function Menu(name) {
  // properties
  this.activator = null;
  this.name = name;
  this.items = [];
  this.color = "#000000";
  this.backgroundColor = "#C0C0C0";
  this.borderColor = "#E0E0E0";
  this.activeBackground = "#4444DD";
  this.activeColor = "#FFFFFF";
  this.visible = false;
  this.count = 0;
  this.align = "right";
  this.valign = "top";
  this.menuMarker = "&raquo;";
  this.separator = "\n<HR>";
  this.border = "2px";
  this.borderStyle = "outset";
  
}
Menu.prototype = new Element;

Menu.prototype.getBorder = function() {
  var b = this.border.replace('p[x|t]','');
  return Number(b);
}

Menu.prototype.validate = function() {
  this.inheritStyle(this);
  document.write('\n<STYLE Type="text/css">\n');
  document.write('  .Menu {\n');
  document.write('    position:absolute;\n');
  document.write('    border:'+this.border+';\n');
  document.write('    border-style:'+this.borderStyle+';\n');
  document.write('    visibility:hidden;\n');
  document.write('    color:'+this.color+';\n');
  document.write('    background-color:'+this.backgroundColor+';\n');
  document.write('    border-color:'+this.borderColor+';\n');
  if (this.fontFamily != null)
    document.write('    font-family:'+this.fontFamily+';\n');
  document.write('  }\n');
  document.write('\n</STYLE>\n');
}

Menu.prototype.inheritStyle = function(iets) {
  this.color = iets.color;
  this.backgroundColor = iets.backgroundColor;
  this.borderColor = iets.borderColor;
  this.activeBackground = iets.activeBackground;
  this.activeColor = iets.activeColor;
  this.align = iets.align;
  this.valign = iets.valign;
  this.menuMarker = iets.menuMarker;
  this.separator = iets.separator;
  this.borderStyle = iets.borderStyle;
  this.border = iets.border;
  if (iets.fontFamily != null) {
    this.fontFamily = iets.fontFamily;
  }
  var i = 0;
  for (i = 0; i < this.count; i++) {
    this.items[i].color = iets.color;
    this.items[i].backgroundColor = iets.backgroundColor;
    this.items[i].borderColor = iets.borderColor;
    this.items[i].activeBackground = iets.activeBackground;
    this.items[i].activeColor = iets.activeColor;
    this.items[i].align = iets.align;
    this.items[i].valign = iets.valign;
    this.items[i].menuMarker = iets.menuMarker;
    this.items[i].separator = iets.separator;
    if (this.items[i].menu != null) {
      this.items[i].menu.inheritStyle(iets);
    }
  }  
}

Menu.prototype.stop = function() {
  this.activator.stop();
}

Menu.prototype.init = function() {
  this.elem = document.getElementById(this.name);
  this.baseElem = this.elem;
  this.elem.guard = this;
  this.elem.style.width = this.elem.scrollWidth;
  this.elem.onmouseover = this.holdActivator;
  var i = 0;
  for (i = 0; i < this.count; i++) {
    this.items[i].init();
  }  
}

Menu.prototype.holdActivator = function() {
  var act = this.guard.activator;
  if (act != null) {
    if (act.timeout != null) {
      clearTimeout(act.timeout);
      act.timeout = null;
    }
    if (act.container != null) {
      if (act.container.activeItem != null
        && act.container.activeItem != act.elemAct) {
        act.elem.style.backgroundColor = act.activeBackground;
        act.elem.style.color = act.activeColor;
      }
      act.container.activeItem = act;
    }
  }
}

Menu.prototype.locate = function() {
  document.write("\n<DIV Class=\"Menu\" ID=\"" + this.name + "\">");
  document.write("\n<TABLE CellPadding=2 CellSpacing=0>");
  var i = 0;
  for (i = 0; i < this.count; i++) {
    this.items[i].locate();
  }
  document.write("\n</TABLE>");
  document.write("\n</DIV>");
  for (i = 0; i < this.count; i++) {
    if (this.items[i].menu != null) {
      this.items[i].menu.locate();
    }
  }
}

Menu.prototype.hide = function() {
  var act = this.activeItem;
  if (act != null) {
    if (act.menu != null) {
      act.menu.hide();
      if (act.timeout != null) {
        clearTimeout(act.timeout);
        act.timeout = null;
      }
    }
    act.elem.style.backgroundColor = act.backgroundColor;
    act.elem.style.color = act.color;
    this.activeItem = null;
  }
  this.elem.style.visibility = 'hidden';
}

Menu.prototype.show = function(x,y) {
  this.elem.style.visibility = 'visible';
  this.elem.style.left=x;
  this.elem.style.top=y;
}

Menu.prototype.calculateLeft = function(iets) {
  var nr;
  var left;
  var right
  if (iets.container == null  // it's not a menu item
    && iets.alignLeft()) {
    left = iets.getLeft();
    right = iets.getRight() - this.getWidth();
  } else {
    left = iets.getLeft() - this.getWidth();
    right = iets.getRight();
  }
  if (iets.alignLeft()) {
    if (iets.container != null) {  // it's a menu item
      if (left < winsize.getXOffset()
        && right+this.getWidth() <= winsize.getXOffset()+winsize.getScrollWidth()) {
        nr = right;
      } else {
        nr = left;
      }
    } else {
      if (left+this.getWidth() > winsize.getXOffset()+winsize.getScrollWidth()
        && right >= winsize.getXOffset()) {
        nr = right;
      } else {
        nr = left;
      }
    }
  } else {
    if (left >= winsize.getXOffset()
      && right+this.getWidth() > winsize.getXOffset()+winsize.getScrollWidth()) {
      nr = left;
    } else {
      nr = right;
    }
  }
  return nr;
}

Menu.prototype.calculateTop = function(iets) {
  var nr;
  var up;
  var down;
  if (iets.alignLeft() && iets.container == null) { // not when menu item
    up = iets.getTop() - this.getHeight();
    down = iets.getBottom();
  } else {
    up = iets.getTop();
    down = iets.getBottom()-this.getHeight(); 
  }
  if (iets.alignLeft() && iets.container == null) { // not when menu item
    if (iets.alignTop()) {
      if (up < winsize.getYOffset()
        && down + this.getHeight() <= winsize.getYOffset()+winsize.getScrollHeight()) {
        nr = down;
      } else {
        nr = up;
      }
    } else {
      if (up >= winsize.getYOffset()
        && down + this.getHeight() > winsize.getYOffset()+winsize.getScrollHeight()) {
        nr = up;
      } else {
        nr = down;
      }
    }
  } else {
    if (iets.alignTop()) {
      if (up+this.getHeight() > winsize.getYOffset()+winsize.getScrollHeight()) {
        if (down > winsize.getYOffset()) {
          nr = down;
        } else {
          nr = winsize.getYOffset()+winsize.getScrollHeight()-this.getHeight();
        }
      } else {
        nr = up;
      }
    } else {
      if (down < winsize.getYOffset()) {
        if (up+this.getHeight() <= winsize.getYOffset()+winsize.getScrollHeight()) {
          nr = up;
        } else {
          nr = winsize.getYOffset();
        }
      } else {
        nr = down;
      }
    }
  }
  return nr;
}

Menu.prototype.placeNear = function(iets) {
  this.show(this.calculateLeft(iets),this.calculateTop(iets));
}

Menu.prototype.addItem = function(name,label,action) {
  this.items[this.count++] = new MenuItem(this,name,label,action,null);
}

Menu.prototype.addSeparator = function() {
  this.items[this.count++] = new MenuItem(this,"-",null,null,null);
}

Menu.prototype.addSubmenu = function(name,label,menu) {
  this.items[this.count++] = new MenuItem(this,name,label,"javascript:;",menu);
}


