(function() {

    const plugin_name = 'affiliation_amazon_plain';
    const action_name = 'affiliation_amazon_plain_action';
    
    tinymce.create('tinymce.plugins.' + plugin_name , {
        
        init : function(ed, url) {

                ed.addButton( plugin_name, {
                        title : 'Inserisci un Amazon Plain',
                        cmd : plugin_name + '_cmd',
                        icon: 'table',
                        image : url + '/assets/affiliation-amazon-plain.svg'
                });

                var post_ID = (document.getElementById("post_ID")) ? document.getElementById("post_ID").value : 0;

                ed.addCommand(plugin_name + '_cmd', 
                        
                        function() {
                                
                                ed.windowManager.open({                                        
                                        file : `${ajaxurl}?action=${action_name}&post_type=${typenow}&post_id=${post_ID}`,                                        
                                        width  : 650,
                                        height : 400,
                                        inline : 1
                                }, {
                                        plugin_url : url,
                                });
                        }
                );

                ed.onNodeChange.add(function(ed, cm, n) {
                        cm.setActive(plugin_name, n.nodeName == 'IMG');
                });
        },

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

        getInfo : function() {
                return {
                        longname : plugin_name + ' plugin',
                        author : 'Some author',
                        authorurl : 'http://tinymce.moxiecode.com',
                        infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/' + plugin_name,
                        version : "1.0"
                };
        }
    });
    
    tinymce.PluginManager.add(plugin_name, tinymce.plugins[plugin_name]);

})();
