Diaporama = new Class({
	initialize : function(cont, ico, params){
		this_dia = this;
		this.content = cont;
		this.thumb	 = ico;
		this.auto 	 = params.auto || true;
		this.stop_handle = params.stop || null;
		this.play_handle = params.auto || null;
		this.indexOf = this.thumb.length - 1;
		this.timer 	 = null;
		if(params.stop){
			this.stop_handle = params.stop;
			this.stop_handle.addEvent('click',function(event){event.stop();if(this_dia.auto)this_dia.stop();});
		}
		if(params.play){
			this.play_handle = params.play;
			this.play_handle.addEvent('click',function(event){event.stop();if(!this_dia.auto)this_dia.play();});
		}
		this.slides  = this.load();
		this.addSelect();
	},
	addSelect : function(){
		this_add = this;
		this.thumb.each(function(el, i){
			el.addEvent('click', function(event){
				event.stop();
				this_add.select(i);
			});
		});
	},
	fetch : function(){
		this_fetch = this;
		big_src = new Array(this.thumb.length);
		this.thumb.each(function(el, i){
			if( el.get('tag') == 'img' )
				big_img = el;
			else
				big_img = el.getFirst('img');

			tab_src = big_img.getProperty('src').split("/");
			src = tab_src.pop();
			tab_src.pop();
			src = tab_src.join("/") + '/' + src;
			//this_fetch.content.appendText('**** '+src);
			big_src[i] = src;
		});
		return big_src;
	},
	load : function(){
		this_load = this;
		var progress = new Element( 'div' ,{'id': 'dia_progress', 'styles':{'width':'150px','margin':'20px auto 0 auto', 'float':'none','text-align': 'center'}} ).injectInside(this.content);
		var loading  = new Element( 'span',{'id':'dia_loading', 'html':'Loading'} ).setStyle('display', 'block').injectInside(progress);
		var percent  = new Element( 'span',{'id':'dia_percent', 'html':'0%' } ).setStyle('display', 'block').injectInside(progress);
		var src = this.fetch();
		var b = '';
		var b = new Asset.images(src,{
			onProgress : function(counter, index){
				var val = Math.floor((counter + 1) * (100 / src.length));
				percent.set('html', val + '%' );
			},
			onComplete : function(){this_load.select(this_load.indexOf + 1);}
		});
		return b;
	},
	show : function(){
		this_show = this;
		var ind = ( this.indexOf < 0 )? 0 : this.indexOf ;
		this.thumb[ind].removeClass("thumb_sel");
		this.indexOf ++;
		if( this.indexOf >= this.thumb.length )this.indexOf = 0;
		this.thumb[this.indexOf].addClass("thumb_sel");

		myFx = new Fx.Tween(this.content,{
			onComplete : function(){
				this_show.content.empty();
				this_show.slides[this_show.indexOf].injectInside(this_show.content);
				this_show.content.fade('in');
			}
		}).set( 'opacity', 1 );
		myFx.start('opacity', 0);
	},
	play : function(){
		this.stop();
		this.auto = true;
		this.show();
		this.timer = this.show.periodical(5000,this);
	},
	stop: function(){
		$clear(this.timer);
		this.auto = false;
	},
	select: function(i){
		//var ind = ( this.indexOf < 0 )? 0 : this.indexOf ;
		this.thumb[this.indexOf].removeClass("thumb_sel");
		this.indexOf = i-1;
		if(this.auto) this.play();
		else this.show();
	}

});