(function(){
	// creates the plugin
	var PluginUrl;
	tinymce.create('tinymce.plugins.perSAAS_editor_button', {
		init : function(ed, url) {
			PluginUrl = url;
			 ed.addButton( 'perSAAS_editor_button', {
                    title : 'Insert shortcode',
                    image : PluginUrl + '/../images/hourglass_plus.png',
                    onclick : function() {

						tb_show( 'Select Campaign', '#TB_inline?width=500&height=400&inlineId=per_editor-form' );
						var content = tinyMCE.get('content').getContent();

                        addOptions();
                        
                    } 
               });
			},
			createControl : function(n, cm) {
               return null;
          },
		
	});
	
	// registers the plugin. DON'T MISS THIS STEP!!!
	tinymce.PluginManager.add('perSAAS_editor_button', tinymce.plugins.perSAAS_editor_button);
	
    function addOptions(){

        jQuery.ajax({
		  type: 'GET',
		  url: 'admin-ajax.php',
		  data: { action: 'get_SAAS_camps' },
		  success: function(response){
			
			jQuery("#TB_ajaxContent").html('');
			jQuery("#TB_ajaxContent").append(response);

		 }

		});

    }

	jQuery(document).on("click", "#insertShortcode", function(){

		var shortcode = jQuery("#perSAASShrt").val();

		var resshortcode = shortcode.split('_');

		var InsertSAAS = '[PERSAAS id="'+resshortcode[0]+'" user="'+resshortcode[1]+'"]';

		tinyMCE.activeEditor.execCommand('mceInsertContent', 0, InsertSAAS);

	});

})()
