/****************************************
|
| A-Line / Muru
| http://www.a-linetool.com
|
| Created by: Kevin Biskaborn
| Copyright 2010 ScriptReaction
| http://www.scriptreaction.com
|
*****************************************
| Load Dependencies	: None
| Run Dependencies	: Global{}
****************************************/

var RFX_FadeCycler = {
	elementsCreated: Array(),
	launch: function ( inputObject ){
		
		// get the incoming container div id
		var containerDivId = inputObject[ "containerDivId" ];
		
		// check for a valid container
		if( containerDivId != null && $( containerDivId ) != null ){
			
			// assign the index to the cycler instance
			inputObject.cyclerId = this.elementsCreated.length;
			
			// save and create the new instance
			this.elementsCreated.unshift( 
				new RFX_FadeCycler_instance( inputObject )
			);
		}
		
		return this.elementsCreated[ 0 ];
	},
	instanceCallBack: {
		fadeNext: function ( cyclerId ){
			
			// call the fade next function on the requested cycler
			RFX_FadeCycler.elementsCreated[ cyclerId ].fadeNext();
		},
		cycleNext: function ( cyclerId ){
			
			// call the cycle next function on the requested cycler
			RFX_FadeCycler.elementsCreated[ cyclerId ].cycleNext();
		}
	}
}

// the instance of the Fade Cycler class
var RFX_FadeCycler_instance = function ( inputObject ){
	
	// extract incoming properties
	this.cyclerId 		= inputObject[ "cyclerId" ];
	this.containerDivId = inputObject[ "containerDivId" ];
	
	// create element variables
	this.containerElement	= $( this.containerDivId );		// the container element itself
	this.elementTagName 	= "a";							// the elements within the container
	this.arrayOfElements 	= new Array();					// the collection of cycle elements
	this.cyclingOrder		= new Array();					// an array of element indexes in the order to be displayed
	this.useRandomCycling	= true;							// flag whether to use a random order
	this.activeCycleIndex	= -1;							// the active index of the cycle
	this.elementTotal		= -1;							// the total number of cycling elements
	this.delayAfterFadeOut	= 500;							// the delay (in ms) after an element fades out
	this.delayAfterFadeIn	= 3000;							// the delay (in ms) after an element fades in
	
	this.launch = function (){
		this.setup();
		this.cycleNext();
	}
	
	this.setup = function (){
		
		// get all elements
		this.arrayOfElements = this.containerElement.getElementsByTagName( this.elementTagName );
		
		// save the total number of elements
		this.elementTotal = this.arrayOfElements.length;
		
		// create element containers
		var element, elementFader;
		
		// cycle through all elements
		for( var i = 0; i < this.elementTotal; i++ ){
			
			// get the next element
			element = this.arrayOfElements[ i ];
			
			// set the element display to none
			JS_Format.divElement.setDisplay_none( element );
		}
		
		// populate the cycling order array
		for( var i = 0; i < this.elementTotal; i++ )
			this.cyclingOrder.push( i );
			
		// check for random cycling
		if( this.useRandomCycling )
			this.cyclingOrder = JS_Format.array.shuffle( this.cyclingOrder );
	}
	
	this.cycleNext = function (){
		
		// if a previous element index exists, fade that element out
		if( this.activeCycleIndex >= 0 ){
			
			// get the active element
			var activeElement = this.arrayOfElements[ this.cyclingOrder[ this.activeCycleIndex ] ];
			
			// create an opacity fade object
			var elementFader = ReactorFX.alpha.createOnElement( activeElement );
			
			// fade the element out
			ReactorFX.alpha.fadeOut( elementFader );
			
			// set the delay to the next action
			setTimeout( "RFX_FadeCycler.instanceCallBack.fadeNext(" + this.cyclerId + ")", this.delayAfterFadeOut );
		}
		else
			this.fadeNext();
			
	}
	
	this.fadeNext = function (){
		
		// if a previous element index exists,
		// set that element display to none
		if( this.activeCycleIndex >= 0 ){
			
			// get the previous element
			var prevElement = this.arrayOfElements[ this.cyclingOrder[ this.activeCycleIndex ] ];
			
			// set the element display to none
			JS_Format.divElement.setDisplay_none( prevElement );
		}
		
		// get the next index in the cycle
		this.activeCycleIndex++;
		
		// check if index is out of bounds
		// and loop back around if it is
		if( this.activeCycleIndex >= this.elementTotal )
			this.activeCycleIndex = 0;	
			
		// get the active element
		var activeElement = this.arrayOfElements[ this.cyclingOrder[ this.activeCycleIndex ] ];
			
		// create an opacity fade object
		var elementFader = ReactorFX.alpha.createOnElement( activeElement );
		
		// set the element alpha to zero
		ReactorFX.alpha.clear( elementFader );
		
		// set the element display to block
		JS_Format.divElement.setDisplay_block( activeElement );
		
		// fade the element in
		ReactorFX.alpha.fadeIn( elementFader );
		
		// set the delay to the next action
		setTimeout( "RFX_FadeCycler.instanceCallBack.cycleNext(" + this.cyclerId + ")", this.delayAfterFadeIn );
	}
	
	// initilize the object
	// call the launching function
	this.launch();
}





