var bltSlideShow = new Class({
    initialize: function(el, thumb, options) {
        this.element = $(el);
        this.img;
        this.slides = [];
        this.thumb = $(thumb);
        this.current = null;
        this.ptr = 0;
        this.active = true;
        this.autoplay = true;
        this.timer = null;
        this.options = {fade: 500,
                        delay: 9000};

        loop = 0;

        this.element.setHTML('')
        this.element.fx = this.element.effect('height', {transition: Fx.Transitions.Quad.easeInOut,
                                                         onComplete: function() { this.current.start(1) }.bind(this)});

        $ES('a', this.thumb).each(function(thumb) {
            thumb.loop = loop;
            thumb.addEvent('click', function(evt) {
                evt = new Event(evt).stop();
                this.goTo(evt.target.getParent().loop);
            }.bindAsEventListener(this));
            var img = new Element('img').setProperty('src', thumb.getProperty('rel'))
                                        .setProperty('alt', $E('img', thumb).getProperty('alt'))
                                        .setStyle('position', 'absolute')
                                        .setStyle('top', '0px')
                                        .setStyle('left', '0px')
                                        .injectInside(this.element);
            var slide = new Fx.Style(img.setStyles({'position':'absolute',
                                                    'left':'0px',
                                                    'right':'0px',
                                                    'opacity':'0',
                                                    'display':'block'}),
                                     'opacity',
                                     {duration: this.options.fade,
                                      onComplete: this._captionUpdate.pass([false], this)});
            this.slides[loop] = slide;
            loop++;
        }.bind(this));
        
        this.captionpnl = new Element('div').setProperty('id', 'gallery_caption')
                                            .setStyle('position', 'absolute')
                                            .setStyle('height', '0px')
                                            .setStyle('overflow', 'hidden');
        this.captiontxt = new Element('div').setStyle('opacity', 0.8)
                                            .injectInside(this.captionpnl);
        this.captionpnl.fx = this.captionpnl.effect('height');
        this.captionpnl.injectInside(this.element);

        this.start.delay(100, this);
    },
    
    start: function() {
        this._reset();
        this.active = true;
        this.next();
    },
    
    stop: function() {
        this._reset();
        this.slides[this.ptr].stop();
        this.active = false;
    },
    
    next: function() {
        if (this.current) {
            this.current.start(0);
        }
        this.current = this.slides[this.ptr];
        this.current.element.setStyle('left', ((426 / 2) - (this.current.element.width / 2)) + 'px');
        this.element.fx.start(this.current.element.height);
        this.ptr++;
        if (this.ptr >= this.slides.length) {
            this.ptr = 0;
        }
        this._captionUpdate(true);
        if (this.autoplay) {
            this.timer = this.next.delay(this.options.delay, this);
        }
    },
    
    goTo: function(el) {
        this.ptr = el;
        this.autoplay = false;
        this.start();
    },
    
    _reset: function() {
        if (this.timer) {
            $clear(this.timer);
            this.timer = null;
        }
    },
    
    _captionUpdate: function(close) {
        this.captionpnl.fx.stop();
        caption = this.slides[this.ptr].element.alt;
        if (close) {
            if (caption) {
                this.captionpnl.fx.start(0);
            }
        } else if (this.active && caption) {
            this.captiontxt.setHTML(caption);
            this.captionpnl.fx.start(this.captiontxt.offsetHeight);
        }
    }
});
