var Slideshow = new Class({

    Implements: [Options, Events],

    options: {
        autoplay: false,
        interval: 5000,
        direction: 'right',
        startItem: 0,
        buttons: {
            prev: null,
            next: null,
            stop: null,
            play: null
        },
        morph: {
             duration: 800,
             transition: Fx.Transitions.Quad.easeInOut,
             wait: true
        }
    },

    initialize: function(el, options) {
        this.setOptions(options);
        this.items = el;

        if (!this.items) {
            return;
        }

        this.itemNum  = this.options.startItem;
        this.width    = this.items[0].getSize().x;
        this.playback = null;

        this.initCss();
        this.initAutoplay();
        this.initButtons();
    },

    initCss: function() {
        this.items.each(function(element, index) {
            if (0 == index) {
                element.setStyle('left', 0);
            } else {
                element.setStyle('left', this.width);
                element.setStyle('opacity', 0);
            }
        });
    },

    initButtons: function() {
        if (this.options.buttons.prev) {
            this.btnPrev = $(this.options.buttons.prev);
            this.btnPrev.addEvent('click', this.slideIt.bind(this, 'left'));
        }

        if (this.options.buttons.next) {
            this.btnNext = $(this.options.buttons.next);
            this.btnNext.addEvent('click', this.slideIt.bind(this, 'right'));
        }

        if (this.options.buttons.stop) {
            this.btnStop = $(this.options.buttons.stop);
            this.btnStop.addEvent('click', this.stop.bind(this));
        }

        if (this.options.buttons.play) {
            this.btnPlay = $(this.options.buttons.play);
            this.btnPlay.addEvent('click', this.play.bind(this));
        }
    },

    initAutoplay: function() {
        if (true == this.options.autoplay) {
            this.play();
        }
    },

    play: function() {
        this.stop();
        this.playback = this.slideIt.periodical(this.options.interval, this, this.options.direction);
    },

    stop: function() {
        $clear(this.playback);
    },

    slideIt: function(dir) {
        var curItem = this.items[this.itemNum];

        if (this.itemNum < (this.items.length - 1)) {
            this.itemNum++;
        } else {
            this.itemNum = 0;
        }

        var newItem  = this.items[this.itemNum];
        var item_in  = new Fx.Morph(newItem, this.options.morph);
        var item_out = new Fx.Morph(curItem, this.options.morph);

        if ('right' == dir) {
            item_in.start({'left': [this.width, 0], 'opacity': [0, 1]});
            item_out.start({'left': -this.width, 'opacity': 0});
        } else {
            item_in.start({'left': [-this.width, 0], 'opacity': [0, 1]});
            item_out.start({'left': this.width, 'opacity': 0});
        }
    }
});