c = null;
mainScrollArea = null;

contentScroller = function(outerHtmlObject,vHeight,vMainScrollArea) {
	/* Konstruktor */
	if(outerHtmlObject) {
		this.container = outerHtmlObject;
		this.container.style.backgroundColor = 'transparent';
		this.text = this.container.getElementsByTagName('DIV')[0];
		this.text.style.marginTop = '0px';
		this.container.style.height = vHeight+'px';
		this.container.style.overflow = 'hidden';
		if(this.container.parentNode.style.position != 'absolute') this.container.parentNode.style.position = 'relative';
		this.showListCounter = (this.text.className.toLowerCase().indexOf('list') > -1) ? true : false;
		this.vW = (this.showListCounter) ? 200 : 24;
		if(vMainScrollArea) mainScrollArea = this;
		this.defaultSpeed = 2;
		this.speed = this.defaultSpeed;
		this.speedMultiplikator = 3;
		this.scrollerActive = false;
		this.Init();
	}
}

contentScroller.prototype.Init = function() {
	if(this.container.offsetHeight < this.text.offsetHeight) {
		this.scrollerActive = true;
		/* Container verkleinern, Platz machen für Scroller */
		this.container.style.height = (parseInt(this.container.offsetHeight)-30) + 'px';
		
		var scrollerContainer = document.createElement('DIV');
		var scrollerArrows = document.createElement('DIV');
		var scrollTopContainer = document.createElement('DIV');
		var scrollBarContainer = document.createElement('DIV');
		var scrollBottomContainer = document.createElement('DIV');
		this.scrollPosContainer = document.createElement('DIV');
		
		scrollTopContainer.innerHTML = '<img src="img/scroller/scrollTop.gif" alt="" width="24" height="7" />';
		scrollBarContainer.innerHTML = '<img src="img/scroller/scrollBar.gif" alt="" width="24" height="7" />';
		scrollBottomContainer.innerHTML = '<img src="img/scroller/scrollBottom.gif" alt="" width="24" height="7" />';
		this.scrollPosContainer.innerHTML = '<img src="img/scroller/scrollPos.gif" alt="" width="1" height="3" />';
				
		scrollBarContainer.style.position = 'relative';
		this.scrollPosContainer.style.position = 'absolute';
		this.scrollPosContainer.style.top = '2px';
		this.scrollPosContainer.style.left = '1px';
		
		scrollBarContainer.appendChild(this.scrollPosContainer);
		
		scrollerArrows.appendChild(scrollTopContainer);
		scrollerArrows.appendChild(scrollBarContainer);
		scrollerArrows.appendChild(scrollBottomContainer);
				
		scrollerContainer.id = 'divScrollerContainer';
		scrollerContainer.style.position = 'relative';
		scrollerContainer.style.textAlign = 'right';
		scrollerContainer.style.width = this.vW+'px';
		scrollerArrows.style.width = '24px';
		scrollerArrows.style.position = 'absolute';
		scrollerArrows.style.right = '0px';
		scrollerArrows.style.top = '0px';
		
		this.vTextHeight = this.text.offsetHeight;
		
		if(this.showListCounter) {
			var scrollPositionTextContainer = document.createElement('DIV');
			var scrollPositionTextContainerText = document.createTextNode('');
			scrollPositionTextContainer.style.width = this.vW-30+'px';
			scrollPositionTextContainer.style.marginRight = '30px';
			scrollPositionTextContainer.style.lineHeight = '20px';
			scrollerContainer.appendChild(scrollPositionTextContainer);
			this.scrollTextContainer = scrollPositionTextContainer;
			this.scrollPositionTextContainerText = scrollPositionTextContainerText;
			
			this.scrollTextContainer.appendChild(this.scrollPositionTextContainerText);
			
			this.arrHeightList = new Array();
			var i;
			var oldOffsetTop = -1;
			for(i=0;i<this.text.childNodes.length;i++) {
				if(this.text.childNodes[i].tagName) {
					if(this.text.childNodes[i].className == 'cListItem') {
						if(this.text.childNodes[i].offsetTop != oldOffsetTop) {
							this.arrHeightList.push(this.text.childNodes[i].offsetHeight);
						}
					}
				}
			}
			this.SetScrollPosition();
		}
		
		scrollerContainer.appendChild(scrollerArrows);
		
		this.container.parentNode.appendChild(scrollerContainer);
		scrollerContainer.style.top = '5px';
		if(this.container.parentNode.offsetLeft != this.container.offsetLeft) {
			vOffsetLeft = this.container.offsetLeft;
		} else {
			vOffsetLeft = 0;
		}
		scrollerContainer.style.left = (vOffsetLeft+this.container.offsetWidth-this.vW-10)+'px';
		
		/* Events */
		scrollTopContainer.c = this;
		scrollTopContainer.onmouseover = function() {
			this.c.StopScrollUp();
			this.c.StopScrollDown();
			this.c.speed = this.c.defaultSpeed;
			this.c.ScrollUp();
		}
		scrollTopContainer.onmouseout = function() {
			this.c.StopScrollUp();
		}
		scrollTopContainer.onmousedown = function() {
			this.c.speed *= this.c.speedMultiplikator;
		}
		scrollTopContainer.onmouseup = function() {
			this.c.speed /= this.c.speedMultiplikator;
		}
		scrollTopContainer.onstartdrag = function() {
			this.c.StopScrollUp();
			return false;
		}
		
		scrollBottomContainer.c = this;
		scrollBottomContainer.onmouseover = function() {
			this.c.StopScrollUp();
			this.c.StopScrollDown();
			this.c.speed = this.c.defaultSpeed;
			this.c.ScrollDown();
		}
		scrollBottomContainer.onmouseout = function() {
			this.c.StopScrollDown();
		}
		scrollBottomContainer.onmousedown = function() {
			this.c.speed *= this.c.speedMultiplikator;
		}
		scrollBottomContainer.onmouseup = function() {
			this.c.speed /= this.c.speedMultiplikator;
		}
		scrollBottomContainer.onstartdrag = function() {
			this.c.StopScrollDown();
			return false;
		}
	}
}

contentScroller.prototype.ScrollUp = function() {
	c = this;
	this.intScrollUp = setInterval(this.DoScrollUp,1,null);
}

contentScroller.prototype.DoScrollUp = function(vDelta) {
	if(vDelta == null) vDelta = c.speed;
	if(parseInt(c.text.style.marginTop) < 0) {
		c.text.style.marginTop = (parseInt(c.text.style.marginTop)+vDelta)+'px';
	}
	if(parseInt(c.text.style.marginTop) > 0) {
		c.text.style.marginTop = '0px';
	}
	c.SetScrollPosition();
}

contentScroller.prototype.StopScrollUp = function() {
	clearInterval(this.intScrollUp);
}

contentScroller.prototype.ScrollDown = function() {
	c = this;
	this.intScrollDown = setInterval(this.DoScrollDown,1,null);
}

contentScroller.prototype.DoScrollDown = function(vDelta) {
	if(vDelta == null) vDelta = c.speed;
	if(parseInt(c.text.style.marginTop) > c.container.offsetHeight-c.vTextHeight) {
		c.text.style.marginTop = (parseInt(c.text.style.marginTop)-vDelta)+'px';
	}
	if(parseInt(c.text.style.marginTop) < c.container.offsetHeight-c.vTextHeight) {
		c.text.style.marginTop = c.container.offsetHeight-c.vTextHeight+'px';
	}
	c.SetScrollPosition();
}

contentScroller.prototype.StopScrollDown = function() {
	clearInterval(this.intScrollDown);
}

var vOldFrom = 0;
var vOldTo = 0;
contentScroller.prototype.SetScrollPosition = function() {
	var vSpace = 21;
	var vScrollSpace = this.vTextHeight-this.container.offsetHeight;
	var vScrollPos = Math.abs(parseInt(this.text.style.marginTop));
	
	var vPos = Math.round(21/vScrollSpace*vScrollPos);
	this.scrollPosContainer.style.marginLeft = vPos+'px';
	
	if(this.showListCounter) {
		var vItemScrollPos = 0;
		var vFromItemCount = 1;
		while(vScrollPos > vItemScrollPos+this.arrHeightList[vFromItemCount-1]) {
			vItemScrollPos += this.arrHeightList[vFromItemCount-1];
			vFromItemCount++;
		}
		
		var vItemScrollPos = 0;
		var vToItemCount = 1;
		while(vScrollPos+this.container.offsetHeight > vItemScrollPos+this.arrHeightList[vToItemCount-1] && vToItemCount < this.arrHeightList.length) {
			vItemScrollPos += this.arrHeightList[vToItemCount-1];
			vToItemCount++;
		}
		if(vOldFrom != vFromItemCount || vOldTo != vToItemCount) {
			vOldFrom = vFromItemCount;
			vOldTo = vToItemCount;
			if(vLang == "de") {
				this.scrollPositionTextContainerText.data = vFromItemCount + ' bis ' + vToItemCount + ' von ' + this.arrHeightList.length;
			} else {
				this.scrollPositionTextContainerText.data = vFromItemCount + ' to ' + vToItemCount + ' till ' + this.arrHeightList.length;
			}
		}
	}
}

function handle(delta) {
	c = mainScrollArea;
	if(c && c.scrollerActive) {
		if (delta < 0)
			mainScrollArea.DoScrollDown(Math.abs(delta)*10);
		else
			mainScrollArea.DoScrollUp(Math.abs(delta)*10);
	}
}

function wheel(event){
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle(delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;
