function ImageObject(s, h, c) {
    this.src = s;
    this.href = h;
    this.caption = c;
    this.image = new Image();
    this.image.src = s;
    this.image.alt = c;
}

/**
 * Based off of the ImageRotator class.  This one will let you auto play slideshow, show next, show previous,
 * pause slideshow.
 *
 * Class for rotating through a list of images.  Performs a crossfade for
 * transitioning between images.  This class will clear out all children of
 * container object on initialization.
 *
 * Note: All functions were declared within parent because of setInterval scoping.
 *
 * @param imageArray {Array}  an array of ImageObject objects containing images to rotate through
 * @param container {String | Element} DOM element (or ID of element) of element containing the rotating images
 * @param controls {Object} custom object for the play controls
 *         example:  controls = {
 *                                playPauseButton: yuiDom.get("playButton"),
 *                                previousButton: yuiDom.get("previousButton"),
 *                                nextButton: yuiDom.get("nextButton"),
 *                              }
 */

function ImagePlayer(imageArray, container, controls, captionContainer) {
    this.id = "imagePlayer1";
    if (imageArray.length == 0)  return;

    this.images = imageArray;
    this.currentIndex = 0;
    this.controls = controls;
    this.controls.playTimeout = this.controls.playTimeout || 4000;

    // convert id to element
    if (YAHOO.lang.isString(container))
        container = document.getElementById(container);

    while (container.hasChildNodes()) {
        container.removeChild(container.firstChild);
    }
    this.imagePlayer = container;

    // convert caption id to element
    if (YAHOO.lang.isString(captionContainer))
        captionContainer = document.getElementById(captionContainer);

    this.captionContainer = captionContainer;
    // clear container of child nodes

    //Check if we have a href for the images
    this.hrefcheck = "false";
    if (imageArray.length > 0) {
        if (imageArray[0].href != "") {
            this.hrefcheck = "true";
        }
    }
    if (this.controls) {
        this.initializeControls();
    }
    this.initializeFadeIn();
    this.initializeFadeOut();
    if (this.captionContainer) {
        this.initializeCaptionFades();
    }

    // imagePlayer state
    this.paused = false;

    // show first image
    yuiDom.setStyle(this.fadeInImg, 'opacity', 0);
    yuiDom.setStyle(this.fadeOutImg, 'opacity', 0);

    yuiDom.setStyle(this.fadeInCapDiv, 'opacity', 0);
    yuiDom.setStyle(this.fadeOutCapDiv, 'opacity', 0);

    this.setImageAttributes(this.fadeInImg, this.fadeInHref, imageArray[0]);
    this.setImageAttributes(this.fadeOutImg, this.fadeOutHref, imageArray[0]);
    if (this.captionContainer != null) {
        this.setCaptionAttributes(this.fadeInCapDiv, this.images[0]);
        this.setCaptionAttributes(this.fadeOutCapDiv, this.images[0]);
        this.fadeInCapAnim.animate();
    }
    // fade in first image
    this.fadeInAnim.animate();

}
ImagePlayer.prototype.initializeControls = function () {
    if (this.controls.playPauseButton) {
        yuiEvent.addListener(this.controls.playPauseButton, "click", this.play)
        yuiDom.addClass(this.controls.playPauseButton, "pause");
    }
    if (this.controls.previousButton) {
        yuiEvent.addListener(this.controls.previousButton, "click", this.previous.bind(this));
        yuiDom.addClass(this.controls.previousButton, "disabled");
    }
    if (this.controls.nextButton) {
        yuiEvent.addListener(this.controls.nextButton, "click", this.next.bind(this))
    }
}

ImagePlayer.prototype.initializeFadeIn = function () {
    // initialize "fade-in image" (currenlty displayed)
    this.fadeInDiv = document.createElement("div");
    this.fadeInDiv.style.position = "absolute";
    this.imagePlayer.appendChild(this.fadeInDiv);

    this.fadeInHref = document.createElement("a");
    if (this.hrefcheck == 'true') {
        this.fadeInDiv.appendChild(this.fadeInHref);
    }

    this.fadeInImg = document.createElement("img");
    this.fadeInImg.setAttribute("border", "0");

    if (this.hrefcheck == 'true') {
        this.fadeInHref.appendChild(this.fadeInImg);
    }
    else {
        this.fadeInDiv.appendChild(this.fadeInImg);
    }
    this.fadeInAnim = new yuiAnim(this.fadeInImg, {opacity: {to: 1 }}, 0.8, YAHOO.util.Easing.easeNone);
}

ImagePlayer.prototype.initializeFadeOut = function () {
    this.fadeOutDiv = document.createElement("div");
    this.fadeOutDiv.style.position = "absolute";
    this.imagePlayer.appendChild(this.fadeOutDiv);

    this.fadeOutHref = document.createElement("a");
    if (this.hrefcheck == 'true') {
        this.fadeOutDiv.appendChild(this.fadeOutHref);
    }

    this.fadeOutImg = document.createElement("img");
    this.fadeOutImg.setAttribute("border", "0");

    if (this.hrefcheck == 'true') {
        this.fadeOutHref.appendChild(this.fadeOutImg);
    }
    else {
        this.fadeOutDiv.appendChild(this.fadeOutImg);
    }

    this.fadeOutAnim = new YAHOO.util.Anim(this.fadeOutImg, {opacity: {to: 0 }}, 0.8, YAHOO.util.Easing.easeNone);
}

ImagePlayer.prototype.initializeCationFades = function () {
    // clear caption container of child nodes
    while (this.captionContainer.hasChildNodes()) {
        this.captionContainer.removeChild(this.captionContainer.firstChild);
    }

    // initialize "fade-in caption" (currenlty displayed)
    this.fadeInCapDiv = document.createElement("div");
    this.fadeInCapDiv.style.position = "absolute";

    this.captionContainer.appendChild(this.fadeInCapDiv);

    // initialize "fade-out caption"
    this.fadeOutCapDiv = document.createElement("div");
    this.fadeOutCapDiv.style.position = "absolute";
    this.captionContainer.appendChild(this.fadeOutCapDiv);

    this.fadeOutCapAnim = new yuiAnim(this.fadeOutCapDiv, {opacity: {to: 0 }}, 0.8, YAHOO.util.Easing.easeNone);
    this.fadeInCapAnim = new yuiAnim(this.fadeInCapDiv, {opacity: {to: 1 }}, 0.8, YAHOO.util.Easing.easeNone);
}
ImagePlayer.prototype.setImageAttributes = function(imgEl, hrefEl, imgObj) {
    imgEl.setAttribute("src", imgObj.src);
    imgEl.setAttribute("alt", imgObj.caption);
    hrefEl.setAttribute("href", imgObj.href);
}

ImagePlayer.prototype.setCaptionAttributes = function(divEl, imgObj) {
    divEl.innerHTML = imgObj.caption;
}

// todo: implement auto play and pause
ImagePlayer.prototype.play = function () {
    this.paused = false;
    var interval = 0;
    var self = this;
    var imageCount = 0;
    while (this.paused != true && (this.images.length >= imageCount)) {
        imageCount++;
        var timeOut = interval;
        setTimeout(function() {
            if (self.images.length > self.currentIndex + 1) {
                self.currentIndex++;
                self.currentIndex = (self.currentIndex % self.images.length);
                self.animate();
            }

            if (self.currentIndex > 0) {
                yuiDom.removeClass(self.controls.previousButton, "disabled");
            }
            if (self.images.length == self.currentIndex + 1) {
                yuiDom.addClass(self.controls.nextButton, "disabled");
            }
        }, timeOut);
        interval = interval + this.controls.playTimeout;
    }
}
ImagePlayer.prototype.playBridge = function (e) {
    this.paused = false;
    if (this.controls) {
        if (this.controls.playPauseButton) {
            yuiDom.addClass(this.controls.playPauseButton, "pause");
        }
    }
    var timeOut = this.controls.playTimeout;
    var self = this;
    this.fadeInAnim.onComplete.subscribe(setTimeout(
        function() {
            if (!self.paused) {
                self.next(null, true);
            }
        }
        , timeOut)
        );
}

ImagePlayer.prototype.pause = function () {
    this.paused = true;
}

ImagePlayer.prototype.pauseBridge = function (e) {
    if (this.controls) {
        if (this.controls.playPauseButton) {
            yuiDom.addClass(this.controls.playPauseButton, "play")
        }
    }
    this.pause();
}


ImagePlayer.prototype.previous = function (e, autoPlay) {

    if (!autoPlay) {
        this.paused = true;
    }
    if (this.images.length > this.currentIndex) {
        yuiDom.removeClass(this.controls.nextButton, "disabled");
    }
    if (this.currentIndex > 0) {
        this.currentIndex--;
        this.animate();
    }
    if (this.currentIndex == 0) {
        yuiDom.addClass(this.controls.previousButton, "disabled");
    }
    return this.currentIndex;
}

ImagePlayer.prototype.next = function (e, autoPlay) {
    if (!autoPlay) {
        this.paused = true;
    }
    if (this.images.length != this.currentIndex + 1) {
        this.currentIndex++;
        this.currentIndex = (this.currentIndex % this.images.length);
        this.animate(autoPlay);
    }

    if (this.currentIndex > 0) {
        yuiDom.removeClass(this.controls.previousButton, "disabled");
    }
    if (this.images.length == this.currentIndex + 1) {
        yuiDom.addClass(this.controls.nextButton, "disabled");
    }
    return this.currentIndex;
}

ImagePlayer.prototype.animate = function (autoPlay) {
    var fadeOutImgTemp = this.fadeOutImg;
    this.fadeOutImg = this.fadeInImg;
    this.fadeInImg = fadeOutImgTemp;
    //    this.fadeOutImg = this.fadeInImg;

    if (this.captionContainer != null) {
        var fadeOutCapDivTemp = this.fadeOutCapDiv;
        this.fadeOutCapDiv = this.fadeInCapDiv;
        this.fadeInCapDiv = this.fadeOutCapDivTemp;
    }

    this.fadeInImg.setAttribute("src", this.images[this.currentIndex].src);
    if (this.captionContainer != null) {
        this.setCaptionAttributes(this.fadeInCapDiv, this.image[this.currentIndex]);
    }


    this.fadeOutAnim = new YAHOO.util.Anim(this.fadeOutImg, {
        opacity: { to: 0 }
    }, 2, YAHOO.util.Easing.easeOut);

    this.fadeInAnim = new YAHOO.util.Anim(this.fadeInImg, {
        opacity: { to: 1 }
    }, 2, YAHOO.util.Easing.easeIn);


    if (autoPlay) {
        var timeOut = this.controls.playTimeout;
        var self = this;

        this.fadeInAnim.onComplete.subscribe(setTimeout(
            function() {
                if (!self.paused) {
                    self.next(null, true);
                }
            }
            , timeOut)
            );
    }
    this.fadeOutAnim.animate();
    this.fadeInAnim.animate();
    if (this.captionContainer != null) {
        this.fadeOutCapAnim = new YAHOO.util.Anim(this.fadeOutCapDiv, {
            opacity: { to: 0 }
        }, 2, YAHOO.util.Easing.easeOut);

        this.fadeInCapAnim = new YAHOO.util.Anim(this.fadeInCapDiv, {
            opacity: { to: 1 }
        }, 2, YAHOO.util.Easing.easeIn);
        this.fadeOutCapAnim.animate();
        this.fadeInCapAnim.animate();
    }
    return true;
}
