//  All code by Gunni Rode | © netbureauet ARANEUM 2000 | http://www.na.dk

NA_Layer.id      = 'NALayer';
NA_Layer.css_id  = 'NALayerCSS';
NA_Layer.layers  = [];
NA_Layer.minZ    = 0;
NA_Layer.maxZ    = 0;

function NA_Layer(pID, pRef, pX, pY, pZ, pVisibility, pWidth, pHeight, pLowColor, pHighColor, pDontClip) {
	this.id          = NA_Layer.css_id + pID;
	this.name        = NA_Layer.id     + pID;
	if (NABrowser.ns) {
	  if (NABrowser.ns4) {
  		this.reference   = (pRef)      ? 'document.' + pRef + '.' : '';
      this.css         = eval(this.reference + 'document.' + this.id);
    	!this.css && alert('UNDEF: ' + this.id + ' ' + this.name);
  	  this.document    = this.css.document;
  	  this.element     = this.css;
    	this.events      = this.css;
		} else {
			this.element  = document.getElementById(this.id);
			this.css      = this.element.style;
    	!this.css && alert('UNDEF: ' + this.id + ' ' + this.name);
			this.document = document;
      this.events   = this.element;    
    }
	} else {
  	this.css         = eval('document.all.' + this.id + '.style');
  	!this.css && alert('UNDEF: ' + this.id + ' ' + this.name);
  	this.document    = document;
	  this.element     = this.document.all[this.id];
  	this.events      = this.element;
  }
	
	this.x           = pX      || ((NABrowser.ns4) ? this.css.left        : this.element.offsetLeft);
	this.y           = pY      || ((NABrowser.ns4) ? this.css.top         : this.element.offsetTop);
	this.z           = pZ      || this.css.zIndex || 0;
	this.width       = pWidth  || ((NABrowser.ns4) ? this.css.clip.width  : (this.element.offsetWidth)  ? this.element.offsetWidth  : this.css.pixelWidth);
	this.height      = pHeight || ((NABrowser.ns4) ? this.css.clip.height : (this.element.offsetHeight) ? this.element.offsetHeight : this.css.pixelHeight);
  this.depth       = 0;
  this.lowColor    = pLowColor;
  this.highColor   = pHighColor;
	this.frozen      = false;
	this.show(pVisibility);
	this.position(this.x, this.y, this.z);
//  !pDontClip && this.clip(0, this.width, this.height, 0);
	
	this.index = NA_Layer.layers.length;
	NA_Layer.layers[this.index] = this;
  eval(this.name + ' = this');
}

NA_Layer.prototype.show =
  function () {
    if (arguments.length) {
  		switch (arguments[0]) {
    	  case 1:
  	    case true:
				case 'show':
  		  	this.css.visibility = (NABrowser.ns4) ? "show" : "visible";
          break;
        case 'inherit':	
  	  		this.css.visibility = "inherit";
			  	break;
      	case 0:
      	case false:
				case 'hide':
      	default:
			  	this.css.visibility = (NABrowser.ns4) ? "hide" : "hidden";
  		}
    } else {
  		return this.css.visibility;
    }
	}

NA_Layer.prototype.position =
  function (pX, pY, pZ) {
    if (arguments.length) {
      this.x = (pX != null) ? pX : this.x;
	    this.y = (pY != null) ? pY : this.y;
  	  this.z = (pZ != null) ? pZ : this.z;

  		if (NABrowser.ns4) {
    	  this.css.left      = this.x;
        this.css.top       = this.y;
			} else if (NABrowser.ns) {
			  this.css.left      = this.x + 'px';
			  this.css.top       = this.y + 'px';
			}	else {   
				this.css.pixelLeft = this.x;
        this.css.pixelTop  = this.y;
    	}
      this.css.zIndex = this.z;

      if (this.z > NA_Layer.maxZ) {
        NA_Layer.maxZ = this.z;
      } else if (this.z < NA_Layer.minZ) {
        NA_Layer.minZ = this.z;
      }
    } else { 
      return [this.x, this.y, this.z];
    }
	}

NA_Layer.prototype.positionBy =
  function (pX, pY, pZ) {
	  this.position((pX != null) ? this.x + pX : null, (pY != null) ? this.y + pY : null, (pZ != null) ? this.z + pZ : null);
  }

NA_Layer.prototype.clip =
  function(pTop, pRight, pBottom, pLeft) {
  	var lClip;
    if (arguments.length) {
			lClip = this.clip();
  	  var lTop    = (pTop    != null) ? pTop    : lClip[0];
    	var lRight  = (pRight  != null) ? pRight  : lClip[1];
	    var lBottom = (pBottom != null) ? pBottom : lClip[2];
  		var lLeft   = (pLeft   != null) ? pLeft   : lClip[3];

    	if (NABrowser.ns4) {
      	this.css.clip.top    = lTop;
	      this.css.clip.right  = lRight;
		    this.css.clip.bottom = lBottom;
    		this.css.clip.left   = lLeft;
	    } else {
				this.css.clip = "rect(" + lTop + "px " + lRight + "px " + lBottom + "px " + lLeft + "px)";
			}
      this.width  = lRight  - lLeft;
      this.height = lBottom - lTop;
		} else {
			if (NABrowser.ns4) {
        return [this.css.clip.top, this.css.clip.right, this.css.clip.bottom, this.css.clip.left];
  		} else {
				if (this.css.clip) {
  				lClip = this.css.clip.split("rect(")[1].split(")")[0].split("px");
        } else {
          lClip = [0, this.width, this.height, 0];
				}
				return [Number(lClip[0]), Number(lClip[1]), Number(lClip[2]), Number(lClip[3])];
		  }
    }
	}

NA_Layer.prototype.clipBy =
  function() {
		var lClip = this.clip();
    for (var i = 0; i < lClip.length; i++) {
      lClip[i] += arguments[i] ? arguments[i] : 0;
    }
		this.clip(lClip[0], lClip[1], lClip[2], lClip[3]);
	}

NA_Layer.prototype.color =
  function () {
		if (arguments.length) {
			if (!arguments[0]) {
  			if (NABrowser.ns4) {
          arguments[0] = null;
        } else {
          arguments[0] = 'transparent';
        }
			}
			if (arguments[0] == 'High') {
			  arguments[0] = this.highColor;
			}
			else if (arguments[0] == 'Low') {
			  arguments[0] = this.lowColor;
			}
			if (NABrowser.ns4) {
        this.css.bgColor = arguments[0];
      } else {
				this.css.backgroundColor = arguments[0];
      }
    } else {
			var lColor = '';
			if (NABrowser.ns4) {
        if (this.css.bgColor) {
  				lColor = this.css.bgColor.toString(16);
        } else {
          return 0;
        }
			} else if (NABrowser.ns) {
				var ltemp = this.css.backgroundColor.toString().match(/(\d+),(\d+),(\d+)/);
        if (ltemp != null) {
					for (var i = 1; i < 4; i++) {
            ltemp[i] = Number(ltemp[i]).toString(16);
            if (ltemp[i].length == 1) {
              ltemp[i] = "0" + ltemp[i];
            }
					  lColor += ltemp[i];
          }
				} else {
          return 0;
        }
			} else {	
				lColor = this.element.currentStyle.backgroundColor.toString(16)
				if (lColor.search(/trans/i) != -1) {
          return 0;
        }
        lColor = lColor.substr(1,7);
			}
			while (lColor.length < 6) {
        lColor = "0" + lColor;
      }
			return lColor;
		}
	}

NA_Layer.prototype.body =
  function () {
		if (arguments.length) {
  		var lHTML = '';
     	for (var i = 0; i < arguments.length; i++) {
		    lHTML += arguments[i];
  		}   
	  	if (NABrowser.ns4) {
  	  	this.document.open();
  	  	this.document.write(lHTML);
	  	  this.document.close();
        this.width  = this.document.width;
        this.height = this.document.height;
        this._body  = lHTML;
      } else if (NABrowser.ns) {
     		while (this.element.hasChildNodes()) {
					this.element.removeChild(this.element.lastChild);
        }
    		var lRange = this.element.ownerDocument.createRange();
        		lRange.setStartAfter(this.element);
    		this.element.appendChild(lRange.createContextualFragment(lHTML));
        this._body = lHTML;
			} else {
	  		this.element.innerHTML = lHTML;
      	this.width  = this.element.scrollWidth;
	      this.height = this.element.scrollHeight;
  		}
    } else {
      if (NABrowser.ns) {
        return this._body;
			} else {
				return this.element.innerHTML; 
      }
		}
	}

