function mran(ma,mi)
{return(Math.round(Math.random()*(ma-mi))+mi)}

var Slideshow = Class.create({
	 initialize: function(delay, totalElmt, elmtName, pulse) {
		this.delay = delay;
		this.totalElmt = totalElmt;
		this.paused = 0;
		this.arrSlideElmt = [totalElmt];
		this.curElmtNum = mran(totalElmt, 1); //randomize first element
		this.pulse = pulse;

		//preload all elements into array and preload all images within element
		for (i = 1; i <= totalElmt; i++) {
			this.arrSlideElmt[i] = $(elmtName + i);
			this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this));  //pause on mouseover
			this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this));  //resume on mouseout
			$$(this.arrSlideElmt[i].img).each(function(img) {	
				img = new Image();
			});
		}
		$$('a.next').each(function(node) {
			node.observe('click', function(s){
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'none'
				});
				this.checkSlide();
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'block'
				});
				s.stop();
			}.bind(this));
		}.bind(this));			
	 },
	start: function() {
		//show first element without effect
		this.arrSlideElmt[this.curElmtNum].setStyle({
		  display: 'block'
		});
		if ( this.pulse ) {
			this.executor = new PeriodicalExecuter(function() { 
				this.next(); //start slidehow
			}.bind(this), this.delay);	
		}	
	},
	
	next: function(){
		if (!this.paused) {
			this.update();
		}
	},
	update: function() {
		new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
			this.checkSlide();
			new Effect.Appear(this.arrSlideElmt[this.curElmtNum]);
		}.bind(this)});
	},
	checkSlide: function() {
		if (this.curElmtNum == this.totalElmt) { this.curElmtNum = 1; }
		else { this.curElmtNum ++; }		
	}
});

window.onload = function() {
	var pulse = true;
	totalFeatures = $('rotateFeature').childElements().size();

	if (totalFeatures == 1) {
		$$('#rotateFeature .next').each(function (nextButton) {
			nextButton.style.display = "none";  //remove next button for only one feature
		});
		if (!($('rotateFeature').down(0).hasClassName('emergency'))) {
			pulse = false;  //only pulse if feature has class emergency
		}			
	}
	
	var slideshow = new Slideshow(6, totalFeatures, "rotate", pulse);
	slideshow.start();
}