document.lightbox = {
    locked: false,

    overlay: null,
    outer: null,
    inner: null,
    frame: null,
    reloads: 0,
    height: 0,
    width: 0,

    closeButton: null,

    init: function() {
        this.overlay = new Element('div', {'class': 'lightbox-overlay'});
        this.overlay.hide();
        this.overlay.observe('click', document.lightbox.closeHandler);

        this.inner = new Element('div', {'class': 'lightbox-inner'});
        this.inner.hide();

        this.closeButton = new Element('img', {'src':   '/media/images/layout/lightbox-close.png',
                                               'alt':   'X',
                                               'title': 'sluiten',
                                               'class': 'lightbox-close'});

        this.closeButton.observe('click', document.lightbox.closeHandler);

        this.frame = new Element('iframe', {'class': 'lightbox-frame',
                                            'frameborder': '0',
                                            'scrolling': 'no'});

        $$('body')[0].insert(this.overlay);
        this.overlay.insert(this.inner);
        this.inner.insert(this.closeButton);
        this.inner.insert(this.frame);

        this.attachObservers();
        this.setOverlayDimensions();
    },

    attachObservers: function() {
        $$('a[rel="lightbox"]').each(function(element) {
            element.observe('click', function(event) {
                if (Prototype.Browser.IE && navigator.userAgent.match(/MSIE 6\.0/)) {
                    window.location = this.href;
                } else {
                    document.lightbox.open(this.href);
                }

                Event.stop(event);
            });
        });

        document.lightbox.frame.observe('load', document.lightbox.setFrameDimensions);
        document.lightbox.frame.observe('load', function() { document.lightbox.inner.show(); });
        document.lightbox.frame.observe('load', document.lightbox.setFrameObservers);
    },

    setOverlayDimensions: function() {
        var overlayHeight = (
            document.viewport.getHeight()
            - parseInt(document.lightbox.overlay.getStyle('padding-top'))
            - parseInt(document.lightbox.overlay.getStyle('padding-bottom'))
        );

        if (overlayHeight > 0) {
            document.lightbox.overlay.setStyle({'height': overlayHeight + 'px'});
        }

        document.lightbox.inner.setStyle({
            'left': Math.round(
                document.viewport.getWidth() / 2
                - parseInt(document.lightbox.inner.getWidth()) / 2
            ) + 'px'
        });
    },

    getInnerDimensions: function() {
        if (! Prototype.Browser.IE) {
            bodyElement = $(document.lightbox.frame.contentWindow.document.getElementsByTagName('html')[0]);
        } else {
            bodyElement = $(document.lightbox.frame.contentWindow.document.getElementsByTagName('body')[0]);
        }

        if (navigator.userAgent.match(/Firefox/)) {
            bodyElement.style.overflow = "scroll";

            var width  = 0;
            var height = 0;
            var loop = setInterval(function() {
                bodyElement.style.overflow = "auto";
                if (width != 0) {
                    document.lightbox.setFrameDimensions([width, height], true);
                    window.clearInterval(loop);
                    return false;
                }
                width  = bodyElement.scrollWidth;
                height = bodyElement.scrollHeight;
            }, 100);
        } else if (Prototype.Browser.IE && navigator.userAgent.match(/MSIE 7\.0/)) {
            if (bodyElement.scrollWidth == 0) {
                width = 1000;
            } else {
                width = bodyElement.scrollWidth;
            }
            if (bodyElement.scrollHeight == 0) {
                height = 1000;
            } else {
                height = bodyElement.scrollHeight;
            }
        } else {
            width  = bodyElement.scrollWidth;
            height = bodyElement.scrollHeight;
        }

        return [width,height];
    },

    setFrameDimensions: function(dimensions, preset) {
        if (preset != true) {
            dimensions = document.lightbox.getInnerDimensions();
            if (navigator.userAgent.match(/Firefox/)) {
                return;
            }
        }

        document.lightbox.width  = dimensions[0];
        document.lightbox.height = dimensions[1];

        document.lightbox.frame.setStyle({
            'width': document.lightbox.width + 'px',
            'height': document.lightbox.height + 'px'
        });

        document.lightbox.inner.setStyle({
            'left': Math.round(document.viewport.getWidth() / 2 - document.lightbox.width / 2) + 'px',
            'width': document.lightbox.width + 10 + 'px',
            'height': document.lightbox.height + 10 + 'px'
        });

        document.lightbox.frame.observe('load', function() { document.lightbox.inner.show(); });
    },
    
    setFrameObservers: function() {
        if ($(document.lightbox.frame.contentWindow.document.getElementsByTagName('body')[0]).innerHTML != '') {
            $(document.lightbox.frame.contentWindow.document.getElementsByTagName('body')[0])
                .select('form select')
                .each(function(element) {
                    element.observe('change', document.lightbox.setFrameDimensions);
                });
        }
    },

    open: function(url) {
        document.lightbox.overlay.show();
        document.lightbox.frame.src = url + (url.indexOf('?') != -1 ? '&' : '?') + 'lightbox=true';

        $$('html')[0].style.overflowY = 'hidden';
    },

    close: function() {
        document.lightbox.overlay.hide();
        document.lightbox.frame.setStyle({'width': 0, 'height': 0});

        $$('html')[0].style.overflowY = 'scroll';
    },

    closeHandler: function(event) {
        if (event.target != this) return;

        document.lightbox.close();

        Event.stop(event);
    }
};

DOMReady.add(function() { document.lightbox.init(); }, false);
Event.observe(window, 'resize', document.lightbox.setOverlayDimensions);
