 // JavaScript Document

var _ScrollBars = new Array();

var _scrollInt = null;
var _scrOrigin = new Object();
_scrOrigin._obj = null;
_scrOrigin._absY = -1;
_scrOrigin._oldOffset = -1;

function _initScrollBar(sbname) {
	var tempobj = new Object();
	// Collect objects
	tempobj._name = sbname;
	if (document.getElementById(sbname)!=null && typeof document.getElementById(sbname) != "undefined" && document.getElementById(sbname).getAttribute("scrollrate")!=null) {
		tempobj._scrollrate = parseInt(document.getElementById(sbname).getAttribute("scrollrate"));
	} else {
		tempobj._scrollrate = 10;
	}
	tempobj._viewport = document.getElementById(sbname+"_view");
	tempobj._scrollable = document.getElementById(sbname+"_scr");
	tempobj._up = document.getElementById(sbname+"_up");
	tempobj._down = document.getElementById(sbname+"_down");
	tempobj._toppath = document.getElementById(sbname+"_pathup");
	tempobj._botpath = document.getElementById(sbname+"_pathdown");
	tempobj._scroller = document.getElementById(sbname+"_scroller");
	// Set dimensions
	//// Set the scrollbar to top
	tempobj._toppath.style.height = "1px";
	//// Calculate scroll parameters
	tempobj._scrollable.scrollTop = 9999;
	tempobj._scrollamount = tempobj._scrollable.scrollTop;
	tempobj._scrollable.scrollTop = 0;
	if (tempobj._scrollamount <= 0) {
		// ADD CODE HERE
		try {
			document.getElementById(sbname + "_all_scroll").style.display = "none";
		} catch(err) {
		}
		return;
	}
	tempobj._pathheight = tempobj._toppath.offsetHeight + tempobj._botpath.offsetHeight + tempobj._scroller.offsetHeight;
	tempobj._scrollerheight = Math.ceil(tempobj._pathheight * tempobj._viewport.offsetHeight / tempobj._scrollable.scrollHeight);
	tempobj._scroller.style.height = tempobj._scrollerheight + "px";
	tempobj._botpath.style.height = (tempobj._pathheight - tempobj._toppath.offsetHeight - tempobj._scrollerheight) + "px";
	// Set event handlers
	tempobj._up.onmousedown = __startButtonUp;
	tempobj._down.onmousedown = __startButtonDown;
	tempobj._scroller.onmousedown = __startDrag;
	document.onmouseup = __stopScrolls;
	document.onmousemove = __checkDrag;
	document.ondrag = _cancel;
	document.ondragstart = _cancel;
	tempobj._scroller.ondragstart = _cancel;
	tempobj._scroller.ondrag = _cancel;
	_ScrollBars.push(tempobj);
}

function _getScrollBarByElement(elem) {
	for (var i=0;i<_ScrollBars.length;i++) {
		var thisone = _ScrollBars[i];
		for (var j in thisone) {
			if (thisone[j] == elem) return _ScrollBars[i];
		}
	}
	return false;
}

function _getScrollBarByName(name) {
	for (var i=0;i<_ScrollBars.length;i++) {
		if (_ScrollBars[i]._name == name) return _ScrollBars[i];
	}
	return false;
}

function __startButtonUp() {
	var thisScroll = _getScrollBarByElement(this);
	_scrollInt = window.setInterval("___buttonUp('"+thisScroll._name+"');",50);
}

function __startButtonDown() {
	var thisScroll = _getScrollBarByElement(this);
	_scrollInt = window.setInterval("___buttonDown('"+thisScroll._name+"');",50);
}

function ___buttonUp(whichscroll) {
	thisScroll = _getScrollBarByName(whichscroll);
	thisScroll._scrollable.scrollTop -= thisScroll._scrollrate;
	___removeSelect();
	_updateScrollerByContent(whichscroll);
}

function ___buttonDown(whichscroll) {
	thisScroll = _getScrollBarByName(whichscroll);
	thisScroll._scrollable.scrollTop += thisScroll._scrollrate;
	___removeSelect();
	_updateScrollerByContent(whichscroll);
}

function __stopScrolls() {
	clearInterval(_scrollInt);
	_scrOrigin._obj = null;
	_scrOrigin._absY = -1;
}

function ___removeSelect() {
	try { document.selection.empty(); }
	catch(eee) {
		window.getSelection().removeAllRanges();
	}
}

function __startDrag(e) {
	if (_scrOrigin._absY == -1) {
		var thisScroll = _getScrollBarByElement(this);
		try { var ev2 = event.clientY; } catch(err) { var ev2 = e.pageY; e.preventDefault(); }
		_scrOrigin._obj = thisScroll;
		_scrOrigin._absY = ev2;
		_scrOrigin._oldOffset = thisScroll._toppath.offsetHeight;
	}
}

function __checkDrag(e) {
	if (_scrOrigin._absY > -1) {
		// Has drag
		var thisScroll = _getScrollBarByElement(this);
		try { var ev = event.clientY; } catch(err) { var ev = e.pageY; }
		___removeSelect();
		// Calculate new scroll position
		var newoffset = ev - _scrOrigin._absY;
		var newtop = _scrOrigin._oldOffset + newoffset;
		var newbot = _scrOrigin._obj._pathheight - newtop - _scrOrigin._obj._scrollerheight;
		// Check boundaries
		if (newtop < 1) {
			newtop = 1;
			newbot = _scrOrigin._obj._pathheight - _scrOrigin._obj._scrollerheight - 1;
		}
		if (newbot < 1) {
			newtop = _scrOrigin._obj._pathheight - _scrOrigin._obj._scrollerheight - 1;
			newbot = 1;
		}
		_scrOrigin._obj._toppath.style.height = newtop + "px";
		_scrOrigin._obj._botpath.style.height = newbot + "px";
		_updateContentByScroller(_scrOrigin._obj._name);
	}
}

function _updateContentByScroller(whichscroll) {
	thisScroll = _getScrollBarByName(whichscroll);
	var perc = thisScroll._toppath.offsetHeight / (thisScroll._pathheight - thisScroll._scrollerheight);
	if (Math.round(perc * thisScroll._scrollamount)<=10) {
		thisScroll._scrollable.scrollTop = 0;
	} else {
		thisScroll._scrollable.scrollTop = Math.round(perc * thisScroll._scrollamount);
	}
}

function _updateScrollerByContent(whichscroll) {
	thisScroll = _getScrollBarByName(whichscroll);
	var perc = thisScroll._scrollable.scrollTop / thisScroll._scrollamount;
	var scrollpos = Math.round((thisScroll._pathheight - thisScroll._scrollerheight) * perc);
	var bottomh = thisScroll._pathheight - scrollpos - thisScroll._scrollerheight;
	// Keep the total height solid
	if (bottomh < 1) {
		bottomh = 1;
		scrollpos = thisScroll._pathheight - bottomh - thisScroll._scrollerheight;
	}
	if (scrollpos < 1) {
		scrollpos = 1;
		bottomh = thisScroll._pathheight - scrollpos - thisScroll._scrollerheight;
	}
	// Set the values
	thisScroll._toppath.style.height = scrollpos + "px";
	thisScroll._botpath.style.height = bottomh + "px";
}

function _cancel() {
	return false;
}