function menu_object(obj, vert, separator) {
  this.obj = obj;
  this.vert = vert;
  this.separator = separator;
  
  var tbl;
  tbl = document.createElement('table');
  tbl.border = 0;
  tbl.cellSpacing = 0;
  tbl.cellPadding = 0;
  obj.appendChild(tbl);
  this.tbl = tbl;
  this.hide_timeout = 1000;

  this.border_bottom = 2;
  this.border_left = 2;
  this.border_right = 2;
  this.border_top = 2;
  
  this.root = new menu_item(this, null, null, '', '', '', '');
  this.root.level = -1;
}

menu_object.prototype.add_item = function(name, title, url, target) {
  var tr, td;
  if (this.vert) {
    tr = this.tbl.insertRow(this.tbl.rows.length);
    td = tr.insertCell(tr.cells.length);
  } else {
    if (this.tbl.rows.length == 0) { this.tbl.insertRow(0); }
    tr = this.tbl.rows[this.tbl.rows.length - 1];
    if (tr.cells.length > 0) {
      td = tr.insertCell(tr.cells.length);
      td.className = 'menu_item';
      td.innerHTML = this.separator;
    }
    td = tr.insertCell(tr.cells.length);
  }
  td.style.background = '#92C4E7';
  var item = new menu_item(this, this.root, td, name, title, url, target);
  if (this.vert) { item.drop_down = false; }
  else { item.drop_down = true; }
  this.root.items[this.root.items.length] = item;
  return item;
}

function menu_item(menu, parent, obj, name, title, url, target) {
  this.menu = menu;
  this.parent = parent;
  if (this.parent) { this.parent.items[this.parent.items.length] = this; }
  if (this.parent) { this.id = this.parent.id + '_' + this.parent.items.length; }
  else { this.id = '0'; }
  if (this.parent) { this.level = this.parent.level + 1; }
  else { this.level = 0; }

  this.obj = obj;
  this.name = name;
  this.title = title;
  if (url == '') { this.url = '_self'; }
  else { this.url = url; }
  this.target = target;

  var tbl, tr, td, img;
  tbl = document.createElement('table');
  tbl.border = 0;
  tbl.cellSpacing = 0;
  tbl.cellPadding = 0;
  tbl.style.width = '100%';
  tr = tbl.insertRow(tbl.rows.length);
  td = tr.insertCell(tr.cells.length);
  if (this.level == 0) { td.className = 'menu_item'; }
  else { td.className = 'sub_menu_item'; }
  td.style.width = '100%';
  td.noWrap = true; 
  td.innerHTML = this.name;
  this.td = td;
  img = document.createElement('img');
  img.src = 'images/sub.gif';
  img.style.visibility = 'hidden';
  img.width = 1;
  img.height = 1;
  this.sub_img = img;
  if (((this.menu) && (this.menu.vert)) || (this.level > 0)) {
    td = tr.insertCell(tr.cells.length);
    td.appendChild(img);
  }

  if (this.obj) {
    this.obj.id = 'mi' + this.id;
    this.obj.title = this.title;
    if (this.level == 0) { this.obj.className = 'menu_item'; }
    else { this.obj.className = 'sub_menu_item'; }
    this.obj.onclick = this.mouse_click;
    this.obj.onmouseover = this.mouse_over;
    this.obj.onmouseout = this.mouse_out;
    this.obj.menu_item = this;
    this.obj.appendChild(tbl);
  }
  
  this.tbl = document.createElement('table');
  this.tbl.border = 0;
  this.tbl.cellSpacing = 0;
  this.tbl.cellPadding = 0;

  this.div = document.createElement('div');
  this.div.menu_item = this;
  this.div.className = 'sub_menu';
  this.div.style.paddingBottom = this.menu.border_bottom;
  this.div.style.paddingLeft = this.menu.border_left;
  this.div.style.paddingRight = this.menu.border_right;
  this.div.style.paddingTop = this.menu.border_top;
  this.div.style.display = 'none';
  this.div.onmouseover = this.div_mouse_over;
  this.div.onmouseout = this.div_mouse_out;
  this.div.appendChild(this.tbl);
  document.body.insertBefore(this.div, document.body.firstChild);

  this.items = new Array();

  this.is_over = false;
  this.visible = false;
  this.drop_down = false;
  this.timer_hide_id = 0;
}

function offsetPosition(element) {
  var offsetLeft = 0, offsetTop = 0;
  do {
    offsetLeft += element.offsetLeft;
    offsetTop  += element.offsetTop;
  } while (element = element.offsetParent);
  return [offsetLeft, offsetTop];
}

function menu_item_hide_sub(id) {
  mi = document.getElementById(id).menu_item;
  mi.timer_hide_id = 0;
  mi.hide_sub();
}

menu_item.prototype.add_item = function(name, title, url, target) {
  var tr, td;
  tr = this.tbl.insertRow(this.tbl.rows.length);
  td = tr.insertCell(0);
  var item = new menu_item(this.menu, this, td, name, title, url, target);
  this.sub_img.style.visibility = 'visible';
  return item;
}

menu_item.prototype.start_hide_timer = function() {
  this.cancel_hide_timer();
  this.timer_hide_id = setTimeout('menu_item_hide_sub(\'' + this.obj.id + '\')', this.menu.hide_timeout);
}

menu_item.prototype.cancel_hide_timer = function() {
  if (this.timer_hide_id != 0) { clearTimeout(this.timer_hide_id); }
  if (this.parent) { this.parent.cancel_hide_timer(); }
}

menu_item.prototype.can_close = function() {
  var r = this.is_over;
  if (!r) {
    for (i = 0; i < this.items.length; i++) {
      r = r | this.items[i].visible;
    }
  }
  return (!r) & (this.level >= 0);
}

menu_item.prototype.show_sub = function() {
  this.visible = true;
  if (this.parent) {
    for (i = 0; i < this.parent.items.length; i++) {
      if (this.parent.items[i] != this) { this.parent.items[i].hide_sub(); }
    }
  }
  if (this.items.length > 0) {
    var pos = offsetPosition(this.obj);
    if ((this.level == 0) && (this.drop_down == true)) {
      this.div.style.left = pos[0] - this.menu.border_left;
      this.div.style.top = pos[1] + this.obj.clientHeight - this.menu.border_top;
    } else {
      this.div.style.left = pos[0] + this.obj.clientWidth - this.menu.border_left;
      this.div.style.top = pos[1] - this.menu.border_top;
    }
    this.div.style.display = 'block';
  }
}

menu_item.prototype.hide_sub = function() {
  this.visible = false;
  this.div.style.display = 'none';
  for (i = 0; i < this.items.length; i++) {
    if (this.items[i].visible) {this.items[i].hide_sub(); }
  }
  if ((this.parent) && (this.parent.can_close())) { this.parent.hide_sub(); }
}

menu_item.prototype.div_mouse_over = function() {
  mi = this.menu_item;
  mi.is_over = true;
  mi.cancel_hide_timer();
}

menu_item.prototype.div_mouse_out = function() {
  mi = this.menu_item;
  mi.is_over = false;
  mi.start_hide_timer();
}

menu_item.prototype.mouse_click = function() {
  mi = this.menu_item;
  window.open(mi.url, mi.target);
}

menu_item.prototype.mouse_over = function() {
  mi = this.menu_item;
  mi.is_over = true;
  if (mi.level == 0) { this.className='menu_item_hover'; }
  else { this.className='sub_menu_item_hover'; }
  mi.cancel_hide_timer();
  mi.show_sub();
}

menu_item.prototype.mouse_out = function() {
  mi = this.menu_item;
  mi.is_over = false;
  if (mi.level == 0) { this.className='menu_item'; }
  else { this.className='sub_menu_item'; }
  mi.start_hide_timer();
}
