var FONT_SIZE_POLL_INTERVAL = 500; // Miliseconds
var WHEEL_INCREMENT = 15; // Pixels
var dragOffsetY;
var dragStartY;

function _onbeforedrag(e) {
	dragStartY = getY(draggingObj.id);
	dragOffsetY = mouseY - dragStartY;
}

function _ondrag(e) {
	thumbContainerObj = document.getElementById(draggingObj.id.split("_")[0] + "_thumb");
	thumbHeight = thumbContainerObj.offsetHeight;
	contentObj = document.getElementById(draggingObj.id.split("_")[0] + "_content");
	contentHeight = contentObj.offsetHeight;
	ratio = (contentHeight - thumbHeight) / (thumbHeight - draggingObj.offsetHeight);
	
	
	newY = mouseY - dragOffsetY - dragStartY + (dragStartY - getY(thumbContainerObj.id));
	contentY = Math.ceil(newY * ratio);
	if (newY < 0) {
		newY = 0;
		contentY = 0;
	}
	if (newY > thumbHeight - draggingObj.offsetHeight) {
		newY = thumbHeight - draggingObj.offsetHeight;
		contentY = contentHeight - thumbHeight;
	}
	draggingObj.style.top = newY + "px";
	contentObj.style.top = -contentY + "px";
}

function _onmousewheel(e) {
	contentObj = null;
	obj = getTarget(e);
	parentObj = obj;
	if (obj.id.indexOf("_content") != -1) {
		contentObj = obj;
	} else {
		if (parentObj.tagName != "BODY") {
			do {
				parentObj = obj.offsetParent;
				obj = obj.offsetParent;
				if (parentObj.id.indexOf("_content") != -1) {
					contentObj = parentObj;
					break;
				}
			} while (parentObj.tagName != 'BODY');
		}
	}
	
	if (contentObj) {
		thumbObj = document.getElementById(contentObj.id.split("_")[0] + "_draggable");
		thumbContainerObj = document.getElementById(thumbObj.id.split("_")[0] + "_thumb");
		thumbHeight = thumbContainerObj.offsetHeight;
		contentHeight = contentObj.offsetHeight;
		
		if (contentHeight > thumbHeight) {
			ratio = (contentHeight - thumbHeight) / (thumbHeight - thumbObj.offsetHeight);
			
			contentY = (getY(thumbContainerObj.id) - getY(contentObj.id)) - ((event.wheelDelta / 120) * WHEEL_INCREMENT)
			newY = Math.ceil(contentY / ratio);;
			
			if (newY < 0) {
				newY = 0;
				contentY = 0;
			}
			if (newY > thumbHeight - thumbObj.offsetHeight) {
				newY = thumbHeight - thumbObj.offsetHeight;
				contentY = contentHeight - thumbHeight;
			}
	
			thumbObj.style.top = newY + "px";
			contentObj.style.top = -contentY + "px";
		}
	}
}


scroll1LastHeight = 0;

function initScrollbox() {
	checkScrollboxSizes();
	setInterval("checkScrollboxSizes()", FONT_SIZE_POLL_INTERVAL);
}

function checkScrollboxSizes() {
	contentObj = document.getElementById("scrollBox1_content");
	thumbObj = document.getElementById("scrollBox1_thumb");
	draggableObj = document.getElementById("scrollBox1_draggable");
	if (contentObj.offsetHeight != scroll1LastHeight) {
		if (contentObj.offsetHeight > thumbObj.offsetHeight) {
			if (thumbObj.style.visibility != "visible") {
				thumbObj.style.visibility = "visible";
			}
		} else {
			if (thumbObj.style.visibility != "hidden") {
				thumbObj.style.visibility = "hidden";
			}
		}
		contentObj.style.top = "0px";
		draggableObj.style.top = "0px";
	}
	scroll1LastHeight = contentObj.offsetHeight;
}


