	
var CosMooFade = new Class({
	initialize: function(div_id, img_id)
	{
		this.element = $(div_id);
		this.img = img_id;
	},
	
	init: function(nomi, dir, time, random, speed, links, targetblank)
	{
		//prima fermo eventuali altri loop
		this.stop();
		
		this.arrFoto = nomi;
		this.dir = dir+"/";
		this.time = time;
		this.random = random;
		this.speed = speed;
		if(!this.speed)
			this.speed='long';
		this.links = links;
		this.targetblank = targetblank;
	},
	
	initDiv: function(divCnt, time, random, speed, targetblank)
	{
		//prima fermo eventuali altri loop
		this.stop();
		var nomi = new Array();
		$$("#"+divCnt+" img").each(function(el){
			nomi.push(el.get('src'));
		});
		
		var links = new Array();
		$$("#"+divCnt+" a").each(function(el){
			links.push(el.get('href'));
		});
		
		this.arrFoto = nomi;
		this.dir = '';
		this.time = time;
		this.random = random;
		this.speed = speed;
		if(!this.speed)
			this.speed='long';
		this.links = links;
		this.targetblank = targetblank;
	},
	
	FXstart: function(show)
	{
		if(!this.fxFade)
		{
			var fxFade = new Fx.Tween(this.element,{'property':'opacity', 'duration': this.speed});
			this.fxFade = fxFade;
		}
		return this.fxFade.start(show);
	},
	
	newImage: function() {
		var obj = this;
		var json = this.arrFoto;
		if (this.count >= json.length) this.count=0;
		
		var nomeold;
		if ($(obj.img)!=null)
		{
			nomeold = $(obj.img).get('nome');
			//$(obj.img).dispose();
		}
		if(!obj.random)
		{
			var nome = json[obj.count];
			obj.count++;
		}
		else
			var nome = obj.scegliRandom(json, nomeold);
		
		var img;
		if ($type(nome)=='string')
			img = new Element('img',{'src': obj.dir+nome, 'id': obj.img, 'nome':nome, 'alt':'', 'border':0});
		else img = nome;
		
		var immagine = img;
		if(obj.links)
		{
			if($type(nome)=='string')
			{
				var nomeimg = img.get('nome');
				var index = obj.arrFoto.indexOf(nomeimg);
			}
			else
				var index = obj.arrFoto.indexOf(img);
			
			if(obj.links[index])
			{				
				var href = obj.links[index];
				var a = new Element('a', {'href':href});
				if(obj.targetblank)
					a.set('target', '_blank');
				img.inject(a);
				immagine = a;
			}				
		}
			
		if ($type(nome)!='string') 
			obj.img = img;
		return immagine;
	},
	
	work: function()
	{
		var obj = this;
		
		this.FXstart(0).chain(function(){
			var oldImg = obj.img;
			var immagine = obj.newImage();
			if ($(oldImg)) $(oldImg).dispose();
			immagine.inject(obj.element);
			return this.callChain();
		}).chain(function(){obj.FXstart(1);});
		
	},

	
	start: function()
	{
		this.count = 0;
		var obj = this;
		var f = function(){obj.work();}
		var delay = 0;
		//se non ci sono foto nella cartella non parte nemmeno
		if(this.arrFoto.length==0)
		{
			return;
		}
		//se c'e una foto precaricata nel div ed e la stessa unica che c'e nella cartella delle foto non fa il fade
		else if($(this.img) && this.arrFoto.length==1 && $(this.img).get('src')==this.dir+this.arrFoto[0])
		{
			return;
		}
		
		// se non c'e un'immagine precaricata il fade parte subito altrimenti prima di partire aspetta un tempo this.time
		if(!$(this.img))
		{
			obj.work();
		}
		
		if (this.arrFoto.length>1)
		{
			this.preLoadImg();
			this.go = f.periodical(this.time);
		}
	},
	
	stop: function()
	{
		$clear(this.go);	
	},
	
	preLoadImg: function()
	{
		var obj = this;
		this.arrFoto.each(function(el, index){
				obj.arrFoto[index] = new Element('img',{'src': obj.dir+el, 'id': obj.img, 'nome':el, 'alt':'', 'border':0});
		});
	},
	
	scegliRandom: function(nomi, nome)
	{
		var nomenew;
		var loop = true;
		do
		{
			nomenew = nomi.getRandom();
			if ($type(nome)=='element') nome = nome.get('nome');
			
			if ($type(nomenew)=='string')
			{
				if (nomenew != nome) loop = false;
			}
			else
			{
				if (nomenew.get('nome') != nome) loop = false;
			}
		}while(loop);
		return nomenew;
	}
});
