SME.Slideshow = Class.create();
SME.Slideshow.prototype = {
    
    images: null,
    thumbs: null,
    imageIndex: 0,
    animationInterval: 2,    // in seconds
    animationDuration: 1.5,  // in seconds
    transitionType: 'cross-fade',
    animationTimer: null,
    
    initialize : function (slideshowName, interval, duration, transition)
    {
        var slideshow = $(slideshowName);
        if (! slideshow) {
            return;
        }
        
        this.images = slideshow.childElements();
        if (this.images.length < 2) {
            return;
        }
        
        if (interval > 0) {
            this.animationInterval = interval;
        }
        if (duration > 0) {
            this.animationDuration = duration;
        }
        if (transition) {
            this.transitionType = transition;
        }
        
        for (var i = 1; i < this.images.length; i++) {
            this.images[i].setStyle({opacity: 0, visibility: 'visible'}).hide();
        }
        
        slideshow.observe('mouseenter', this.clearTimer.bind(this));
        slideshow.observe('mouseleave', this.startTimer.bind(this, true));
        
        this.startTimer(true);
        
        var thumbContainer = $(slideshowName + 'Thumbnails');
        if (thumbContainer) {
            this.thumbs = thumbContainer.select('IMG');
            for (var i = 0; i < this.thumbs.length; i++) {
                $(this.thumbs[i]).observe('click', this.thumbnailClicked.bind(this, this.thumbs[i]));
            }
        }
    },
    
    clearTimer : function ()
    {
        window.clearTimeout(this.animationTimer);
        this.animationTimer = null;
    },

    startTimer : function (useHalfTime)
    {
        this.clearTimer();
        this.animationTimer = window.setTimeout(this.animateNext.bind(this), this.animationInterval * (useHalfTime ? 500 : 1000));
    },
    
    animateNext : function ()
    {
        this.showNextImage(this.imageIndex + 1);
        this.startTimer();
    },
    
    thumbnailClicked : function (img)
    {
        this.clearTimer();
        img.siblings().each(function (sibling) {
            sibling.removeClassName('current');
        });
        img.addClassName('current');
        var nextImageIndex;
        for (nextImageIndex = 0; nextImageIndex < this.thumbs.length; nextImageIndex++) {
            if (this.thumbs[nextImageIndex] == img) {
                break;
            }
        }
        this.showNextImage(nextImageIndex);
    },
    
    showNextImage : function (nextImageIndex)
    {
        var thisImage = $(this.images[this.imageIndex]);
        this.imageIndex = nextImageIndex;
        if (this.imageIndex >= this.images.length) {
            this.imageIndex = 0;
        }
        var nextImage = $(this.images[this.imageIndex]);

        switch (this.transitionType) {

            case 'cut':
                nextImage.show();
                thisImage.hide();
                break;

            case 'fade':
                thisImage.setStyle({zIndex: 10});
                nextImage.setStyle({opacity: 1.0}).show();
                new Effect.Opacity(thisImage,
                    {from: 1.0, to: 0.0,
                     duration: this.animationDuration,
                     afterFinish: function () {
                         thisImage.setStyle({zIndex: 0}).hide();
                     }
                    });
                break;

            case 'cross-fade':
            default:
                thisImage.setStyle({zIndex: 10});
                nextImage.setStyle({opacity: 0.0}).show();
                new Effect.Opacity(thisImage,
                    {from: 1.0, to: 0.0,
                     duration: this.animationDuration,
                     afterFinish: function () {
                         thisImage.setStyle({zIndex: 0}).hide();
                     }
                    });
                new Effect.Opacity(nextImage,
                    {from: 0.0, to: 1.0,
                     duration: this.animationDuration
                    });
                break;

        }
    }
    
};

