var BannerAleatorio = Class.create();
BannerAleatorio.prototype = {
	initialize: function(idContenedor, claseBanners, tiempo) {
		this.idContenedor = idContenedor;
		this.claseBanners = claseBanners;
		this.tiempo = tiempo;
		this.actual = -1;
		this.timeout = 0;
		this.sobre = false;
		this.banners = $$('#'+this.idContenedor+' .'+this.claseBanners);
		if ( this.banners.length > 0 ) {
			this.contenedor = $(idContenedor);
			Event.observe(this.contenedor,'mouseover',function(e){ this.sobre = true }.bind(this));
			Event.observe(this.contenedor,'mouseout',function(e){ this.sobre = false }.bind(this));
			this.eventoPeriodico();
		}
	},
	dameAleatorio: function() {
		var n = -1;
		var c = 0;
		if ( this.banners.length > 1 ) {
			do {
				c++;
				n = Math.floor(Math.random()*this.banners.length)
			} while ( n == this.actual && c<1000 );
			if ( c>=1000 ) { return 0 };
		}
		else {
			return 0;
		}
		return n;
	},
	cambiaBanner: function() {
		var n = this.dameAleatorio();
		if ( n != this.actual ) {
			this.ocultaBanner(this.actual);
			this.actual = n;
			this.muestraBanner(this.actual);
		}
	},
	muestraBanner: function(n) {
		if ( n != -1 ) {
			this.banners[n].appear({duration:1.0});
		}
	},
	ocultaBanner: function(n) {
		if ( n != -1 ) {
			this.banners[n].fade({duration:1.0});
		}
	},
	eventoPeriodico: function() {
		if ( !this.sobre ) {
			this.cambiaBanner();
		}
		this.timeout = this.eventoPeriodico.bind(this).delay(this.tiempo);
	}
};

