var Slideshow = {
    current: 0,
    path: '',
    images: [],

    // {{{ init()

    init: function()
    {
        this.bstop = this.stop.bindAsEventListener(this);
        this.bstart = this.start.bindAsEventListener(this);
        // 0-based array
        this.current--;
        this.cacheImg = new Image;
        this.cacheImg.src = this.path + this.images[this.current + 1];
        this.attachStart();
    },

    // }}}
    // {{{ attachStart()

    attachStart: function()
    {
        Event.stopObserving($('gallery-slideshow'), 'click', this.bstop);
        Event.observe($('gallery-slideshow'), 'click', this.bstart);
        $('gallery-slideshow').firstChild.nodeValue = '> Play Slideshow';
        $('gallery-slideshow').title = 'Play Slideshow';
    },

    // }}}
    // {{{ attachStop()

    attachStop: function()
    {
        Event.stopObserving($('gallery-slideshow'), 'click', this.bstart);
        Event.observe($('gallery-slideshow'), 'click', this.bstop);
        $('gallery-slideshow').firstChild.nodeValue = 'Pause Slideshow';
        $('gallery-slideshow').title = 'Pause Slideshow';
    },

    // }}}
    // {{{ start()

    start: function(e)
    {
        Event.stop(e);
        this.attachStop();
        var handler = this.cycle.bindAsEventListener(this);
        this.pe = new PeriodicalExecuter(this.cycle.bind(this), 3);
    },

    // }}}
    // {{{ stop()

    stop: function(e)
    {
        if (e) Event.stop(e);
        this.pe.stop();
        this.attachStart();
    },

    // }}}
    // {{{ finished()

    finished: function()
    {
        this.stop();
        Event.stopObserving($('gallery-slideshow'), 'click', this.bstart);
        Event.observe($('gallery-slideshow'), 'click', function(e) { Event.stop(e); });
        $('gallery-slideshow').firstChild.nodeValue = 'Slideshow Complete';
        $('gallery-slideshow').title = 'Slideshow Complete';
    },

    // }}}
    // {{{ cycle()

    cycle: function()
    {
        this.current++;
        if (this.images[this.current] == undefined) {
            return this.finished();
        }

        var next = this.images[this.current + 1] == undefined ? 1 : this.current + 2;
        var prev = this.images[this.current - 1] == undefined ? this.images.length : this.current;
        new Effect.Fade('gallery-img', { 
            duration: 0.5,
            from: 0.99,
            to: 0.1,
            afterFinish: function() {
                $('gallery-img').src = this.path + this.images[this.current];
                $('gallery-current').firstChild.nodeValue = this.current + 1;
                $('gallery-next').href = $('gallery-next').href.replace(/\d+$/, next);
                $('gallery-next2').href = $('gallery-next2').href.replace(/\d+$/, next);
                $('gallery-prev').href = $('gallery-prev').href.replace(/\d+$/, prev);
                new Effect.Appear('gallery-img', { duration: 1.0, to: 0.99, queue: 'end' });
                this.cacheImg.src = this.path + this.images[this.current + 1];
            }.bind(this)
        });
    }

    // }}}
}
