function ImageObject(s,h,a) {
    this.src = s;
    this.href = h;
    this.alt = a;
}

/**
 * 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) {
    var IMG_WIDTH = "655";
    var IMG_HEIGHT = "246";
    var currentIndex = 0;          // current index in the image array

    if(imageArray.length == 0)  return;

    // convert id to element
    var containerEl = $(container);

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

    // preload images
    for (var i=0; i<imageArray.length; i++) {
        var img = document.createElement("img");
        img.setAttribute("src", imageArray[i].src);
    }

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

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

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

    var fadeInImg = document.createElement("img");
    fadeInImg.setAttribute("class", "slideshow-image");

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

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

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

    var fadeOutImg = document.createElement("img");
    fadeOutImg.setAttribute("class", "slideshow-image");

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

    /**
     * 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.alt);
        hrefEl.setAttribute("href", imgObj.href);
    };

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

        var fadeOutImgTemp = fadeOutImg;
        fadeOutImg = fadeInImg;
        fadeInImg = fadeOutImgTemp;

        currentIndex = ((currentIndex + 1) < imageArray.length) ? currentIndex + 1 : 0;
        fadeInImg.setAttribute("src",imageArray[currentIndex].src);
        $(fadeInImg).set('tween', {duration: 'long'});
        $(fadeOutImg).set('tween', {duration: 'long'});

        $(fadeOutImg).tween('opacity', '0');
    	$(fadeInImg).tween('opacity', '1');
    	
        setTimeout(nextImage, 6000);
    }

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

        setImageAttributes(fadeInImg, fadeInHref, imageArray[0]);
        setImageAttributes(fadeOutImg, fadeOutHref, imageArray[0]);
     
        $(fadeInImg).set('tween', {duration: 'long'});        
        $(fadeInImg).tween('opacity', '1');
    }

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

