	
	
var g_objDragSource = null;
var g_arrDragObjects = new Array();
	
	
function InitDragHandling()
{	
	document.onmousedown = DragCheckOnMouseDown;
	document.onmouseup = DragMouseUp;
	document.onmousemove = DragShowMovement;
}	
	
function CreateNewDraggableObject(strName)
{	
	var objDraggable = new clsDraggableDiv();
	
	
	objDraggable.m_strName = strName;
	
	g_arrDragObjects[g_arrDragObjects.length] = objDraggable;
	
	//alert(objDraggable.m_strName);
	
	
	return objDraggable;
}	
	
function DragCheckOnMouseDown(evt)
{	
	var intX = 0, intY = 0;
	
	
	/*	
	MouseDown
	 layerX, layerY, modifiers, pageX, pageY, screenX, screenY, target, type, which
	 altKey, button, cancelBubble, clientX, clientY, ctrlKey, offsetX, offsetY, returnValue, screenX, screenY, srcElement, shiftKey, type, x, y   
	 
	MouseMove
	 layerX, layerY, pageX, pageY, screenX, screenY, target, type
	 altKey, cancelBubble, clientX, clientY, ctrlKey, offsetX, offsetY, screenX, screenY, srcElement, shiftKey, type, x, y  
	 
	MouseOut 
	 layerX, layerY, pageX, pageY, screenX, screenY, target, type 
	 altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, offsetX, offsetY, screenX, screenY, srcElement, shiftKey, toElement, type, x, y  
	 
	MouseOver 
	 layerX, layerY, pageX, pageY, screenX, screenY, target, type 
	 altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, offsetX, offsetY, returnValue, screenX, screenY, srcElement, shiftKey, toElement, type, x, y  
	 
	MouseUp
	 layerX, layerY, modifiers, pageX, pageY, screenX, screenY, target, type, which
	 altKey, button, cancelBubble, clientX, clientY, ctrlKey, offsetX, offsetY, returnValue, screenX, screenY, srcElement, shiftKey, type, x, y
	*/	
	
	if (evt)
	{	
		intX = evt.clientX;
		intY = evt.clientY;
	}	
	else
	{	
		intX = event.x;
		intY = event.y;
	}	
	
	for (i = 0; i < g_arrDragObjects.length; i++)
	{	
		if (g_arrDragObjects[i].isDragged(evt, intX, intY))
		{	
			g_objDragSource = g_arrDragObjects[i];
			
			g_objDragSource.drag(intX, intY);
			
			if (document.selection && document.selection.empty) 
				document.selection.empty();
			
			document.body.onselectstart = function() { event.returnValue = false; };
		}	
	}	
}	
	
function DragShowMovement(evt)
{	
	var blnEntered = false;
	var intX = 0, intY = 0;
	
	
	if (evt)
	{	
		intX = evt.pageX;
		intY = evt.pageY;
	}	
	else
	{	
		intX = event.x;
		intY = event.y;
	}	
	
	if (g_objDragSource != null)
	{	
		g_objDragSource.move(intX, intY);
	}	
}	
	
function DragMouseUp()
{	
	var blnDragged = false;
	
	
	if (g_objDragSource != null)
	{	
		for (i = 0; i < g_arrDragObjects.length && !blnDragged; i++)
		{	
			if (g_objDragSource != g_arrDragObjects[i])
			{	
				if (g_arrDragObjects[i].isEntered(g_objDragSource))
				{	
					blnDragged = true;
					
					g_arrDragObjects[i].dragDrop(g_objDragSource);
				}	
			}	
		}	
		
		if (!blnDragged)
		{	
			g_objDragSource.cancelDrag();
		}	
		
		g_objDragSource = null;
		document.body.onselectstart = null;
	}	
	
}	
	
function clsDraggableDiv()
{	
	
this.m_objDiv = null;
this.m_strName = "none";
this.m_intSaveTop = 0;
this.m_intSaveLeft = 0;
this.m_intAddTop = 0;
this.m_intAddLeft = 0;
this.m_blnEnableXMove = true;
this.m_blnEnableYMove = true;
this.m_strNCLeftExcept = "#DIV#";
this.m_strNCTopExcept = "#DIV#";
this.m_strIELeftExcept = "#TABLE#DIV#";
this.m_strIETopExcept = "#DIV#";
	
this.isDragged = function (evtTarget, intX, intY)
{	
	var blnDragged = false;
	
	
	if ( !document.all )
	{	
		blnDragged = (evtTarget.target && evtTarget.target == this.m_objDiv);
	}	
	else
	{	
		blnDragged = (event.srcElement && event.srcElement == this.m_objDiv);
	}	
	
	
	return blnDragged;
}	
	
this.convertNum = function (strNum)
{	
	strNum = strNum.substring(0, strNum.indexOf("px")); 
	
	return strNum * 1;
}	
	
this.getLeft = function()
{	
	return this.convertNum(this.m_objDiv.style.left);
}	
	
this.setLeft = function(intLeft)
{	
	this.m_objDiv.style.left = intLeft + "px";
}	
	
this.getTop = function()
{	
	return this.convertNum(this.m_objDiv.style.top);
}	
	
this.setTop = function(intTop)
{	
	this.m_objDiv.style.top = intTop + "px";
}	
	
this.getWidth = function()
{	
	return this.convertNum(this.m_objDiv.style.width);
}	
	
this.getHeight = function()
{	
	return this.convertNum(this.m_objDiv.style.height);
}	
	
this.move = function (intX, intY)
{	
	if (this.m_blnEnableXMove)
		this.setLeft(intX);
	
	if (this.m_blnEnableYMove)
	{	
		if (!document.all && this.m_objDiv.offsetParent)
			intY = intY - FindY(this.m_objDiv.offsetParent, "");
		
		this.setTop(intY);
	}	
}	
	
this.isEntered = function (objSource)
{	
	var blnEntered = false;
	
	
	if (objSource.getLeft() >= this.getLeft()
		&& objSource.getLeft() <= (this.getLeft() + this.getWidth()) 
		&& objSource.getTop() >= this.getTop()
		&& objSource.getTop() <= (this.getTop() + this.getHeight()) 
		)
	{	
		blnEntered = true;
	}	
	
	
	return blnEntered;
}	
	
this.cancelDrag = function ()
{	
	this.setTop(this.m_intSaveTop);
	this.setLeft(this.m_intSaveLeft);
	
	this.m_objDiv.style.zIndex--;
}	
	
this.drag = function (intX, intY)
{	
	this.m_intSaveTop = this.getTop();
	this.m_intSaveLeft = this.getLeft();
	
	this.m_intAddTop = intX - this.getLeft();
	this.m_intAddLeft = intY - this.getTop();
	
	this.m_objDiv.style.zIndex++;
}	
	
this.dragDrop = function (objSource)
{	
	this.onDragDrop(objSource);
}	
	
this.onDragDrop = function (objSource)
{	
	alert("dragDrop src: " + objSource.m_strName);
	alert("dragDrop trg: " + this.m_strName);
}	
	
this.setDiv = function (objDiv)
{	
	this.m_objDiv = objDiv;
	
	if ( !document.all )
	{	
		intLeft = FindX(this.m_objDiv, this.m_strNCLeftExcept);
		intTop = FindY(this.m_objDiv, this.m_strNCTopExcept);
	}	
	else
	{	
		intLeft = FindX(this.m_objDiv, this.m_strIELeftExcept);
		intTop = FindY(this.m_objDiv, this.m_strIETopExcept);
	}	
	
	this.m_objDiv.style.position = "absolute";
	
	this.setLeft(intLeft);
	this.setTop(intTop);
}	
	
this.setPositionFromObject = function(objObject)
{	
	if ( !document.all )
	{	
		intLeft = FindX(objObject, this.m_strNCLeftExcept);
		intTop = FindY(objObject, this.m_strNCTopExcept);
	}	
	else
	{	
		intLeft = FindX(objObject, this.m_strIELeftExcept);
		intTop = FindY(objObject, this.m_strIETopExcept);
	}	
	
	this.setLeft(intLeft);
	this.setTop(intTop);
}	
	
}	
	
function FindX(obj, strExcept)	
{	
	var x = 0;
	
	
	while ( obj ) 
	{	
		strTag = "#" + obj.tagName + "#";
		
		if (strExcept.indexOf(strTag) == -1)
			x += parseInt(obj.offsetLeft, 10);
		
		obj = obj.offsetParent || null;
	}	
	
	
	return x;
}	
		
function FindY(obj, strExcept)	
{	
	var y = 0;
	
	
	while ( obj ) 
	{	
		strTag = "#" + obj.tagName + "#";
		
		if (strExcept.indexOf(strTag) == -1)
			y += parseInt(obj.offsetTop, 10);
		
		obj = obj.offsetParent || null;
	}	
	
	
	return y;
}	
		
function DragOrdererDragDrop(objSource)
{	
	var objForm = eval("document.frmDragOrderer_" + objSource.m_strStructName);
	
	
	objForm.src_id.value = objSource.m_strID;
	objForm.src_position.value = objSource.m_strPosition;
	objForm.trg_position.value = this.m_strPosition;
	
	createTarget(objForm);
	
	objForm.submit();
}	
	
