var Lightbox = new Class({
    Implements: [Options, Events],
    
    overlay: null,
    content: null,
        closeWrapper: null,
            title: null,
        close: null,
        body: null,
    showing: false,
    
    options: {},
    
    initialize: function (options) {
        this.setOptions(options);
    },
    
    show: function () {
        if(this.showing) return;
        this.showing = true;
        
        var calls = {
            overlay: function () {
                this.overlay = new Element('div', {
                    styles: {
                        opacity: 0,
                        position: 'absolute', left: 0, top: 0,
                        width: window.getScrollSize().x, height: window.getScrollSize().y
                    },
                    morph: {
                        duration: 'short',
                        onComplete: calls.content.bind(this)
                    }
                }).inject($(document.body)).addClass('lightbox-overlay').morph({opacity: 0.6});
            },
            
            content: function () {
                this.content = new Element('div', {
                    styles: {
                        opacity: 0,
                        position: 'absolute', top: (Window.getScrollTop() + 100) + "px"
                    },
                    morph: {
                        duration: 'short',
                        onComplete: calls.done.bind(this)
                    }
                }).inject($(document.body)).set('id', 'lightbox-content').addClass('lightbox-content').morph({opacity: 1});
            },
            
            done: function () {
                this.closeWrapper = new Element('div').inject(this.content).addClass('lightbox-close-wrapper');
                this.title = new Element('span').inject(this.closeWrapper);
                this.title = new Element('a', {
                    'href':'', 
                    'target': '_blank',
                    'id': 'lightbox-download-now'
                }).inject(this.closeWrapper);
                new Element('div').inject(this.closeWrapper).addClass('clear');
                this.close = new Element('div', {
                    events: {
                        click: this.hide.bind(this)
                    },
                    text: 'X'
                }).inject(this.closeWrapper).addClass('lightbox-close');
                this.body = new Element('div', {
                }).inject(this.content).addClass('lightbox-body');
                
                this.fireEvent('show');
            }
        };
        
        calls.overlay.run([], this);
        return this;
    },
    
    hide: function () {
        var calls = {
            content: function () {
                this.content.empty();
                this.content.set({
                    morph: {
                        duration: 'short',
                        onComplete: calls.overlay.bind(this)
                    }
                }).morph({opacity: 0});
            },
            
            overlay: function () {
                this.overlay.set({
                    morph: {
                        duration: 'short',
                        onComplete: calls.done.bind(this)
                    }
                }).morph({opacity: 0});
            },
            
            done: function () {
                this.overlay.dispose();
                this.content.dispose();
                
                this.overlay = null;
                this.content = null;
                this.showing = false;
                
                this.fireEvent('close');
            }
        };
        
        calls.content.run([], this);
        return this;
    }
});
