var GalleryTop = new Class({
	Implements:[Options,Chain],
	
	options:{
		elenco:Array(),
		elencotesti:Array(),
		contenitore: 'gallerytop',
		selettoreElementi: '.imagineGallery',
		fixedDim:false,
		fixedWidth:'560px',
		fixedHeight:'420px',
		textWidth:'560px',
		textHeight:'20px',
		urlImgs:'',
		totaleElenco:0,
		conta:0,
		tempoAttesa:7000,
		settaCampoTesto:false,
		campoTesto:'gallery_gabriel_img_descrition',
		inizio_random:true,
		inprocesso:null
	},
	
	initialize: function(opt){
		this.setOptions(opt);
		this.options.contenitore = $(this.options.contenitore);

		this.inizia();
		
		this.options.campoTesto=$(this.options.campoTesto);
		this.options.totaleElenco = this.options.elenco.length - 1;
		
		if(this.options.inizio_random) this.options.conta = Math.floor(Math.random()*(this.options.elenco.length)); else this.options.conta=0;
		this.animaImmagine();
		
	},
	
	inizia: function(){
		var prop = this;
		$$(this.options.selettoreElementi).each(function(item){
			prop.options.elenco.include(item.src);
			prop.options.elencotesti.include(item.alt);
		});
		this.options.contenitore.empty();
	},
	
	animaImmagine: function(){
		var prop = this;
		this.chain(
			    function(){
			    	prop2=this;
			    	//if(prop.options.inprocesso!=null) prop.options.inprocesso.fade('out');
			    	if(prop.options.inprocesso!=null){
			    		prop.options.inprocesso.tween('opacity', 1, 0).get('tween').chain(function(){
			    			prop2.callChain();
			    		}) 
			    	}else{
			    		this.callChain();
			    	}
			    	//this.callChain();
			    },
			    function(){
			    	if(prop.options.inprocesso!=null) prop.options.inprocesso.destroy();
			    	this.callChain();
			    },
			    function(){
			    	var prop2 = this;
			    	prop.options.inprocesso=new Asset.image(
			    			prop.options.elenco[prop.options.conta], 
			    			{
			    				id: 'myImage', 
			    				alt: prop.options.elencotesti[prop.options.conta], 
			    				onload: function(){
			    					this.inject(prop.options.contenitore).fade('hide');
			    					if(prop.options.conta < prop.options.totaleElenco){
			    						prop.options.conta++;
			    					}else{
			    						prop.options.conta=0;
			    					}
			    					prop2.callChain();
			    				}
			    			}
			    		);
			    },
			    function(){
			    	prop.options.inprocesso.fade('in');
			    	this.callChain();
			    },
			    function(){
			    	prop.animaImmagine();
			    }
		).wait(prop.options.tempoAttesa);
		
		this.callChain();

	}
	
});

window.addEvent('domready',function(){ggallery = new GalleryTop();});
