var Inficarousel = new Class({
	Implements: [Events, Options],
	options: {
		tail:5,
		fx_duration:750,
		timed:5000
	},
	initialize: function(el,options) {
		this.el = el;
		this.width=0;
		this.setOptions(options);
		this.lis = el.getElements('li');
		
		this.lis.each(function(item){
			// item.setStyle('width',item.getElement('span').getCoordinates().width+10);
			
			if ( (span_w = item.getElement('span').getCoordinates().width+10) < 200) span_w = 200;
			
			
			item.getElement('a').setStyle('width',span_w);
			
			
			
			item.addEvent('mouseover', function() {
				$clear(this.timer);
			}.bind(this)).addEvent('mouseout', function() {
				this.set_timer()
			}.bind(this));
			
			this.width+=item.getSize().x;
			item.store('margin-left',item.getStyle('margin-left').toInt() );
			this.width+=item.getStyle('margin-left').toInt()+item.getStyle('margin-right').toInt();
		}.bind(this));

		this.el.setStyle('width', this.width);

		var last = this.lis.getLast();
		last.inject(this.el,'top').setStyle('margin-left',( this.options.tail-last.getSize().x ) );

		this.lis = this.el.getElements('li');
		
		
		this.options.go_left.addEvent('click', function(){
			this.go_left();
			return false;
		}.bind(this));

		this.options.go_right.addEvent('click', function(){
			this.go_right();
			return false;
		}.bind(this));



		this.el.set('tween', {
			onComplete: function()	{
				if ( this.dir == 'l') {
					var first = this.lis[0];
					first.inject(this.el,'bottom').setStyle('margin-left',first.retrieve('margin-left'));
					this.lis = this.el.getElements('li');
					this.lis[0].setStyle('margin-left', (this.options.tail-this.lis[0].getSize().x ));
					this.el.setStyle('margin-left',0);
				}
			}.bind(this),
			duration:this.options.fx_duration,
			'wait':true				
								});

		this.set_timer();

	},
	go_left: function() {
		$clear(this.timer);
		this.dir = 'l'
		this.lis = this.el.getElements('li');
		this.el.tween('margin-left',-this.lis[1].getSize().x-this.lis[1].retrieve('margin-left'));
		this.set_timer();
		
	},
	go_right: function() {
		$clear(this.timer);
		this.dir = 'r';
		var last = this.lis.getLast();
		var first = this.lis[0];
		first.setStyle('margin-left',first.retrieve('margin-left'));
		this.el.setStyle('margin-left',-first.getSize().x);

		last.inject(this.el,'top').setStyle('margin-left',( this.options.tail-last.getSize().x ) );
		this.lis = this.el.getElements('li');
		this.el.tween('margin-left',0);
		this.set_timer();
	},
	set_timer: function() {
		if ( this.options.timed ) {
			this.timer =  this.go_left.periodical(this.options.timed, this);
		}						
	}
	

})