//----------------------------------
//-node.js (v0.2)
//-
//-TODO: previousSibling, nextSibling, ...
//----------------------------------

// Retrieved from http://ejohn.org/blog/javascript-array-remove/
//
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function Node(parentNode) {
  this.parentNode = (parentNode) ? parentNode : null;
  this.childNodes = new Array();
  this.firstChild = null;
  this.lastChild = null;

  this.data = new Object();

  this.appendChild = function(childNode) {
    if (! childNode.parentNode) { childNode.parentNode = this; }
    this.childNodes.push(childNode);
    this._fixChildLinks();
    return childNode;
  }

  this.removeChild = function(childNode) {
    var childIndex = -1;
    for (var i=0; i<this.childNodes.length; i++) {
      if (this.childNodes[i] === childNode) {
        childIndex = i;
      }
    }

    if (childIndex > -1) {
      this.childNodes.remove(childIndex);
      this._fixChildLinks();
    }

    return childNode;
  }

  this._fixChildLinks = function() {
    this.firstChild = this.childNodes[0];
    this.lastChild = this.childNodes[this.childNodes.length-1];
  }
}
