/******************************************************
 *
 * This script was written for
 * http://other.lookstrike.com
 *
 * If you plan to use it for your own, you can but you
 * MUST leave this whole comment intact in each file.
 *
 * Copyright		: (C) Jean-Sébastien Goupil - 2006-2008
 * Website		: http://other.lookstrike.com
 *
 ******************************************************/

var registeredDropObject = new Array(); // All dropable objects
var staticInitVal = false;

function moveHandlerDrop(event) {
	var events = ToolBox.events();
	var realEvent = events.get(event);
	var currDrag = oDynamicGroup.groupDrag();

	var c = registeredDropObject.length;

	for(var i = 0; i < c; i++) { // We check for all registered objects
		if(realEvent.clientX > registeredDropObject[i][0] && realEvent.clientY > registeredDropObject[i][1]
		   && realEvent.clientX < (registeredDropObject[i][0] + registeredDropObject[i][2])
		   && realEvent.clientY < (registeredDropObject[i][1] + registeredDropObject[i][3])
		   && currDrag != "") { // We found that we are over this object
			registeredDropObject[i][4].currentlyOver = true;
			oDynamicGroup.startDrop(registeredDropObject[i][4]); // We start listen for dropping
			registeredDropObject[i][4].beginOver(event);
		} else if (registeredDropObject[i][4].currentlyOver == true) {
			registeredDropObject[i][4].currentlyOver = false;
			oDynamicGroup.endDrop(); // We stop listening
			registeredDropObject[i][4].endOver(event);
		}
	}
}

function DynamicDrop(strId, strImgNormal, strImgAccepted, strImgRefused) {
	if(staticInitVal == false) {
		this.staticInit();
	}

	this.init(strId, strImgNormal, strImgAccepted, strImgRefused);
}

DynamicDrop.prototype = {
	staticInit: function() { // Init the mousemove on the document only once
		var events = ToolBox.events();
		events.register(document, "mousemove", moveHandlerDrop, true);
		staticInitVal = true;
	},

	init: function(strId, strImgNormal, strImgAccepted, strImgRefused) {
		this.objN = document.getElementById(strId);
		if(this.objN == null) {
			throw "[DROP] Id Object1 is wrong";
		}
		// Set Style value
		this.objN.style.position = "absolute";
		this.objN.style.left = this.objN.offsetLeft + "px";
		this.objN.style.top = this.objN.offsetTop + "px";

		// Create new span
		this.objA = this.objN.cloneNode(false);
		this.objR = this.objN.cloneNode(false);
		this.objA.id = this.objN.id + "_accepted";
		this.objR.id = this.objN.id + "_refused";
		this.objN.parentNode.appendChild(this.objA);
		this.objN.parentNode.appendChild(this.objR);

		// Create Images
		this.imgN = document.createElement("img");
		this.imgN.src = strImgNormal;
		this.imgA = document.createElement("img");
		this.imgA.src = strImgAccepted;
		this.imgR = document.createElement("img");
		this.imgR.src = strImgRefused;

		// Associate Image with span
		this.objN.appendChild(this.imgN);
		this.objA.appendChild(this.imgA);
		this.objR.appendChild(this.imgR);
		this.objA.style.visibility = "hidden"; // Hide
		this.objR.style.visibility = "hidden"; // Hide

		// zIndex - all same
		this.objN.style.zIndex = 0;
		this.objA.style.zIndex = 0;
		this.objR.style.zIndex = 0;

		this.strGroup = ""; // Group for DragNDrop

		this.bCurrentlyOver = false;

		// For mouse handler to detect if we are over the span
		this.styleX = parseInt(this.objN.offsetLeft);
		this.styleY = parseInt(this.objN.offsetTop);
		this.styleW = parseInt(this.objN.offsetWidth);
		this.styleH = parseInt(this.objN.offsetHeight);

		var nIndex = registeredDropObject.length;
		registeredDropObject[nIndex] = new Array();
		registeredDropObject[nIndex][0] = this.styleX;
		registeredDropObject[nIndex][1] = this.styleY;
		registeredDropObject[nIndex][2] = this.styleW;
		registeredDropObject[nIndex][3] = this.styleH;
		registeredDropObject[nIndex][4] = this;
	},

	setGroup: function(strGroup) {
		this.strGroup = strGroup;
	},

	/**
	 * Called when the mouse is drag over the icon
	 * We need to know if we switch to Accepted or Refused
	 */
	beginOver: function(event) {
		var currDrag = oDynamicGroup.groupDrag();
		if(currDrag != "") {
			if(currDrag == this.strGroup && this.objA.style.visibility != "visible") { // We set the accepted visible if it was not
				this.objN.style.visibility = "hidden";
				this.objA.style.visibility = "visible";
			} else if (currDrag != this.strGroup && this.objR.style.visibility != "visible") { // We set the refused visible if it was not
				this.objN.style.visibility = "hidden";
				this.objR.style.visibility = "visible";
			}
		}
	},

	/**
	 * Called when the drag is no more over the image
	 */
	endOver: function(event) {
		this.objN.style.visibility = "visible";
		this.objA.style.visibility = "hidden";
		this.objR.style.visibility = "hidden";
	},

	/**
	 * Called if we were listening with DynamicGroup
	 */
	DragReleased: function(target) {
		if(target.GetGroup() != this.strGroup) { // We refused the drag
			return false;
		} else { // We accept the drag
			target.returnX = this.styleX;
			target.returnY = this.styleY;
			return true;
		}
	}
}