var scrollBars = new Array();

function initScrollBars() {
	var args = initScrollBars.arguments;
	var i,idObj;
	for(i=0;i<args.length;i++) {
		idObj = document.getElementById(args[i].id);
		if(idObj != null) {
			idObj.scrollArrayId = scrollBars.length;
			idObj.scrollOrient = args[i].orient;
			idObj.scrollFrameSize = args[i].frameSize;
			idObj.scrollBWiF = args[i].BWiF;
			idObj.scrollCycled = (idObj.scrollBWiF == 1) ? args[i].cycled : false;
			idObj.scrollActiveFrame = 1;
			idObj.scrollFrameShift = 0;
			idObj.scrollPosition = 0;
			idObj.scrollSpeed = 0;
			idObj.scrollAcceleration = args[i].acceleration;
			idObj.scrollTopSpeed = args[i].topSpeed;
			if(args[i].orient == 0) {
				idObj.scrollLength = idObj.scrollWidth;
			} else {
				idObj.scrollLength = idObj.scrollHeight;
			}
			idObj.scrollFrameCount = idObj.scrollCycled ? (idObj.scrollLength / idObj.scrollFrameSize) : 
				(idObj.scrollLength / idObj.scrollFrameSize + 1);
			
			idObj.scrollIncFrame = function() {
				this.scrollFrameShift = (this.scrollFrameShift > 0)?(this.scrollFrameShift + 1):1;
				if(!this.scrollCycled && ((this.scrollActiveFrame + this.scrollFrameShift) > (this.scrollFrameCount - this.scrollBWiF)))
					this.scrollFrameShift = this.scrollFrameCount - this.scrollBWiF - this.scrollActiveFrame;
				return false;
			}
			idObj.scrollDecFrame = function() {
				this.scrollFrameShift = (this.scrollFrameShift < 0)?(this.scrollFrameShift - 1):-1;
				if(!this.scrollCycled && (this.scrollFrameShift < 1 - this.scrollActiveFrame)) this.scrollFrameShift = 1 - this.scrollActiveFrame;
				return false;
			}
			idObj.scrollIncSpeed = function(directionSign) {
				if(Math.abs(this.scrollSpeed + this.scrollAcceleration * directionSign) <= this.scrollTopSpeed) this.scrollSpeed += this.scrollAcceleration * directionSign;
				else this.scrollSpeed = this.scrollTopSpeed * directionSign;
				return false;
			}
			idObj.scrollDecSpeed = function(directionSign) {
				if(Math.abs(this.scrollSpeed + this.scrollAcceleration * directionSign) >= 0) this.scrollSpeed -= this.scrollAcceleration * directionSign;
				else this.scrollSpeed = 0;
				return false;
			}
			idObj.scrollToStart = function(shiftPos) {
				this.scrollPosition = 0 + shiftPos;
			}
			idObj.scrollToFinish = function(shiftPos) {
				this.scrollPosition = this.scrollLength - this.scrollFrameSize * this.scrollBWiF + shiftPos;
			}
			idObj.scrollFixPos = function() {
				this.scrollPosition = (this.scrollActiveFrame - 1) * this.scrollFrameSize;
			}
			idObj.scrollProcessData = function() {
				var directionSign = (this.scrollFrameShift == 0)?0:(this.scrollFrameShift / Math.abs(this.scrollFrameShift));
				var brakeTime = Math.abs(this.scrollSpeed / this.scrollAcceleration);
				var brakeDistance = Math.abs(this.scrollSpeed * brakeTime) 
					- ((Math.abs(this.scrollAcceleration) * brakeTime * brakeTime) / 2) 
					+ ((this.scrollAcceleration * this.scrollAcceleration * brakeTime) / 2)
					/* + this.scrollSpeed*/;
				/*var absoluteFrameShift = this.scrollFrameShift % this.scrollFrameCount + 1;
				if(absoluteFrameShift < 0) absoluteFrameShift += this.scrollFrameCount;*/
				var finishPos = (this.scrollActiveFrame + this.scrollFrameShift - 1) * this.scrollFrameSize; //(absoluteFrameShift) * this.scrollFrameSize;
				
				if(directionSign != 0) {
					if(Math.abs(finishPos - this.scrollPosition) > brakeDistance) {
						this.scrollIncSpeed(directionSign);
					} else {
						this.scrollDecSpeed(directionSign);
					}
					this.scrollPosition += this.scrollSpeed;
					if(this.scrollCycled) {
						if(this.scrollPosition < 0) {
							this.scrollPosition += this.scrollFrameCount * this.scrollFrameSize;
							this.scrollFrameShift++;
						}
						else if((this.scrollPosition > (this.scrollFrameCount - 1) * this.scrollFrameSize) && this.scrollActiveFrame == this.scrollFrameCount) {
							this.scrollPosition -= (this.scrollFrameCount - 1) * this.scrollFrameSize;
							//this.scrollFrameShift--;
						}
					} else {
						if(this.scrollPosition < 0) this.scrollPosition = 0;
						if(this.scrollPosition > (this.scrollFrameCount - 1) * this.scrollFrameSize) this.scrollPosition = (this.scrollFrameCount - 1) * this.scrollFrameSize;
					}
					if(((Math.floor(this.scrollPosition / this.scrollFrameSize) + 2) < this.scrollActiveFrame)) {
						this.scrollActiveFrame = Math.floor(this.scrollPosition / this.scrollFrameSize) + 2;
						this.scrollFrameShift -= directionSign;
					}
					if(((Math.floor(this.scrollPosition / this.scrollFrameSize) - 1) > this.scrollActiveFrame)) {
						this.scrollActiveFrame = Math.floor(this.scrollPosition / this.scrollFrameSize) - 1;
						//this.scrollFrameShift -= directionSign;
					}
					if(Math.abs(((this.scrollActiveFrame - 1) * this.scrollFrameSize) - this.scrollPosition) >= this.scrollFrameSize) {
						this.scrollFrameShift -= directionSign;
						this.scrollActiveFrame += directionSign;
					}
				} else {
					this.scrollFixPos();
					this.scrollSpeed = 0;
				}
				return true;
			}
			
			idObj.scrollIncObj = document.getElementById(args[i].incObjId);
			idObj.scrollIncObj.scrollParent = idObj;
			idObj.scrollIncObj.onclick = function() {
				this.scrollParent.scrollIncFrame();
				this.blur();
				if(this.scrollParent.scrollFrameShift - 1 == 0) scrollRender();
				return false;
			}
			idObj.scrollDecObj = document.getElementById(args[i].decObjId);
			idObj.scrollDecObj.scrollParent = idObj;
			idObj.scrollDecObj.onclick = function() {
				this.scrollParent.scrollDecFrame();
				this.blur();
				if(this.scrollParent.scrollFrameShift + 1 == 0) scrollRender();
				return false;
			}
			scrollBars.push(idObj);
		}
		setTimeout('scrollRender()',20);
	}
}

scrollRender = function() {
	var i,f = 0;
	for(i=0;i<scrollBars.length;i++) {
		scrollBars[i].scrollProcessData();
		if(scrollBars[i].scrollOrient == 0) {
			scrollBars[i].scrollLeft = scrollBars[i].scrollPosition;
		} else {
			scrollBars[i].scrollTop = scrollBars[i].scrollPosition;
		}
		f += Math.abs(scrollBars[i].scrollFrameShift);
	}
	if(f != 0) setTimeout('scrollRender()',20);
	return true;
}
