var g_source = null;
var g_target = null;
var g_callback = null;
var g_submit = null;
var g_jumpTo = null;
var g_action = null;

function ResetMove()
{
	g_source = null;
	g_target = null;
	document.body.style.cursor="";
}

function GetAbsoluteLeft(Obj)
{ 
	var Left=Obj.offsetLeft;
	if(Obj.offsetParent.tagName == "BODY")
		 return Left;
	else
		return Left + GetAbsoluteLeft(Obj.offsetParent);
}

function MoveItem()
{
	if (g_source != null)
	{
		var e = (arguments.length > 0 ? arguments[0] : event);
		var srcElement = e.srcElement || e.target;

		document.body.style.cursor="move";
		
		var movingObj = document.getElementById("Moving");
		movingObj.style.display = "";
		movingObj.style.left = e.clientX + document.body.parentNode.scrollLeft + "px";
		movingObj.style.top = e.clientY + document.body.parentNode.scrollTop + 10 + "px";

	    if (srcElement.getAttribute("sort") == null)
	    {
	        if (srcElement.parentNode == null || typeof(srcElement.parentNode.getAttribute) != "function" || srcElement.parentNode.getAttribute("sort") == null)
	            srcElement = null;
	        else
	            srcElement = srcElement.parentNode;
        }
    	
		if(srcElement != null && srcElement.getAttribute("sort") != g_source.getAttribute("sort"))
		{
			g_target = srcElement;
			movingObj.innerHTML = "<br>&nbsp;<span style='color:#FF0;font-weight:bold' class='" + _lang + "Font'>"+g_source.name+"</span><br><br> &nbsp;&nbsp;移动到 &nbsp;&nbsp;<br><br>&nbsp;<span style='color:#FFF;font-weight:bold' class='" + _lang + "Font'>"+g_target.name+"</span>&nbsp;<br><br>";
		}
		else
		{
			g_target = null;
			movingObj.innerHTML = "<br>&nbsp;<span style='color:#FF0;font-weight:bold' class='" + _lang + "Font'>"+g_source.name+"</span>&nbsp;<br>";
		}
	}
}

function EndMove()
{
	if (g_source != null && g_target != null)
	{
		if (g_submit != null)
			window.location.href = g_submit + "?Action=" + g_action + "&From=" + g_source.getAttribute("sort") + "&To=" + g_target.getAttribute("sort") + "&JumpTo=" + escape(g_jumpTo);
		else
			g_callback(g_source.getAttribute("sort"), g_target.getAttribute("sort"), g_action);
	}
	
	if (g_source != null)
	    g_source.className = "Move";
	
	ResetMove();
	document.getElementById("Moving").style.display="none";
	if (navigator.userAgent.indexOf("Gecko") == -1)
		document.releaseCapture();
}

function ResetMoveItem(e)
{
	e = e || window.event;
	var srcElement = e.srcElement || e.target;
	if (srcElement.getAttribute("sort") == null)
	{
	    if (srcElement.parentNode == null || typeof(srcElement.parentNode.getAttribute) != "function" || srcElement.parentNode.getAttribute("sort") == null)
	        srcElement = null;
	    else
	        srcElement = srcElement.parentNode;
    }
	
	if (srcElement == null)
	{
		ResetMove();
	}
	else
	{
	    g_source = srcElement;
	    if (navigator.userAgent.indexOf("Gecko") == -1)
	    {
		    document.body.setCapture();
		}
		else
		{
            e.preventDefault();
            e.stopPropagation();
		}
	}
}

function Body_OnSelectStart()
{
	if (g_source == null)
		return true;
	else
		return false;
}

function InitMove(submit, action, jumpTo)
{
    g_submit = submit;
    g_action = action;
    g_jumpTo = jumpTo;
	if (arguments.length == 4)
		g_callback = arguments[3];
    
	if (navigator.userAgent.indexOf("Gecko") == -1)
	{
		document.body.onmouseup = EndMove;
		document.body.onmousemove = MoveItem;
		document.body.onmousedown = ResetMoveItem;
		document.body.onselectstart = Body_OnSelectStart;
	}
	else
	{
		document.addEventListener("mousedown",ResetMoveItem,true);
		document.addEventListener("mousemove",MoveItem,true);
		document.addEventListener("mouseup",EndMove,true);
	}
}

document.write("<div id=\"Moving\" style=\"display:none\"></div>");
