
function SlideShowViewer (elementId, imageUrlList)
{
    this.fadeTime = 3000;
    this.showTime = 5000;
    this.container = jQuery('#'+elementId);
    this.imageUrlList = imageUrlList;
    this.imageCount = imageUrlList.length;
    this.currentIndex = 0;
    this.preLoadedCount = 0;
    this.isPreLoaded = false;
    this.imageList = Array;
    thisViewer = this;
    this._preLoadImages();
    this.playAction = function () {
        thisViewer._nextSlide();
    }
}
        
SlideShowViewer.prototype.play = function () {
    setInterval(this.playAction, this.showTime);
    return false;
}

SlideShowViewer.prototype._nextSlide = function () {
    nextImageSrc = this.imageUrlList[this.currentIndex];
    this.container.css({background: 'transparent url('+nextImageSrc+') left top no-repeat'});
    image = this.container.find('img:first');
    image.animate({opacity: 0}, this.fadeTime, function(){
        $(this)
        .attr('src', nextImageSrc)
        .css({'opacity': '1'})
    });
    
    if (this.currentIndex < this.imageCount-1) {
        this.currentIndex++;
    } else {
        this.currentIndex = 0;
    }
    return false;
}

SlideShowViewer.prototype._preLoadImages = function() {
    thisSlideShowViewer = this;
    for(var i = 0; i<this.imageUrlList.length; i++)
    {
        this.imageList[i] = new Image();
        this.imageList[i].onload = function () {
            thisSlideShowViewer.preLoadedCount++;
            if (thisSlideShowViewer.preLoadedCount == thisSlideShowViewer.imageCount) {
                thisSlideShowViewer.isPreLoaded = true;
                nextImageSrc = thisSlideShowViewer.imageUrlList[thisSlideShowViewer.currentIndex];
                image = thisSlideShowViewer.container.find('img:first');
                image.attr('src', nextImageSrc);
                if (thisSlideShowViewer.currentIndex < thisSlideShowViewer.imageCount-1) {
                    thisSlideShowViewer.currentIndex++;
                } else {
                    thisSlideShowViewer.currentIndex = 0;
                }
                thisSlideShowViewer.play();
            }
        };
        this.imageList[i].src = this.imageUrlList[i];
    }
}