//  All code by Gunni Rode | © netbureauet ARANEUM 2000 | http://www.na.dk

NA_Document.id      = 'NADocument';

function NA_Document (pID, pWindow, pDocument, pX, pY, pWidth, pHeight) {
  this.id       = NA_Document.id + pID;
  this.window   = pWindow   || window
	this.document = pDocument || this.window.document;
  this.x        = pX        || 0;
	this.y        = pY        || 0;
	this.width    = pWidth    || 0;
  this.height   = pHeight   || 0;
	eval(this.id + '=this');
	this.addHandler("mousemove", new Function("pEvent", "return " + this.id + ".defaultMouseMove(pEvent);"));
	this.addHandler("mouseup",   new Function("pEvent", "return NA_CancelDrag(pEvent);"));
}

NA_Document.prototype.position = 
  function (pX, pY) {
    if (arguments.length) {
      this.x = (pX != null) ? pX : this.x;
      this.y = (pY != null) ? pY : this.y;
		} else {
      return [this.x, this.y];
    }
  }

NA_Document.prototype.dimension =
  function (pWidth, pHeight) {
    this.width  = (NABrowser.ns) ? this.window.innerWidth  : this.document.body.offsetWidth;
    this.height = (NABrowser.ns) ? this.window.innerHeight : this.document.body.offsetHeight;
    return [this.width, this.height];
  }

NA_Document.prototype.scrollTo =
  function (pX, pY) {
    this.position(pX, pY);  
		this.window.scrollTo(this.x, this.y);
  }

NA_Document.prototype.addHandler =
  function (pEvent, pHandler) {
    if (NABrowser.ns) {
		  this.document.captureEvents(eval('Event.' + pEvent.toUpperCase()));
  	} 
	  this.document["on" + pEvent.toLowerCase()] = pHandler
    return false;
  }

NA_Document.prototype.removeHandler =
  function (pEvent) {
    if (mNetscape) {
      this.document.releaseEvents(eval('Event.' + pEvent.toUpperCase()));
  	}
    this.document["on" + pEvent.toLowerCase()] = null;
  }

NA_Document.prototype.toString =
  function () {
    var lString = '';
		for (var lAttribute in this) {
      lString += '\n' + lAttribute + ' (' + (typeof (this[lAttribute])) + '): ';
      if (typeof (this[lAttribute]) == 'function') {
        lString += '[..]';
			} else {
        lString += this[lAttribute];
      }
		}
    return lString;
  }

NA_Document.prototype.remove =
  function () {
		eval(this.id + '=null');
	}

NA_Document.prototype.defaultMouseMove =
  function (pEvent) {
  	var lEvent = (NABrowser.ns) ? pEvent : this.window.event;
    var lXY    = NA_Document.getXYFromEvent(lEvent, this.document);
                 this.position(lXY[0], lXY[1]);

    if (NABrowser.ns && lEvent.target != this.document) {
      routeEvent(pEvent);
    }

    var lMatch = true;
    for (var i in NA_Layer.drag) {
      var lNA_Layer = NA_Layer.drag[i];
      if (!lNA_Layer.frozen && lNA_Layer.draging) {
        lNA_Layer.dragMove(this.x - lNA_Layer.dragOffsetX, this.y - lNA_Layer.dragOffsetY, NA_Layer.maxZ);
        lMatch = false;
      }
    }
    
    for (var i in NA_Layer.track) {
      var lNA_Layer = NA_Layer.track[i];
      if (!lNA_Layer.frozen && lNA_Layer.tracking) {
        lNA_Layer.trackMove();
      }
    }
    return lMatch;
  }

NA_Document.getXYFromEvent = function (pEvent, pDocument) {
  return [(NABrowser.ns) ? pEvent.pageX : pEvent.x + pDocument.body.scrollLeft, (NABrowser.ns) ? pEvent.pageY : pEvent.y + pDocument.body.scrollTop];
}

function NA_CancelDrag (pEvent) {
  for (var i in NA_Layer.drag) {
    NA_Layer.drag[i].draging = false;
  }
  if (NABrowser.ns) {
		routeEvent(pEvent);
  }
  return true;
}
