
var Scroller = Class.create(
{
	initialize: function(scroll,options)
	{
		this.executer = false;
		this.progress = 0;
		this.scroll = $(scroll);
		this.maxWidth = this.scroll.scrollWidth;
		this.options = {afterChange: Prototype.emptyFunction,interval: 0.05,step: 5};
		this.scrollStep = 91;
		this.actualStep = 0;
		Object.extend(this.options,options || {});
	},
	left: function()
	{
		this.stop();
		this.executer = new PeriodicalExecuter(this.stepLeft.bind(this,this.options.step),this.options.interval);
	},
	right: function()
	{
		this.stop();
		this.executer = new PeriodicalExecuter(this.stepRight.bind(this,this.options.step),this.options.interval);
	},
	stop: function(reset)
	{
		if(this.executer)
			this.executer.stop();
	},
	stepLeft: function(amount)
	{
		var nextStep = this.scrollStep*(this.actualStep+-1);
		if (this.scroll.scrollLeft  > nextStep)
		{
			var next = this.progress-amount;
			if (next < nextStep)
				next = nextStep;
			this.setProgress(Math.max(0,next));
		}
		else
		{
			this.actualStep -= 1;
			this.stop();
		}
	},
	stepRight: function(amount)
	{
		var nextStep = this.scrollStep*(this.actualStep+1);
		if (this.scroll.scrollLeft  < nextStep)
		{
			var next = this.progress+amount;
			if (next > nextStep)
				next = nextStep;
			this.setProgress(Math.min(this.maxWidth,next));
		}
		else
		{
			this.actualStep += 1;
			this.stop();
		}
	},
	setProgress: function(value)
	{
		this.progress = value;
		this.scroll.scrollLeft = value;
	}
});

