/**
 * Slideshow object for displaying an array of images
 * @param {HTMLElement} elem Slideshow html element
 * @param {String}      dir Path to image directory
 * @param {int}         size Number of images in slideshow
 * @param {int}         time Length between images (in milliseconds)
 * @param {int}         type File extension (0 - jpg, 1 - gi, 2 - png)
 * @param {String}      name Filename of images (example: filenameX.gif 
 *                      where X is image number)
 * @namespace object.namespace
 * @class Slideshow
 * @constructor
 */
function Slideshow( elem, size, time ) {
  /**
   * Path to image directory and image file
   * @type String
   * @property _dir
   * @private
   */
  this._path   /*:String*/ = "img";
  /**
   * Html element where the slideshow is displayed
   * @type HTMLElement
   * @property _elem
   * @private
   */
  this._elem  /*:Object*/ = document.getElementById(elem);
  /**
   * Name of the html element the slideshow is in
   * @type String
   * @property _elemName
   * @private
   */
  this._elemName = elem;
  /**
   * Number of images in the slideshow
   * @type int
   * @property _count
   * @private
   */
  this._count /*:Int*/ = size;
  /**
   * Length of time between images (in milliseconds)
   * @type int
   * @property _time
   * @private
   */
  this._time  /*:Int*/ = time;
  /**
   * File extension for images (either jpg gif or png)
   * @type String
   * @property _type
   * @private
   */
  this._type  /*:String*/ = ".jpg";
  /**
   * Timeout id for setting timeouts between images
   * @type int
   * @property _tId
   * @private
   */
  this._tId   /*:Int*/ = 0;
  /**
   * New image path to replace the slideshow's src attribute
   * @type String
   * @property _newImg
   * @private
   */
  this._newImg /*:String*/ = "";
  /**
   * Index of current image displayed from the image array
   * @type int
   * @property
   * @private
   */
  this._curImg /*:Int*/ = 0;
  /**
   * Array of image objects for the slideshow
   * @type Object[]
   * @property
   * @private
   */
  this._images /*:Array*/;
  /**
   * YUI fade in effect for slideshow fade in of new images
   * @type YAHOO.util.Anim
   * @property _fadeIn
   * @private
   */
  this._fadeIn /*:Object*/;
  /**
   * YUI fade out effect for slideshow fade out of old images
   * @type YAHOO.util.Anim
   * @property _fadeOut
   * @private
   */
  this._fadeOut /*:Object*/;
}

Slideshow.prototype = {
  /**
   * Updates the slideshow elements source attribute to the new image
   * path and starts the fade in effect
   * @return void
   * @method changeImage
   */
  changeImage : function() {
    YAHOO.log("changeImage(): entering", "debug", "Slideshow");
    this._elem.src /*:String*/ = this._newImg;
    this._fadeIn.animate();
  },
  /**
   * Retrieves the next image in the array, wrapping around at the end
   * @return {String} Path of the image source
   * @method nextImage
   */
  nextImage : function() {
    YAHOO.log( "nextImage(): entering", "debug", "Slideshow" );
    this._curImg = ( this._curImg + 1 ) % this._count;
    return( this._images[ this._curImg ].src );
  },
  /**
   * Updates the slideshow images and sets the next timeout event for the next image
   * @return void
   * @method runShow
   */
  runShow : function() {
    YAHOO.log("runShow(): entering", "debug", "Slideshow");
    this._newImg = this.nextImage();
    this._fadeOut.animate();
    var _old = this;
    this._tId = setTimeout( function() { _old.runShow() }, this._time );
  },
  /**
   * Initializes the image array of the slideshow and sets the
   * fadeout onComplete event to change the image, then begins the slideshow
   * @return void
   * @method init
   */
  init : function() {
    YAHOO.log("init(): entering", "debug", "Slideshow");
    /* Initialize image array with image paths */
    this._type = this._elem.src.substring( this._elem.src.indexOf( "0." ) + 1 );
    this._dir = this._elem.src.substring( 0, this._elem.src.indexOf( "0." ) );
    this._images = new Array( this._count );
    for ( var i = 0; i < this._count; i++ ) {
      this._images[i] /*:Image*/ = new Image();
      this._images[i].src = this._dir + i + this._type;
    }
    this._fadeOut = new YAHOO.util.Anim( this._elem, 
                                         { opacity: { from: 1, to: 0 } },
                                         0.4, YAHOO.util.Easing.easeOut );
    this._fadeIn = new YAHOO.util.Anim( this._elem,
                                        { opacity: { from: 0, to: 1 } },
                                        0.4, YAHOO.util.Easing.easeIn );
    /**
     * The onComplete event allows the image to change in the slideshow
     * while the fade out is in effect.  Then it fades in after the image is loaded
     * @namespace YAHOO.util
     * @class Event
     * @static
     */
    this._fadeOut.onComplete.subscribe( this.changeImage, this, true );
    var _old /*:Object*/ = this;
    this._tId = setTimeout( function() { _old.runShow() }, this._time );
  }
}