function CreateLayer(id, nest, left, top, width, height) {
  return new _Layer(id, nest, left, top, width, height);
}

function _Layer(id, nest, left, top, width, height) {
  // public:
//  this.browser = CreateBrowser();
  
  nest = (!nest) ? "" : "document." + nest + ".";
  
  // private:
  this._obj   = (window.browser.ns) ? eval(nest + "document." + id) : eval("document.all." + id);
  this._style = (window.browser.ns) ? this._obj : this._obj.style;
  this._clip  = (window.browser.ns) ? this._style.clip : this._style;
  
	// public:
  this.id = id;
  
  // public:
  this.getLeft    = _Layer_getLeft;
  this.setLeft    = _Layer_setLeft;
  this.getTop     = _Layer_getTop;
  this.setTop     = _Layer_setTop;
  
  this.getWidth   = _Layer_getWidth;
  this.setWidth   = _Layer_setWidth;
  this.getHeight  = _Layer_getHeight;
  this.setHeight  = _Layer_setHeight;
  
  this.moveTo     = _Layer_moveTo;
  this.moveBy     = _Layer_moveBy;
  this.resizeTo   = _Layer_resizeTo;
  this.resizeBy   = _Layer_resizeBy;
  
  this.slideTo    = _Layer_slideTo;
  this.onSlideEnd = null;
  this.isSliding  = _Layer_isSliding;

  // private:
  this._doSlideTo = _Layer__doSlideTo;
  this._slideLeft = null;
  this._slideTop  = null;
  
  // public:
  this.setVisible = _Layer_setVisible;
  this.isVisible  = _Layer_isVisible;
  
  // public:
  this.setBackgroundColor = _Layer_setBackgroundColor;
  
  // public:
  this.toString   = _Layer_toString;
  
  // public:
  this.setHTML    = _Layer_setHTML;
  this.getHTML    = _Layer_getHTML;
  // private:
  this._html      = this.getHTML();
  
  // public:
  this.refresh      = _Layer_refresh;
  
  // private:
  this._ref       = "_" + id + "Layer";
  eval(this._ref + " = this");
  
  
  // init:
  this.moveTo(left, top);
  this.resizeTo(width, height);
}

function _Layer_getLeft() {
  return parseInt(this._style.left);
}

function _Layer_setLeft(left) {
  if (left != null) {
    this._style.left = left;
  }
  return true;
}

function _Layer_getTop() {
  return parseInt(this._style.top);
}

function _Layer_setTop(top) {
  if (top != null) {
    this._style.top = top;
  }
  return true;
}

function _Layer_getWidth() {
  return parseInt(this._clip.width);
/*  if (window.browser.ns) {
    return this._layer.clip.width;
  } else {
    return parseInt(this._layer.width);
  }
*/
}

function _Layer_setWidth(width) {
  if (width != null) {
    this._clip.width = width;
/*    if (window.browser.ns) {
      this._layer.clip.width = width;
    } else {
      this._layer.width = width;
    }
*/
  }
  return true;
}

function _Layer_getHeight() {
  return parseInt(this._clip.height);
/*
  if (window.browser.ns) {
    return this._layer.clip.height;
  } else {
    return parseInt(this._layer.height);
  }
*/
}

function _Layer_setHeight(height) {
  if (height != null) {
    this._clip.height = height;
/*    
    if (window.browser.ns) {
      this._layer.clip.height = height;
    } else {
      this._layer.height = height;
    }
*/
  }
  return true;
}

function _Layer_moveTo(left, top) {
  this.setLeft(left);
  this.setTop(top);
  return true;
}

function _Layer_moveBy(left, top) {
  this.setLeft(this.getLeft() + left);
  this.setTop(this.getTop() + top);
  return true;
}

function _Layer_resizeTo(width, height) {
  this.setWidth(width);
  this.setHeight(height);
  return true;
}

function _Layer_resizeBy(width, height) {
  this.setWidth(this.getWidth() + width);
  this.setHeight(this.getHeight() + height);
  return true;
}

function _Layer_slideTo(left, top, step, delay) {
  if (this.isSliding())
    return false;
  
  this._slideLeft = left;
  this._slideTop  = top;
  window.setTimeout(this._ref+"._doSlideTo("+left+", "+top+", "+step+", "+delay+")", 0);
  return true;
}

function _Layer__doSlideTo(left, top, step, delay) {
  if (this._slideLeft != left || this._slideTop != top) {
    // another slide has been requested. Abort!!! Abort!!!
    // note there is no call to onSlideEnd
    return false;
  }
  
  var leftDiff = left - this.getLeft();
  if (leftDiff > 0) {
    this.setLeft(Math.min(left, this.getLeft() + step));
  } else if (leftDiff < 0) {
    this.setLeft(Math.max(left, this.getLeft() - step));
  }
  
  var topDiff = top - this.getTop();
  if (topDiff > 0) {
    this.setTop(Math.min(top, this.getTop() + step));
  } else if (topDiff < 0) {
    this.setTop(Math.max(top, this.getTop() - step));
  }
  
  if (leftDiff != 0 || topDiff != 0) {
    window.setTimeout(this._ref + "._doSlideTo("+left+", "+top+", "+step+", "+delay+")", delay);
  } else {
    this._slideLeft = null;
    this._slideTop = null;
    if (typeof(this.onSlideEnd) == "function") {
      return this.onSlideEnd(this);
    } else if (this.onSlideEnd != null && typeof(this.onSlideEnd) == "object") {
      // it's an event object.
      return this.onSlideEnd.trigger(this);
    }
  }
  return true;
}

function _Layer_isSliding() {
  return this._slideLeft != null || this._slideTop != null;
}

function _Layer_setVisible(vis) {
  if (vis) {
    this._style.visibility = "visible";
  } else {
    this._style.visibility = "hidden";
  }
  return true;
}

function _Layer_isVisible() {
  return this._style.visibility != "hidden" && this._style.visibility != "hide";
}

function _Layer_setBackgroundColor(color){
	if(window.browser.dom || window.browser.ie4) {
	  this._style.backgroundColor = color;
	} else if(bw.ns4) {
	  this._style.bgColor= color;
	}
}

function _Layer_toString() {
  var str = "_Layer {";
  str += "left: " + this.getLeft() + "; ";
  str += "top: " + this.getTop() + "; ";
  str += "width: " + this.getWidth() + "; ";
  str += "height: " + this.getHeight() + "; ";
  str += (this.isVisible()) ? "visible; " : "hidden; ";
  str += "}";
  return str;
}

function _Layer_setHTML(text){
  this._html = text;
	if (window.browser.ns4) {
	  this._obj.document.open();
	  this._obj.document.write(text);
	  this._obj.document.close();
	}	else if(window.browser.ie) {
	  this._obj.innerHTML = text;
	} else if(window.browser.ns5) {
	  null; //ADD NS5 HERE.
	}
}

function _Layer_getHTML() {
  if (window.browser.ie) {
    return this._obj.innerHTML;
  }
  return null;
}

function _Layer_refresh() {
  if (window.browser.ns && this._html != null) {
    this.setHTML(this._html);
  }
}
