/**
 * @author thomas
 */
var Player = new Class({
	Implements: [Options, Events],
	
	options: {
		'items': null,
		'count': 0,
		'interval': 3000,
		'fxOpts': {},
		'autoPlay': true,
		'autoPlayDirection': 'next'
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.curIndex = 0;
		this.lastIndex = 0;
		this.options.items = $$(this.options.items);
		this.dir = 'jump';
		
		this.options.count = $pick(this.options.items.length, this.options.count);
		
		if (this.options.autoPlay && this.options.count > 1) {
			this.slideTo(this.curIndex);
			this.play(this.options.interval, this.options.autoPlayDirection);
		}
	},
	
	slide: function(){
		return this;
	},
	
	slideTo: function(index, manual, fx){
		fx = ($defined(fx) ? fx : true);
		
		if (manual) {
			this.stop();
		}
		
		this.lastIndex = this.curIndex;
		this.curIndex = index;
		
		if (this.curIndex >= this.options.count) {
			this.curIndex = 0;
		}
		else if (this.curIndex < 0) {
			this.curIndex = this.options.count - 1;
		}
		this.fireEvent('slide');
		this.slide();
		this.dir = 'jump';
		return this;
	},
	
	next: function(manual, fx){
		this.dir = 'next';
		return this.slideTo(this.curIndex + 1, manual, fx);
	},
	prev: function(manual, fx){
		this.dir = 'prev';
		return this.slideTo(this.curIndex - 1, manual, fx);
	},
	
	play: function(interval, direction){
		this.stop();
		this._play = this[direction].periodical(interval, this);
		return this;
	},
	
	resume: function(){
		if (this.options.autoPlay) {
			return this.play(this.options.interval, this.options.autoPlayDirection);
		}
		return this;
	},
	
	stop: function(){
		$clear(this._play);
		return this;
	}
});
