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;
}

/**
 * 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
 */
function ImageRotator(imageArray, container, capContainer) {
    var currentIndex = 0;          // current index in the image array

    if(imageArray.length == 0)  return;

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

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

    // clear container of child nodes
    while(container.hasChildNodes()) {
        container.removeChild(container.firstChild);
    }

    //Check if we have a href for the images
    var index=0;
    var hrefcheck="false";
    if(index< imageArray.length) {
        if(imageArray[index].href!='') {
            hrefcheck="true";
        }
    }

    // initialize "fade-in image" (currenlty displayed)
    var fadeInDiv = document.createElement("div");
    fadeInDiv.style.position = "absolute";
    container.appendChild(fadeInDiv);

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

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

    if(hrefcheck=='true') {
    fadeInHref.appendChild(fadeInImg);
        } else {
        fadeInDiv.appendChild(fadeInImg);
    }

    // initialize "fade-out image"
    var fadeOutDiv = document.createElement("div");
    fadeOutDiv.style.position = "absolute";
    container.appendChild(fadeOutDiv);

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

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

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

     if(capContainer != null) {
     // clear caption container of child nodes
     while(capContainer.hasChildNodes()) {
        capContainer.removeChild(container.firstChild);
    }

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

    capContainer.appendChild(fadeInCapDiv);

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

    /**
     * Fade-out animation
     */
    var fadeOutAnim = new YAHOO.util.Anim(fadeOutImg, {
            opacity: { to: 0 }
        }, 0.8, YAHOO.util.Easing.easeNone);

    /**
     * Fade-in animation
     */
    var fadeInAnim = new YAHOO.util.Anim(fadeInImg, {
            opacity: { to: 1 }
        }, 0.8, YAHOO.util.Easing.easeNone);

     var fadeOutCapAnim = new YAHOO.util.Anim(fadeOutCapDiv, {
            opacity: { to: 0 }
        }, 0.8, YAHOO.util.Easing.easeNone);

    var fadeInCapAnim = new YAHOO.util.Anim(fadeInCapDiv, {
            opacity: { to: 1 }
        }, 0.8, YAHOO.util.Easing.easeNone);
    /**
     * Sets images attributes from an ImageObject
     *
     * @param imgEl {Element} image element to set attributes on
     * @param hrefEl {Element} anchor element to set href on
     * @param imgObj {ImageObject}  image object to pull values from
     */
    var setImageAttributes = function(imgEl, hrefEl, imgObj) {
        imgEl.setAttribute("src", imgObj.src);
        imgEl.setAttribute("alt", imgObj.caption);
        hrefEl.setAttribute("href", imgObj.href);
    }

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

    /**
     * Rotates to the next image
     */
    var nextImage = function() {
        if(imageArray.length <= 0)
            return;

         // prepare image for fading out
       // fadeOutImg.setAttribute("src", fadeInImg.getAttribute("src"));
         fadeOutImg.setAttribute("src", imageArray[currentIndex].src);

        YAHOO.util.Dom.setStyle(fadeOutImg, 'opacity', 1);

        // increment index
        currentIndex = ((currentIndex + 1) < imageArray.length) ? currentIndex + 1 : 0;
         fadeOutHref.setAttribute("href",imageArray[currentIndex].href);
        // make fade-in image invisible and update to show next image
        YAHOO.util.Dom.setStyle(fadeInImg, 'opacity', 0);
        setImageAttributes(fadeInImg, fadeInHref, imageArray[currentIndex]);

         if(capContainer != null) {
        // prepare caption for fading caption
       fadeOutCapDiv.innerHTML = fadeInCapDiv.innerHTML;
        YAHOO.util.Dom.setStyle(fadeOutCapDiv, 'opacity', 1);

        // make fade-in caption invisible and update to show next image
        YAHOO.util.Dom.setStyle(fadeInCapDiv, 'opacity', 0);
        setCaptionAttributes(fadeInCapDiv, imageArray[currentIndex]);
    }
        // perform crossfade
        fadeOutAnim.animate();
        fadeInAnim.animate();
         if(capContainer != null) {
        fadeOutCapAnim.animate();
        fadeInCapAnim.animate();
             }
        setTimeout(nextImage, 5000);
    };

    // initialize images
    if(imageArray.length > 0) {
        YAHOO.util.Dom.setStyle(fadeInImg, 'opacity', 0);
        YAHOO.util.Dom.setStyle(fadeOutImg, 'opacity', 0);

        YAHOO.util.Dom.setStyle(fadeInCapDiv, 'opacity', 0);
        YAHOO.util.Dom.setStyle(fadeOutCapDiv, 'opacity', 0);

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

    // set rotation interval timer
    setTimeout(nextImage, 5000);   // anonymous function for scoping issues
}
