// $Id: hoyaeu_topmenumgr.js,v 1.3 2005/07/20 15:25:19 richardw Exp $

function TopMenuManager(document) {
  if(document == null) window.document;
  if(document.menus == null) document.menus = [];
  this.myIndex = document.menus.length;
  document.menus[this.myIndex] = this;
  this.doc = document;
  this.last_ident = "";
  this.menus = [];
  this.active = document.getElementById ? true : false;
  
  this.indexByIdent = function (ident) {
    for(var i = 0; i < this.menus.length; i++) {
      if(this.menus[i].id == ident) return i;
    }
    return -1;
  }
  
  this.getSize = function (obj, alternate) {
    var result = { 
                   width : (alternate ? 0 : obj.offsetWidth),
                   height : (alternate ? 0 : obj.offsetHeight)
                 }
    if(obj.hasChildNodes()) {
      var cn = obj.childNodes;
      for(var i = 0; i < cn.length; i++) {
        if(cn[i].offsetWidth > result.width) result.width = cn[i].offsetWidth;
        if(cn[i].offsetHeight > result.height) result.height = cn[i].offsetHeight;
      }
    }
    return result;
  }
  
  this.getBounds = function (node, alternate) {
    var result = { left : 0,
                   top : 0,
                   right : 0,
                   bottom : 0,
                   width : 0,
                   height : 0,
                   scroll_x : this.doc.body.scrollLeft,
                   scroll_y : this.doc.body.scrollTop }
    
    if(this.doc.getElementById || this.doc.all) {
      var obj = node;
      while(obj.offsetParent) {
        result.left += obj.offsetLeft;
        result.scroll_x += obj.scrollLeft;
        result.top += obj.offsetTop;
        result.scroll_y += obj.scrollTop;
        obj = obj.offsetParent;
      }
    } else if(this.doc.layers) {
      result.left += obj.x;
      result.top += obj.y;
    }
   
    if(obj.hasChildNodes()) {
      var cn = obj.childNodes;
      for(var i = 0; i < cn.length; i++) {
        if(cn[i].offsetWidth > result.width) result.width = cn[i].offsetWidth;
        if(cn[i].offsetHeight > result.height) result.height = cn[i].offsetHeight;
      }
    }
    var size = this.getSize(node, alternate);
    result.width = size.width;
    result.height = size.height; 
    result.right = result.left + result.width - 1;
    result.bottom = result.top + result.height - 1;
    
    return result;
  }
  
  this.open_smenu = function (sender, sIdent) {
    if(!this.active) return;
    var oMenuA = this.doc.getElementById(sIdent + "_a");
    var oMenuB = this.doc.getElementById(sIdent + "_b");
    if(oMenuA && oMenuB) {
      var boundsSender = this.getBounds(sender.parentNode);
      oMenuA.style.left = "" + boundsSender.left + "px";
      oMenuA.style.top = "" + boundsSender.top + "px";
      oMenuA.style.display = "";
      var sizeMenuA = this.getSize(oMenuA);
      oMenuB.style.left = "" + boundsSender.left + "px";
      oMenuB.style.top = "" + (boundsSender.top + sizeMenuA.height) + "px";
      oMenuB.style.display = "";
      var boundsMenu = this.getSize(oMenuB);
      boundsMenu.width += sizeMenuA.height;
      boundsMenu.left = boundsSender.left;
      boundsMenu.top = boundsSender.top;
      boundsMenu.right = boundsMenu.left + boundsMenu.width - 1;
      boundsMenu.bottom = boundsMenu.top + boundsMenu.height - 1 + sizeMenuA.height;
      var found = false;
      for(var i = 0; i < this.menus.length; i++) {
        if(this.menus[i].ident == sIdent) {
          found = true;
          this.menus[i].used += 1;
          this.menus[i].bounds = boundsMenu;
        } else {
          this.menus[i].used = 0;
        }
      }
      if(!found) {
        this.menus[this.menus.length] = { ident : sIdent, used : 1, menuA : oMenuA, menuB: oMenuB, bounds : boundsMenu };
      }
    } else {
      for(var i = 0; i < this.menus.length; i++) {
        this.menus[i].used = 0;
      }
    }
    // this.refresh();
    this.showhide();
  }
  
  this.refresh = function () {
    if(!this.active) return;
    window.setTimeout("document.menus[" + this.myIndex + "].showhide();", 250);
  }
  
  this.showhide = function () {
    if(!this.active) return;
    var displayText;
    for(var i = 0; i < this.menus.length; i++) {
      displayText = this.menus[i].used > 0 ? "" : "none";
      if(this.menus[i].menuA.style.display != displayText) this.menus[i].menuA.style.display = displayText;
      if(this.menus[i].menuB.style.display != displayText) this.menus[i].menuB.style.display = displayText;
    }
  }
  
  this.show = function (ident) {
    if(!this.active) return;
    var i = this.indexByIdent(ident);
    if(i >= 0) {
      this.menus[i].used += 1;
      if(this.menus[i].used == 1) this.refresh();
    }
  }
  
  this.hide = function (ident) {
    if(!this.active) return;
    var i = this.indexByIdent(ident);
    if((i >= 0) && (this.menus[i].used > 0)) {
      this.menus[i].used -= 1;
      this.refresh();
    }
  }
  
  this.mousemove = function (x, y) {
    if(!this.active) return;
    for(var i = 0; i < this.menus.length; i++) {
      if(this.menus[i].used > 0) {
        if(   (x >= this.menus[i].bounds.left) && (x <= this.menus[i].bounds.right)
           && (y >= this.menus[i].bounds.top) && (y <= this.menus[i].bounds.bottom)) {
        } else {
          window.status =   "(" + x  + ", " + y + ") "
                          + "(" + this.menus[i].bounds.left + ", " + this.menus[i].bounds.top + ", " + this.menus[i].bounds.right + ", " + this.menus[i].bounds.bottom + ")";
          this.menus[i].used = 0;
          this.refresh();
        }
      }
    }
  }
}
