var SlideShow = new Class({

    els: [],
    waiting: 0,
    timer: 0,
    ctr: 0,
    looping: false,

    Implements: Options,

    options: {
        delay:                  5000,
        duration:               2500,
        transition:             Fx.Transitions.Cubic.easeIn,
        itemClass:              '.promotion',
        controls:               false
    },

    initialize: function(els, options) {
        this.setOptions(options);
        this.waiting            = 0;
        this.el                 = new Element('div')
            .setProperty('id', 'slideshow')
            .setStyle('background-image', 'url(/img/common/spinner.gif)');
        if ($('flash-content')) {
            $('flash-content').empty().adopt(this.el);
        }
        $A(els).each(function(src) {
            var img             = new Element('img')
                .set('opacity', 0)
                .addEvent('load', function(evt) {
                    this.waiting--;
                }.bindWithEvent(this))
                .setProperty('src', src)
                .inject(this.el, 'bottom');
            this.els[this.els.length] = img;
            this.waiting++;
        }.bind(this));
        this.checkload();
    },

    checkload: function() {
        if (this.waiting > 0) {
            return this.checkload.delay(100, this);
        }
        this.el.setStyle('background-image', 'none')
        this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 1);
        return this.loop.delay(this.options.delay, this);
    },

    loop: function() {
        if (this.ctr > -1) {
            this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 0);
        }
        this.ctr++;
        if (this.ctr == this.els.length) {
            this.ctr = 0;
        }
        this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 1);
        return this.loop.delay(this.options.delay, this);
    }
});

window.addEvent('domready', function() {
    $$('a.external').each(function(link) {
        link.target = '_blank';
    });
});
