/**
 * @author guille
 */
var SlideShow=new Class({
	options:{
		fade_delay:8000,
		effect_time:2000,
		fotos:[],
		contenedor:null
	},
	initialize: function(options){
		this.setOptions(options);
		this.fotos=[];
		this.divs=[];
		this.effects=[];
		this.contenedor=this.options.contenedor;
		this.fade_delay=this.options.fade_delay;
		this.effect_time=this.options.effect_time;
		this.addFotos(this.options.fotos);
		this.index_array=this.generate_index_array();
		this.indice_actual=0;
		if(this.fotos.length){
			this.aparecer_primera();
			this.empezar_slideshow();
		}
	},
	preload_all:function(){
		var img=new Image();
	
	},
	generate_random:function(){
		var rand_no = Math.random();
		rand_no = rand_no * 10;
		rand_no = Math.ceil(rand_no);
		rand_no = rand_no%this.fotos.length;
		return rand_no;
	},
	generate_index_array:function(){
		var indices=new Array();
		var primera=this.generate_random();
		for(var i=primera; i<this.fotos.length;i++){
			indices[indices.length]=i;
		}
		for(var i=0;i<primera;i++){
			indices[indices.length]=i;
		}
		return indices;
	},
	addFotos:function(fotos){		
		$$(fotos).each(function(foto,index){
			this.fotos.include($(foto));
			$(foto).setStyle('visibility','hidden');
			$(foto).setStyle('opacity',0);
			//this.effects[this.fotos.indexOf(foto)]=new Fx.Style(foto,'opacity',{duration:this.effect_time});
		},this);
	},
	addFoto: function(foto){
		this.addFotos(foto);
	},
	siguiente:function(){
		this.fade_out_picture(this.index_array[this.indice_actual]);
		this.aumentar_indice();
		this.fade_in_picture(this.index_array[this.indice_actual]);
		this.siguiente.delay(this.fade_delay,this);
	},
	aumentar_indice:function(){
		if(this.indice_actual < this.fotos.length-1) this.indice_actual++;
		else this.indice_actual=0;
	},
	fade_in_picture:function(index){		
		this.fotos[index].effects({duration:this.effect_time}).start({opacity:1});
	},
	fade_out_picture:function(index){
		this.fotos[index].effects({duration:this.effect_time}).start({opacity:0});
	},
	aparecer_primera:function(){
		this.fotos[this.index_array[0]].setStyle('visibilty','visible');
		this.fade_in_picture(this.index_array[0]);
	},
	empezar_slideshow:function(){
		this.siguiente.delay(this.fade_delay,this);
	}
});

SlideShow.implement(new Options);
