var	maincontent;
var	textcontent;
var	scrollArea;
var	scroller;
var	buttonUp;
var	buttonDown;
var	scrolldocH = 0;
var	scrollcontH = 0;
var	scrollAreaH = 0;
var	scrollH = 0;
var	scrollDist= 0;
var	scrollInterval = 0;
var	scrollDirection = 0;
var scrollEnabled = true;
	
function initScrollbar()
{
	resetScrollbar();
	
	//setScrollbar();
}

function resetScrollbar()
{
	maincontent = document.getElementById("maincontent");
	textcontent = document.getElementById("textcontent");
	scroller 	= document.getElementById("scroller");
	scrollArea 	= document.getElementById("scrollArea");
	buttonUp 	= document.getElementById("scrollButtonUp");
	buttonDown 	= document.getElementById("scrollButtonDown");
	
	scrollArea.style.display = "none";
	buttonUp.style.display = "none";
	buttonDown.style.display = "none";
	
	if (window.addEventListener) {
		/** DOMMouseScroll is for mozilla. */
		window.addEventListener('DOMMouseScroll', onWheel, false);
		/** mousewheel is for chrome. */
		window.addEventListener('mousewheel', onWheel, false);
	} else {
		/** IE/Opera. */
		window.onmousewheel = document.onmousewheel = onWheel;
	}
	
	buttonUp.onmousedown = onButtonUp;
	buttonUp.onmouseup = resetTimer;
	buttonDown.onmousedown = onButtonDown;
	buttonDown.onmouseup = resetTimer;
}

function setScrollbar()
{
	if (!scrollArea) return;
	
	scrollArea.style.display = "block";
	
	scrollcontH = xHeight(maincontent);
	
	var scrollAreaHeight = scrollcontH - 16;
	var buttonDownTop = scrollcontH - 8;
		
	scrolldocH  = xHeight(textcontent);
	scrollAreaH = xHeight(scrollArea);
	
	textcontent.style.top = 0;
	scroller.style.top = 0;

	//calculate height of scroller and resize the scroller div
	//(however, we make sure that it isn't to small for long pages)
	scrollH = Math.round((scrollcontH * scrollAreaH) / scrolldocH);
			
	if (scrollH < 15) scrollH = 15;
	
	scroller.style.height = scrollH+"px";
	
	if (scrollH < scrollAreaH)
	{
		scrollEnabled = true;
		
		scrollArea.style.display = "block";
		buttonUp.style.display = "block";
		buttonDown.style.display = "block";
	}
	else
	{
		scrollEnabled = false;
		
		scrollArea.style.display = "none";
		buttonUp.style.display = "none";
		buttonDown.style.display = "none";
	}
	
	//what is the effective scroll distance once the scoller's height has been taken into account
	scrollDist = Math.round(scrollAreaH - scrollH);
	
	Drag.init(scroller, null, 0, 0, -1, scrollDist);
	
	scroller.onDrag = function (x,y)
	{
		var scrollY = scroller.style.top;
		scrollY = scrollY.substr(0, scrollY.length-2);
		scrollY = parseInt(scrollY);
		var docY = 0 - (scrollY * (scrolldocH - scrollcontH) / scrollDist);
		docY = Math.round(docY);
		
		textcontent.style.top = docY+"px";
	}
}

function scrollHandle(delta)
{
	var multiplier = 9;
	var scrollY = scroller.style.top;
	scrollY = scrollY.substr(0, scrollY.length-2);
	scrollY = parseInt(scrollY);
	
	if (delta < 0) {
		if (scrollY <= scrollDist) {
			if ((scrollY + multiplier) > scrollDist) {
				scrollY = scrollDist;
			}
			else {
				scrollY -= (delta * multiplier);
			}
		}
	} else {
		if (scrollY > 0) {
			if ((scrollY - multiplier) < 0) {
				scrollY = 0;
			}
			else {
				scrollY -= (delta * multiplier);
			}
		}
	}
	
	if (scrollY < 0) scrollY = 0;
	if (scrollY > scrollDist) scrollY = scrollDist;
	
	var docY = 0 - (scrollY * (scrolldocH - scrollcontH) / scrollDist);
	docY = Math.round(docY);
	
	scroller.style.top = scrollY+"px";
	textcontent.style.top = docY+"px";
}

function startTimer()
{
	scrollInterval = window.setInterval("startMove()", 100);
}

function startMove()
{
	scrollHandle(scrollDirection);
}

function resetTimer()
{
	scrollDirection = 0;
	if (scrollInterval != undefined) {
		window.clearInterval(scrollInterval);
	}
}

function onButtonUp()
{
	scrollDirection = 1;
	scrollHandle(scrollDirection);
	startTimer();
	
}

function onButtonDown()
{
	scrollDirection = -1;
	scrollHandle(scrollDirection);
	startTimer();
}

function onWheel(e)
{
	if (scrollEnabled)
	{
		// except the scrollDirection is REVERSED, and the event isn't normalized! one more line to normalize that:
		var isMozilla = false;
		var ieMultiply = 1;
		
		if (!e) {/* For IE. */
			e = window.event;
			ieMultiply = 2;
		}
		
		if (e.detail) 
			isMozilla = true;
		
		var delta = e[(!isMozilla ? "wheelDelta" : "detail")] * (!isMozilla ? 1 : -1);
		
		if (delta < 0)
			delta = -1;
		else
			delta = 1;
		
		scrollHandle(delta * ieMultiply);
	}
}

function destroyScrollbar() 
{
	document.onmousewheel=function() { inherited(arguments); }
	
	/** DOMMouseScroll is for mozilla. This kills the window mousewheel scrolling */
	if (window.removeEventListener)
		window.removeEventListener('DOMMouseScroll', mozillaScrollHandler, false);
}
