function drag(mainEvent, obj, horizontalOnly, verticalOnly, alternativeOnMove) { //used to do drag and drop, but is also the "dragging"-function in resize

	mainEvent = mainEvent || window.event;

	obj = id(obj);
	if(!obj) return;

	disableSelection();

	var deltaX = mainEvent.screenX - currentStyle(obj, "left");
	var deltaY = mainEvent.screenY - currentStyle(obj, "top");

	// since mousemoves happens a lot(!) while dragging it makes sense to keep these functions as simple as possible, so I guess this is less overhead than one function. ugly though
	var moveHandler = null;
	if(verticalOnly)
		moveHandler = function(e) { obj.style.top = (e.screenY - deltaY) + "px"; };
	else if(horizontalOnly)
		moveHandler = function(e) { obj.style.left = (e.screenX - deltaX) + "px"; };
	else if(alternativeOnMove)
		moveHandler = function(e) { alternativeOnMove(e, deltaY, deltaX); };
	else
		moveHandler = function(e) {
			obj.style.top = (e.screenY - deltaY) + "px";
			obj.style.left = (e.screenX - deltaX) + "px";
		};

	addEvent(document, "mousemove", moveHandler, false);
	addEvent(document, "mouseup", releaseHandler, false);

	stopEvent(mainEvent);

	function releaseHandler() {

		removeEvent(document, "mousemove", moveHandler, false);
		removeEvent(document, "mouseup", releaseHandler, false);

		enableSelection();
	}
}

function resize(e, target, scrollTop, scrollLeft) {

	//scrollTop and scrollLeft are not required but if there is a
	//chance target is inside a scrolled container this function will
	//get wrong values otherwise

	e = e || window.event;

	target = id(target);

	var resizeHeight = target.resizeHeight, resizeWidth = target.resizeWidth;
	var catchWidth = target.catchWidth || 5;

	if(resizeHeight === undefined || resizeWidth === undefined) { //i.e setCursorEvents() hasn't been used

		var resizeWidth = false, resizeHeight = false;

		if(catchWidth == "all") {
			resizeWidth = resizeHeight = true;
		} else {
			var right = currentStyle(target, "left") + currentStyle(target, "width") - (scrollLeft ? scrollLeft : 0), bottom = currentStyle(target, "top") + currentStyle(target, "height") - (scrollTop ? scrollTop : 0);

			var ex = e.clientX, ey = e.clientY;

			if((ex > (right - catchWidth) && ex <= right)) resizeWidth = true;
			if((ey > (bottom - catchWidth) && ey <= bottom)) resizeHeight = true;

		}
	}

	if(resizeHeight || resizeWidth) {

		var targetTop = currentStyle(target, "top"), targetLeft = currentStyle(target, "left");

		var newHeight = currentStyle(target, "height") - currentStyle(target, "border-top-width") - currentStyle(target, "border-bottom-width"), newWidth = currentStyle(target, "width") - currentStyle(target, "border-left-width") - currentStyle(target, "border-right-width");
		var oldHeight, oldWidth;

		var ratio = newHeight / newWidth;

		if(newHeight < newWidth) { //måste låsa så den inte tappar proportioner, vet inte om det här är nåt att ha
			if(ratio > 1) minHeight = 1, minWidth = ratio;
			else minHeight = ratio, minWidth = 1;
		} else {
			if(ratio > 1) minWidth = 1, minHeight = ratio;
			else minWidth = ratio, minHeight = 1;
		}

		drag(e, target, false, false, function(e, deltaY, deltaX) {

			oldHeight = newHeight;
			oldWidth = newWidth;

			if(resizeHeight) {

				newHeight = e.clientY - targetTop;
				if(newHeight > 0) {

					target.style.height = newHeight + "px";

					if(e.shiftKey) {
						newWidth = oldWidth + (newHeight - oldHeight) / ratio; //must work on the real values, otherwise small errors in rounding will accumulate
						target.style.width = Math.round(newWidth) + "px";
					}
				}
			}

			if(resizeWidth) {

				newWidth = e.screenX - targetLeft;

				if(newWidth > 0) {

					target.style.width = newWidth + "px";

					if(e.shiftKey) {
						newHeight = oldHeight + (newWidth - oldWidth) * ratio;
						target.style.height = Math.round(newHeight) + "px";
					}
				}

			}
		});

		return true;
	} else
		return false;
}

function setCursorEvents(target, choice, settings) { //settings in objectform because this is a general function, different choices might need different settings

	target = id(target);
	if(!target) return false;
	var that = this;

	if(choice == "resize") {

		var keepCursor = false;
		var catchWidth = (!settings || (!settings.catchWidth && settings.catchWidth !== 0)) ? 5 : settings.catchWidth;

		target.catchWidth = catchWidth;

		addEvent(target, "mousemove", function(e) {

			e = e || window.event;
			var target = e.target || e.srcElement;

			if(!keepCursor) {

				var right = currentStyle(target, "left") + currentStyle(target, "width") - 0, bottom = getFixedPosition(target, "top") + currentStyle(target, "height");

				var ex = e.clientX, ey = e.clientY;

				var resizeWidthCursor = false, resizeHeightCursor = false;

				if((ex > (right - catchWidth) && ex <= right)) resizeWidthCursor = true;
				if((ey > (bottom - catchWidth) && ey <= bottom)) resizeHeightCursor = true;

				target.resizeHeight = resizeHeightCursor;
				target.resizeWidth = resizeWidthCursor;

				if(resizeWidthCursor && resizeHeightCursor) target.style.cursor = "nw-resize";
				else if(resizeWidthCursor) target.style.cursor = "ew-resize";
				else if(resizeHeightCursor) target.style.cursor = "ns-resize";
				else {
					var cssCode;
					if(cssCode = target.getAttribute("style")) {
						cssCode = cssCode.replace(/\bcursor:\s?[a-zA-Z-]*\;/, "");
						target.setAttribute("style", cssCode);
					}
				}
			}
		});

		addEvent(target, "mousedown", function() {

			//lock the cursor and set it to body to prevent flickering
			keepCursor = true;
			document.body.style.cursor = target.style.cursor;
		});

		addEvent(document, "mouseup", function() { //reset and unlock the cursor
			document.body.style.cursor = target.style.cursor = "";
			keepCursor = false;
		});

		return true;
	}

	return false;
}
