MenuNode.prototype.id = null;
MenuNode.prototype.element = null;
MenuNode.prototype.parent = null;
MenuNode.prototype.childMenu = null;
MenuNode.prototype.menuArrow = null;
MenuNode.prototype.text = "NO TEXT SET";
MenuNode.prototype.index = null;
MenuNode.prototype.thumbNailBox = null;

/**
 * Set the text that will be displayed to the user for this menu item.
 */
MenuNode.prototype.appendNode = function(style) {
  var elt = document.cbe.createElement('div');
  elt.className = 'cbeElt';

  this.parent.menu.cbe.appendChild(elt);
  if (style) {
    if (is.nav4) {
      var events = '';
      for (i=0; i<this.parent.nodeEvents.length;i++) {
        var type = this.parent.nodeEvents[i][0];
        var event = this.parent.nodeEvents[i][1];
        if (this.parent.parentNode) {
          events += "on" + type + "='" + event + "(" + this.parent.parentNode.index + ", " + this.index + ")' ";
        } else {
          events += "on" + type + "='" + event + "(" + this.index + ")' ";
        }
      }
      var childArrowImg = "";
      if (this.hasChildMenu()) {
        childArrowImg = '<img name="node.'+this.id+'.arrow.img" border="0" align="right" height="8" width="5" src="../images/main/menu/child_menu_arrow_off.gif">';
      }
      if (is.mac) {
        elt.cbe.innerHtml('<table border="0" cellpadding="0" cellspacing="0" width="'+(this.parent.menuWidth+1)+'"><tr><td valign="middle"><a href="#" ' + events + ' style="text-decoration:none;padding-top:1px;"><font point-size="9" color="'+this.parent.itemTextColor+'" face="arial,verdana,sans-serif">&nbsp;' + this.text + '</font></a></td><td valign="middle" align="right"><a href="#" ' + events + ' style="text-decoration:none;padding-top:1px;">' + childArrowImg + '</a></td></tr></table>');
      } else {
        elt.cbe.innerHtml('<table border="0" cellpadding="0" cellspacing="0" width="'+(this.parent.menuWidth+1)+'"><tr><td valign="middle"><a href="#" ' + events + ' style="text-decoration:none;padding-top:1px;"><font point-size="7" color="'+this.parent.itemTextColor+'" face="arial,verdana,sans-serif">&nbsp;' + this.text + '</font></a></td><td valign="middle" align="right"><a href="#" ' + events + ' style="text-decoration:none;padding-top:1px;">' + childArrowImg + '</a></td></tr></table>');
      }
      if (childArrowImg != "") {
        this.menuArrow = cbeGetImageByName('node.'+this.id+'.arrow.img');
        this.menuArrow.src = cbeGetPreloadedImageByName('childMenuArrowOff').src;
      }
    } else {
      if (is.dom1getbyid) {
        var textElt = document.createTextNode(this.text);
        elt.className = style;
        var span = document.createElement('span');
        span.style.paddingLeft = "2px";
        span.style.paddingTop = "1px";
        span.appendChild(textElt);
        //span.appendChild(cbeGetPreloadedImageByName('childMenuArrowOff'));

        var childArrowImg = null;
        if (this.hasChildMenu()) {
          var childArrowImg = document.createElement('img');
          childArrowImg.src = cbeGetPreloadedImageByName('childMenuArrowOff').src;
          childArrowImg.name = "node." + this.id + ".arrow.img";
          childArrowImg.id = "node." + this.id + ".arrow.img";
          childArrowImg.width = "5";
          childArrowImg.height = "8";
          childArrowImg.align = "right";
          childArrowImg.border = "0";
          elt.appendChild(childArrowImg);
        }

        //elt.cbe.innerHtml(childArrowImg);
        elt.appendChild(span);

        if (childArrowImg != null) {
          this.menuArrow = cbeGetImageByName('node.'+this.id+'.arrow.img');
          this.menuArrow.src = cbeGetPreloadedImageByName('childMenuArrowOff').src;
        }
      } else {
        elt.cbe.innerHtml('<div class="' + style + '">' + this.text + '</div>');
      }
      //Add event listener.
      for (i=0; i<this.parent.nodeEvents.length;i++) {
        var type = this.parent.nodeEvents[i][0];
        var event = this.parent.nodeEvents[i][1];
        if (this.parent.parentNode) {
          event += '(' + this.parent.parentNode.index + ', ' + this.index + ')';
        } else {
          event += '(' + this.index + ')';
        }
        elt.cbe.addEventListener(type, event);
      }
    }
  } else {
    elt.cbe.innerHtml(this.text);
  }
  elt.id = this.id;
  elt.cbe.show();
  this.element = elt;

  return this;
}

/**
 * Returns true if the menu node has a child menu.
 */
MenuNode.prototype.hasChildMenu = function() {
  if (this.childMenu != null) return true;
  return false;
}

/**
 * Gives a menu node a child menu.
 */
MenuNode.prototype.setChildMenu = function(menu) {
  this.childMenu=menu;
}

/**
 * Returns the child menu of this node. null if one does not exist.
 */
MenuNode.prototype.getChildMenu = function(menu) {
  return this.childMenu;
}


/**
 * Constructor
 */
function MenuNode(id, menu) {
  if (id == null) {
    alert('A node must have a id');
    return;
  }
  this.id = id;
  if (menu == null) {
    alert('A node must have a menu');
    return;
  }
  this.parent = menu;
  this.thumbNailBox = new ThumbNailBox(this);
}
