// variables
var mouseover = false;
var scrolldirection;
var scrollratio;
var theheartbeat, scrolling, reset;
var menuOffsetTop = menuOffsetLeft = menuMaxHeight = 0;
var currentX = currentY = 0;
var whichIt = null;
var count = null;

// events
document.onmousedown = grabIt;
document.onmousemove = moveIt;
document.onmousewheel = moveWheel;
document.onmouseup = dropIt;
document.onkeydown = keypress;
document.onkeyup = dropIt;
reset = window.setInterval("dropIt();",5000);


// 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;
		//dropIt;
	}
}

function scroll()
{
	var sbox = document.getElementById('scrollbox');
	if (((document.getElementById('scrollbox').style.pixelTop + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((document.getElementById('scrollbox').style.pixelTop + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - document.getElementById('scrollbox').style.pixelHeight)))
	{
		document.getElementById('scrollbox').style.pixelTop += scrolldirection;
	}
	if (((parseInt(document.getElementById('scrollbox').style.top) + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((parseInt(document.getElementById('scrollbox').style.top) + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - parseInt(document.getElementById('scrollbox').style.height))))
	{
		document.getElementById('scrollbox').style.top = parseInt(sbox.style.top) + scrolldirection + "px";
	}
}

function heartBeat()
{
	document.getElementById('data').style.pixelTop = -1 * scrollratio * (document.getElementById('scrollbox').style.pixelTop + menuOffsetTop - menuOffsetTop);
}
function heartBeat_NS()
{
	scrollratio = parseInt(document.getElementById('data').offsetHeight) / parseInt(document.getElementById('floater').style.height);
	var tmp = parseInt(document.getElementById('scrollbox').style.top) *  -1 * scrollratio;
	document.getElementById('data').style.top = tmp + "px";
}

function grabIt(e) {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		if (event.button == 1)
		{
			if (mouseover)
				scrolling = window.setInterval("scroll();",1);
			else
			{
				
				whichIt = event.srcElement;
				while (whichIt.id.indexOf("scrollbox") == -1) {
					whichIt = whichIt.parentElement;
					if (whichIt == null) { return true; }
			    }
			   whichIt.style.pixelTop = whichIt.offsetTop;
				currentY = (event.clientY + document.body.scrollTop);
			}
		
			theheartbeat = window.setInterval("heartBeat()",1);
		} 
	} else {
		menuOffsetTop = parseInt(document.getElementById('floater').style.top);
		menuMaxHeight = parseInt(document.getElementById('floater').style.height);
		if (e.which == 1)
		{
			if (mouseover)	{
				scrolling = window.setInterval("scroll();",1);
			} else {
				whichIt = document.getElementById('scrollbox');
				while (whichIt.id.indexOf("scrollbox") == -1) {
					whichIt = whichIt.parentElement;
					if (whichIt == null) { return true; }
			    }
			   whichIt.style.top = whichIt.offsetTop;
				currentY = (e.screenY + document.body.scrollTop);
			}
		
			theheartbeat = window.setInterval("heartBeat_NS()",1);
		} 
	}

}

function keypress(e) {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		if (event.keyCode == 33 || event.keyCode == 34) {
			//alert(event.keyCode);
			var floatheight = parseInt(document.getElementById('floater').style.pixelHeight);
			var boxheight = parseInt(document.getElementById('scrollbox').style.pixelHeight);
			var boxtop = parseInt(document.getElementById('scrollbox').style.pixelTop);
			var offset = parseInt(document.getElementById('floater').style.pixelTop);
			var count = 20;

			if (((boxtop == 0) && (event.keyCode == 33)) || (((boxtop + boxheight) == floatheight) && (event.keyCode == 34))) {
				//alert("oben" + boxtop);
				clearInterval(theheartbeat);
				return false;
			}
			
			if (event.keyCode == 33) {
				scrolldirection = count * -1;
				if ( (boxtop + offset + scrolldirection) <= offset) {
					scrolldirection = boxtop * -1;
					//alert("boxtop: "+boxtop+", offset: "+offset+", scrolldir: "+scrolldirection);
				}
			}	
			if (event.keyCode == 34) {
				scrolldirection = count;
				if ( (boxtop + offset + scrolldirection) >= (floatheight - boxheight) ) {
					scrolldirection = (floatheight - boxheight - boxtop);
					//alert(scrolldirection);
				}
			}
			scrolling = scroll();
			//scrolling = window.setInterval("scroll();",1);
			theheartbeat = window.setInterval("heartBeat_NS()",1);
			//heartbeat_NS();
		}
	} else {		
		if (e.which == 33 || e.which == 34) {
			var floatheight = parseInt(document.getElementById('floater').style.height);
			var boxheight = parseInt(document.getElementById('scrollbox').style.height);
			var boxtop = parseInt(document.getElementById('scrollbox').style.top);
			var offset = parseInt(document.getElementById('floater').style.top);
			var count = 20;
			
			if (((boxtop == 0) && (e.which == 33)) || (((boxtop + boxheight) == floatheight) && (e.which == 34))) {
				//alert("oben" + boxtop);
				clearInterval(theheartbeat);
				return false;
			}
			
			if (e.which == 33) {
				scrolldirection = count * -1;
				if ( (boxtop + offset + scrolldirection) <= offset) {
					scrolldirection = boxtop * -1;
					//alert("boxtop: "+boxtop+", offset: "+offset+", scrolldir: "+scrolldirection);
				}
			}	
			if (e.which == 34) {
				scrolldirection = count;
				if ( (boxtop + offset + scrolldirection) >= (floatheight - boxheight) ) {
					scrolldirection = (floatheight - boxheight - boxtop);
					//alert(scrolldirection);
				}
			}
			scrolling = scroll();
			//scrolling = window.setInterval("scroll();",1);
			theheartbeat = window.setInterval("heartBeat_NS()",1);
		}
	}
}

function moveIt(e) {
	if (whichIt == null) { return false; }
	if (navigator.appName == 'Microsoft Internet Explorer') {
		newX = (event.clientX + document.body.scrollLeft);
   	newY = (event.clientY + document.body.scrollTop);
   	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)))
		{
   	 	scrollbox.style.pixelTop += distanceY;
		}
		event.returnValue = false;
	} else {
		var sbox = document.getElementById('scrollbox');
		newX = (e.screenX + document.body.scrollLeft);
   	newY = (e.screenY + document.body.scrollTop);
   	distanceX = (newX - currentX);
		distanceY = (newY - currentY);
   	currentX = newX;
		currentY = newY;
		menuOffsetTop = parseInt(document.getElementById('floater').style.top);
		menuMaxHeight = parseInt(document.getElementById('floater').style.height);
		
		if (((parseInt(sbox.style.top) + menuOffsetTop + distanceY) >= menuOffsetTop) && ((parseInt(sbox.style.top) + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - parseInt(sbox.style.height))))
		{
   	 	var tmp = parseInt(sbox.style.top) + distanceY;
   	 	sbox.style.top = tmp + "px";
		}
		e.returnValue = false;
	}
	return false;
}

function moveWheel()
{
		count = Counting(count);
		wheelScroll(count);
		return false;
}

function Counting(count){   
    if (event.wheelDelta >= 120) 
      count = -15; 
    else if (event.wheelDelta <= -120) 
        count = 15;   
    return count; 
}

function wheelScroll(count){    
	var tmp = menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight;
	var tmp1 = scrollbox.style.pixelTop + menuOffsetTop + count;
	
    if (tmp < tmp1) { count = 15-(tmp1-tmp); }
    if (tmp1 < 15) { count = (-1)*(tmp1-2); }

    if (((scrollbox.style.pixelTop + menuOffsetTop + count) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + count  ) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
	{
    	scrollbox.style.pixelTop += count;
    	scrollratio = data.offsetHeight / floater.style.pixelHeight;
    	data.style.pixelTop = -1 * scrollratio * (scrollbox.style.pixelTop + menuOffsetTop - menuOffsetTop);
	}
}


function dropIt() {
	clearInterval(scrolling);
	clearInterval(theheartbeat);
	mouseover = false;
	whichIt = null;
}

function init()
{
	menuOffsetTop = document.getElementById('floater').style.pixelTop;
	menuMaxHeight = document.getElementById('floater').style.pixelHeight;
	
	getscrollinc();
	//document.getElementById('content').focus();
	//document.getElementById('data').select();
}

function getscrollinc()
{
	var contentinc;
	var scrollerinc;
	var contentHeight;
	var scrollerHeight;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		contentHeight = document.getElementById('data').offsetHeight;
		scrollerHeight = document.getElementById('content').style.pixelHeight;
		if (scrollerHeight < contentHeight)
		{
			contentinc = document.getElementById('content').style.pixelHeight / contentHeight;
			scrollerinc = document.getElementById('floater').style.pixelHeight*contentinc;
			scrollratio = contentHeight / floater.style.pixelHeight;
		}
		else
		{
			scrollerinc = document.getElementById('floater').style.pixelHeight;
			contentinc = 1;
		}
		
		document.getElementById('scrollbox').style.height = scrollerinc;
	} else {
		contentHeight = document.getElementById('data').offsetHeight;
		scrollerHeight = parseInt(document.getElementById('content').style.height);
		if (scrollerHeight < contentHeight)
		{
			contentinc = parseInt(document.getElementById('content').style.height) / contentHeight;
			scrollerinc = parseInt(document.getElementById('floater').style.height) * contentinc;
			scrollratio = contentHeight / parseInt(document.getElementById('floater').style.height);
		}
		else
		{
			scrollerinc = parseInt(document.getElementById('floater').style.height);
			contentinc = 1;
		}
		
		document.getElementById('scrollbox').style.height = scrollerinc + "px";
	}
}