var carousel = new Class({
 	
	Implements: [Events, Options],
	
	options: {
		'items': 		  'div.object',
		'selected': 	  0,
		'autoStart':      true,
		'useButtons':     false,
		'timer':		  6000,
		'transition': 	  Fx.Transitions.Sine.easeInOut,
		'mode': 		  'h',
		'buttons': {
			'left': 'button_left',
			'right': 'button_right'
		}
	},
	
	initialize: function(container, options){
		this.container = $(container);
		this.setOptions(options);
		
		this.items = Array();
		
		this.initSlide();
	},
	
	initSlide: function(){
		switch(this.options.mode){
			case 'h':
				var width  = 10000;
				var height = this.container.getSize().y.toInt();
				break;
			case 'v':
				var width  = this.container.getSize().x.toInt();
				var height = 10000;
				break;
		}
		
		this.container.setStyle('overflow', 'hidden');
		
		this.wrapper = new Element('div', {
			'class': 'wrapper',
			styles: {
				position: 'absolute',
				top: 0,
				left: 0,
				width: 	width,
				height: height
			}
		}).inject(this.container, 'top');

		var top = 0;
		var left = 0;
		this.container.getElements(this.options.items).each(function(item, i){
			item.setStyles({
				'position': 'relative',
				'float': 'left'
			});
			
			item.top  = top;
			item.left = left;
			
			this.wrapper.adopt(item);
			this.items[i] = item;
			
			top  -= item.getSize().x.toInt() + item.getStyle('margin-top').toInt() + item.getStyle('margin-bottom').toInt() + item.getStyle('padding-top').toInt() + item.getStyle('padding-bottom').toInt();
			left -= item.getSize().x.toInt() + item.getStyle('margin-left').toInt() + item.getStyle('margin-right').toInt() + item.getStyle('padding-left').toInt() + item.getStyle('padding-right').toInt();
		}.bind(this));

		this.slider = new Fx.Morph(this.wrapper, {
			duration: 500,
			transition: this.options.transition,
			fps: 2000,
			onComplete: function(){
				//this.cloneItem();
			}.bind(this)
		});
		
		this.start();
	},
	
	start: function(){
		if(this.options.autoStart){
			this.slideshow();
		}else if(this.options.useButtons){
			$(this.options.buttons.left).addEvent('click', function(){
				this.prev();
			}.bind(this));
			$(this.options.buttons.right).addEvent('click', function(){
				this.next();
			}.bind(this));
		}
		
		this.slideIn(0);
	},
	
	prev: function(){
		var item = (this.options.selected-1);
		if(item >= 0) this.slideIn(item);
	},
	
	next: function(){
		var item = (this.options.selected+1);
		switch(this.options.mode){
			case 'h':
				var last = -((this.items.getLast().getPosition(this.wrapper).x + this.items.getLast().getSize().x.toInt()) - this.container.getSize().x.toInt()+20);
				if(this.items[item].left >= last && item <= this.items.length) this.slideIn(item);
				break;
			case 'v':
				if(item <= this.items.length) this.slideIn(item);
				break;
		}
	},
	
	slideIn: function(item){
		this.slideOut();
		this.options.selected = item;
		
		this.slider.cancel();
		
		switch(this.options.mode){
			case 'h':
				this.slider.start({ 'left': this.items[item].left});
				break;
			case 'v':
				this.slider.start({ 'top': this.items[item].top});
				break;
		}
	},
	
	slideOut: function(item){
		if(typeof(item)=='undefined') item = this.options.selected;
	},
	
	slideshow: function(){
		if (this.options.autoStart) {
			this.periodical = (function(){
				this.next();
			}.bind(this)).periodical(this.options.timer);
		}
	}
	
});
