/************************************************************************************************************
(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

//***********************************************************************************************************

edited to incorporate class of scrolldiv

************************************************************************************************************/	
function scroll_div(scrolldivId){
	var contentHeight = 0; 	// The total height of the content
	var visibleContentHeight = 0;	
	var scrollActive = false;

	var scrollHandleObj = false; // reference to the scroll handle
	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;
	
	var scrolldiv = "";
	var scrolldiv_slider = "";
	var scrolldiv_content = "";	
	var scrolldiv_theScroll = "";
	var scrolldiv_scrollUp = "";
	var scrolldiv_scrollUp_click = "";
	var scrolldiv_scrollDown = "";
	var scrolldiv_scrollDown_click = "";
	var scrolldiv_parentContainer = "";

	//this.init=init;
	this.initScroll=scrolldiv_initScroll;
	this.buttonSpeed=setScrollButtonSpeed;
	this.setWidth=scrolldiv_setWidth;
	this.setHeight=scrolldiv_setHeight;
	this.setScrollTimer=setScrollTimer;
	this.scrollButtonScroll=scrolldiv_scrollButtonScroll;

	init(scrolldivId);

	function scrolldiv_scrollButton() {
		if (this.id==scrolldiv_scrollDown) {
			scrollbuttonDirection = scrollbuttonSpeed;
		} else {
			scrollbuttonDirection = scrollbuttonSpeed*-1;
		}
		scrollbuttonActive=true;
		scrolldiv_scrollButtonScroll();
	}
	
	function scrolldiv_scrollButton_quick() {
		if (this.id==scrolldiv_scrollDown_click) {
			scrollbuttonDirection = scrollbuttonSpeed+30;
		} else {
			scrollbuttonDirection = (scrollbuttonSpeed+30)*-1;
		}
		scrollbuttonActive=true;
		scrolldiv_scrollButtonScroll();
	}
	
	function scrolldiv_scrollButtonScroll() {
		if (!scrollbuttonActive) return;
		//var topPos = document.getElementById(scrolldiv_theScroll).style.top.replace("px","");
		var topPos = document.getElementById(scrolldiv_content).style.top.replace("px","");
		topPos = topPos/1 - scrollbuttonDirection;
		if (topPos>0) {
			topPos=0;
			scrollbuttonActive=false;
		}
		if(topPos/1<-contentHeight) {
			topPos = -contentHeight;
			scrollbuttonActive=false;
		}
		document.getElementById(scrolldiv_content).style.top = topPos+"px";
		setTimeout(scrolldiv+".scrollButtonScroll()",scrollTimer);
	}

	
	
	function scrolldiv_scrollButtonStop() {
		scrollbuttonActive = false;
	}

	function scrolldiv_scrollButtonStop_quick() {
		scrollbuttonActive = false;
	}
	
	function init(Id){
		scrolldiv = Id;
		scrolldiv_slider = Id + "_slider";
		scrolldiv_content = Id + "_content";	
		scrolldiv_theScroll = Id + "_theScroll";
		scrolldiv_scrollUp = Id + "_scrollUp";
		scrolldiv_scrollUp_click = Id + "_upclick";
		scrolldiv_scrollDown = Id + "_scrollDown";
		scrolldiv_scrollDown_click = Id + "_doclick";
		scrolldiv_parentContainer = Id + "_parentContainer";
		//alert(scrolldiv_scrollDown_click);
		
	}

	function scrolldiv_initScroll() {

		//visibleContentHeight = document.getElementById("scrolldiv_scrollbar").offsetHeight ;
		visibleContentHeight = document.getElementById(scrolldiv_slider).offsetHeight;
		if (document.getElementById(scrolldiv_content).offsetHeight<=visibleContentHeight) {
			return;
		}
		document.getElementById(scrolldiv_slider).style.visibility = "visible";
		contentHeight = document.getElementById(scrolldiv_content).offsetHeight - visibleContentHeight;
		scrollHandleObj = document.getElementById(scrolldiv_theScroll);
		document.getElementById(scrolldiv_scrollDown).onmousedown = scrolldiv_scrollButton;
		document.getElementById(scrolldiv_scrollDown_click).onmousedown = scrolldiv_scrollButton_quick;
		document.getElementById(scrolldiv_scrollUp).onmousedown = scrolldiv_scrollButton;
		document.getElementById(scrolldiv_scrollUp_click).onmousedown = scrolldiv_scrollButton_quick;
		document.getElementById(scrolldiv_scrollDown).onmouseup = scrolldiv_scrollButtonStop;
		document.getElementById(scrolldiv_scrollDown).onmouseout = scrolldiv_scrollButtonStop;
		document.getElementById(scrolldiv_scrollDown_click).onmouseup = scrolldiv_scrollButtonStop_quick;
		document.getElementById(scrolldiv_scrollDown_click).onmouseout = scrolldiv_scrollButtonStop_quick;
		document.getElementById(scrolldiv_scrollUp).onmouseup = scrolldiv_scrollButtonStop;
		document.getElementById(scrolldiv_scrollUp).onmouseout = scrolldiv_scrollButtonStop;
		document.getElementById(scrolldiv_scrollUp_click).onmouseup = scrolldiv_scrollButtonStop_quick;
		document.getElementById(scrolldiv_scrollUp_click).onmouseout = scrolldiv_scrollButtonStop_quick;
	}
	/*
	Setting total width of scrolling div
	*/
	function scrolldiv_setWidth(newWidth) {
		document.getElementById(scrolldiv).style.width = newWidth + "px";
		document.getElementById(scrolldiv_parentContainer).style.width = newWidth-30 + "px";		
	}

	/*
	Setting total height of scrolling div
	*/
	function scrolldiv_setHeight(newHeight) {
		document.getElementById(scrolldiv).style.height = newHeight + "px";
		document.getElementById(scrolldiv_parentContainer).style.height = newHeight + "px";
		document.getElementById(scrolldiv_slider).style.height = newHeight + "px";
	}
	/*
	Setting scroll button speed
	*/
	function setScrollButtonSpeed(newScrollButtonSpeed) {
		scrollbuttonSpeed = newScrollButtonSpeed;
	}
	/*
	Setting interval of the scroll
	*/
	function setScrollTimer(newInterval) {
		scrollTimer = newInterval;
	}
}
