/************************************************************************************************************
	(C) www.dhtmlgoodies.com, September 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/	
	var contentHeight = 0; 	// The total height of the content
	var visibleContentHeight = 0;	
	var scrollActive = false;
	
	var scrollHandleObj = false; // reference to the scroll handle
	var scrollHandleHeight = false;
	var scrollbarTop = false;
	var eventYPos = false;

	var scrollbuttonActive = false;
	var scrollbuttonDirection = false;
	var scrollbuttonSpeed = 2; // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
	var scrollTimer = 10;	// Also how fast the content scrolls. By decreasing this value, the content will move faster	
	
	var scrollMoveToActive = false;
	var scrollMoveToYPosition = false;
	function startScroll(e)
	{
		if(document.all)e = event;
		scrollbarTop = document.getElementById('theScroll').offsetTop;
		eventYPos = e.clientY;
		scrollActive = true;
	}
	
	function stopScroll()
	{
		scrollActive = false;
		scrollbuttonActive = false;
		scrollMoveToActive = false;
	}
	function scroll(e)
	{
		if(!scrollActive)return;
		if(document.all)e = event;
		if(e.button!=1 && document.all)return;
		var topPos = scrollbarTop + e.clientY - eventYPos; 
		if(topPos<0)topPos=0;
		if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1)topPos = visibleContentHeight-(scrollHandleHeight+4);
		document.getElementById('theScroll').style.top = topPos + 'px';
		document.getElementById('ScrollContent').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 
	}
	
	/*
	Click on the slider
	Move the content to the this point
	*/
	function scrollMoveToInit(e)
	{		
		if(document.all)e = event;
		scrollMoveToActive = true;
		scrollMoveToYPosition = e.clientY - document.getElementById('scrollbar').offsetTop;
		if(document.getElementById('theScroll').offsetTop/1 > scrollMoveToYPosition) scrollbuttonDirection = scrollbuttonSpeed*-2; else  scrollbuttonDirection = scrollbuttonSpeed*2;
		scrollMoveTo();	
	}
	
	function scrollMoveTo()
	{
		if(!scrollMoveToActive || scrollActive)return;
		var topPos = document.getElementById('theScroll').style.top.replace('px','');
		topPos = topPos/1 + scrollbuttonDirection;
		if(topPos<0){
			topPos=0;
			scrollMoveToActive=false;
		}
		if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
			topPos = visibleContentHeight-(scrollHandleHeight+4);	
			scrollMoveToActive=false;
		}
		if(scrollbuttonDirection<0 && topPos<scrollMoveToYPosition-scrollHandleHeight/2)return;	
		if(scrollbuttonDirection>0 && topPos>scrollMoveToYPosition-scrollHandleHeight/2)return;			
		document.getElementById('theScroll').style.top = topPos + 'px';
		document.getElementById('ScrollContent').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 		
		setTimeout('scrollMoveTo()',scrollTimer);		
	}
	
	function cancelEvent()
	{
		return false;			
	}

	function scrollButton()
	{
		if(this.id=='scrollDown')scrollbuttonDirection = scrollbuttonSpeed; else scrollbuttonDirection = scrollbuttonSpeed*-1;
		scrollbuttonActive=true;
		scrollButtonScroll();
	}
	function scrollButtonScroll()
	{
		if(!scrollbuttonActive)return;
		var topPos = document.getElementById('theScroll').style.top.replace('px','');
		topPos = topPos/1 + scrollbuttonDirection;
		if(topPos<0){
			topPos=0;
			scrollbuttonActive=false;
		}
		if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
			topPos = visibleContentHeight-(scrollHandleHeight+4);	
			scrollbuttonActive=false;
		}	
		document.getElementById('theScroll').style.top = topPos + 'px';
		document.getElementById('ScrollContent').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 			
		setTimeout('scrollButtonScroll()',scrollTimer);
	}
	function scrollButtonStop()
	{
		scrollbuttonActive = false;
	}
	
	
	function initScroll()
	{
		visibleContentHeight = document.getElementById('scrollbar').offsetHeight ;
		contentHeight = document.getElementById('ScrollContent').offsetHeight - visibleContentHeight;		
		scrollHandleObj = document.getElementById('theScroll');
		scrollHandleHeight = scrollHandleObj.offsetHeight;
		scrollbarTop = document.getElementById('scrollbar').offsetTop;		
		document.getElementById('theScroll').onmousedown = startScroll;
		document.body.onmousemove = scroll;
		document.getElementById('scrollbar').onselectstart = cancelEvent;
		document.getElementById('theScroll').onmouseup = stopScroll;
		if(document.all)document.body.onmouseup = stopScroll; else document.documentElement.onmouseup = stopScroll;
		document.getElementById('scrollDown').onmousedown = scrollButton;
		document.getElementById('scrollUp').onmousedown = scrollButton;
		document.getElementById('scrollDown').onmouseup = scrollButtonStop;
		document.getElementById('scrollUp').onmouseup = scrollButtonStop;
		document.getElementById('scrollUp').onselectstart = cancelEvent;
		document.getElementById('scrollDown').onselectstart = cancelEvent;
		document.getElementById('scrollbar').onmousedown = scrollMoveToInit;
	}
	/*
	Change from the default color
	*/	
	function setColor(rgbColor)
	{
		document.getElementById('scrollbar').style.borderColor = rgbColor;
		document.getElementById('theScroll').style.backgroundColor = rgbColor;
		document.getElementById('scrollUp').style.borderColor = rgbColor;
		document.getElementById('scrollDown').style.borderColor = rgbColor;
		document.getElementById('scrollUp').style.color = rgbColor;
		document.getElementById('scrollDown').style.color = rgbColor;
		document.getElementById('scrollContainer').style.borderColor = rgbColor;
	}
	/*
	Setting total width of scrolling div
	*/
	function setWidth(newWidth)
	{
		document.getElementById('scrolldiv').style.width = newWidth + 'px';
		document.getElementById('scrollContainer').style.width = newWidth-30 + 'px';		
	}
	
	/*
	Setting total height of scrolling div
	*/
	function setHeight(newHeight)
	{
		document.getElementById('scrolldiv').style.height = newHeight + 'px';
		document.getElementById('scrollContainer').style.height = newHeight + 'px';
		document.getElementById('scroll_slider').style.height = newHeight + 'px';
		document.getElementById('scrollbar').style.height = newHeight-40 + 'px';		
	}
	/*
	Setting new background color to the slider 
	*/
	function setSliderBgColor(rgbColor)
	{
		document.getElementById('scrollbar').style.backgroundColor = rgbColor;
		document.getElementById('scrollUp').style.backgroundColor = rgbColor;
		document.getElementById('scrollDown').style.backgroundColor = rgbColor;
	}
	/*
	Setting new content background color
	*/
	function setContentBgColor(rgbColor)
	{
		document.getElementById('scrollContainer').style.backgroundColor = rgbColor;
	}
	
	/*
	Setting scroll button speed
	*/
	function setScrollButtonSpeed(newScrollButtonSpeed)
	{
		scrollbuttonSpeed = newScrollButtonSpeed;
	}
	/*
	Setting interval of the scroll
	*/
	function setScrollTimer(newInterval)
	{
		scrollTimer = newInterval;
	}
	