var Bandeau = new Class({
	Implements: [ Options, Chain, Events ],

	defaultOptions: function(){
		return {
			direction: 'top',
			transition: 1000,
			pause: 5000
		};
	},

	initialize: function(el, options){
		this.bandeau = $(el);
		
		if ($defined(this.bandeau))
		{
			this.setOptions(this.defaultOptions(), options);
			this.items = this.bandeau.getElements('li');

			this.setup();
			
			this.myTimerCover = this.play.periodical(this.options.pause + this.options.transition, this);
		}
	},

	setup: function() {
		var w = 0;
		var h = 0;
		var t = 0;
		var l = 0;	

		if ( (this.options.direction.toLowerCase()=='left') || (this.options.direction.toLowerCase()=='right') ) {
			h = this.bandeau.getSize().y;
			this.items.each(function(li,index) {
				w += li.getSize().x;
			});
		} else if ( (this.options.direction.toLowerCase()=='bottom') || (this.options.direction.toLowerCase()=='top') ) {
			w = this.bandeau.getSize().x;
			this.items.each(function(li,index) {
				h += li.getSize().y;
			});
		}

		if (this.options.direction.toLowerCase() == 'bottom')	{
			t = -this.items[this.items.length-1].getSize().y;
		}
		if (this.options.direction.toLowerCase() == 'right')	{
			l = -this.items[this.items.length-1].getSize().x;
		}
		
		this.bandeau.setStyles({
			position: 'absolute',
			top: t,
			left: l,
			width: w,
			height: h
		});

		this.fx = new Fx.Morph(this.bandeau,{duration:this.options.transition,onComplete:function() {
			if (this.options.direction.toLowerCase() == 'bottom')	{
				var i = this.current;
				this.items[i].injectBefore(this.bandeau.firstChild);
				this.bandeau.setStyles({
					top: -this.items[i].getSize().y
				});
			} else if (this.options.direction.toLowerCase() == 'top') {
				var i = (this.current==0)?this.items.length:this.current;
				this.items[i-1].injectInside(this.bandeau);
				this.bandeau.setStyles({
					top: 0
				});
			}			
		}.bind(this)});

		this.current = 0;
		this.play();
	},
	
	play: function() {
		if (this.options.direction.toLowerCase() == 'bottom') {
			this.current--;
			if (this.current < 0) this.current = this.items.length - 1;
			this.fx.start({
				top: 0
			});
		} else if (this.options.direction.toLowerCase() == 'top') {
			this.current++;
			if (this.current >= this.items.length) this.current = 0;
			var pos = this.items[this.current];
			this.fx.start({
				top: - pos.offsetTop
			});
		}
	}
});
