function IDragSource()
{
  this.onDragStart = new REvent();
  this.onDragMove  = new REvent();
  this.onDragEnd   = new REvent();
}

function IDropTarget()
{
  this.onDragOver = new REvent();
  this.onDragOut  = new REvent();
  this.onDrop     = new REvent();
}
function DragMonitor()
{
  this.mouseX;
  this.mouseY;
  this.dragSource = null;
  this.dropTarget = null;
  this.dragNode = null;

  var documentProperties = DocumentProperties.statics.getInstance();
  documentProperties.onMouseMove.addEventListener(new REventListener(this.onMouseMove, this));
  documentProperties.onMouseUp.addEventListener(new REventListener(this.onMouseUp, this));
}
DragMonitor.statics = new Object();
DragMonitor.statics.getInstance = function()
{
  if (!DragMonitor.statics.instance)
    DragMonitor.statics.instance = new DragMonitor();

  return DragMonitor.statics.instance;
}

DragMonitor.prototype.setDragSource = function(source, object)
{
	this.dragSource = source;
  this.dragObject = object;
  this.dragStart = false;
}
DragMonitor.prototype.setDropTarget = function(target, object)
{
	if ( object.Disabled == 1 )
		return false;
  this.dropTarget = target;
  this.dropObject = object;
  return true;
}
DragMonitor.prototype.removeDragSource = function()
{
  this.dragSource = null;
  this.dragObject = null;
  this.dragStart = false;
}
DragMonitor.prototype.removeDropTarget = function()
{
  if (!this.dropping)
  {
    this.dropTarget = null;
    this.dropObject = null;
  }
}
DragMonitor.prototype.getDragObject = function()
{
  return this.dragObject;
}

DragMonitor.prototype.onMouseMove = function(x, y)
{
  this.mouseX = x;
  this.mouseY = y;
  if (this.dragSource != null)
  {
    if (!this.dragStart)
    {
      this.dragNode = document.createElement("div");
      this.dragNode.style.position = "absolute";
      document.getElementsByTagName("body")[0].appendChild(this.dragNode);

      this.dragSource.onDragStart.trigger(this.dragObject, this.dragNode, x, y);
      this.dragStart = true;
    }

    this.dragNode.style.left = (x+1)+"px";
    this.dragNode.style.top = (y+1)+"px";
    this.dragSource.onDragMove.trigger(this.dragObject, this.dragNode, x, y);
  }
}
DragMonitor.prototype.onMouseUp = function()
{
  if (this.dropTarget != null)
  {
    this.dropping = true;
    if (this.dragSource)
      this.dropTarget.onDrop.trigger(this.dragObject, this.dropObject);
	  this.dropTarget.onDragOut.trigger(this.dragObject, this.dropObject);
    this.dropTarget = null;
    this.dropObject = null;
    this.dropping = false;
  }
  
  if (this.dragSource != null)
  {
    this.dragSource.onDragEnd.trigger(this.dragObject, this.dragNode, this.mouseX, this.mouseY);
    this.dragSource = null;
    this.dragObject = null;
  }

  if (this.dragNode != null)
  {
    document.getElementsByTagName("body")[0].removeChild(this.dragNode);
    this.dragNode = null;
  }
}