var Scroller = Class.create ({
	
	initialize: function(container)
	{
		this.container = container;
		this.numItems = $$('div.sliderItem').size();
		this.scrollerWidth = (960*this.numItems);
		this.currentPos = $$('div.sliderItem').size();
		
		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), 7);
	},
	
	addControls: function()
	{	
		$$('a').each(function(container)
		{
			if (container.hasClassName('displayNone')) 
			{
				container.removeClassName('displayNone');
				container.addClassName('displayBlock');
			}
		});
		
		$$('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));
		
		/*
		this.container.select('video.video-js').each(function(link){
			link.observe("timeupdate", this.stopTimer.bind(this));
		}.bind(this));
		
		this.container.select('video.video-js').each(function(link){
			link.observe("pause", this.restartTimer.bind(this));
		}.bind(this));
		
		this.container.select('video.video-js').each(function(link){
			link.observe("ended", this.restartTimer.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();
		
		if(event.element().retrieve('page') > this.currentPos)
		{
			var amount = event.element().retrieve('page') - this.currentPos;
			//this.restartTimer();
			this.doMove(-amount);
		}
		
		if(event.element().retrieve('page') < this.currentPos)
		{
			var amount = this.currentPos - event.element().retrieve('page');
			//this.restartTimer();
			this.doMove(amount);
		}
	},
	
	doMove: function(amount)
	{	
		if(((this.currentPos - amount) > 0) && ((this.currentPos - amount) <= this.numItems))
		{
			this.currentPos = this.currentPos - amount;
			new Effect.Move($('sliderContent'), { x: (960*amount*-1), y: 0, mode: 'relative', duration: 1, queue: { position: 'end', scope: 'image' } });
			this.updateControls();
		}
	},
	
	updateControls: function()
	{
		var currentLi = 0;
		
		this.container.select('li').each(function(container)
		{
			if (container.hasClassName('selected')) 
			{
				container.removeClassName('selected');
			}
		});	
		
		this.container.select('li')[this.currentPos-1].addClassName('selected');
	},
	
	/*
	stopTimer: function()
	{
		this.timer.stop();
	},
	
	restartTimer: function()
	{
		this.timer.stop();
		this.timer = new PeriodicalExecuter(this.timedMove.bind(this), 7)
	},
	
	timedMove: function()
	{
		if((this.currentPos) == 1)
		{
			this.doMove((this.numItems-1)*-1);
		}
		else
		{
			this.doMove(1);
		}
	}
	*/
});


document.observe('dom:loaded', function()
{	
	if ($("sliderBox"))
	{
		new Scroller($('sliderBox'));
	}
});
