function BeginDrag(pSource){
	if (pSource.userWindow.state == _userWindow_state.maximized){return;}

	window.lastX = event.clientX;
	window.lastY = event.clientY;

	document.onmousemove = function(){DoDrag(pSource)};
	document.onmouseup = function(){EndDrag(pSource)};
}

function DoDrag(pSource){
	var e = event; //setting e for other browsers later
	diffX = e.clientX - window.lastX;
	diffY = e.clientY - window.lastY;
	pSource.style.left = (parseInt(pSource.style.left, 10) + diffX) + "px";
	pSource.style.top = (parseInt(pSource.style.top, 10) + diffY) + "px";
	window.lastX = e.clientX;
	window.lastY = e.clientY;
	pSource.userWindow.savePosition();
}

function EndDrag(pSource){
	document.onmousemove = null;
	document.onmouseup = null;

}

function BeginResize(pSource){
	window.lastX = event.clientX;
	window.lastY = event.clientY;
	
	document.onmousemove = function(){DoResize(pSource)};
	document.onmouseup = function(){EndDrag(pSource)};//can be reused
}

function DoResize(pSource){
	var e = event;
	diffX = e.clientX - window.lastX;
	diffY = e.clientY - window.lastY;
	
	
	if (pSource.offsetWidth < 60){
		pSource.style.width = "65px";
		EndDrag();
	}
	if (pSource.offsetHeight < 60){
		pSource.style.height = "65px";
		EndDrag();
	}
	
	pSource.style.width = pSource.offsetWidth + diffX + "px";
	pSource.style.height = pSource.offsetHeight + diffY + "px";
	window.lastX = e.clientX;
	window.lastY = e.clientY;
	
	var spanDiffX = document.getElementById("spanDiffX");
	var spanDiffY = document.getElementById("spanDiffY");
	var spanLastX = document.getElementById("spanLastX");
	var spanLastY = document.getElementById("spanLastY");
	var spanOffsetWidth = document.getElementById("spanOffsetWidth");
	var spanOffsetHeight = document.getElementById("spanOffsetHeight");
	
	spanDiffX.innerText = diffX;
	spanDiffY.innerText = diffY;
	spanLastX.innerText = window.lastX;
	spanLastY.innerText = window.lastY;
	spanOffsetWidth.innerText = pSource.offsetWidth;
	spanOffsetHeight.innerText = pSource.offsetHeight;

	cancelEvent(e);
}

		function cancelEvent(e, c)
		{
		 e.returnValue = false;
		 if (e.preventDefault) e.preventDefault();
		 if (c)
		 {
		  e.cancelBubble = true;
		  if (e.stopPropagation) e.stopPropagation();
		 }
		};
