var Ad = Class.create({
	initialize: function(adid) {
		this.imgs = $(adid).select('img');
		this.length = this.imgs.length;
		this.imgs.each(function (i, index) {if (index < this.length - 1) i.setOpacity(0); }, this);
		if (this.length > 1) {
			this.current = this.length - 1;
			func = this.startCrossFade.bind(this);
			setTimeout(func, Math.random()*5000);
		}
	},
	startCrossFade: function() {
		func = this.crossFade.bind(this);
		func();
		setInterval(func, 10000);
	},
	crossFade: function() {
		this.old = this.current;
		this.current++;
		this.current%=this.length;
		new Effect.Opacity(this.imgs[this.old], { 
				from: 1.0, to: 0.0, 
				duration: 2,
				afterFinish: function() {
					this.imgs[this.old].style.zIndex = 0;
				}.bind(this)
			});
		new Effect.Opacity(this.imgs[this.current], { 
				from: 0.0, to: 1.0, 
				duration: 2,
				afterFinish: function() {
					this.imgs[this.current].style.zIndex = 1;
				}.bind(this)
			});
	}
});

