var scrollBar;
var initialised = false;

function initScrollBar(divContainer, arrow_up, bar, arrow_down, speed)
{
	scrollBar = new ScrollBar(divContainer, arrow_up, bar, arrow_down, 50, speed)
	document.getElementById(divContainer).style.visibility = "visible";

	if(scrollBar.initialised)
	{
		document.getElementById(arrow_up).style.visibility = "visible";
		document.getElementById(bar).style.visibility = "visible";
		document.getElementById(arrow_down).style.visibility = "visible";

		// events
		scrollBar.arrows[0].onmousedown = scrollUp;
		scrollBar.arrows[0].onmouseup   = stopScroll;
		scrollBar.arrows[0].onmouseout  = stopScroll;

		scrollBar.arrows[1].onmousedown = scrollDown;
		scrollBar.arrows[1].onmouseup   = stopScroll;
		scrollBar.arrows[1].onmouseout  = stopScroll;

		scrollBar.bar.onmousedown = startDrag;
		document.onmouseup        = stopDrag;

		scrollBar.barPanel.onclick = scrollPage;
	}
	return scrollBar;
}

function ScrollBar(divElem, arrow1, bar, arrow2, speed, step)
{
	// hilfsvariablen
	this.initialised = false;
	this.interval;
	this.drag;

	// browser check
	this.browser = getBrowserInfo();
	if(!this.browser.dom)
		return;

	// objekte
	this.arrows    = new Array(document.getElementById(arrow1), document.getElementById(arrow2));
	this.barPanel  = document.getElementById(bar);
	this.bar       = this.barPanel.firstChild;
	this.clip      = document.getElementById(divElem).offsetHeight;
	this.scrollObj = document.getElementById(divElem).firstChild;

	while(this.scrollObj.nodeType != 1)
		this.scrollObj = this.scrollObj.nextSibling;

	// eigenschaften
	this.speed = speed;
	this.step = step;
	this.maxHeight = this.scrollObj.offsetHeight;
	this.top = 0;

	this.barPanelHeight = this.clip - (this.arrows[0].offsetHeight + this.arrows[0].offsetHeight);
	this.barHeight = this.barPanelHeight * this.clip / this.maxHeight;
	if(this.barHeight < 10)
		this.barHeight = 10;
	this.barStep = this.step * this.barHeight / this.clip;
	this.barTop = 0;

	this.barPanel.style.height = this.barPanelHeight;

	if(!this.browser.ie && !this.browser.opera)
		this.barPanel.style.position = "relative";

	this.bar.style.height = this.barHeight;
	this.bar.style.top = this.barTop;

	if(this.browser.ie)
		this.bar.style.width = this.bar.offsetWidth + 3;
	if(this.browser.opera)
		this.bar.style.width = this.bar.offsetWidth + 2;

	if(this.maxHeight < this.clip)
		return;

	this.setPosition = setPosition;

	this.initialised = true;
}

function scrollUp()
{
	if(scrollBar.interval)
		stopScroll();
	if(scrollBar.barTop >= scrollBar.barStep)
		scrollBar.interval = window.setInterval("scroll(-1)", scrollBar.speed);
}

function scrollDown()
{
	if(scrollBar.interval)
		stopScroll();

	if(scrollBar.barPanelHeight - scrollBar.barHeight - scrollBar.barTop >= scrollBar.barStep)
		scrollBar.interval = window.setInterval("scroll(1)", scrollBar.speed);
}

function scroll(direction)
{
	scrollBar.top -= (direction * scrollBar.step);
	scrollBar.barTop += (direction * scrollBar.barStep);
	scrollBar.setPosition();

	if((direction==-1 && scrollBar.barTop < scrollBar.barStep) || (direction==1 && scrollBar.barPanelHeight-scrollBar.barTop-scrollBar.barHeight < scrollBar.barStep))
		stopScroll(direction);
}

function setPosition()
{
	this.scrollObj.style.top = scrollBar.top;
	this.bar.style.top = scrollBar.barTop;
}

function stopScroll(direction)
{
	if(scrollBar.interval)
		window.clearInterval(scrollBar.interval);

	if(direction == -1)
		scrollBar.barTop = 0;
	else if(direction == 1)
		scrollBar.barTop = scrollBar.barPanelHeight - scrollBar.barHeight;

	scrollBar.bar.style.top = scrollBar.barTop;
}

function startDrag(e)
{
	if(!e)
		e = window.event;

	scrollBar.drag = e.screenY;
	document.onmousemove = handleDrag;
}

function handleDrag(e)
{
	if(!e)
		e = window.event;

	scrollBar.barTop += e.screenY - scrollBar.drag;
	if(scrollBar.barTop < 0)
		scrollBar.barTop = 0;
	else if(scrollBar.barTop > scrollBar.barPanelHeight - scrollBar.barHeight)
		scrollBar.barTop = scrollBar.barPanelHeight - scrollBar.barHeight;

	scrollBar.top = (-1) * scrollBar.barTop * scrollBar.maxHeight / scrollBar.barPanelHeight;

	scrollBar.setPosition();

	if(scrollBar.barTop > 0 && scrollBar.barTop < scrollBar.barPanelHeight - scrollBar.barHeight)
		scrollBar.drag = e.screenY;
}

function stopDrag()
{
	document.onmousemove = null;
	scrollBar.drag = 0;
}

function scrollPage(e)
{
	var target, posY;

	if(!e)
	{
		e = window.event;
		target = e.srcElement;
		posY = e.offsetY;
	}
	else
	{
		target = e.target;
		if(scrollBar.browser.opera)
			posY = e.offsetY;
		else
			posY = e.layerY;
	}

	if(target != scrollBar.barPanel)
		return;

	if(posY <= scrollBar.barTop)
	{
		scrollBar.top += (scrollBar.clip-20);

		if(scrollBar.top > 0)
			scrollBar.top = 0;
	}
	else if(posY >= scrollBar.barTop + scrollBar.barHeight)
	{
		scrollBar.top -= (scrollBar.clip-20);

		if(scrollBar.top < (-1) * (scrollBar.maxHeight - scrollBar.clip))
			scrollBar.top = (-1) * (scrollBar.maxHeight - scrollBar.clip);
	}

	scrollBar.barTop = (-1) * scrollBar.top * scrollBar.barPanelHeight / scrollBar.maxHeight;
	scrollBar.setPosition();
}

