﻿
var ScrollLayer = {
	_currentObj: null,
	_interval: null,
	_dir: "",
	_jumpstep: 81 * 2,
	SetJumpStepSize: function(size) {
		this._jumpstep = size;
	},
	SmoothMove: function() {
		if (!this._currentObj && this._interval) {
			window.clearInterval(this._interval);
			this._interval = false;
			return;
		}
		if (!this._interval) {
			var root = this;
			var func = function() {
				root.SmoothMove.call(root);
			};
			this._interval = window.setInterval(func, 50);
		} else {
			if (this._currentObj) {
				var obj = this._currentObj;
				if (this._dir == "left") {
					obj.scrollLeft -= 10;
				} else {
					obj.scrollLeft += 10;
				}
			} else {
				window.clearInterval(this._interval);
				this._interval = false;
			}
		}
	},
	Move: function(boxid, dir) {
		var obj = document.getElementById(boxid);
		if (!obj) {
			return;
		}
		this._currentObj = obj;
		this._dir = dir;
		this.SmoothMove();
	},
	_lastJump_t: null,
	SlowJump: function() {
		var obj = this._currentObj;
		if (!obj) { return; }
		var targetPos = obj.scrollLeft;
		var jump = this._jumpstep;
		if (this._dir == "left") {
			targetPos -= jump;
		} else {
			targetPos += jump;
		}
		if (targetPos < 0) {
			targetPos = 0;
		} else if (targetPos > obj.scrollWidth - obj.offsetWidth) {
			targetPos = obj.scrollWidth - obj.offsetWidth;
		}
		//alert(obj.scrollWidth-obj.offsetWidth);
		var root = this;
		var t = null;
		var go = function() {
			var space = targetPos - obj.scrollLeft;
			if (space != 0) {
				if (space >= 2 || space <= -2) {
					var step = space / 2;
					if (step >= 30) {
						step = 30;
					} else if (step <= -30) {
						step = -30;
					}
					obj.scrollLeft += step;
				} else {
					obj.scrollLeft += space;
				}
				//alert(obj.scrollLeft + " => " + targetPos);
			} else {
				//alert("stopped.");
				window.clearInterval(t);
				root._lastJump_t = null;
			}
		};
		t = window.setInterval(go, 50);
		this._lastJump_t = t;
	},
	Jump: function(boxid, dir) {
		if (this._lastJump_t) { return; }
		var obj = document.getElementById(boxid);
		if (!obj) {
			return;
		}
		this._currentObj = obj;
		this._dir = dir;
		this.SlowJump();
	},
	Stop: function() {
		if (this._currentObj) { this._currentObj = null; }
		if (this._interval) {
			window.clearInterval(this._interval);
			this._interval = false;
		}
	}
};
