var DragAndDrop = klass.create();
Object.extend(DragAndDrop, {
  currentDraggable: null,
  dropId: 0,
  dropAcceptors: $H(),
  mouseMove: function(evt) {
    if (DragAndDrop.currentDraggable) {
      var x = Event.pointerX(evt);
      var y = Event.pointerY(evt);
      var d = DragAndDrop.currentDraggable;

      d.setDragPosition([x, y]);

      DragAndDrop.dropAcceptors.each(function (n) {
        acc = n.value;
        if (Position.within(acc.getDropElement(), x, y)
            && (acc.acceptDragType(d.getDraggableTypeString()))) {
          acc.dragIn();
        } else {
          acc.dragOut();
        }
      });

      Event.stop(evt);
    }
  },

  mouseUp: function(evt) {
    if (DragAndDrop.currentDraggable) {
      var d = DragAndDrop.currentDraggable;
      var x = Event.pointerX(evt);
      var y = Event.pointerY(evt);
      d.stopDrag();
      DragAndDrop.currentDraggable = null;
      document.onmousedown = null;

      DragAndDrop.dropAcceptors.each(function (n) {
        acc = n.value;
        if (Position.within(acc.getDropElement(), x, y)
            && (acc.acceptDragType(d.getDraggableTypeString()))) {
          acc.dragOut();
          acc.dropAccept(d.getDraggableData());
        } else {
          acc.dragOut();
        }
      });
    }
  },

  registerDropAcceptor: function(obj) {
    DragAndDrop.dropId++;
    DragAndDrop.dropAcceptors[String(DragAndDrop.dropId)] = obj;
    return DragAndDrop.dropId;
  },

  removeDropAcceptor: function(id) {
    delete DragAndDrop.dropAcceptors[String(id)];
  }
});
Event.observe(window.document, 'mousemove',
              DragAndDrop.mouseMove.bindAsEventListener());
Event.observe(window.document, 'mouseup',
              DragAndDrop.mouseUp.bindAsEventListener());
// vim: set sts=2 sw=2 expandtab:

