var $SlideShow = {
	currentSlide: 0,
	Slides: Array(),
	slideTimer: null,
	slideTimeout: 5000,
	Init: function() {
		// Put all the background image elements into an array:
		this.Slides = $$('.ad_link');
		// Randomize the order:
		this.Slides.sort(function() {
			return (Math.random()) > 0.5 ? 1 : -1;
		});
		for (var i=0;i < this.Slides.length;i++) {
			var currIndex = 10000-i;
			this.Slides[i].setStyle({
				zIndex: currIndex
			});
		}
		// Show the first one:
		var myIndex = parseInt(this.Slides[0].getStyle("z-index"));
		var newIndex = myIndex+3000;
		this.Slides[0].setStyle({
			zIndex: newIndex
		});
		Effect.Appear(this.Slides[0],{duration: 2, afterFinish: function() {
			if ($SlideShow.Slides.length > 1) {
				setTimeout("$SlideShow.Next();",($SlideShow.slideTimeout));
			}
		}});
	},
	Next: function() {
		var lastSlide = this.currentSlide;
		// Move the last slide back down the z-order:
		var myIndex = parseInt(this.Slides[lastSlide].getStyle("z-index"));
		var newIndex = myIndex-3000;
		this.Slides[lastSlide].setStyle({
			zIndex: newIndex
		});
		this.currentSlide += 1;
		if (this.currentSlide == this.Slides.length) {
			this.currentSlide = 0;
		}
		// Now move the next slide to the top of the order and fade it in:
		var myIndex = parseInt(this.Slides[this.currentSlide].getStyle("z-index"));
		var newIndex = myIndex+3000;
		this.Slides[this.currentSlide].setStyle({
			zIndex: newIndex
		});
		Effect.Appear(this.Slides[this.currentSlide],{duration: 2, afterFinish: function() {
			// Hide the last slide:
			var lastSlide = $SlideShow.currentSlide-1;
			if (lastSlide < 0) {
				lastSlide = $SlideShow.Slides.length-1;
			}
			$SlideShow.Slides[lastSlide].hide();
			setTimeout("$SlideShow.Next();",$SlideShow.slideTimeout);
		}});
	}
}
