/**
 * Check for mootools!
 */
if(typeof(Class) !== "undefined")
{

	/**
	 * Image Carousel
	 * 
	 * @version 1.0
	 * @author Paul Lewis
	 */
	var ImageCarousel = new Class({
	
		_aNextImageLnk: null,
		_aPrevImageLnk: null,
		_iImageCounter: null,
		_arrImages:		null,
		_imgCurrent: 	null,
		_bFading: 		null,
		_fxFade: 		null,
		
		/**
		 * Constructor - finds the
		 * images for our carousel
		 */
		initialize: function()
		{
			// initialize - not fading
			this._bFading 			= false;
			
			// locate our images
			this._arrImages			= $$('.carousel-image', '.hidden-carousel-image');
			
			// get the image count
			var iImageCount			= this._arrImages.length;
			
			// now work backwards and remove
			// the hidden class, and add a real one on
			while(iImageCount--)
			{
				// get the current image
				var objImage = this._arrImages[iImageCount];
				
				// if this is not our showing image
				if(!objImage.hasClass('carousel-image'))
				{
					// add the proper class to it
					objImage.addClass('carousel-image');
					
					// hide it
					objImage.addClass('forcehidden');
					
					// and remove the old class
					objImage.removeClass('hidden-carousel-image');
				}
			}
			
			// declare we are on our first image
			this._iImageCounter 	= 0;
			
			// assign the first
			if(this._arrImages.length)
				this._imgCurrent = this._arrImages[0];
			
			// get our links
			this._aNextImageLnk		= $('next-image');
			this._aPrevImageLnk		= $('prev-image');
			
			// add next handler
			if(this._arrImages.length > 1 && this._aNextImageLnk && this._imgCurrent)
			{
				// unhide it
				this._aNextImageLnk.removeClass('forcehidden');
				this._aNextImageLnk.addEvent('click', this.nextImage.bind(this));
			}
			
			// and previous
			if(this._arrImages.length > 1 && this._aPrevImageLnk && this._imgCurrent)
			{
				// unhide it
				this._aPrevImageLnk.removeClass('forcehidden');
				this._aPrevImageLnk.addEvent('click', this.prevImage.bind(this));
				
				// now calculate links
				this._handleLinks();
			}
		},
		
		nextImage: function()
		{
			if(this._iImageCounter < this._arrImages.length - 1)
			{
				// increment the counter
				this._iImageCounter++;
				
				// fade out the current image
				this._fadeCurrent();
				
				// update the links
				this._handleLinks();
			}
		},
		
		prevImage: function()
		{
			if(this._iImageCounter > 0)
			{
				// decrement the counter
				this._iImageCounter--;
		
				// fade out the current image
				this._fadeCurrent();
				
				// update the links
				this._handleLinks();
			}
		},
		
		_fadeCurrent: function()
		{
			// check we're not fading first
			if(!this._bFading)
			{
				// we definitely are now
				this._bFading = true;
				
				// stop any existing animation
				if(this._fxFade)
					this._fxFade.cancel();
				
				// create a new Fade
				this._fxFade = new Fx.Morph(this._imgCurrent,
											{duration:		400,
											 transition:	Fx.Transitions.Expo.easeIn,
											 onComplete:	this._introduceNext.bind(this)});
				// now fade out
				this._fxFade.start({opacity: 0});
			}
		},
		
		_introduceNext: function()
		{
			// allow a fade, if they so desire
			this._bFading = false;
			
			// hide the image we had
			this._imgCurrent.addClass('forcehidden');
			
			// get the next image
			this._imgCurrent = this._arrImages[this._iImageCounter];
			
			// set to unhidden
			this._imgCurrent.removeClass('forcehidden');
			this._imgCurrent.setStyle('opacity', 0);
			this._imgCurrent.setStyle('display', 'inline');
		
			// stop any existing animation
			if(this._fxFade)
				this._fxFade.cancel();
			
			// create a new Fade
			this._fxFade = new Fx.Morph(this._imgCurrent,
										{duration:		700,
										 transition:	Fx.Transitions.Expo.easeOut});
			// now fade in
			this._fxFade.start({opacity: [0,1]});
		},
		
		_handleLinks: function()
		{
			// keep the counter within limits
			if(this._iImageCounter >= this._arrImages.length)
				this._iImageCounter = this._arrImages.length-1;
			
			// and the other direction
			if(this._iImageCounter <= 0)
				this._iImageCounter = 0;
			
			// handle opacity
			if(this._iImageCounter == this._arrImages.length-1)
			{
				this._aNextImageLnk.setStyle('opacity', 0.4);
				this._aNextImageLnk.setStyle('cursor', 'default');
			}
			else
			{
				this._aNextImageLnk.setStyle('opacity', 1);
				this._aNextImageLnk.setStyle('cursor', 'pointer');
			}
			
			// and the same for prev
			if(this._iImageCounter == 0)
			{
				this._aPrevImageLnk.setStyle('opacity', 0.4);
				this._aPrevImageLnk.setStyle('cursor', 'default');
			}
			else
			{
				this._aPrevImageLnk.setStyle('opacity', 1);
				this._aPrevImageLnk.setStyle('cursor', 'pointer');
			}
		}
	})
	
	var imageCarousel = new ImageCarousel();
}
