// tools to manage drag actions IE & Standards compliant
// look for a grip element and then find a drag element. 
// If not drag element then move the grip.
// when a move is found 

var currentX = currentY = 0;
var draggingEl = null;

document.onmousedown = stdGrabEl;
document.onmousemove = stdMoveEl;
document.onmouseup = stdDropEl;
document.onselectstart = stdCheckEl;
function stdGrabEl(e) {
	var gripEl = null;
	var dragEl = null;
	if (!e) {
		var e = window.event;
	} else {
	}
	var targ;
	
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	if (e.target && !e.srcElement) {
		// Standards
		gripEl = targ;
		if ("undefined" == typeof gripEl) return;
		if (null == gripEl) return;
		if ("undefined" == typeof gripEl.id) return;
		if (null == gripEl.id) return;
		if ("string" != typeof gripEl.id) return;
	    while (gripEl.id.indexOf("GRIP") == -1) {
	        gripEl = gripEl.parentNode;
			if ("undefined" == typeof gripEl) return;
			if (null == gripEl) return;
			if ("undefined" == typeof gripEl.id) return;
			if (null == gripEl.id) return;
			if ("string" != typeof gripEl.id) return;
	    }
		// see if a parent is a drag element
		dragEl = gripEl;
		var foundDrag = true;
	    while (dragEl.id.indexOf("DRAG") == -1) {
	        dragEl = dragEl.parentNode;
			if ("undefined" == typeof dragEl) {
				foundDrag = false;
				break;
			}
			if (null == dragEl) {
				foundDrag = false;
				break;
			}
			if ("undefined" == typeof dragEl.id) {
				foundDrag = false;
				break;
			}
			if (null == dragEl.id) {
				foundDrag = false;
				break;
			}
			if ("string" != typeof dragEl.id) {
				foundDrag = false;
				break;
			}
		}
	} else {
		// IE /safari
	    gripEl = targ;
	    while (typeof gripEl.id == "string" && gripEl.id.indexOf("GRIP") == -1) {
		//if (debug) alert(gripEl.id)
	        gripEl = gripEl.parentElement;
	        if (gripEl == null) { 
				//window.status="failed GRIP grab..."; 
				return;
			}
	    }
		// see if a parent is a drag element
		dragEl = gripEl;
		var foundDrag = true;
	    while (typeof dragEl.id == "string" && dragEl.id.indexOf("DRAG") == -1) {
	        dragEl = dragEl.parentElement;
			if (dragEl == null) {
				foundDrag = false;
				break;
			}
		}
		if (foundDrag && typeof dragEl.id != "string") return;
	}

    if (foundDrag) {
		draggingEl = dragEl;
	} else {
		draggingEl = gripEl;
	}
	
	document.onmousemove = stdMoveEl;
	document.onmouseup = stdDropEl;
	
	currentX = 0;
	currentY = 0;
	if (e.pageX || e.pageY)
	{
		currentX = e.pageX;
		currentY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		currentX = e.clientX + document.body.scrollLeft;
		currentY = e.clientY + document.body.scrollTop;
	}

	if ("undefined" != typeof draggingEl.actionObject) {
		switch (typeof (draggingEl.actionObject.grabAction)) {
		case "undefined":
			break;
		case "function":
			draggingEl.actionObject.grabAction(currentX, currentY, e);
			break
		}
	}
}

function stdMoveEl(e) {
    if (draggingEl == null) { return };
	if (!e) var e = window.event;
	var newX = 0;
	var newY = 0;
	if (e.pageX || e.pageY)
	{
		newX = e.pageX;
		newY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		newX = e.clientX + document.body.scrollLeft;
		newY = e.clientY + document.body.scrollTop;
	}
    distanceX = (newX - currentX);
    distanceY = (newY - currentY);
    currentX = newX;
    currentY = newY;
	var dragid = draggingEl.id;
	
	hasAction = false;
	if ("undefined" != typeof draggingEl.actionObject) {
		switch (typeof (draggingEl.actionObject.moveAction)) {
		case "undefined":
			break;
		case "function":
			hasAction = true;
			break
		}
	}

	if (hasAction) {
		draggingEl.actionObject.moveAction(distanceX, distanceY, newX, newY, e);
	} else {
    	draggingEl.style.left = (parseInt(draggingEl.style.left) + distanceX)+'px';
		draggingEl.style.top  = (parseInt(draggingEl.style.top) + distanceY)+'px';
	}

    e.returnValue = false;
	//window.status = "move "+gripEl.id+" to "+newX+", "+newY
}

function stdDropEl() {
 	if (draggingEl != null) {
		if ("undefined" != typeof draggingEl.actionObject) {
			switch (typeof (draggingEl.actionObject.dropAction)) {
			case "undefined":
				break;
			case "function":
				draggingEl.actionObject.dropAction();
				break
			}
		}
	}
	draggingEl = null;
	document.onmousemove = null;
	document.onmouseup = null;
}

function stdCheckEl() {
	// if draging disable select
    if (draggingEl!=null) { return false }
}



