﻿function roll(btnArr, obj){
	this.obj = this.get(obj);
	if(!this.obj)return;
	this.offsetWidth = this.obj.offsetWidth// - parseInt(this.getStyle('borderLeftWidth')) - parseInt(this.getStyle('borderRightWidth'));
	this.scrollWidth = this.obj.scrollWidth;
	if(this.scrollWidth <= this.offsetWidth)return;
	this.left = this.get(btnArr[0]);
	this.right= this.get(btnArr[1]);
	if(!this.left || !this.right)return;
	this.left.onclick = this.scrollLeft;
	this.right.onclick = this.scrollRight;
	this.left['roll'] = this.right['roll'] = this; //!
	this.scrollLeft = 0;
	this.MaxRight = this.scrollWidth-this.offsetWidth;
	this.status = false;
}
roll.prototype = {
	get: function(o){return document.getElementById(o);},
	//getStyle: function(v){return (typeof this.obj.currentStyle != 'undefined')?this.obj.currentStyle[v]:document.defaultView.getComputedStyle(this.obj, null)[v];},
	scrollLeft: function(){
		if(this.roll.status)return false;
		if(this.roll.scrollLeft == this.roll.MaxRight)return false;//如果已经移动到最左边了.就不用再执行移动函数了
		this.roll.status = true;
		var s = this.roll.scrollLeft;//开始位置
		s = s + this.roll.offsetWidth;//结束位置
		if(s > this.roll.MaxRight) s = this.roll.MaxRight;
		this.roll.move.apply(this.roll, [this.roll.scrollLeft, s]);
		return false;
	},
	scrollRight: function(){
		if(this.roll.status)return false;
		if(this.roll.scrollLeft == 0)return false;
		this.roll.status = true;
		var s = this.roll.scrollLeft - this.roll.offsetWidth;
		s = s<0?0:s;
		this.roll.move.apply(this.roll, [this.roll.scrollLeft, s]);
		return false;
	},
	move: function(e, s){//起始位置,结束位置
		this.scrollLeft = s;
		var _this = this;
		var a = e;
		if(e<s){
			(function(){
				if((_this.obj.scrollLeft = a = Math.ceil(s - (s-a)/1.2))<s){
					setTimeout(arguments.callee, 20);
				}else{
					_this.status = false;
					a = _this = null;
				}
			})();
		}else if(e>s){
			(function(){
				if((_this.obj.scrollLeft = a = Math.floor(a - (a-s)/10.2))>s){
					setTimeout(arguments.callee, 20);
				}else{
					_this.status = false;
					a = _this = null;
				}
			})();
		}
	}
}
