if(!window.DHTMLSuite)var DHTMLSuite = new Object();
DHTMLSuite.dragDrop = function()
{
var mouse_x;					// mouse x position when drag is started
var mouse_y;					// mouse y position when drag is started.
var el_x;						// x position of dragable element
var el_y;						// y position of dragable element
var dragDropTimer;				// Timer - short delay from mouse down to drag init.
var numericIdToBeDragged;		// numeric reference to element currently being dragged.
var dragObjCloneArray;			// Array of cloned dragable elements. every
var dragDropSourcesArray;		// Array of source elements, i.e. dragable elements.
var dragDropTargetArray;		// Array of target elements, i.e. elements where items could be dropped.
var currentZIndex;				// Current z index. incremented on each drag so that currently dragged element is always on top.
var okToStartDrag;				// Variable which is true or false. It would be false for 1/100 seconds after a drag has been started.
var moveBackBySliding;			// Variable indicating if objects should slide into place moved back to their location without any slide animation.
var dragX_allowed;				// Possible to drag this element along the x-axis?
var dragY_allowed;				// Possible to drag this element along the y-axis?
var currentEl_allowX;
var currentEl_allowY;
var drag_minX;
var drag_maxX;
var drag_minY;
var drag_maxY;
var dragInProgress;				// Variable which is true when drag is in progress, false otherwise
var eventsInitialized;
var dragMoveEvent;  // Event while moving the draggable element
var dragClickEvent;  // Event when click on the draggable element (click but not drag)
try{
if(!standardObjectsCreated)DHTMLSuite.createStandardObjects();	// This line starts all the init methods
}catch(e){
alert('You need to include the dhtmlSuite-common.js file');
}
this.dragX_allowed = true;
this.dragY_allowed = true;
this.currentZIndex = 21000;
this.dragDropTimer = -1;
this.dragObjCloneArray = new Array();
this.numericIdToBeDragged = false;
this.okToStartDrag = true;
this.moveBackBySliding = true;
this.dragInProgress = false;
this.eventsInitialized = false;
this.dragMoveEvent = null;
this.dragClickEvent = null;
var objectIndex;
this.objectIndex = DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects.length;
DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[this.objectIndex] = this;
}
DHTMLSuite.dragDrop.prototype = {
addSource:function(sourceId,slideBackAfterDrop,xAxis,yAxis,dragOnlyWithinElId)
{
	if(!this.dragDropSourcesArray)
	  this.dragDropSourcesArray = new Array();
	if(!document.getElementById(sourceId)) return;
	if(xAxis!==false) xAxis = true;
	if(yAxis!==false) yAxis = true;
	var obj = document.getElementById(sourceId);
	this.dragDropSourcesArray[this.dragDropSourcesArray.length]  = [obj,slideBackAfterDrop,xAxis,yAxis,dragOnlyWithinElId];
	obj.setAttribute('dragableElement',this.dragDropSourcesArray.length-1);
	obj.dragableElement = this.dragDropSourcesArray.length-1;
	
	// Inits the object
	var ind = this.objectIndex;
	var refToThis = this;
	var no = this.dragDropSourcesArray.length - 1;
	var el = this.dragDropSourcesArray[no][0].cloneNode(true);
	var el2 = this.dragDropSourcesArray[no][0];
	eval("el.onmousedown =function(e,index){ DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[" + ind + "].__initializeDragProcess(e," + no + "); }");
	DHTMLSuite.commonObj.__addEventElement(el);
	el.id = el2.id;	/* Override the value above */
	el.style.position='absolute';
	el.style.visibility='hidden';
	el.style.display='none';
	el.style.opacity='.50';
	el.style.filter = 'alpha(opacity=50)';
	document.body.appendChild(el);
	el.style.top = DHTMLSuite.commonObj.getTopPosScrolled(this.dragDropSourcesArray[no][0]) + 'px';
	el.style.left = DHTMLSuite.commonObj.getLeftPos(this.dragDropSourcesArray[no][0]) + 'px';
	eval("el2.onmousedown =function(e,index){ return DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[" + ind + "].__initializeDragProcess(e," + no + "); }");
	DHTMLSuite.commonObj.__addEventElement(this.dragDropSourcesArray[no][0]);
	this.dragObjCloneArray[no] = el;

	if(!this.eventsInitialized) {
	  this.eventsInitialized = true;
	  eval("DHTMLSuite.commonObj.addEvent(document.documentElement,\"mousemove\",function(e){ DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[" + ind + "].__moveDragableElement(e); } )");
	  eval("DHTMLSuite.commonObj.addEvent(document.documentElement,\"mouseup\",function(e){ DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[" + ind + "].__stopDragProcess(e); } );");
	  if(!document.documentElement.onselectstart)document.documentElement.onselectstart = function() { return DHTMLSuite.commonObj.__isTextSelOk(); };
	  document.documentElement.ondragstart = function() { return DHTMLSuite.commonObj.cancelEvent() };
	  DHTMLSuite.commonObj.__addEventElement(document.documentElement);
	}
}
,
setDragMoveEvent:function(func) {
  this.dragMoveEvent = func;
},
setDragClickEvent:function(func) {
  this.dragClickEvent = func;
},
removeAllSources:function() {
	if(!this.dragDropSourcesArray)
	  return;
	var el;
	for(var n=0;n<this.dragDropSourcesArray.length;n++) {
	  el = this.dragObjCloneArray[n];
	  if(el) document.body.removeChild(el);
	  DHTMLSuite.commonObj.__removeEventElement(this.dragDropSourcesArray[n][0]);
	}
	this.dragDropSourcesArray.splice(0, this.dragDropSourcesArray.length);
	this.dragObjCloneArray.splice(0, this.dragObjCloneArray.length);
}
,
addTarget:function(targetId,functionToCallOnDrop)
{
if(!this.dragDropTargetArray)this.dragDropTargetArray = new Array();
var obj = document.getElementById(targetId);
if(!obj) {
//alert('The target element with id ' + targetId + ' does not exists.  Check your HTML code');
return;
}

/* Modified by Carlos */
var exists = false;
var tot = this.dragDropTargetArray.length;
for(var n = 0; n < tot; n++) {
  if(this.dragDropTargetArray[n][2] == targetId) {  // The target already exists
    exists = true;
	this.dragDropTargetArray[n][0] = obj;
	this.dragDropTargetArray[n][1] = functionToCallOnDrop;
    break;
  }
}
if(!exists)
  this.dragDropTargetArray[tot]  = [obj,functionToCallOnDrop,targetId];
}
,
/* Created by Carlos */
removeTarget:function(targetId)
{
  var tot = this.dragDropTargetArray.length;
  for(var n = 0; n < tot; n++) {
    if(this.dragDropTargetArray[n][2] == targetId) {
	  this.dragDropTargetArray.splice(n, 1);
	  break;
	}
  }
}
,
setSlide:function(isSlidingAnimationEnabled)
{
this.moveBackBySliding = isSlidingAnimationEnabled;
}
,
__initializeDragProcess:function(e,index)
{
var ind = this.objectIndex;
if(!this.okToStartDrag)return false;
setTimeout('DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[' + ind + '].okToStartDrag = true;',100);
if(document.all)e = event;
this.numericIdToBeDragged = index;
this.numericIdToBeDragged = this.numericIdToBeDragged + '';
this.dragDropTimer=0;
DHTMLSuite.commonObj.__setTextSelOk(false);
this.mouse_x = e.clientX;
this.mouse_y = e.clientY;
this.currentZIndex = this.currentZIndex + 1;
this.dragObjCloneArray[this.numericIdToBeDragged].style.zIndex = this.currentZIndex;
this.currentEl_allowX = this.dragDropSourcesArray[this.numericIdToBeDragged][2];
this.currentEl_allowY = this.dragDropSourcesArray[this.numericIdToBeDragged][3];
var parentEl = this.dragDropSourcesArray[this.numericIdToBeDragged][4];
this.drag_minX = false;
this.drag_minY = false;
this.drag_maxX = false;
this.drag_maxY = false;
if(parentEl){
var obj = document.getElementById(parentEl);
if(obj){
this.drag_minX = DHTMLSuite.commonObj.getLeftPos(obj);
this.drag_minY = DHTMLSuite.commonObj.getTopPosScrolled(obj);
this.drag_maxX = this.drag_minX + obj.clientWidth;
this.drag_maxY = this.drag_minY + obj.clientHeight;
}
}
if(this.dragDropSourcesArray[this.numericIdToBeDragged][1]){
this.dragObjCloneArray[this.numericIdToBeDragged].style.top = DHTMLSuite.commonObj.getTopPosScrolled(this.dragDropSourcesArray[this.numericIdToBeDragged][0]) + 'px';
this.dragObjCloneArray[this.numericIdToBeDragged].style.left = DHTMLSuite.commonObj.getLeftPos(this.dragDropSourcesArray[this.numericIdToBeDragged][0]) + 'px';
}
this.el_x = this.dragObjCloneArray[this.numericIdToBeDragged].style.left.replace('px','')/1;
this.el_y = this.dragObjCloneArray[this.numericIdToBeDragged].style.top.replace('px','')/1;
this.__waitBeforeDragProcessStarts();
return false;
}
,
__waitBeforeDragProcessStarts:function()
{
var ind = this.objectIndex;
if(this.dragDropTimer>=0 && this.dragDropTimer<5){
this.dragDropTimer = this.dragDropTimer + 1;
setTimeout('DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[' + ind + '].__waitBeforeDragProcessStarts()',2);
return;
}
if(this.dragDropTimer>=5){
if(this.dragObjCloneArray[this.numericIdToBeDragged].style.display=='none'){
//this.dragDropSourcesArray[this.numericIdToBeDragged][0].style.visibility = 'hidden';
this.dragObjCloneArray[this.numericIdToBeDragged].style.display = 'block';
this.dragObjCloneArray[this.numericIdToBeDragged].style.visibility = 'visible';
this.dragObjCloneArray[this.numericIdToBeDragged].style.top = DHTMLSuite.commonObj.getTopPosScrolled(this.dragDropSourcesArray[this.numericIdToBeDragged][0]) + 'px';
this.dragObjCloneArray[this.numericIdToBeDragged].style.left = DHTMLSuite.commonObj.getLeftPos(this.dragDropSourcesArray[this.numericIdToBeDragged][0]) + 'px';
}
}
}
,
__moveDragableElement:function(e)
{
var ind = this.objectIndex;
if(document.all)e = event;
if(this.dragDropTimer<5)return false;
if(this.dragInProgress)return false;
this.dragInProgress = true;
var dragObj = this.dragObjCloneArray[this.numericIdToBeDragged];
if(this.currentEl_allowX){
var leftPos = (e.clientX - this.mouse_x + this.el_x);
if(this.drag_maxX){
var tmpMaxX = this.drag_maxX - dragObj.offsetWidth;
if(leftPos > tmpMaxX)leftPos = tmpMaxX
if(leftPos < this.drag_minX)leftPos = this.drag_minX;
}
dragObj.style.left = leftPos + 'px';
}
if(this.currentEl_allowY){
var topPos = (e.clientY - this.mouse_y + this.el_y);
if(this.drag_maxY){
var tmpMaxY = this.drag_maxY - dragObj.offsetHeight;
if(topPos > tmpMaxY)topPos = tmpMaxY;
if(topPos < this.drag_minY)topPos = this.drag_minY;
}
dragObj.style.top = topPos + 'px';
}
if(this.dragMoveEvent)
  this.dragMoveEvent(dragObj, leftPos, topPos);
this.dragInProgress = false;
return false;
}
,
__stopDragProcess:function(e)
{
if(this.dragDropTimer<5)return false;
if(document.all)e = event;

if(e.clientX == this.mouse_x  &&  e.clientY == this.mouse_y) {
	if(this.dragClickEvent)
	  this.dragClickEvent(this.dragDropSourcesArray[this.numericIdToBeDragged][0]);
}

var dropDestination = DHTMLSuite.commonObj.getSrcElement(e);
var leftPosMouse = e.clientX + Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
var topPosMouse = e.clientY + Math.max(document.body.scrollTop,document.documentElement.scrollTop);
if(!this.dragDropTargetArray)this.dragDropTargetArray = new Array();
for(var no=0;no<this.dragDropTargetArray.length;no++){
var leftPosEl = DHTMLSuite.commonObj.getLeftPos(this.dragDropTargetArray[no][0]);
var topPosEl = DHTMLSuite.commonObj.getTopPosScrolled(this.dragDropTargetArray[no][0]);
var widthEl = this.dragDropTargetArray[no][0].offsetWidth;
var heightEl = this.dragDropTargetArray[no][0].offsetHeight;
var dropped = false;
if(leftPosMouse > leftPosEl && leftPosMouse < (leftPosEl + widthEl) && topPosMouse > topPosEl && topPosMouse < (topPosEl + heightEl)){
if(this.dragDropTargetArray[no][1]){
try{
  dropped = eval(this.dragDropTargetArray[no][1] + '("' + this.dragDropSourcesArray[this.numericIdToBeDragged][0].id + '","' + this.dragDropTargetArray[no][0].id + '",' + e.clientX + ',' + e.clientY + ')');
}catch(e){
alert('Unable to execute \n' + this.dragDropTargetArray[no][1] + '("' + this.dragDropSourcesArray[this.numericIdToBeDragged][0].id + '","' + this.dragDropTargetArray[no][0].id + '",' + e.clientX + ',' + e.clientY + ')');
}
}
break;
}
}
if(this.dragDropSourcesArray[this.numericIdToBeDragged][1]){
  if(dropped)
    this.dragObjCloneArray[this.numericIdToBeDragged].style.display='none';
  else
    this.__slideElementBackIntoItsOriginalPosition(this.numericIdToBeDragged);
}
this.dragDropTimer = -1;
DHTMLSuite.commonObj.__setTextSelOk(true);
this.numericIdToBeDragged = false;
}
,
__slideElementBackIntoItsOriginalPosition:function(numId)
{
var currentX = this.dragObjCloneArray[numId].style.left.replace('px','')/1;
var currentY = this.dragObjCloneArray[numId].style.top.replace('px','')/1;
var targetX = DHTMLSuite.commonObj.getLeftPos(this.dragDropSourcesArray[numId][0]);
var targetY = DHTMLSuite.commonObj.getTopPosScrolled(this.dragDropSourcesArray[numId][0]);;
if(this.moveBackBySliding){
this.__processSlideByPixels(numId,currentX,currentY,targetX,targetY);
}else{
this.dragObjCloneArray[numId].style.display='none';
//this.dragDropSourcesArray[numId][0].style.visibility = 'visible';
}
}
,
__processSlideByPixels:function(numId,currentX,currentY,targetX,targetY)
{
var slideX = Math.round(Math.abs(Math.max(currentX,targetX) - Math.min(currentX,targetX)) / 10);
var slideY = Math.round(Math.abs(Math.max(currentY,targetY) - Math.min(currentY,targetY)) / 10);
if(slideY<3 && Math.abs(slideX)<10)slideY = 3;	// 3 is minimum slide value
if(slideX<3 && Math.abs(slideY)<10)slideX = 3;	// 3 is minimum slide value
if(currentX > targetX) slideX*=-1;	// If current x is larger than target x, make slide value negative<br>
if(currentY > targetY) slideY*=-1;	// If current y is larger than target x, make slide value negative
currentX = currentX + slideX;
currentY = currentY + slideY;
if(Math.max(currentX,targetX) - Math.min(currentX,targetX) < 4)currentX = targetX;
if(Math.max(currentY,targetY) - Math.min(currentY,targetY) < 4)currentY = targetY;
this.dragObjCloneArray[numId].style.left = currentX + 'px';
this.dragObjCloneArray[numId].style.top = currentY + 'px';
if(currentX!=targetX || currentY != targetY){
window.thisRef = this;	// Reference to this dragdrop object
setTimeout('window.thisRef.__processSlideByPixels("' + numId + '",' + currentX + ',' + currentY + ',' + targetX + ',' + targetY + ')',5);
}else{	// Slide completed. Make absolute positioned element invisible and original element visible
this.dragObjCloneArray[numId].style.display='none';
//this.dragDropSourcesArray[numId][0].style.visibility = 'visible';
}
}
}
