var AutoSlideShow = new Class({
    'Extends': SimpleSlideShow,
    'options': {
        'delay': 3000,
        'playOnStart': false
    },

    'initialize': function (options) {
        this.setOptions(options);
        this.toggleLink = $(this.options.toggleLink);
        this.toggleLink.addEvent('click', this.togglePlaying.bind(this));

        if (this.options.playOnStart) {
            this.play();
        } else {
            this.pause();
        }

        $$(this.options.nextLink, this.options.prevLink)
        .addEvent('click', this.pause.bind(this));

        this.parent(options);
    },

    'paused': true,
    'timer': null,

    'pause': function Slideshow_pause() {
        this.paused = true;
        this.toggleLink.addClass('paused');
        $clear(this.timer);
        this.fireEvent('pause');
        return this;
    },

    'play': function Slideshow_play() {
        this.paused = false;
        this.toggleLink.removeClass('paused');
        this.timer = this.forward.periodical(this.options.delay, this); 
        this.fireEvent('play');
        return this;
    },

    'togglePlaying': function Slideshow_togglePlaying() {
        this.paused ? this.play() : this.pause();
        this.fireEvent('toggle');
        return this;
    }
});

function setupSlideshow() {
    function $E() {
        var r = $$.apply(this, arguments);
        return r.length && r[0];
    }


    var slideshow = new AutoSlideShow({
        'slides': $$('#SlideShow .slides .slide'),
        'currentIndexContainer': $E('#SlideShow .current'),
        'maxContainer': $E('#SlideShow .max'),
        'prevLink': $E('#SlideShow .previous'),
        'nextLink': $E('#SlideShow .next'),
        'toggleLink': $E('#SlideShow .toggle'),
        'delay': 2000
    });

    $$('ul.slideshowThumbnails a.slide').each(
        function(link, n) {
            link.addEvent('click', function(e) {
                new Event(e).preventDefault();
                slideshow.show(n);
            });
        }
    );   

    var thumbsFX = new Fx.Reveal($E('ul.slideshowThumbnails').hide());
    $E('#ImagesGallery .hideShow').addEvent('click', function (e) {
        new Event(e).preventDefault();
        this.toggleClass('showing');
        thumbsFX.toggle();
    });


}

window.addEvent('domready', setupSlideshow);
