if (!TT)
	var TT = {};

TT.Slideshow = Class.create({
    initialize: function(domId) {
        this.options = Object.extend({
            delay: 4,
            slidesSelector: ".slide",
            startPaused: false,
            startingIndex: 0,
            fadeTime: 1,
            playPauseClass: ".slideshow-button.playPause",
            nextClass: ".slideshow-button.next",
            previousClass: ".slideshow-button.previous"
        }, arguments[1] || {})

        this.slides = $(domId).select(this.options.slidesSelector);
        this.currentIndex = this.options.startingIndex;
        this.isPaused = this.options.startPaused;
        this.zindex = 500;
        this.timer = 0;
        this.fadeTime = this.options.fadeTime;
        this.locked = false;
        this.delay = this.options.delay;  // in seconds
        this.slides[this.currentIndex].setStyle({
            zIndex: this.zindex,
            visibility: "visible",
            display: "block"
        })

        this.playPauseButton = $(domId).down(this.options.playPauseClass)
        this.playPauseButton.observe("click", this.togglePlayPause.bind(this));

        $(domId).down(this.options.nextClass).observe("click", this.next.bind(this));
        $(domId).down(this.options.previousClass).observe("click", this.previous.bind(this));

        if (!this.isPaused)
            this.timedNext(this.delay);
    },

    togglePlayPause: function() {
        this.isPaused ? this.play() : this.pause();  // if paused, begin playback, otherwise (playing), pause
    },

    play: function() {
        this.resetTimer();
        this.isPaused = false;
        this.timedNext(this.delay / 2);
        this.playPauseButton.removeClassName("paused");
    },

    pause: function() {
        this.resetTimer();
        this.isPaused = true;
        this.playPauseButton.addClassName("paused");
    },

    next: function() {
        this.resetTimer();
        
        var newIndex = this.currentIndex + 1;
        this.showSlide((newIndex >= this.slides.length) ? 0 : newIndex);
        if (!this.isPaused)
            this.timedNext(this.delay); 
    },

    timedNext: function(delay) {
        this.timer = setTimeout(this.next.bind(this), (delay * 1000));  // delay converted to milliseconds
    },

    previous: function() {
        this.resetTimer();

        var newIndex = this.currentIndex - 1;
        this.showSlide((newIndex < 0) ? (this.slides.length - 1) : newIndex);
        
        if (!this.isPaused)
            this.timedNext(this.delay);
    },

    showSlide: function(index) {
        if (this.locked) 
            return;

        if (this.zindex == 1) {
            this.zindex = 5000;
            this.slides[this.currentIndex].style.zIndex = this.zindex;
        }

        this.slides[index].setStyle({
            zIndex: --this.zindex,
            visibility: "visible",
            display: "block",
            opacity: 1
        });

        if (this.isPaused) {
            this.slides[this.currentIndex].hide();
            this.slides[this.currentIndex].style.zIndex = 0;
            this.slides[this.currentIndex].style.visibility = "hidden";
        } else {
            this.locked = true;
            Effect.Fade(this.slides[this.currentIndex], {
                duration: this.fadeTime,
                afterFinish: function(effect) {
                    effect.element.style.zIndex = 0;
                    effect.element.style.visibility = "hidden";
                    this.locked = false;
                }.bind(this)
            });
        }

        this.currentIndex = index;
    },

    resetTimer: function() {
        clearTimeout(this.timer);
        this.timer = 0;
    }
});
