var Scroller=function(direction, containerId, elementSize, numElements, increment, delay, autoDelay, indexImgId, indexImgPrefix, indexImgSuffix)
{
	this.containerId=containerId;
	this.elementSize=elementSize;
	this.numElements=numElements;
	this.increment=increment;
	this.delay=delay;
	this.autoDelay=autoDelay;
	this.indexImgId=indexImgId;
	this.indexImgPrefix=indexImgPrefix;
	this.indexImgSuffix=indexImgSuffix;
	
	this.targetIndex=0;
	this.timer=null;
	this.autoTimer=null;
	
	this.valueFunc=null;
	if (direction == 0)
	{
		this.valueFunc=function(size) { return $(this.containerId).scrollLeft(size)};
	}
	else
	{
		this.valueFunc=function(size) { return $(this.containerId).scrollTop(size)};
	}
	
	if (this.autoDelay > 0)
	{
		var othis=this;
		$(containerId).hover(function() { othis.autoScrollStop.call(othis); }, function() { othis.autoScrollStart.call(othis); });
	}
};
		
Scroller.prototype.autoScrollStart=function()
{
	if (this.autoDelay > 0)
	{
		var othis=this;
		this.autoTimer = setInterval( function() { othis.autoScrollToNextItem.call(othis); } , this.autoDelay);
	}
};

Scroller.prototype.autoScrollStop=function()
{
	if (this.autoDelay > 0)
	{
		clearInterval(this.autoTimer);
		this.autoTimer=null;
	}
};
	
Scroller.prototype.autoScrollToNextItem=function()
{
	this.targetIndex=(this.targetIndex+1)%this.numElements;
	
	this.startScrolling();
};
	
Scroller.prototype.scrollToIndex=function(index)
{	
	this.autoScrollStop();
	
	this.targetIndex=index;
	
	this.startScrolling();
	
	this.autoScrollStart();
};
	
Scroller.prototype.startScrolling=function()
{
	if (this.timer != null)
	{
		clearInterval(this.timer);
		this.timer=null;
	}
	
	var othis=this;
    this.timer = setInterval( function() { othis.scrollIteration.call(othis); } , this.delay);
};

Scroller.prototype.scrollIteration=function()
{
	
	if (this.valueFunc() > (this.targetIndex*this.elementSize)+this.increment)
	{
		this.valueFunc(this.valueFunc()-this.increment);
	}
	else if (this.valueFunc() < (this.targetIndex*this.elementSize)-this.increment)
	{
		this.valueFunc(this.valueFunc()+this.increment);
	}
	else
	{
		this.valueFunc(this.targetIndex*this.elementSize);
		clearInterval(this.timer);
		this.timer=null;
	}
	
	if (this.indexImgId !== null)
	{
		$(this.indexImgId).attr('src',this.indexImgPrefix+(Math.floor(this.valueFunc()/this.elementSize)%this.numElements)+this.indexImgSuffix);
	}
};