function CustomScrollbar() {
	this.contentObj = null;
	this.contentHolder = null;
	this.gripObj = null;
	this.barObj = null;
	this.arrowLeft = null;
	this.arrowRight = null;
	this.direction = null;
	this.id = null;
	
	this.startDrag = function(e) {
		this.gripObj.clickedAt = (this.direction=='vertical')?project.MouseManager.mouseYPos-this.gripObj.offsetTop:project.MouseManager.mouseXPos-this.gripObj.offsetLeft;
		project.MouseManager.preventSelecting = true;
		var o = this;
		document.addToDoOMM('CustomScrollbarDrag_'+o.id, function(){o.drag();});
		document.addToDoOMU('CustomScrollbarRelease_'+o.id, function(){o.release();});
		if (e) e.preventDefault();
		else if (window.event) window.event.returnValue = false;
		else return false;
	}
	
	this.drag = function() {
		var minPos = 0;
		var maxPos = (this.direction=='vertical')?(this.barObj.offsetHeight - this.gripObj.offsetHeight):(this.barObj.offsetWidth - this.gripObj.offsetWidth);
		var newPos = ((this.direction=='vertical')?project.MouseManager.mouseYPos:project.MouseManager.mouseXPos)-this.gripObj.clickedAt;
		
		if (newPos < minPos) newPos = minPos;
		else if (newPos > maxPos) newPos = maxPos;
		
		if (this.direction=='vertical') this.gripObj.style.top = newPos+"px";
		else this.gripObj.style.left = newPos+"px";
		
		this.setContainer(newPos, maxPos);
	}
	
	this.release = function() {
		document.delToDoOMM('CustomScrollbarDrag_'+this.id);
		document.delToDoOMU('CustomScrollbarRelease_'+this.id);
	}
	
	this.goBack = function() {
		var minPos = 0;
		var maxPos = (this.direction=='vertical')?(this.barObj.offsetHeight - this.gripObj.offsetHeight):(this.barObj.offsetWidth - this.gripObj.offsetWidth);
		var newPos = (this.direction=='vertical')?(this.gripObj.offsetTop - (this.barObj.offsetHeight/10)):(this.gripObj.offsetLeft - (this.barObj.offsetWidth/10));
		if (newPos < minPos) newPos = minPos;
		
		if (this.direction=='vertical') this.gripObj.style.top = newPos+"px";
		else this.gripObj.style.left = newPos+"px";
		
		this.setContainer(newPos, maxPos);
	}
	
	this.goForward = function() {
		var minPos = 0;
		var maxPos = (this.direction=='vertical')?(this.barObj.offsetHeight - this.gripObj.offsetHeight):(this.barObj.offsetWidth - this.gripObj.offsetWidth);
		var newPos = (this.direction=='vertical')?(this.gripObj.offsetTop + (this.barObj.offsetHeight/10)):(this.gripObj.offsetLeft + (this.barObj.offsetWidth/10));
		if (newPos > maxPos) newPos = maxPos;
		
		if (this.direction=='vertical') this.gripObj.style.top = newPos+"px";
		else this.gripObj.style.left = newPos+"px";
		
		this.setContainer(newPos, maxPos);
	}
	
	this.setContainer = function(newPos, maxPos) {
		var scale = newPos / maxPos;
		if (this.direction=='vertical') this.contentObj.style.top = Math.round(-((this.contentObj.offsetHeight-this.contentHolder.offsetHeight)*scale))+"px";
		else this.contentObj.style.left = Math.round(-((this.contentObj.offsetWidth-this.contentHolder.offsetWidth)*scale))+"px";
	}
	
	this.resetGrip = function() {
		if (this.direction=='vertical') this.gripObj.style.top = "0px";
		else this.gripObj.style.left = "0px";
		
		if (this.direction=='vertical') this.contentObj.style.top = "0px";
		else this.contentObj.style.left = "0px";
	}
	
	this.setGrip = function(newPos, maxPos) {
		var scale = newPos / maxPos;
		if (this.direction=='vertical') this.gripObj.style.top = Math.round((this.gripObj.offsetHeight-this.barObj.offsetHeight)*scale)+"px";
		else this.gripObj.style.left = Math.round((this.gripObj.offsetWidth-this.barObj.offsetWidth)*scale)+"px";
	}
	
	this.initialize = function(id,dir,sliding) {
		this.id = id;
		this.direction = (dir=='vertical')?'vertical':'horizontal';
		this.contentObj = document.getElementById(id+'_Content');
		if (this.direction=='horizontal') {
			var maxWidth = 0;
			for (var i=0; i<this.contentObj.childNodes.length; i++) {
				var obj = this.contentObj.childNodes[i];
				var curWidth = obj.offsetLeft + obj.offsetWidth;
				if (curWidth > maxWidth) maxWidth = curWidth;
			}
			this.contentObj.style.width = maxWidth+"px";
		}
		this.contentHolder = this.contentObj.parentNode;
		this.gripObj = document.getElementById(id+'_Grip');
		this.gripObj.scrollbarObj = this;
		this.gripObj.onmousedown = function(e) {
			this.scrollbarObj.startDrag(e);
		}
		this.barObj = this.gripObj.parentNode;
		this.arrowBack = document.getElementById(id+'_ArrowBack');
		this.arrowBack.scrollbarObj = this;
		this.arrowBack.onclick = function() {
			this.scrollbarObj.goBack();
		}
		this.arrowForward = document.getElementById(id+'_ArrowForward');
		this.arrowForward.scrollbarObj = this;
		this.arrowForward.onclick = function() {
			this.scrollbarObj.goForward();
		}
	}
	
	this.reinitialize = function() {
		this.resetGrip();
		if (this.direction=='horizontal') {
			this.contentObj.style.width = "100000px";
			var maxWidth = 0;
			for (var i=0; i<this.contentObj.childNodes.length; i++) {
				var obj = this.contentObj.childNodes[i];
				var curWidth = obj.offsetLeft + obj.offsetWidth;
				if (curWidth > maxWidth) maxWidth = curWidth;
			}
			this.contentObj.style.width = maxWidth+"px";
		}
	}
}