mouseY = 0;
mouseX = 0;
mouseButton = 0;

draggingObj = null;

dom = document.getElementById ? true : false;
ie = document.all ? true : false;

document.onmousedown = mouseDown;
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
document.onmousewheel = mouseWheel;

// window.onresize = reloadPage;
function reloadPage(){
	history.go(0);
}

function mouseWheel(e) {
	callEvent(getTarget(e).id, "onmousewheel", e);
}

function mouseDown(e) {
	if (getMouseButton(e) == 1) {
		draggingObj = findElementBySuffix(e, "_draggable");
	
		if (draggingObj != null) {
			getMouse(e);
			callEvent(draggingObj.id, "onbeforedrag", e);
			return false;
		}
	}

	return true;
}

function mouseUp(e) {
	mouseButton == 0;
	if (draggingObj != null) {
		callEvent(draggingObj.id, "onafterdrag", e);
		draggingObj = null;
		getMouse(e);
		return false;
	}
	return true;
}

function mouseMove(e) {
	if (draggingObj != null) {
		getMouse(e);
		callEvent(draggingObj.id, "ondrag", e);
		return false;
	}
	return true;
}

function getMouseButton(e) {
	if (ie) {
		mouseButton = event.button;
	} else if (dom){
		mouseButton = e.which;
    }
	
	return mouseButton;
}

function getMouse(e) {
	if (ie) {
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
		if (draggingObj != null && mouseButton != 1) {
			draggingObj = null;
		}
	} else if (dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
    }
}

function findElementBySuffix(e, suffix) {
	tmpObj = null
	if (mouseButton == 1) {
		tmpObj = findParentBySuffix(getTarget(e), suffix);
	}
	return tmpObj;
}


function findParentBySuffix(obj, suffix) {
	tmpObj = null;
	parentObj = obj;
	if (obj.id.indexOf(suffix) != -1) {
		tmpObj = obj;
	} else {
		if (parentObj.tagName != "BODY" && parentObj.tagName != "HTML") {
			do {
				parentObj = obj.offsetParent;
				obj = obj.offsetParent;
				if (parentObj != null) {
					if (parentObj.id.indexOf(suffix) != -1) {
						tmpObj = parentObj;
						break;
					}
				}
			} while (parentObj != null && parentObj.tagName != "BODY");
		}
	}
	
	return tmpObj;
}

function getTarget(e) {
	if (ie) {
		return event.srcElement;
	} else if (dom) {
		return e.target;
	}
}


function callEvent(rootId, eventName, eventObj) {
	callGlobal = true;
	if (eval("window." + rootId.split("_")[0] + "_" + eventName)) {
		callGlobal = eval(rootId.split("_")[0] + "_" + eventName + "(eventObj)");
	}
	if (callGlobal && eval("window._" + eventName)) {
		eval("window._" + eventName + "(eventObj)");
	}
}

function getY(objId) {
	var parentObj = null;
	var offsetY = 0;
	var obj;
	
	if (ie) {
		obj = document.all[objId];
	} else if (dom) {
		obj = document.getElementById(objId);
	}
	do {
		offsetY += obj.offsetTop;
		parentObj = obj.offsetParent.tagName;
		obj = obj.offsetParent;
	} while (parentObj != 'BODY');

	return offsetY;
}