(function(){
	if(typeof window.tw == 'undefined') tw = {};

	//	定义函数域
	tw.mainScroll = (function(){
		var _Scroll = function(config){
			this.imgScroll = config.imgScroll || {};
			this.prePage = config.prePage || {};
			this.nextPage = config.nextPage || {};
			this.currPage = config.currPage || {};
			this.distance = config.distance || 0;
			this.step = config.step || 0;
			this.scrollTimes = 0;
			this.currPageIndex = 1;
		};

		_Scroll.prototype = {
			//	滚动函数
			_setScroll : function(){
				this.scrollTimes = this.step;
				var _this = this;
				var nextScrollEnd = false;
				var prevScrollEnd = false;

				//	下一张
				this.nextPage.onclick = function(){
					if(nextScrollEnd)	return;
					var from = parseInt(_this.imgScroll.style.marginLeft) || 0;
					var to = from-_this.distance;

					if(_this.step === 1) return;
					setTimeout(function(){
						nextScrollEnd = true;
						new $JN.tween(_this.imgScroll, {
							from : from,
							to : to,
							scrollEnd : function(){
								nextScrollEnd = false;	//	滚动完成，可以开始下一轮滚动
							},
							dire : 'marginLeft'
						});
						_this.step--;
						_this.currPage.innerHTML = ++_this.currPageIndex;
					}, 0);
				};

				//	上一张
				this.prePage.onclick = function(){
					if(prevScrollEnd)	return;
					var from = parseInt(_this.imgScroll.style.marginLeft) || 0;
					var to = from+_this.distance;

					if(_this.step == _this.scrollTimes) return;
					setTimeout(function(){
						prevScrollEnd = true;
						new $JN.tween(_this.imgScroll, {
							from : from,
							to : to,
							scrollEnd : function(){
								prevScrollEnd = false;	//	滚动完成，可以开始下一轮滚动
							},
							dire : 'marginLeft'
						});
						_this.step++;
						_this.currPage.innerHTML = --_this.currPageIndex;
					}, 0);
				};
			}
		};

		return{
			init : function(config){
				var scroll = new _Scroll(config);
				scroll._setScroll();
			}
		}
	})()
})();