// Requires: xelement.js, xelementobject.js, xwindow.js
if(typeof(xElement) == "undefined") alert("xElement Required");
if(typeof(xElementObject) == "undefined") alert("xElementObject Required");
if(typeof(xWindow) == "undefined") alert("xWindow Required");

// ||||||||||||||||||||||||||||||||||||||||||||||||||

xScroll = function(speed, dragHeight, trackHeight, trackDiv, upDiv, downDiv, dragDiv, maskDiv, contentDiv){
	this.speed = speed;
	this.dragHeight = dragHeight;
	this.trackHeight = trackHeight;
	this.trackObj = new xElementObject(trackDiv);
	this.upObj = new xElementObject(upDiv);
	this.downObj = new xElementObject(downDiv);
	this.dragObj = new xElementObject(dragDiv);
	this.maskObj = new xElementObject(maskDiv);
	this.contentObj = new xElementObject(contentDiv);
	this.obj = "xScrollObject"+contentDiv;
	eval(this.obj+"=this");
	this.trackTop = this.dragObj.getTop();
	this.trackLength = this.trackHeight-this.dragHeight;
	this.trackBottom = this.trackTop+this.trackLength;
	this.maskHeight = this.maskObj.getClipHeight();
	this.contentHeight = this.contentObj.getHeight();
	this.contentLength = this.contentHeight-this.maskHeight;
	this.scrollLength = this.trackLength/this.contentLength;
	this.scrollTimer = null;
	if(this.contentHeight <= this.maskHeight){
		this.trackObj.hideVis();
		this.upObj.hideVis();
		this.downObj.hideVis();
		this.dragObj.hideVis();
	}else{
		var _this = this;
		this.trackObj.addEvent("mousedown", function(e){_this.scrollJump(e);return false});
		this.upObj.addEvent("mousedown", function(){_this.scroll(_this.speed);return false});
		this.upObj.addEvent("mouseup", function(){_this.stopScroll()});
		this.upObj.addEvent("mouseout", function(){_this.stopScroll()});
		this.downObj.addEvent("mousedown", function(){_this.scroll(-_this.speed);return false});
		this.downObj.addEvent("mouseup", function(){_this.stopScroll()});
		this.downObj.addEvent("mouseout", function(){_this.stopScroll()});
		this.dragObj.addEvent("mousedown", function(e){_this.startDrag(e);return false});
		if(document.all) this.dragObj.addEvent("dragstart", function(){return false});
		document.addEvent = function(event,method){xElement.addEvent(document,event,method)};
		document.removeEvent = function(event){xElement.removeEvent(document,event)};
	}
};
xScroll.prototype.startDrag = function(e){
	this.dragStartMouse = xWindow.getMouseTop(e);
	this.dragStartOffset = this.dragObj.getTop();
	var _this = this;
	document.addEvent("mousemove", function(e){_this.drag(e)});
	document.addEvent("mouseup", function(){_this.stopDrag()});
};
xScroll.prototype.stopDrag = function(){
	document.removeEvent("mousemove");
	document.removeEvent("mouseup");
};
xScroll.prototype.drag = function(e){
	var currentMouse = xWindow.getMouseTop(e);
	var mouseDifference = currentMouse-this.dragStartMouse;
	var dragDistance = this.dragStartOffset+mouseDifference;
	var dragMovement = (dragDistance<this.trackTop) ? this.trackTop : (dragDistance>this.trackBottom) ? this.trackBottom : dragDistance;
	this.dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-this.trackTop)*(1/this.scrollLength);
	this.contentObj.setTop(contentMovement);
};
xScroll.prototype.scroll = function(speed){
	var contentMovement = this.contentObj.getTop()+speed;
	var dragMovement = this.trackTop-Math.round(this.contentObj.getTop()*(this.trackLength/this.contentLength));
	if(contentMovement > 0){
		contentMovement = 0;
	}else if(contentMovement < -this.contentLength){
		contentMovement = -this.contentLength;
	}
	if(dragMovement < this.trackTop){
		dragMovement = this.trackTop;
	}else if(dragMovement > this.trackBottom){
		dragMovement = this.trackBottom;
	}
	this.contentObj.setTop(contentMovement);
	this.dragObj.setTop(dragMovement);
	this.scrollTimer = window.setTimeout(this.obj+".scroll("+speed+")",25);
};
xScroll.prototype.stopScroll = function(){
	if(this.scrollTimer){
		window.clearTimeout(this.scrollTimer);
		this.scrollTimer = null;
	}
};
xScroll.prototype.scrollJump = function(e){
	var currentMouse = xWindow.getMouseTop(e);
	var dragDistance = currentMouse-(this.dragHeight/2);
	var dragMovement = (dragDistance<this.trackTop) ? this.trackTop : (dragDistance>this.trackBottom) ? this.trackBottom : dragDistance;
	this.dragObj.setTop(dragMovement);
	var contentMovement = -(dragMovement-this.trackTop)*(1/this.scrollLength);
	this.contentObj.setTop(contentMovement);
};
xScroll.prototype.scrollTo = function(pct) {
	var dragMovement = pct * (this.trackBottom - this.trackTop) / 100 + this.trackTop;
	this.dragObj.setTop(dragMovement);
	
	var contentMovement = -(dragMovement-this.trackTop)*(1/this.scrollLength);
	this.contentObj.setTop(contentMovement);	
};


