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 SetMoveItem(srcObj)
{
	g_source = srcObj;
	if (g_browser == "IE")
		document.body.setCapture();
}

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.pixelLeft = e.clientX + document.body.scrollLeft;
		movingObj.style.pixelTop = e.clientY + document.body.scrollTop + 10;

	    if (srcElement.getAttribute("sort") == null)
	    {
	        if (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;<font color='yellow'><b>"+g_source.name+"</b></font><br><br> &nbsp;&nbsp;移动到 &nbsp;&nbsp;<br><br>&nbsp;<font color='white'><b>"+g_target.name+"</b></font>&nbsp;<br><br>";
		}
		else
		{
			g_target = null;
			movingObj.innerHTML = "<br>&nbsp;<font color='yellow'><b>"+g_source.name+"</b></font>&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 (g_browser == "IE")
		document.releaseCapture();
}

function ResetMoveItem()
{
	var e = (arguments.length > 0 ? arguments[0] : event);
	var srcElement = e.srcElement || e.target;
	if (srcElement.getAttribute("sort") == null)
	{
	    if (srcElement.parentNode == null || srcElement.parentNode.getAttribute("sort") == null)
	        srcElement = null;
	    else
	        srcElement = srcElement.parentNode;
    }
	
	if (srcElement == null)
		ResetMove();
	else
		SetMoveItem(srcElement);
}

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 (g_browser == "IE")
	{
		document.body.onmouseup = EndMove;
		document.body.onmousemove = MoveItem;
		document.body.onmousedown = ResetMoveItem;
		document.body.onselectstart = Body_OnSelectStart;
	}
	else
	{
		document.onmouseup = function(e){EndMove(e);};
		document.onmousemove = function(e){MoveItem(e);};
		document.onmousedown = function(e){ResetMoveItem(e);};
	}
}

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