var Slider = Class.create ({
	
	initialize: function(container, time)
	{
		this.time = time; //time in seconds betwen sliding
		this.container = container;
		this.itemWidth = this.container.select('div.sliderItem')[0].getWidth();
		this.numItems = this.container.select('div.sliderItem').size();
		this.scrollerWidth = (this.itemWidth*this.numItems);
		this.currentPos = 1;
		
		this.container.select('div.sliderContent')[0].setStyle({width: this.scrollerWidth + "px"});
		
		if (!this.container)
		{
			return false;
		}
		this.addControls();
		this.updateControls();
		
		this.timer = new PeriodicalExecuter(this.timedMove.bind(this), this.time);
	},
	
	addControls: function()
	{	
		this.container.select('a').each(function(container)
		{
			if (container.hasClassName('displayNone')) 
			{
				container.removeClassName('displayNone');
				container.addClassName('displayBlock');
			}
		});
		
		this.container.select('div').each(function(container)
		{
			if (container.hasClassName('displayNone')) 
			{
				container.removeClassName('displayNone');
				container.addClassName('displayBlock');
			}
		});
		
		this.container.select('a.quick').each(function(container, index)
		{
			container.store('page', index+1);
		});	
		
		this.container.select('a.prev').each(function(link){
			link.observe("click", this.prevclicked.bind(this));
		}.bind(this));
		
		this.container.select('a.next').each(function(link){
			link.observe("click", this.nextclicked.bind(this));
		}.bind(this));
		
		this.container.select('a.quick').each(function(link){
			link.observe("click", this.quickclicked.bind(this));
		}.bind(this));
	},
	
	prevclicked: function(event)
	{
		if(!event)
		{
			return false;
		}
		
		event.stop();
		
		this.restartTimer();
		this.doMove(-1);
	},
	
	nextclicked: function(event)
	{
		if(!event)
		{
			return false;
		}
		
		event.stop();
		
		this.restartTimer();
		this.doMove(1);
	},
	
	quickclicked: function(event)
	{
		if(!event)
		{
			return false;
		}
		
		event.stop();
		this.restartTimer();
		
		if(event.element().retrieve('page') > this.currentPos)
		{
			var amount = event.element().retrieve('page') - this.currentPos;
			this.doMove(amount);
		}
		
		if(event.element().retrieve('page') < this.currentPos)
		{
			var amount = this.currentPos - event.element().retrieve('page');
			this.doMove(-amount);
		}
	},
	
	updateControls: function()
	{		
		this.container.select('li').each(function(container)
		{
			if (container.hasClassName('selected'))
			{
				container.removeClassName('selected');
			}
		});	
		
		if(this.container.select('li')[this.currentPos-1])
		{
			this.container.select('li')[this.currentPos-1].addClassName('selected');
		}
	},
	
	doMove: function(amount)
	{
		if((this.currentPos + amount >= 1) && (this.currentPos + amount <= this.numItems))
		{
			new Effect.Move(this.container.select('div.sliderContent')[0], { x: (this.itemWidth*amount*-1), y: 0, mode: 'relative', duration: 1.5, queue: { position: 'end', scope: this.container.identify() } });
			this.currentPos = this.currentPos + amount;
		}
			
		this.updateControls();
	},
	
	restartTimer: function()
	{
		this.timer.stop();
		this.timer = new PeriodicalExecuter(this.timedMove.bind(this), this.time)
	},
	
	timedMove: function()
	{
		if((this.currentPos) == this.numItems)
		{
			this.doMove((this.numItems-1)*-1);
		}
		else
		{
			this.doMove(1);
		}
	}
});


document.observe('dom:loaded', function()
{		
	if ($("sliderBox2"))
	{
		new Slider($('sliderBox2'), 7);
	}
});
