// Scrollbar

var theScrollbar;
var mousey = -1;

function ClassScrollbar(sbar_id,varname) {
	this._id = new String(sbar_id);
	// Init objects
	this._buttonUp = document.getElementById(sbar_id+"_buttonup");
	this._buttonDown = document.getElementById(sbar_id+"_buttondown");
	this._topPath = document.getElementById(sbar_id+"_toppath");
	this._bottomPath = document.getElementById(sbar_id+"_bottompath");
	this._scroller = document.getElementById(sbar_id+"_scroller");
	this._content = document.getElementById(sbar_id+"_scrollable");
	// Setup backwards compatibility
	this._buttonUp._myObject = this;
	this._buttonDown._myObject = this;
	this._scroller._myObject = this;
	// Init variables
	this._scrollAmount = 0;
	this._clientHeight = this._content.offsetHeight;
	this._doScroll = 0;
	this._dragging = false;
	this._grabY = 0;
	this._grabTop = 0;
	// Calculations
	this._totalHeight = this._content.scrollHeight;
	this._maxScroll = this._content.scrollHeight - this._content.offsetHeight;
	if (this._totalHeight == this._maxScroll || this._maxScroll < 0) {
		this._topPath.style.height = "1px";
		this._scroller.style.height = "1px";
		this._bottomPath.style.height = "100%";
		return false;
	}
	this._pathHeight = this._bottomPath.offsetHeight + this._topPath.offsetHeight + 1;
	this._scrollerHeight = this._pathHeight * this._clientHeight / this._totalHeight;
	this._topPathHeight = 1;
	this._bottomPathHeight = this._pathHeight - this._scrollerHeight - 1;
	this._leftover = this._pathHeight - this._scrollerHeight;
	// Init setting
	this._topPath.style.height = "1px";
	this._scroller.style.height = Math.round(this._scrollerHeight) + "px";
	this._bottomPath.style.height = Math.round(this._bottomPathHeight) + "px";
	// Setup the interval
	this._intRunner = function(whichvar) {
		var p = 0;
		if (whichvar._dragging == false) {
			whichvar._scrollAmount += whichvar._doScroll;
			p = whichvar._scrollAmount / whichvar._maxScroll;
		} else {
			var dist = mousey - whichvar._grabY + whichvar._grabTop;
			if (dist <= 0) {
				whichvar._scrollAmount = 0;
				p = 0;
			} else if (dist >= whichvar._leftover) {
				whichvar._scrollAmount = whichvar._maxScroll;
				p = 1;
			} else {
				p = dist / whichvar._leftover;
				whichvar._scrollAmount = whichvar._maxScroll * p;
			}
		}
		// Perform the scroll
		whichvar._content.scrollTop = Math.floor(whichvar._scrollAmount);
		whichvar._scrollAmount = whichvar._content.scrollTop;
		// Update the scroller position
		try {
			whichvar._topPathHeight = Math.floor(whichvar._leftover * p);
			if (whichvar._topPathHeight == 0) whichvar._topPathHeight = 1;
			whichvar._bottomPathHeight = whichvar._leftover - whichvar._topPathHeight;
			if (whichvar._bottomPathHeight < 1) {
				whichvar._bottomPathHeight = 1;
				whichvar._topPathHeight = whichvar._leftover - whichvar._bottomPathHeight;
			}
			whichvar._topPath.style.height = whichvar._topPathHeight + "px";
			whichvar._bottomPath.style.height = whichvar._bottomPathHeight + "px";
		} catch(e) {
		}
	}
	this._scrollButtonInterval = window.setInterval(varname+"._intRunner("+varname+");",50);
	// Apply event handlers
	//// Top button
	this._buttonDown.onmousedown = function(ev) {
		var s = (typeof event == "undefined")?ev.target:event.srcElement;
		s._myObject._doScroll = 5;
		return false;
	}
	this._buttonDown.ondragstart = function(ev) {
		return false;
	}
	this._buttonDown.ondrag = function(ev) {
		return false;
	}
	//// Bottom button
	this._buttonUp.onmousedown = function(ev) {
		var s = (typeof event == "undefined")?ev.target:event.srcElement;
		s._myObject._doScroll = -5;
		return false;
	}
	this._buttonUp.ondragstart = function(ev) {
		return false;
	}
	this._buttonUp.ondrag = function(ev) {
		return false;
	}
	//// Scroller
	this._scroller.onmousedown = function(ev) {
		var s = (typeof event == "undefined")?ev.target:event.srcElement;
		if (typeof s._myObject == "undefined") s = s.parentNode;
		s._myObject._grabY = (typeof event == "undefined")?ev.pageY:(event.clientY + document.body.scrollTop);
		s._myObject._grabTop = s._myObject._topPathHeight;
		mousey = s._myObject._grabY;
		document.body.onmousemove = function(ev) {
			mousey = (typeof event == "undefined")?ev.pageY:(event.clientY + document.body.scrollTop);
		}
		s._myObject._dragging = true;
		return false;
	}
	this._scroller.ondragstart = function(ev) {
		return false;
	}
	this._scroller.ondrag = function(ev) {
		return false;
	}
	// Apply global handler
	document.body.onmouseup = function(ev) {
		var allvars = document.body.getAttribute("scrollbars");
		if (allvars == null || allvars == "") {
		} else {
			var vars = allvars.split(",");
			for (var i=0; i<vars.length; i++) {
				eval(vars[i] + "._doScroll = 0");
				eval(vars[i] + "._dragging = false");
			}
		}
		document.body.onmousemove = function() {}
	}
	// Register on the body
	if (document.body.getAttribute("scrollbars") != null && document.body.getAttribute("scrollbars") != "") {
		document.body.setAttribute("scrollbars",document.body.getAttribute("scrollbars") + "," + varname);
	} else {
		document.body.setAttribute("scrollbars",varname);
	}
}

function InitScrollbar() {
	var brw = window.navigator.appName.toLowerCase();
	if (brw.indexOf("opera")>-1) {
		document.getElementById("scrollbar").style.display = "none";
		document.getElementById("sb1_scrollable").style.overflow = "auto";
	} else {
		theScrollbar = new ClassScrollbar("sb1","theScrollbar");
	}
}

function _getRealX(obj) {
	var thisobj = obj;
	var pos = obj.offsetLeft;
	while (1>0) {
		if (thisobj == document.body) break;
		thisobj = thisobj.offsetParent;
		pos += thisobj.offsetLeft;
	}
	return pos;
}

function _getRealY(obj) {
	var thisobj = obj;
	var pos = obj.offsetTop;
	while (1>0) {
		if (thisobj == document.body) break;
		thisobj = thisobj.offsetParent;
		pos += thisobj.offsetTop;
	}
	return pos;
}
