/**
 * add (node)
 * add (id, node)
 */
function menuAdd (entry) {

  entry.container = this;
  this.rootNode.add (entry);

}

function menuFindByProperty (key, value) {

  var result = new Array();
  var entry;
  var i;

  for (i = 0; i < this.allNodes.length; i++) {
    entry = this.allNodes[i].properties.get (key, null);

    if (entry != null && entry == value) {
      result [result.length] = this.allNodes[i];
    }
  }

  if (result.length == 0) {
    return null;
  }

  return result;

}

function menuRender () {

  //alert ("Render called");

  var i = 0;
  var r, j;
  var out = "";
  var start, ende;

  // First Level
  out = this.rootNode.render();
  out += '<div height="50"><img src="/images/spacer.gif" width="10" height="50"></div>';
  
  if ((is.ns && is.v > 4) || (is.ie && is.v >= 4)) {
    if (this.container_id != null) {
      layerWrite (getElement (this.container_id), out);
    } else {
      return out;
    }
  }
}

function menuToggle (id) {
  if (this.cache[id] && this.cache[id].expanded) {
    this.collapse (id);
  } else {
    this.expand (id);
  }
}

function menuSetContainerId (id) {
  
  this.container_id = id;

}

function menuGetContainerId (id) {

  return this.container_id;

}

function menuGetId() {
  return this.id;
}

function menuExpand (id) {
  var entry = this.cache [id];

  if (entry && !this.locked) {
    entry.expanded = true;
    this.render();
  }
}

function menuCollapse (id) {
  
  var entry = this.cache[id];

  if (entry && !this.locked) {
    entry.expanded = false;
    this.render();
  }
}

function menuExpandAll () {

  var i;

  for (i in this.cache) {
    this.cache[i].expanded = true;
  }
  
  this.render();
}

function menuCollapseAll () {

  var i;
  
  for (i in this.cache) {
    this.cache[i].expanded = false;
  }

  this.render();
}

function menu (id) {
  this.rootNode = new node(-1, "ROOT");
  this.rootNode.container = this;
  this.cache = new Array();
  this.maxLevel = -1;
  this.add = menuAdd;
  this.id = id;
  this.locked = false;
  this.getId = menuGetId;
  this.render = menuRender;
  this.findByProperty = menuFindByProperty;
  this.toggle = menuToggle;
  this.container_id = null;
  this.setContainer = menuSetContainerId;
  this.getcontainer = menuGetContainerId;
  this.collapse = menuCollapse;
  this.expand = menuExpand;
  this.collapseAll = menuCollapseAll;
  this.expandAll = menuExpandAll;
}
