var _SWFPOPUP;
var SWFPopup = new Class({

    width: 1024,
    height: 768,
    sWidth: 1280,
    sHeight: 1024,

    _shader: null,
    _curPopup: null,
    _uid: null,

    initialize: function() {

        this._shader = new Element('div').setStyles({
            position: 'absolute',
            top: '0px',
            left: '0px',
            background: '#ffffff',
            opacity: 0.9,
            display: 'none',
            'z-index': 100
        });

        this._updateSize();

        $$('body').adopt(this._shader);

        this._handleLinks();
    },

    _updateSize: function() {
        this.width = window.getWidth() - 50;
        this.height = window.getHeight() - 50;

        if (this._curPopup != null) {
            size = {width: this.width + 'px', height: this.height + 'px'};
            this._curPopup.setStyles(size);
            //$(this._uid).setStyles(size);
        }

        //Update scrollwidth after popup resize
        this._updateShaderSize.bind(this).delay(150);
    },

    _updateShaderSize: function() {
        this.sWidth = window.getScrollWidth();
        this.sHeight = window.getScrollHeight();

        this._shader.setStyles({
           width:   this.sWidth + 'px',
           height:  this.sHeight + 'px'
        });
    },

    _handleLinks: function() {
        links = $$('.swfpopup');
        links.addEvent('click', function(e) {
            e.stop();
            this.showPopup(e.target);
        }.bind(this));
    },

    showPopup: function(link) {
        this.closePopup();

        this._uid = '_swfp' + Math.floor(Math.random() * 10000000);

        this.showShader(true);
        this._curPopup =
            new Element('div').adopt(
                new Element('div', {id: this._uid})
            ).setStyles({
                position: 'absolute',
                top: '25px',
                left: '25px',
                width: this.width + 'px',
                height: this.height + 'px',
                background: 'transparent',
                'z-index': 200
            });

        $$('body').adopt(this._curPopup);
        swfobject.embedSWF(link.get('href'), this._uid, this.width, this.height, "9.0.0", null, null, {wmode: 'transparent', allowFullScreen: 'true', scale: 'showAll'});

        window.scrollTo(0,0);
    },

    showUrlInPopup: function(link) {
        this.closePopup();

        this._uid = '_swfp' + Math.floor(Math.random() * 10000000);

        this.showShader(true);
        this._curPopup =
            new Element('div').adopt(
                new Element('div', {id: this._uid})
            ).setStyles({
                position: 'absolute',
                top: '25px',
                left: '25px',
                width: this.width + 'px',
                height: this.height + 'px',
                background: 'transparent',
                'z-index': 200
            });

        $$('body').adopt(this._curPopup);
        swfobject.embedSWF(link, this._uid, this.width, this.height, "9.0.0", null, null, {wmode: 'transparent', allowFullScreen: 'true', scale: 'showAll'});

        window.scrollTo(0,0);
    },


    closePopup: function() {
        this.showShader(false);
        if (this._curPopup != null) {
            this._curPopup.setStyle('display', 'none');
            (function() {
                this._curPopup.destroy();
                this._curPopup = null;
            }).bind(this).delay(1000);
        }
    },

    showShader: function(show) {
        this._shader.setStyle('display', show ? 'block' : 'none');
    }

});

function editorSluiten() {
    _SWFPOPUP.closePopup();
    location.reload(true);
}

window.addEvents({
    'domready': function() {
        _SWFPOPUP = new SWFPopup();
    },
    'resize': function() {
        if (_SWFPOPUP != null) _SWFPOPUP._updateSize();
    }
});

