Effect.Transitions.easeInOutCubic=function(pos){if((pos/=0.5)<1)return 0.5*Math.pow(pos,3);return 0.5*(Math.pow((pos-2),3)+2);}

// JavaScript Document
Scroller = Class.create({
	// Elements
	container: null,
	slides: null,
	
	// Optional properties
	delay: 1,
	slide_width:155,
	
	// State flags
	is_animating: false,
	
	// Initialize
	initialize: function(container, params)
	{
		this.broadcast('Init Scroller');
		
		// Get container
		this.broadcast('Getting container');
		if($(container)) this.container = $(container);
		else this.broadcast('Container element does not exist.');
		
		// Get slides
		this.broadcast('Getting slides');
		if(this.container.select('li').length) this.slides = this.container.select('li');
		else this.broadcast('Container element contains no slides (slides are defined by nested li elements)');
		
		// Parse options parameters
		if(!params) var params = {};
		
		if(params.delay) this.delay = params.delay;
		if(params.slide_width) this.slide_width = params.slide_width;
		
		// Prepare slides
		this.broadcast('Preparing slides');
		this.slides.each(this.prepareSlide.bind(this));
		
		// Attach trigger behaviours
		if(this.container.down('#scroller-next')) this.container.down('#scroller-next').observe('click', this.next.bind(this));
		else this.broadcast('\'Next\' trigger cannot be found');
		if(this.container.down('#scroller-prev')) this.container.down('#scroller-prev').observe('click', this.previous.bind(this));
		else this.broadcast('\'Previous\' trigger cannot be found');
		
		// Set timer
		this.broadcast('Setting timer');
    this.roll.delay(2, this);
	},
	
	prepareSlide: function(slide, i)
	{
		slide.setStyle({
			width: this.slide_width + 'px',
			left: (this.slide_width * i) + 'px',
			position: 'absolute',
			top:'0'
		});
	},
	
	next: function(event)
	{
		this.broadcast('Next');
		
		if(event) event.stop();
		
		if(this.is_animating) return;
		
		this.slides.each(function(slide)
		{
			this.is_animating = true;
			
			new Effect.Move(slide, {
				x: -this.slide_width,
				/*y: 0,*/
				duration: this.delay,
				transition: Effect.Transitions.easeInOutCubic,
				afterFinish: function() { this.is_animating = false; }.bind(this)
			});
			
			if(parseInt(slide.getStyle('left')) < 0) slide.setStyle({left: ((this.slides.length - 1) * this.slide_width) + 'px'});
		}.bind(this));
	},
	
	previous: function(event)
	{
		this.broadcast('Previous');
		
		if(event) event.stop();
		
		if(this.is_animating) return;
		
		this.slides.each(function(slide)
		{
			if(parseInt(slide.getStyle('left')) >= (this.slides.length - 1) * this.slide_width) slide.setStyle({left: '-' + this.slide_width + 'px'});
			
			this.is_animating = true;
			
			new Effect.Move(slide, {
				x: this.slide_width,
				/*y: 0,*/
				duration: this.delay,
				transition:Effect.Transitions.easeInOutCubic,
				afterFinish: function() { this.is_animating = false; }.bind(this)
			});
		}.bind(this));
	},
	
	roll: function(self)
	{
		self.next();
		self.roll.delay(4, self);
	},
	
	broadcast: function(message, public)
	{
		if(public) alert(message);
		else try{console.log(message);} catch(e) {}
	}
});
