// variables
var mouseover = false;
var scrolldirection;
var scrollratio;
var theheartbeat, scrolling;
var menuOffsetTop = menuOffsetLeft = menuMaxHeight = 0;
var currentX = currentY = 0;
var whichIt = null;

// events

if (!document.all) {
	window.captureEvents(Event.MOUSEDOWN);
	window.captureEvents(Event.MOUSEMOVE);
	window.captureEvents(Event.MOUSEUP);
}

document.onmousedown = grabIt;
document.onmousemove = moveIt;
document.onmouseup = dropIt;

// switch direction scroller will move while pressing a button
function switchit(obj, cmd)
{
	if (cmd == 'in')
	{
		mouseover = true;
		if (obj.getAttribute('id') == "scrollup")
		{
			scrolldirection = -1;
		}
		else
		{
			scrolldirection = 1;
		}
	}
	else
	{
		mouseover = false;
	}
}

function scroll()
{
	// if (((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))	
	if (((parseInt(scrollbox.style.top) + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((parseInt(scrollbox.style.top) + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - parseInt(scrollbox.style.height))))
	{
		scrollbox.style.top = (parseInt(scrollbox.style.top) + scrolldirection) + "px";
	}
}

function heartBeat()
{
	data.style.top = (-1 * scrollratio * (parseInt(scrollbox.style.top) + menuOffsetTop - menuOffsetTop)) + "px";
}

function grabIt(e) {
	if (e) {
		window.event = e;
	}
	if ((window.event.button == 1) || (window.event.button == 0))
	{
		if (mouseover) {
			scrolling = window.setInterval("scroll();",1);
		}	
		else
		{
			whichIt = window.event.srcElement ? window.event.srcElement : window.event.target;
			while (whichIt.id.indexOf("scrollbox") == -1) {
				whichIt = whichIt.parentElement;
				if (whichIt == null) { return true; }
		    }
		    
		    if (document.all) {
//			whichIt.style.pixelLeft = whichIt.offsetLeft;
		    	whichIt.style.pixelTop = whichIt.offsetTop;
			currentX = (window.event.clientX + document.body.scrollLeft);
			currentY = (window.event.clientY + document.body.scrollTop);			
		    } else {
		    	currentX = window.event.pageX;
			currentY = window.event.pageY;
		    }
			
			
		}
	
		theheartbeat = window.setInterval("heartBeat()",1);
	}
}

function moveIt(e) {
	if (e) {
		window.event = e;
	}	
	if (whichIt == null) { return false; }
	if (document.all) {
		newX = (window.event.clientX + document.body.scrollLeft);
		newY = (window.event.clientY + document.body.scrollTop);
	} else {
		newX = window.event.pageX;
		newY = window.event.pageY;
	}
    	distanceX = (newX - currentX);
	distanceY = (newY - currentY);
    	currentX = newX;
	currentY = newY;
	// if (((scrollbox.style.pixelTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
	if (((parseInt(scrollbox.style.top) + menuOffsetTop + distanceY) >= menuOffsetTop) && ((parseInt(scrollbox.style.top) + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - parseInt(scrollbox.style.height))))
	{
		// scrollbox.style.pixelTop += distanceY;
    		scrollbox.style.top = (parseInt(scrollbox.style.top) + distanceY) + "px";
	}
	window.event.returnValue = false;
	return false;
}

function dropIt() {
	clearInterval(scrolling);
	clearInterval(theheartbeat);
	whichIt = null;
}

function getscrollinc()
{
	var contentinc;
	var scrollerinc;
	var contentHeight = data.offsetHeight;
	var scrollerHeight = parseInt(content.style.height);

	if (scrollerHeight < contentHeight)
	{
		contentinc = parseInt(content.style.height) / contentHeight;
		scrollerinc = parseInt(floater.style.height)*contentinc;
		scrollratio = contentHeight / parseInt(floater.style.height);
	}
	else
	{
		scrollerinc = parseInt(floater.style.height);
		contentinc = 1;
	}
	scrollbox.style.height = scrollerinc + "px";
}

