//document.onmouseup = stop_scroll;
   
var scrollTimer;  
var scrollData = new Array();	 
var scrollMaxSpeed = 4;

var scrollCurrentSpeed = 0;
var scrollDestinationSpeed = scrollMaxSpeed;

var noscrollbarupdate = false;
	
		 
function scroll_init(text,up,down,scrollHeight,scrollbar,scrollslider,scrollbar_width,parent_id)
{	 
	scrollData[text] = new Object(); 
	scrollData[text].elem = document.getElementById(text);
	scrollData[text].up = document.getElementById(up);
	scrollData[text].down = document.getElementById(down);	
	scrollData[text].height = scrollHeight;  
	scrollData[text].scrollbar = document.getElementById(scrollbar);
	scrollData[text].scrollslider = document.getElementById(scrollslider);	
	scrollData[text].scrollheight = scrollData[text].elem.offsetHeight - scrollData[text].height;
	scrollData[text].parent_id = parent_id;

	$("#"+scrollData[text].parent_id).scrollTop(0);
	heightInt = parseInt(scrollData[text].elem.offsetHeight);

	if (heightInt > (scrollHeight+10)) {
		scrollData[text].scrollslider.style.width = scrollbar_width/(heightInt/scrollHeight)+"px";
		scrollData[text].scrollbar_width = scrollbar_width - parseInt(scrollData[text].scrollslider.style.width);  


		scrollData[text].up.style.visibility = "visible";
		scrollData[text].down.style.visibility = "visible";
		scrollData[text].scrollbar.style.visibility = "visible";
		
		$(function() {
			$( "#"+scrollslider).draggable({ 
				axis: "x",
				containment: "parent",
				drag: function() {
					pos = $(this).position();
					offset = scrollData[text].scrollheight*pos.left/scrollData[text].scrollbar_width;
					$("#"+scrollData[text].parent_id).scrollTop(offset);
				}
			});
			$( "#"+scrollslider).bind( "dragstart", function(event, ui) {
 				noscrollbarupdate = true;
			});
			$( "#"+scrollslider).bind( "dragstop", function(event, ui) {
 				noscrollbarupdate = false;
			});
		});
	}		
	else
	{
		scrollData[text].elem.style.height = scrollHeight+"px";
		scrollData[text].up.style.visibility = "hidden";
		scrollData[text].down.style.visibility = "hidden";
		scrollData[text].scrollbar.style.visibility = "hidden";
	}		

	// Hide Scrollbar on iPad/iPhone
	if ( (navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null) || (navigator.userAgent.match(/iPad/i) != null))
	{
		$("#"+scrollbar).hide();
	}
}

function update_scrollbar(id)
{
	if (!noscrollbarupdate)
	{
		//offset = 0-scrollData[id].scrollbar_width*parseInt(scrollData[id].elem.style.top)/scrollData[id].scrollheight;
		offset = scrollData[id].scrollbar_width*$("#"+scrollData[id].parent_id).scrollTop()/scrollData[id].scrollheight;
		scrollData[id].scrollslider.style.left = offset+"px";
	}
}

function scrolldown(id) 
{
	elem = document.getElementById(id);	   
	heightInt = parseInt(elem.offsetHeight);
	topInt = $("#"+scrollData[id].parent_id).scrollTop();
	if (scrollCurrentSpeed < scrollDestinationSpeed) scrollCurrentSpeed += 0.3;
	if (scrollCurrentSpeed > scrollDestinationSpeed) scrollCurrentSpeed -= 0.3;
	if (scrollCurrentSpeed <= 0) scroll_end();
	topInt += scrollCurrentSpeed;
	if (topInt > 0-(scrollData[id].height-heightInt)) topInt = 0-(scrollData[id].height-heightInt);
	$("#"+scrollData[id].parent_id).scrollTop(topInt);
	update_scrollbar(id);
}

function scrollup(id) 
{
	elem = document.getElementById(id);
	topInt = $("#"+scrollData[id].parent_id).scrollTop();
	if (scrollCurrentSpeed < scrollDestinationSpeed) scrollCurrentSpeed += 0.3;
	if (scrollCurrentSpeed > scrollDestinationSpeed) scrollCurrentSpeed -= 0.3;
	if (scrollCurrentSpeed <= 0) scroll_end();
	topInt -= scrollCurrentSpeed;
	if (topInt < 0) topInt = 0;
	$("#"+scrollData[id].parent_id).scrollTop(topInt);
	update_scrollbar(id);
}

function start_scrolldown(id)
{							   
	scroll_end();  
	scrollTimer = setInterval("scrolldown('"+id+"')",10);	
}

function start_scrollup(id)
{		
	scroll_end();  
	scrollTimer = setInterval("scrollup('"+id+"')",10);
}						
					   
function scroll_end()
{
	scrollCurrentSpeed = 0;
	scrollDestinationSpeed = scrollMaxSpeed;
	clearInterval(scrollTimer);
}

function stop_scroll()
{	   							
	scrollDestinationSpeed = 0;
}
