//global this hold the string of the variable name so that it can be called dynamically
function MooRotate(class_name_of_targets, global_this)
{
	//the line right below will NOT work without Prototype
	this.targets = document.getElementsByClassName(class_name_of_targets);
	this.numTarget = this.targets.length;
	this.currentKey = 1;
	this.prevKey = 0;
	this.delay = 5000;
	this.myGlobalVarName = global_this; //keep this variable name (for setTimeout calls)
	this.fxArray = new Array();
    this.duration = 500;
	
	//build all the effects
	this.buildEffects = function()
	{
		for(i=0; i<this.numTarget ; i++)
		{
			this.fxArray[i] = new fx.Opacity(this.targets[i],{duration:this.duration});
			//except for the first one, turn everything else off
			if(i) {this.fxArray[i].toggle();}
		}
	}

	//method that actually does the rotate
	this.rotate = function()
	{
        //will not rotate if there is only one element
        if (this.numTarget == 1) return false;

		//does the rotate
		this.fxArray[eval(this.prevKey)].toggle();
		this.fxArray[eval(this.currentKey)].toggle();
		//increase keys
		this.prevKey = this.currentKey;
		this.currentKey = (this.currentKey + 1) % this.numTarget ;
		//call rotation again in the set delay
		cmd = this.myGlobalVarName + ".rotate()";
		setTimeout(cmd,this.delay);
	}
	
	//start rotation (allow settings of delay and duration)
	this.start  = function(delay, duration)
	{
		//fall back to default duration as necessary
		this.duration = (duration>0)?duration:500;
		
        //(re)build effects
		this.buildEffects();
				
		//fall back to default delay as necessary
		this.delay = (delay>0)?delay:this.delay;
		
		//since "this" does not retain its scope when setTimeout triggers in the future, we need to use this variable name (held in this.myGlobalVarName)
		cmd = this.myGlobalVarName + ".rotate()";
		setTimeout(cmd, this.delay);
	}
}

