/**
 * Initialises the Pexels modal popup by registering a button
 * in the TinyMCE instance.
 *
 * @since 	1.0.0
 */
( function() {

	tinymce.PluginManager.add( 'page_generator_pro_pexels', function( editor, url ) {

		// Add Button to Visual Editor Toolbar
		editor.addButton( 'page_generator_pro_pexels', {
			title: 	'Insert Pexels Image',
			image: 	url + '../../../images/icons/pexels.svg',
			cmd: 	'page_generator_pro_pexels',
		} );	

		// Load View when button clicked
		editor.addCommand( 'page_generator_pro_pexels', function() {
			// Open the TinyMCE Modal
			editor.windowManager.open( {
				id: 	'page-generator-pro-modal-body',
				title: 	'Insert Pexels Image',
                width: 	800,
                height: 800,
                inline: 1,
                buttons:[],
            } );

			// Perform an AJAX call to load the modal's view
			jQuery.post( 
	            ajaxurl,
	            {
	                'action': 	'page_generator_pro_output_tinymce_modal',
	                'shortcode':'pexels'
	            },
	            function( response ) {
	            	// Inject HTML into modal
	            	jQuery( '#page-generator-pro-modal-body-body' ).html( response );
	            	
	            	// Initialize tabbed interface
	            	wp_zinc_tabs_init();

	            	// Reload autocomplete instances
	            	wp_zinc_autocomplete_initialize();

	            	// Initialize conditional fields
	            	page_generator_pro_conditional_fields_initialize();
	            	jQuery( 'select.wpzinc-conditional, .wpzinc-conditional select' ).trigger( 'change' );
	            }
	        );
		} );
	} );

} )();