function initScroller() {
	contentdiv = document.getElementById('contentdiv');
	content = document.getElementById('content');
	dummy = document.getElementById('dummy');
	scroller = document.getElementById('scrollera');
	
	scroller.style.position="relative";	
	maxheight = parseInt(document.getElementById('scrollertd').offsetHeight) - 1;
	scroller.onmousedown = mousedown;
	maxtop = scroller.offsetTop;
	
	if(dummy.offsetHeight > contentdiv.offsetHeight) {
		contentdiv.style.overflow = "hidden";
		contentdiv.style.width = "483px";
		document.getElementById('scroller').style.visibility = "visible";
		scroller.style.display = "";
		/*temp = (((100 * contentdiv.offsetHeight) / dummy.offsetHeight) * maxheight) / 100;
		scroller.style.height = temp > 50 ? temp + "px" : 50 + "px";*/
		scroller.style.height = "50px";
		allowscroll=1;
		if(IE) {
			contentdiv.onscroll = autoControl;
		}
		else {
			autoControl();
			autotimer = window.setInterval("autoControl()",500);
		}
	}
	else {
		document.getElementById('scroller').style.visibility = "hidden";
		scroller.style.display = "none";
		allowscroll=0;
	}
	
	if((parseInt(scroller.style.top) * 1) + (parseInt(scroller.style.height) * 1) > maxheight) {
		scroller.style.top = maxheight - parseInt(scroller.style.height);
	}
}

function scrolldown(i) {
	if(dummy.offsetHeight > contentdiv.offsetHeight) {
		contentdiv.scrollTop += i;
		scroller.style.position="relative";
		if(IE) {
			z = 0;
		}
		else {
			z = 2;
		}
		scroller.style.top = (((contentdiv.scrollTop * 100) / (dummy.offsetHeight - contentdiv.offsetHeight)) * ((maxheight - parseInt(scroller.style.height))) / 100) - z + "px";
	}
}

function scrollup(i) {
	if(dummy.offsetHeight > contentdiv.offsetHeight) {
		contentdiv.scrollTop -= i;
		scroller.style.position="relative";
		scroller.style.top = (((parseInt(contentdiv.scrollTop) * 100) / (parseInt(dummy.offsetHeight) - parseInt(contentdiv.offsetHeight))) * (maxheight - parseInt(scroller.style.height))) / 100 + "px";
	}
}

function startScrollDown() {
	aktiv = window.setInterval("scrolldown(10)",50);
}

function stopScrollDown() {
	window.clearInterval(aktiv);
}

function startScrollUp() {
	aktiv = window.setInterval("scrollup(10)",50);
}

function stopScrollUp() {
	window.clearInterval(aktiv);
}

function mousedown(e) {
	if(allowscroll == 1) {
		setscroller=1;
	}
	if(IE) {
		startpos = event.y;
	}
	else {
		temp = parseInt(scroller.style.top);
		startpos = e.pageY - maxtop - temp;
	}
	return false;
}

function mouseup() {
	setscroller=0;
	return false;
}

function getMouseXY(e) {
	if(setscroller == 1) {
		if (IE) {
			tempY = event.clientY;
		}
		else {
			tempY = e.pageY;
		}  
		if (tempY < 0){tempY = 0;}

		if( ((tempY - maxtop) - startpos) >= 0 && ((tempY - maxtop) - startpos) < (maxheight - parseInt(scroller.style.height))) {
			scroller.style.top = (tempY - maxtop) - startpos + "px";
			temptest = (((parseInt(scroller.style.top) * 100) / (maxheight - parseInt(scroller.style.height))) * (parseInt(dummy.offsetHeight) - parseInt(contentdiv.offsetHeight))) / 100;
			contentdiv.scrollTop = temptest;
		}
		else if(((tempY - maxtop) - startpos) < 0) {
			scroller.style.top = "0px";
			contentdiv.scrollTop = "0";
		}
		else {
			scroller.style.top = maxheight - parseInt(scroller.style.height) + "px";
			contentdiv.scrollTop = dummy.offsetHeight - contentdiv.offsetHeight + 10;			
		}
	}
	return false;
}

function getmousewheel(e) {
	if(IE) {
		if(event.wheelDelta >= 120) {
			scrollup(30);
		}
		if(event.wheelDelta <= -120) {
			scrolldown(30);
		}
	}
	else {
		if(e.detail <= -3) {
			scrollup(30);
		}
		if(e.detail >= 3) {
			scrolldown(30);
		}
	}
}

function autoControl() {
	if(allowscroll == 1) {
		scroller.style.position="relative";
		scroller.style.top = (((contentdiv.scrollTop * 100) / (dummy.offsetHeight - contentdiv.offsetHeight)) * (maxheight - parseInt(scroller.style.height))) / 100;
	}
}

function keyup(e) {
	if(IE) {
		keyCode = event.keyCode;
	}
	else {
		keyCode = e.keyCode;
	}
	
	if(keyCode == 33) {
		scrollup(230);
	}
	else if(keyCode == 34) {
		scrolldown(230);
	}
}

var IE = document.all?true:false;
var aktiv;
var contentdiv;
var content;
var dummy;
var setscroller=0;
var tempY = 0;
var maxtop;
var maxheight;
var scrollerheight;
var scroller;
var startpos;
var contentOffsetHeight;
var allowscroll;
var autotimer;

if (!IE) {
	document.captureEvents(Event.MOUSEMOVE)
	window.addEventListener('DOMMouseScroll', getmousewheel, false);
}

document.onmousemove = getMouseXY;
document.onmouseup = mouseup;
document.onmousewheel = getmousewheel;
document.onkeyup = keyup;

window.onload = initScroller;
window.onresize = initScroller;