var CosMooFadeExt = new Class({
	Extends: CosMooFade,

	initDiv: function(els, time, random, speed)
	{
		//prima fermo eventuali altri loop
		this.stop();
		var nomi = new Array();
		var preloads = new Array();
		els.each(function(el){
			nomi.push(el);
			preloads.push(el.get('rel'));
		});
		this.arrFotoBig = new Asset.images(preloads);
		
		this.fade = new Fx.Tween(this.element, {'property':'opacity'});
		var ul = nomi[0].getParent().getParent().getParent();
		this.scroll = new Fx.Scroll(ul);
		
		this.arrFoto = nomi;
		this.dir = '';
		this.time = time;
		this.random = random;
		this.currentDiv = null;
		this.speed = speed;
		if(!this.speed)
			this.speed='long';
	},

		
	work: function()
	{
		var obj = this;
		var json = this.arrFoto;
		if (this.count >= json.length) this.count=0;
		
		var div = json[this.count];
		
		this.fade.start(0).chain(function(){
			obj.element.empty();
			var newFoto = obj.arrFotoBig[obj.count];
			if (!newFoto) newFoto = new Element('img', {'src':div.get('rel')});
			obj.element.grab(newFoto);
			var titolo = new Element('div', {'class':'titolo'});
			titolo.addEvent('click', function(){
				window.location.href = div.getParent().href;
			});
			var titoloClone = titolo.clone();
			titolo.set('html',div.get('alt'));
			titoloClone.setStyle('opacity',0.5);
			titoloClone.addClass('fondo');
			obj.element.grab(titoloClone);
			obj.element.grab(titolo);
			obj.scroll.toElement(div.getParent().getParent());
			obj.count++;
			return this.callChain();
		}).chain(function(){obj.fade.start(1)});
	},

	
	start: function()
	{
		this.count = 0;
		var obj = this;
		var f = function(){obj.work();}

		//se non ci sono foto nella cartella non parte nemmeno
		if(this.arrFoto.length==0)
		{
			return;
		}
		
		this.work();
		this.go = f.periodical(this.time);
		
	}
});
