/*
Script: MooCrossFade.js
	MooTools CrossFader

License:
	MIT-style license.

Copyright:
	copyright (c) 2007 Michael Swan, <http://www.digimute.com>

Credits:
	- Based on orginal Crossafder by Timothy Groves - http://www.brandspankingnew.net

*/

/*
Class: Crossfader
	Creates a new Crossfader Class 

Properties:
	delay - Delay between Crossfade
	fadetime - Duration of Crossfade
	
*/

function randOrd(){
return (Math.round(Math.random())-0.5); }  


 var Crossfader = new Class ({
 	initialize: function(options){
		this.options = Object.extend({
			delay:1000,
			fadetime: '1000',
			rande: false
		}, options || {});
	},
	
	/*
	Property: start
		Starts the crossfade

	Arguments:
		divs - Array of DIVS to crossfade
		delay - Delay between Crossfade
		fadetime - Duration of Crossfade
	*/	
	start: function (divs, fadetime, delay ){	
		this.nAct = -1;
		this.arrayDivs = divs;
		if (this.options.rande){
			this.arrayDivs.sort( randOrd );
		}
		this.transition="elasticInOut";
		for (var i=0;i<divs.length;i++)
		{
			$(divs[i]).setStyle('opacity', 0);
			$(divs[i]).setStyle('position',"absolute");
			$(divs[i]).setStyle('opacity', 0);
			$(divs[i]).setStyle('visibility',"hidden");
		}
		this.nDur = fadetime;
		this.pDur = fadetime;
		this.nDelay = delay;
		this.pDelay = delay;
		
		this.newfade();
	},
	
/*
	Property: pausefade
		Pauses the current Crossfade

	Arguments:
		none - none		
*/	
	
	pausefade: function(){
		// pause the corssfade
		if (this.nID1)
			clearInterval(this.nID1);
	},

/*
	Property: restartFade
		REstart the current Paused Crossfade

	Arguments:
		none - none		
*/	

	restartFade: function(){
		// unpause the crossfade
		this.newfade();
	},
	newfade:  function(){
		if (this.nID1)
			clearInterval(this.nID1);
		
		this.divOldActive = this.divActive;
		this.divActive++;
		
		if (!this.arrayDivs[this.divActive])	this.divActive = 0;
		// check to see if 'delay' is specified on the DIV, otherwise use the default
		
		var fadeOptions = Json.evaluate(this.arrayDivs[this.divActive].getProperty('alt')) || {};   
		if (fadeOptions.delay!=null){
			this.nDelay = fadeOptions.delay;	
		} else {
			this.nDelay = this.pDelay;
		}
		if (fadeOptions.fadetime!=null){
			this.nDur = fadeOptions.fadetime;	
		} else {
			this.nDur = this.pDur;
		}
		if (this.divActive == this.divOldActive)
			return false;
		$(this.arrayDivs[this.divActive]).setStyle('visibility', "visible");
		this.nInt = 50;
		this.nTime = 0;
		var p=this;
		this.nID2 = setInterval(function() { p.fade() }, this.nInt);
	},
	linearTween : function (t, b, c, d) {
		return c*t/d + b;
	},
	fade : function(){
		this.nTime += this.nInt;
		var op = Math.round( this.linearTween(this.nTime, 0, 1, this.nDur) * 100 ) / 100;
		$(this.arrayDivs[this.divActive]).setStyle('opacity', op);
		if (this.divOldActive > -1)
		{
			$( this.arrayDivs[this.divOldActive] ).setStyle('opacity',1-op);
		}
		if (this.nTime == this.nDur)
		{
			clearInterval( this.nID2 );
			if (this.divOldActive > -1) {
				$(this.arrayDivs[this.divOldActive]).setStyle('visibility',"hidden");	
				}
			var p=this;
			this.nID1 = setInterval(function() { p.newfade() }, this.nDelay);
		}
}}
)


