/** Modal manager, adomas@8magic.com */

var ModalManager = new Class({
    /** Whether modal window is now open. */
    opened: false,
    initialize: function() {
        this.linkify();
        this.bindEvents();
    },
    /** Bind common events. */
    bindEvents: function() {
        window.addEvent('keydown', function(ev) {
            ev = new Event(ev);
            if (ev.key == 'esc' && this.close()) {
                ev.preventDefault();
                ev.stopPropagation();
            }
        }.bind(this));        
    },
    /** Load given URL into modal frame. */
    load: function(url) {
        var fn = function(url) {
            this.getFrame().src = url;
        }.pass(url, this);
        var opened = this.opened;
        this.opened = true;
        $(window).scrollTo(0, 0);
        opened ? fn() : this.createNodes(fn);
    },
    /** Clicked on a link. Load href into modal window. */
    linkLoad: function(ev, href) {
        ev = new Event(ev);
        ev.preventDefault();
        ev.stopPropagation();
        this.load(href);
    },
    /** Resize model window to given height. 
        Called from inner document mostly. */
    resize: function(height) {
        if (this.opened) {            
            this.getOverlay().setStyle('min-height', height);
            this.getFrame().setStyle('height', height);
            this.getWrapper().addClass('resized'); /* indicate */
            this.removeIndicator();
        }
    },
    /** Close modal window. */
    close: function() {
        if (this.opened) {
            this.opened = false;
            this.getFrame().remove();
            this.getWrapper().remove();
            this.removeIndicator();
            new Fx.Style('modal-overlay', 'opacity', {
                duration: 200,
                onComplete: function() {
                    this.getOverlay().remove()
                }.bind(this)
            }).start(0.5, 0);
            return true;
        }
        return false;
    },
    /** Modify all <a class="modal"> to open using modal windows. */
    linkify: function() {
        $ES('a.modal').each(function(a) {
            /* linkify() will be called multiple times on same
               document, so don't waste time with removing/adding
               events. Use the old way, Luke. */
            var href = a.getProperty('href');
            a.onclick = this.linkLoad.bindAsEventListener(this, href);
        }.bind(this));
    },
    /** Get iframe. */
    getFrame: function() {
        return $('modal-frame');
    },
    /** Get iframe wrapper. */
    getWrapper: function() {
        return $('modal-frame-wrapper');
    },
    /** Get loading indicator. */
    getIndicator: function() {
        return $('modal-loading-indicator');
    },
    /** Get overlay. */
    getOverlay: function() {
        return $('modal-overlay');        
    },
    /** Remove loading indicator. */
    removeIndicator: function() {
        var indicator = this.getIndicator();
        !indicator || indicator.remove();
    },
    /** Create required nodes, e.g. loading indicator, iframe. */
    createNodes: function(onComplete) {
        var frameWrapper = new Element('div', { 'id': 'modal-frame-wrapper' });
        var top = new Element('div').addClass('top').addClass('pngfix').injectInside(frameWrapper);
        var frame = new Element('iframe', { 
            'id': 'modal-frame',
            'frameborder': '0', // die redmond
            'hspace': '0',
            'scrolling': 'no'
        }).injectInside(top);
        var bottom = new Element('div').addClass('bottom').addClass('pngfix').injectInside(frameWrapper);
        var overlay = new Element('div', {
            'id': 'modal-overlay',
            'events': { 'click': this.close.bindAsEventListener(this) },
            'styles': { 'opacity': 0 }
        });
        var indicator = new Element('div', {
            'id': 'modal-loading-indicator'
        });
        $(document.body).adopt([frameWrapper, overlay, indicator]);
        new Fx.Style('modal-overlay', 'opacity', {
            duration: 200,
            onComplete: onComplete
        }).start(0, 0.5);        
    }
});
window.addEvent('domready', function() {
    window.MODAL_MANAGER = new ModalManager();
});
