// imageFadeSet - Handy image rotator which does cross fades on a set of images
// -----------
// By: David Rugendyke
// Ver: 0.1


window.addEvent('domready', function() {

	// Initialise the image fade set
	var fade = new imageFadeSet();


});


// Image Fade Rotator Class
var imageFadeSet = new Class({
	
	// Class vars
	options: {
		fadeSpeed: 0,
		rotateSpeed: 0,
		imageSet: null,
		imageCount: 0,
		imageIndex: 0
	},
	
	// Initialise the class
    initialize: function(){
		
		// Defaults
		this.fadeSpeed = 4000;
		this.rotateSpeed = 4000;
		this.imageCount = 0;

		// Now get all the images to fade and apply the correct styles to them so that they overlap each other
		this.imageStyles();
		// If we have some to fade, lets start the cycle
		if(this.imageCount > 0) {
			// Now start crossfading them
			this.imageFadeCycle.delay(this.rotateSpeed, this);
		}
    },
	
	// Adds styles to all the images in the set
	imageStyles: function(){

		var Ob = this;
		// Get all the images in the container div
		this.imageSet = $$('#image_fade_set div'); 
		
		// Attach events to each main menu
		this.imageSet.each(function(elMenu) {
				
			// Now hide each one except the first and apply the styles to them so that they all appear in the same position
			$(elMenu).setStyles({
				position: 'absolute',
				top: 0,
				left: 0,
				display: 'block'
			});
			
			if(Ob.imageCount != 0) {
				$(elMenu).setStyle('opacity', 0); 	
			}
				
			Ob.imageCount++;	
		}); // End Menu Loop
	
	},
	
	imageFadeCycle: function() {
		
		var Ob = this;
		// Get the current and next image index
		var nextIndex;
		var currentIndex;
		// Set the current image array index or set the default to the start if it doesnt exist yet
		if(this.imageIndex) { currentIndex = this.imageIndex; }
					   else { currentIndex = 0; }
		
		var nextImageIndex = currentIndex + 1;
		// Now get the next index
		if(this.imageSet[nextImageIndex]) {
			nextIndex = nextImageIndex;
		}else{
			nextIndex = 0;	
		}

		var myFxOut = new Fx.Tween(this.imageSet[currentIndex],	{  transition: Fx.Transitions.Quad.easeIn,duration: Ob.fadeSpeed }).start('opacity', 0);
	
		var myFxIn = new Fx.Tween(this.imageSet[nextIndex], 
									{ duration: Ob.fadeSpeed, 
									  transition: Fx.Transitions.Quad.easeIn,
									  onComplete: function(){ 
										  
										  Ob.imageIndex = nextIndex;
										  Ob.imageFadeCycle.delay(Ob.rotateSpeed, Ob);
										  
										  } 
									 }).start('opacity', 1);
		
	}
	
});
	