// Scrollbar

var mousey = -1;

function ClassHScrollbar(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.offsetWidth;
	this._doScroll = 0;
	this._dragging = false;
	this._grabY = 0;
	this._grabTop = 0;
	this._disabled = false;
	// Calculations
	this._totalHeight = this._content.scrollWidth;
	this._maxScroll = this._content.scrollWidth - this._content.offsetWidth;
	if (this._totalHeight == this._maxScroll || this._maxScroll < 0) {
		this._disabled = true;
		this._topPath.style.width = "1px";
		this._scroller.style.width = "1px";
		this._bottomPath.style.width = "100%";
		return false;
	}
	this._pathHeight = this._bottomPath.offsetWidth + this._topPath.offsetWidth + 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
	try { this._topPath.style.width = "1px"; } catch(e) {}
	try { this._scroller.style.width = Math.round(this._scrollerHeight) + "px"; } catch(e) {}
	try { this._bottomPath.style.width = Math.round(this._bottomPathHeight) + "px"; } catch(e) {}
	// Setup the interval
	this._intRunner = function(whichvar) {
		if (whichvar._disabled == true) return false;
		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;
			}
			try { document.selection.empty(); }
			catch(eee) {
				window.getSelection().removeAllRanges();
			}
		}
		// Perform the scroll
		whichvar._content.scrollLeft = Math.floor(whichvar._scrollAmount);
		whichvar._scrollAmount = whichvar._content.scrollLeft;
		// 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.width = whichvar._topPathHeight + "px";
			whichvar._bottomPath.style.width = 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) {
		try {
			var s = (typeof event == "undefined")?ev.target:event.srcElement;
			if (typeof s == "undefined" || s == null) return false;
			if (typeof s._myObject == "undefined") s = s.parentNode;
			if (s._myObject._disabled == true) return false;
			s._myObject._grabY = (typeof event == "undefined")?ev.pageX:(event.clientX + document.body.scrollLeft);
			s._myObject._grabTop = s._myObject._topPathHeight;
			mousey = s._myObject._grabY;
			document.body.onmousemove = function(ev) {
				mousey = (typeof event == "undefined")?ev.pageX:(event.clientX + document.body.scrollLeft);
			}
			s._myObject._dragging = true;
			return false;
		} catch(e) {
			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++) {
				if (eval(vars[i]+"._disabled") == "false" || eval(vars[i]+"._disabled") == false) {
					try {
						eval(vars[i] + "._doScroll = 0;");
						eval(vars[i] + "._dragging = false;");
					} catch(e) {
					}
				}
			}
		}
		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);
	}
	// Apply update function
	this._UpdateScrollValues = function(whichvar) {
		whichvar._content.scrollTop = 0;
		whichvar._scrollAmount = 0;
		whichvar._clientHeight = whichvar._content.offsetWidth;
		whichvar._doScroll = 0;
		whichvar._dragging = false;
		whichvar._grabY = 0;
		whichvar._grabTop = 0;
		whichvar._disabled = false;
		whichvar._totalHeight = whichvar._content.scrollWidth;
		whichvar._maxScroll = whichvar._content.scrollWidth - whichvar._content.offsetWidth;
		if (whichvar._maxScroll <= 0) {
			whichvar._disabled = true;
			whichvar._topPath.style.width = "1px";
			whichvar._scroller.style.width = "1px";
			whichvar._bottomPath.style.width = "100%";
			return false;
		}
		whichvar._pathHeight = whichvar._bottomPath.offsetWidth + whichvar._topPath.offsetWidth + whichvar._scroller.offsetWidth;
		whichvar._scrollerHeight = whichvar._pathHeight * whichvar._clientHeight / whichvar._totalHeight;
		whichvar._topPathHeight = 1;
		whichvar._bottomPathHeight = whichvar._pathHeight - whichvar._scrollerHeight - 1;
		whichvar._leftover = whichvar._pathHeight - whichvar._scrollerHeight;
		// Init setting
		whichvar._topPath.style.width = "1px";
		whichvar._scroller.style.width = Math.round(whichvar._scrollerHeight) + "px";
		whichvar._bottomPath.style.width = Math.round(whichvar._bottomPathHeight) + "px";
	}
}

function InitScrollbar(sbarname,varref,varname) {
	var brw = window.navigator.appName.toLowerCase();
	if (brw.indexOf("opera")>-1) {
		document.getElementById(sbarname+"_Bar").style.display = "none";
		document.getElementById(sbarname+"_scrollable").style.overflow = "auto";
	} else {
		varref = new ClassScrollbar(sbarname,varname);
	}
}
