﻿function DragDropHelper(){
}

DragDropHelper.dropReceivers = new Array();

DragDropHelper.lastDrop = null;

DragDropHelper.registerForDrop = function(obj){
	DragDropHelper.dropReceivers.push(obj);
}

DragDropHelper.drop = function(obj, dx, dy){
	if (DragDropHelper.lastDrop != null){
		DragDropHelper.lastDrop.removeDrop();
		DragDropHelper.lastDrop = null;
	}
	for (var i = 0; i < DragDropHelper.dropReceivers.length; ++i){
		var x = findPosX(DragDropHelper.dropReceivers[i]);
		var y = findPosY(DragDropHelper.dropReceivers[i]);
		var w = DragDropHelper.dropReceivers[i].clientWidth;
		var h = DragDropHelper.dropReceivers[i].clientHeight;
		if (dx >= x && dx < x + w && dy >= y && dy < y + h){
			DragDropHelper.dropReceivers[i].drop(obj, dx - x, dy - y);
			return;
		}
	}
}

DragDropHelper.preDrop = function(obj, dx, dy){
	for (var i = 0; i < DragDropHelper.dropReceivers.length; ++i){
		var x = findPosX(DragDropHelper.dropReceivers[i]);
		var y = findPosY(DragDropHelper.dropReceivers[i]);
		var w = DragDropHelper.dropReceivers[i].clientWidth;
		var h = DragDropHelper.dropReceivers[i].clientHeight;
		if (dx >= x && dx < x + w && dy >= y && dy < y + h){
			DragDropHelper.dropReceivers[i].preDrop(obj, dx - x, dy - y);
			var ld = DragDropHelper.lastDrop;
			DragDropHelper.lastDrop = DragDropHelper.dropReceivers[i];
			if (ld != null && ld != DragDropHelper.lastDrop){
				ld.removeDrop();
				DragDropHelper.lastDrop = null;
			}
			return;
		}
	}
	if (DragDropHelper.lastDrop != null){
		DragDropHelper.lastDrop.removeDrop();
		DragDropHelper.lastDrop = null;
	}
}
