var CosMooFadeExt = new Class({
	Extends: CosMooFade,

	initDiv: function(divCnt, divImm, divTitolo, divPag, time, random, speed)
	{
		//prima fermo eventuali altri loop
		this.stop();
		var immagini = new Array();
		$$("#"+divCnt+" ."+divImm).each(function(el){
			immagini.push(el.clone());
		});
		var titoli = new Array();
		$$("#"+divCnt+" ."+divTitolo).each(function(el){
			titoli.push(el.clone());
		});
		this.arrFoto = immagini;
		this.arrTitoli = titoli;
		this.divPag = divPag;
		this.divTitolo = divTitolo;
		this.dir = '';
		this.time = time;
		this.random = random;
		this.currentDiv = null;
		this.speed = speed;
		if(!this.speed)
			this.speed='long';
		this.count = 0;
		this.titolo = null;
		this.pag = null;
	},

	
	slide: function(dir, verso, pag)
	{
		if(!verso)	verso = "avanti";
		//se non ci sono foto nella cartella non parte nemmeno
		if(this.arrFoto.length<=1)
		{
			return;
		}
		
		var obj = this;
		var json = this.arrFoto;
		var jsonTitoli = this.arrTitoli;
		
		if(pag>0) {
			this.count=pag-1;
			this.stop();
		}
		else if(verso=="avanti")	this.count++;
		else if(verso=="indietro")	this.count--;
		if (this.count >= json.length) this.count=0;
		if (this.count<0) this.count=json.length-1;
		if (this.currentDiv)
		{
			this.currentDiv.setStyle('z-index',60);
		}
		else this.currentDiv = this.element;
		var cnt = this.currentDiv.getParent();
		this.currentTitolo = cnt.getFirst('.'+this.divTitolo);
		
		pagina = this.count+1;
		if(this.divPag)
			this.pag = $$('.'+this.divPag+'[rel="'+pagina+'"]')[0];
		
		var div = json[this.count].clone();
		var titoloNew = jsonTitoli[this.count].clone();
		titoloNew.setStyle("opacity",0);
				
		div.setStyle('z-index', 50);
		div.setStyle('position', 'absolute');
		div.setStyle('left', 0);
		cnt.grab(div);
		cnt.grab(titoloNew);
		var morphImg = new Fx.Morph(this.currentDiv, {duration: 'long'});
		if(dir=="left")
		{
			morphImg.start({'left':0-this.currentDiv.getSize().x, 'opacity':0});
			this.currentTitolo.fade(0);
			titoloNew.fade(1);
		}
		else if(dir=="right")
		{
			morphImg.start({'left':0+this.currentDiv.getSize().x, 'opacity':0});
		}
		if(this.divPag) {
			$$(".pagina").each(function(el){
				if(el.get("rel")!=pag)
					el.removeClass("sel");
			});
			this.pag.addClass("sel");
		}
		
		/*if(pag>0)
			this.start();*/
		
		(function(){
			if (obj.currentDiv) obj.currentDiv.destroy();
			obj.currentDiv = div;
			if(obj.currentTitolo) obj.currentTitolo.destroy();
		}).delay(1000);
		
	},
	
	start: function()
	{
		var obj = this;
		
		var f = function(){obj.slide("left", "avanti");};
		this.go = f.periodical(this.time);
	}
});
