jQuery(function() {

    tinymce.create("tinymce.plugins.thememakers_tiny_shortcodes",
            {
                init: function(ed, url)
                {

                    ed.addCommand("thememakers_shortcode_tiny_popup", function(a, params)
                    {
                        jQuery('body').append('<div id="thememakers_shortcode_tiny_popup"></div>');
                        var dialog = jQuery("#thememakers_shortcode_tiny_popup");

                        var data = {
                            action: "app_shortcodes_get_shortcode_template",
                            shortcode_name: params.identifier
                        };
                        jQuery.post(ajaxurl, data, function(html) {

                            jQuery(dialog).html(html);

                            jQuery(dialog).dialog({
                                autoOpen: false,
                                width: 800,
                                height: 600,
                                position: [200, 200],
                                zIndex: 1,
                                stack: true,
                                title: params.title,
                                buttons: {
                                    "Preview": function() {
                                        var shortcode = thememakers_app_shortcodes.get_html_from_buffer();
                                        var preview_IFrame = document.getElementById('thememakers_shortcode_template_preview');
                                        thememakers_app_shortcodes.show_static_info_popup("Updating Preview ...");

                                        var data = {
                                            action: "app_shortcodes_base64_encode",
                                            string: shortcode
                                        };
                                        jQuery.post(ajaxurl, data, function(responce) {
                                            var preview_url = thememakers_app_shortcodes_preview_url + "?shortcode=" + responce;
                                            jQuery(preview_IFrame).attr('src', preview_url);
                                            thememakers_app_shortcodes.hide_static_info_popup();
                                        });

                                    },
                                    "Insert": function() {

                                        var shortcode = thememakers_app_shortcodes.get_html_from_buffer();

                                        if (window.tinyMCE) {
                                            window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, shortcode);
                                        }

                                        jQuery(this).dialog("close");
                                    },
                                    "Cancel": function() {
                                        jQuery(this).dialog("close");
                                    }
                                },
                                close: function(event, ui) {
                                    jQuery(this).dialog("destroy");
                                    jQuery(dialog).remove();
                                }
                            });
                            jQuery(dialog).dialog('open');

                        });

                    });
                },
                createControl: function(btn, e)
                {

                    if (btn == "thememakers_shortcodes")
                    {

                        var a = this;

                        // adds the tinymce button
                        btn = e.createMenuButton("thememakers_tinymce_button",
                                {
                                    title: "Insert Shortcode",
                                    image: thememakers_app_shortcodes_app_link + "/images/add_icon.png",
                                    icons: false
                                });

                        // adds the dropdown to the button
                        btn.onRenderMenu.add(function(c, b)
                        {
                            jQuery.each(thememakers_app_shortcodes_items, function(key, value) {
                                a.addWithPopup(b, value.name, value.key);
                            });

                        });

                        return btn;
                    }

                    return null;
                },
                addWithPopup: function(ed, title, id) {
                    ed.add({
                        title: title,
                        onclick: function() {
                            tinyMCE.activeEditor.execCommand("thememakers_shortcode_tiny_popup", false, {
                                title: title,
                                identifier: id
                            })
                        }
                    })
                },
                addImmediate: function(ed, title, sc) {
                    ed.add({
                        title: title,
                        onclick: function() {
                            tinyMCE.activeEditor.execCommand("mceInsertContent", false, sc)
                        }
                    })
                },
                getInfo: function() {
                    return {
                        longname: 'realmag777 Shortcodes',
                        version: "1.0.1"
                    }
                }
            });

    tinymce.PluginManager.add("thememakers_tiny_shortcodes", tinymce.plugins.thememakers_tiny_shortcodes);

});
