var CosMooSlide = new Class({
	Implements: [Events, Options],
	options: {
		id: '',
		time: 10000,
		random: false,
		bgClass: '',
		fade: 1
	},
	
	initialize: function(cnt_id, el_sel, options)
	{
		this.setOptions(options);
		
		this.content = $(cnt_id);
		this.elements = $$(el_sel);
		this.time = this.options.time;
		this.random = this.options.random;
		this.bgClass = this.options.bgClass;
		this.fade = this.options.fade;
		
		this.height = parseInt(this.content.getStyle('height'));
		this.topStart = parseInt(this.content.getStyle('top'));
		this.topEnd = this.topStart+this.height;
	},
	
	
	work: function()
	{
		var obj = this;
		var element;
		if (this.random)
			element = obj.scegliRandom(this.elements, '');
		else element = this.elements[this.count];
		
		var clone = element.clone();
		clone.setStyle('position','absolute');
		clone.setStyle('top',5);
		clone.setStyle('right',923);
		/*var bgdiv = new Element('div', {
			'styles':
			{
				'width': this.content.getStyle('width'),
				'height': this.content.getStyle('height'),
				'position':'absolute',
				'top':0
			}
		});
		bgdiv.addClass(this.bgClass);
		bgdiv.fade(this.fade);*/
		
		//var fxTransf = new Fx.Tween(this.content,{'property':'height'});
		var fxTransf = new Fx.Morph(clone,{'duration': 'short'});
		//var fxTransf = new Fx.Slide(this.content);
		
		
		fxTransf.start({
			'right':'923px'
		}).chain(function(){
			
			obj.content.empty();
			/*obj.content.grab(bgdiv);*/
			obj.content.grab(clone);
			return this.callChain();
		}).chain(function(){fxTransf.start({
			'right':'5px'
		});});
		
		this.count++;
		if (this.elements.length == this.count) this.count=0;
		
	},

	
	start: function()
	{
		this.count = 0;
		var obj = this;
		var f = function(){obj.work();}
		this.work();
		if (this.elements.length>1) this.go = f.periodical(this.time);
	},
	
	stop: function()
	{
		$clear(this.go);	
	},
	
	scegliRandom: function(nomi, nome)
	{
		var nomenew;
		do{nomenew = nomi.getRandom();}
		while(nomenew==nome);
		return nomenew;
	}
});