/**
 * ImageRotater.js
 * Forty-Second Solutions, LLC.
 * version 1.1 / October 2007
 *
 * This script utilizes the Prototype JS and Scriptaculous libraries
 * in to rotate images with a fade effect. 
 *
 * In order to utilize this script, the HTML elements must...
 * - Be contained in a <div> that is passed into the script's constructor.
 * - The images must have the class name 'rotate'
 *
 * It also helps if the images are styled as display:none; in the HTML;
 * however, this script will automatically set that styling. It helps with
 * browser rendering if the styles are hard coded.
 */
var ImageRotater = Class.create();
ImageRotater.prototype = {

	/*-----------------------------------------------------
	 * constructors
	 */

    /**
     * initializes this image rotater with the images
     * that are contained in the specified div.
     *
     * imageDiv: the div containing the images to be rotated.
     */
    initialize: function(imageDiv, toFade) {
		// a reference to this function
        var This = this;

        // the array of images
        this.images = $(imageDiv).getElementsByClassName('rotate');
		
		// whether or not to fade the images
		this.fadeImages = toFade;

        // the image being displayed
        this.activeImage = This._rotateImages(null, this.images[0]);
        
    }, // end initialize
    
	/*-----------------------------------------------------
	 * public functions
	 */
    
    /**
     * begins rotating the images indefinately
     */
    start: function() {
    
        var This = this;
        new PeriodicalExecuter(function(pe) {
            This._rotate();
        }, 5);
    
    }, // end start
    
	/*-----------------------------------------------------
	 * private functions
	 */
	
    /**
     * rotates the images based on the currently active image. this
     * method is called periodically from this.start()
     */
    _rotate: function() {
	
		// fail safe if activeImage hasn't been set
		if(this.activeImage == null) {
			this.activeImage = this.images[0];
		} else {
		
			// get the index of the currently active image
			var curIndex = this.findIndexOfActiveImage(this.images);
			
			//var curIndex = this.images.indexOf(this.activeImage);
			
			// get the next image - if we're at the last image, restart at 0
			var nextImage = null;
			if(curIndex == (this.images.length - 1)) {
				nextImage = this.images[0];
			} else {
				nextImage = this.images[curIndex + 1];
			} // end if/else
					
		} // end if/else
		
		// swap the current image with the next
		this.activeImage = this._rotateImages(this.activeImage, nextImage);
		
    }, // end rotate
    
	/**
	 * Rotates the current image with the next image in the images array. If the
	 * ImageRotater is set to fade the images, then a scriptaculous toggle will fire;
	 * otherwise, a simple CSS switch will occur.
	 * 
	 * @param {Object} current The currently active image.
	 * @param {Object} next The next image to display.
	 */
	_rotateImages: function(current, next) {
		
		// fade the images, if specified in instance dta
		if(this.fadeImages) {
			
			// show the next image
			new Effect.Appear(next);
			
			// fade the current image, if it has been set
			if(current != null)
				new Effect.Fade(current);
		
		// otherwise, use a css swap	
		} else {
			
			// hide the current image
			if(current != null)
				current.setStyle({
					display: 'none'
				});
			
			// show the next image
			next.setStyle({
				display: 'block'
			});
			
		} // end if/else
		
		// update the current active image
		return next;
		
	}, // end rotateImages

	findIndexOfActiveImage: function(aImages) {
		
		for(var i = 0 ; i < aImages.length; i++) {
			if(aImages[i] === this.activeImage) {
				return i;
			}
		}
		
		return 0;
	}
	
} // end prototype

/*--------------------------------------------------------------*
 *
 * Revision History
 * 
 * - version 1.1 / October 2007
 *   . Major code overhaul. Nearly re-wrote entire script.
 *   . Improved effect performance
 *     - Better reference management
 *     - Used different scriptaculous effects
 *   . Implemented a CSS toggle (without fading)
 *   
 * - version 0.1 / July 2007
 *   . Intial release
 *   
 *--------------------------------------------------------------*/
