
var goupdown = 0;
var scrollabe = false;
var inScroll = false;
var dNow = new Date();
var maxOffset = 0;
var absOffset = 0;
var scrollThread;

var inDrag = false;

function updown(tal){
	goupdown = tal;
	clearInterval(scrollThread);
	scrollThread = setInterval('move()', 4);
}

document.onmousemove = mouseMove;
document.onmouseup   = stopDrag;
document.onmousedown = function() { if (inDrag) { return false; } };

function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its
	 if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

function stopIt(){
	goupdown = 0;
}

function move(){
	inScroll = true;
//	var startTime = dNow.getTime();

	if (scrollabe == false) {
		if(ElmHeight("holder") < ElmHeight("toscroll")){
			var the_up = getStyleObject("arrowup");
			var the_down = getStyleObject("arrowdown");
			the_up.visibility = "visible";
			the_down.visibility = "visible";
			document.getElementById('scroller').style.visibility = "visible";
			scrollabe = true;
		}
	}
	
	if(scrollabe){
		var the_style = getStyleObject("toscroll");
		var the_holder = getStyleObject("holder");
		
		var the_top = parseInt(the_style.top) + goupdown;
	
		if(the_top > 0){
			the_top = 0;
			goupdown = 0;
			
		}
	
		var min = (ElmHeight("holder") - ElmHeight("toscroll"));
		if(the_top < min){
			the_top = min;
			goupdown = 0;
			
		}
		
	
		if (document.layers){
			the_style.top = the_top;
		}else {
			the_style.top = the_top + "px";
		}


	// Move the scroll indicator
	
	absOffset = parseInt(the_style.top);
	if (absOffset < 0) {
		absOffset = absOffset - (absOffset * 2);
	}

//	if (maxOffset == 0) {
	maxOffset = ElmHeight("toscroll") - ElmHeight("holder");
//	}

	if (absOffset > 0) {
		var scrollerPos = ((absOffset / maxOffset) * 261) + 42;
	} else {
		var scrollerPos = 42;
	}

	var the_scroller = getStyleObject("scroller");
	the_scroller.top = scrollerPos+ "px";
	}	 
//	var endTime = dNow.getTime();

//	document.getElementById('korv').value = 'framerate: ' + (endTime - startTime);
//	document.getElementById('korv').value = 'framerate: ' + endTime;
	inScroll = false;
}


function ElmHeight(elmID) {
	if(document.getElementById(elmID).clientHeight) {
		return document.getElementById(elmID).clientHeight;
	}else {
		if(document.getElementById(elmID).offsetHeight) {
			return document.getElementById(elmID).offsetHeight;
		}
	}
}				


function startUp() {
	scrollThread = setInterval('move()',4);
}

setTimeout('startUp()', 250);
	
function startDrag() {
	inDrag = true;
	inScroll = false;
	clearInterval(scrollThread);
	document.getElementById('scroller').onDrag = function() { return false; }
	return false;
}

function stopDrag() {
	inDrag = false;
}

function mouseMove(event) {
	if (inDrag) {
			
		var scroller = document.getElementById('scroller');
		event            = event || window.event;
		var mousePos     = mouseCoords(event);
		
		// scroller.style.left = mousePos.x;
		var newY = mousePos.y;
		
		if (newY < 42) {
			newY = 42;
		} else if (newY > 261 + 42) {
			newY = 261 + 42;
	
		}
		scroller.style.top =  newY +"px";
	
		the_style = getStyleObject("toscroll");
		the_holder = getStyleObject("holder");
			
		the_top = parseInt(the_style.top);
		
		if(the_top > 0){
			the_top = 0;
		}
		
		var min = (ElmHeight("holder") - ElmHeight("toscroll"));
		
		
		var percentage = (ElmHeight("toscroll") - ElmHeight("holder")) / 261.000;
		
		the_top = -(percentage * (newY - 42));
		
			if(the_top < min){
				the_top = min;
				goupdown = 0;
			}
			
		
			if (document.layers){
				the_style.top = the_top;
			}else {
				the_style.top = the_top + "px";
			}
	
	
		return false;
	}	 
}

function mouseCoords(ev){
	if(ev.pageX != null && ev.pageY != null){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

	