actualPos = 0;
more = false;
content = "";
overflowHeight = 0;
actualMode = 0;
maxBarLength = 260;
needScroll = false;
ratio = -1;

speed_mode0 = 8;
speed_mode1 = 26;
sleep = 50;

function init(){
	contentDiv = document.getElementById('scrollContentDiv');
	parentDiv  = document.getElementById('scrollWindowDiv');
	barDiv     = document.getElementById('bar');
	overflowHeight = contentDiv.offsetHeight * (-1) + parentDiv.offsetHeight;
	// check if scolling is necessary and adjust scrollbar length
	if (contentDiv.offsetHeight - parentDiv.offsetHeight > 0){
		barLength = maxBarLength - (contentDiv.offsetHeight - parentDiv.offsetHeight);
		barLength = (barLength<20) ? 20 : barLength;
		barHeight = maxBarLength - barLength;
		barDiv.style.height = barLength;
		barDiv.style.visibility = "visible";
		needScroll = true;
		
		
		ratio = overflowHeight / barHeight;
	}
	
/*	alert("parent  : "+ parentDiv.offsetHeight  + "\n"+
		  "content : "+ contentDiv.offsetHeight +"\n"+
		  "overflow: "+ overflowHeight + "\n" +
		  "scroll  : "+ needScroll + "\n" +
		  "bar     : "+ barDiv.style.height + "\n" +
		  "ratio   : "+ ratio);
*/

		  
}

function scroll(speed, mode){
	actualPos += speed;
	if    (actualPos < overflowHeight) {actualPos = overflowHeight;}
	else if (actualPos > 1){actualPos = 0;}
	contentDiv.style.top = actualPos;
	barDiv.style.top = actualPos / ratio;
	if (more && actualMode==mode)setTimeout("scroll("+speed+","+mode+")",sleep);
}

function startScroll(mode,dir){
	if(needScroll){
		more = true;
		actualMode=mode;
		var speed = mode?speed_mode1:speed_mode0;
		scroll(speed*dir, mode);
	}
}

function stopScroll(){
	more = false;
}
