(function () {
    tinymce.create('tinymce.plugins.wpfilebase', {
        init: function (ed, url) {
            ed.addButton('wpfbInsertTag', {
                title: 'WP-Filebase',
                image: url + '/images/btn.gif',
                onclick: (function () {
                    var postId = '';
                    try {
                        postId = tinymce.DOM.get("post_ID").value;
                    } catch (ex) {
                    }
                    ed.windowManager.open(
                        {
                            file: (ajaxurl.replace("admin-ajax.php", "admin.php?wpfilebase-screen=editor-plugin").replace('editor-plugin?', 'editor-plugin&') + '&post_id=' + postId + '&content=' + encodeURIComponent(tinyMCE.activeEditor.selection.getContent())),
                            title: 'WP-Filebase',
                            width: Math.min(1000, document.body.clientWidth - 100),
                            height: (document.body.clientHeight - 100),
                            inline: 1
                        },
                        {plugin_url: url}
                    );
                })
            });
        },

        createControl: function (n, cm) {
            return null;
        },

        getInfo: function () {
            return {
                longname: 'WP-Filebase',
                author: 'Fabian Schlieper',
                authorurl: 'https://fabi.me/',
                infourl: 'https://fabi.me/wp-filebase/',
                version: '1.0'
            };
        }
    });
    tinymce.PluginManager.add('wpfilebase', tinymce.plugins.wpfilebase);
})();