// Moo Tools Class written to crose fade anything that doesnt have any tranparency on it!

// Written By: Michael Worrall
// Date: 17/12/2007
// Amended: 14/01/2009 (updated to work with mootools 1.2
// Perameters: ClassName of divs you wish to fade!



// Note for this to work you must have a wrapper that is pos relative and each div you wish to fade must be pos absolute so they are stacked on top of one another!

var crossfade = new Class({
	initialize: function(classname) {
		var arr = $$('.' + classname); //creates array of each element we want to cross fade by returning a list of classes		
		
		for (x=0; x<arr.length; x++) { //sets z-index's on each wrapper - highest z-index on the 1st div in the flow!
			if(isNaN(arr[x].getStyle('z-index').toInt())==true) {
				arr[x].setStyle('z-index', 0);
			} 
			var z = arr[x].getStyle('z-index');			
			arr[x].setStyle('z-index',(z*1)+((arr.length-x)*1));
			
		}
		
		this.arr = arr;
	},
	
	start: function() {
		if (this.fadeDuration==null) {
			this.fadeDuration = 3000;
		}
		if (this.waitDuration==null) {
			this.waitDuration = 5000;
		}
		var waitfor = this.waitDuration;
		var fadefor = this.fadeDuration;
		var effect = new Array(); //creates an array to house an effect object for each object we want to croos fade
		var arr = this.arr; //retrieves array from initialize function!
		var currentSlide = 0;
		
		for (x=0; x<arr.length; x++) {
			//effect[x] = new Fx.Style(arr[x], 'opacity', {duration: fadefor, transition: Fx.Transitions.Quad.easeInOut, onComplete: continuefade}); //creates and effect object for each element we want to fade!
			effect[x] = new Fx.Morph(arr[x], {duration: fadefor, transition: Fx.Transitions.Quad.easeInOut, onComplete: continuefade});
		}
		
		var startfade = (function() { dofade() }).delay(waitfor); //starts the fading on execution of the .start method
		
		function continuefade() { //continues the fade onComplete of each Fx.Style
			(function() { dofade() }).delay(waitfor);
		};
		
		function dofade() {
			if (currentSlide == (arr.length-1)) {
				/*
				var restartSlide = new Fx.Style(arr[0], 'opacity', {
					duration: fadefor, 
					transition: Fx.Transitions.Quad.easeInOut
				});
				*/
				/*
				var endSlide = new Fx.Style(arr[currentSlide], 'opacity', {
					duration: fadefor, 
					transition: Fx.Transitions.Quad.easeInOut, 
					onComplete: function() {
						for (x=1; x<arr.length; x++) {
							//effect[x].set(1);
							effect[x].set({ 'opacity' : '1' });
						}
						currentSlide = 0;
						continuefade();	
					}
				});
				*/
				
				var restartSlide = new Fx.Morph(arr[0], {duration: fadefor, transition: Fx.Transitions.Quad.easeInOut});
				
				var endSlide = new Fx.Morph(arr[currentSlide], {
					duration: fadefor, 
					transition: Fx.Transitions.Quad.easeInOut, 
					onComplete: function() {
						for (x=1; x<arr.length; x++) {
							//effect[x].set(1);
							effect[x].set({ 'opacity' : '1' });
						}
						currentSlide = 0;
						continuefade();	
					}
				});
				
				restartSlide.start({ 'opacity' : '1' });
				endSlide.start(1,0);
			} else {
				//effect[currentSlide].start(1,0);
				effect[currentSlide].start({ 'opacity': 0 });
			}
			currentSlide++;
		};
	}
	
});

crossfade.implement({ //adds the optional properties to the class
	duration: function(fadeDuration){
        this.fadeDuration = fadeDuration;
    },
	
	wait: function(waitDuration){
        this.waitDuration = waitDuration;
    }
});