/**
 * Callback function for pages
 */
function paf_page_cb()
{
    global $paf;
    global $paf_options, $paf_pages, $paf_sections, $paf_tabs;
    global $paf_page_tabs, $paf_page_sections, $paf_page_options;
    global $paf_page, $paf_tab;
    // Get submit button text (looks in: tab > page > default )
    if ($paf_tab && ($submit_button_text = K::get_var('submit_button', $paf_page_tabs[$paf_tab]))) {
    } else {
        if ($submit_button_text = K::get_var('submit_button', $paf_pages[$paf_page])) {
        } else {
            $submit_button_text = __('Save Changes');
        }
    }
    // Get reset button text (looks in: tab > page )
    if ($paf_tab && ($reset_button_text = K::get_var('reset_button', $paf_page_tabs[$paf_tab]))) {
    } else {
        if ($reset_button_text = K::get_var('reset_button', $paf_pages[$paf_page])) {
        }
    }
    // Start output
    echo '<div class="wrap">' . '<h2>' . K::get_var('title', $paf_pages[$paf_page], $paf_page) . '</h2>';
    // Print tabs links
    if ($paf_page_tabs) {
        echo '<h2 class="nav-tab-wrapper">';
        foreach ($paf_page_tabs as $slug => $page_tab) {
            printf('<a href="?page=%s&amp;tab=%s" class="nav-tab %s">%s</a>', $paf_page, $slug, $paf_tab === $slug ? 'nav-tab-active' : '', $page_tab['menu_title']);
        }
        echo '</h2>';
        echo '<h2>' . $paf_page_tabs[$paf_tab]['title'] . '</h2>';
    }
    // Print the options
    echo '<form id="paf-form" class="hidden" action="' . paf_url() . '" method="post">';
    // Show options that don't have sections
    reset($paf_page_options);
    foreach ($paf_page_options as $id => $page_option) {
        if (K::get_var('section', $page_option)) {
            continue;
        }
        paf_print_option($id);
    }
    // Show options that have sections
    reset($paf_page_options);
    foreach ($paf_page_sections as $section_id => $page_section) {
        K::wrap(K::get_var('title', $page_section, $section_id), array('class' => 'title'), array('in' => 'h3'));
        foreach ($paf_page_options as $id => $page_option) {
            if ($section_id === K::get_var('section', $page_option)) {
                paf_print_option($id);
            }
        }
    }
    // Nonce
    wp_nonce_field('paf_save', 'paf_nonce');
    // Submit and Reset buttons
    echo '<p>';
    K::input('paf_submit', array('class' => 'button button-large button-primary', 'href' => '#', 'id' => 'paf-submit', 'type' => 'submit', 'value' => $submit_button_text), array('in' => 'input'));
    if ($reset_button_text) {
        echo ' ';
        K::wrap('Reset', array('class' => 'button button-large paf-reset', 'href' => '#', 'id' => 'paf-reset'), array('in' => 'a'));
    }
    echo '</p>';
    echo '</form>';
    // Add JS and CSS
    paf_asset_js('paf', TRUE);
    paf_asset_css('paf', TRUE);
    echo '</div>';
}
function skelet_tinyMCE_php($tag)
{
    global $paf_shortcodes;
    $select2_enqueued = false;
    $protocol = is_ssl() ? 'https' : 'http';
    // Head
    printf('<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title>');
    // CSS
    printf('<link rel="stylesheet" href="%s" />', admin_url('css/common' . (is_rtl() ? '-rtl' : '') . '.css'));
    printf('<link rel="stylesheet" href="%s" />', admin_url('css/forms' . (is_rtl() ? '-rtl' : '') . '.css'));
    printf('<link rel="stylesheet" href="%s" />', site_url('wp-includes/css/dashicons' . (is_rtl() ? '-rtl' : '') . '.min.css'));
    printf('<link rel="stylesheet" href="%s" />', site_url('wp-includes/css/buttons' . (is_rtl() ? '-rtl' : '') . '.min.css'));
    print '<style>body { height: auto; margin: 0; min-width: 0; padding: 1em; }</style>';
    paf_asset_css('paf');
    // JS
    printf('<script src="%s"></script>', "{$protocol}://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js");
    printf('<script src="%s"></script>', "{$protocol}://cdnjs.cloudflare.com/ajax/libs/jquery.serializeJSON/1.2.0/jquery.serializeJSON.min.js");
    paf_asset_js('paf');
    ob_start();
    ?>
	<script>
		var paf;
		var shortcode = '';
		var wrap = <?php 
    echo K::get_var('wrap', $paf_shortcodes[$tag]) ? 'true' : 'false';
    ?>
;

		jQuery( document ).ready( function ( $ ) {

			/* Fill content field with selected text if any */
			var content = parent.tinymce.activeEditor.selection.getContent( { format : 'text' } );
			$( '#content' ).val( content );

			// Update shortcode
			$( 'input,select,textarea', 'form' ).on( 'change keyup', function() { $( 'form' ).change(); } );

			// Autoselect shortcode
			$( "#shortcode" ).mouseover( function() { $( this ).select(); } );

			/**
			 * Bind to form events
			 *
			 * - On submit: Fill the WP editor
			 * - On change: update the shortcode value
			 */
			$( 'form' ).on( 'submit change', function( e ) {

				e.preventDefault();

				shortcode = '';
				paf = $( this ).serializeJSON().paf;
				content = $( '#content' ).val();

				if( 'undefined' === typeof paf ) {
					paf = {};
				}

				// Build the shortcode
				Object.keys( paf ).map( function(v) {
					if( 'undefined' !== paf[ v ] && paf[ v ] ) {
						shortcode += ' '
							+ v
							+ '="'
							+ paf[ v ].toString().split().join().replace( '"', '\\"', 'g' )
							+ '"'
						;
					}
				} );

				if( wrap ) {
					shortcode = "[<?php 
    echo $tag;
    ?>
" + shortcode + "]"
						+ content
						+ "[/<?php 
    echo $tag;
    ?>
]"
					;
				} else {
					shortcode = "[<?php 
    echo $tag;
    ?>
" + shortcode + "]";
				}

				// Update the demo
				$( "#shortcode" ).val( shortcode );

				// Fill the editor and close
				if ( 'submit' === e.type ) {
					parent.tinymce.activeEditor.execCommand( "mceInsertContent", false, shortcode );
					parent.tinymce.activeEditor.windowManager.close( window );
				}
			} ).change();
		} );
	</script>
	<?php 
    print JSMin::minify(ob_get_clean());
    // Close head
    print '</head><body class="wp-core-ui">';
    // Fields
    $parameters = $paf_shortcodes[$tag]['parameters'];
    echo '<form id="paf-form" action = "">';
    foreach ($parameters as $k => $v) {
        // Validate title
        $v['title'] = k::get_var('title', $v, $k);
        // Print option
        paf_print_option($k, $v);
        if ('select' === K::get_var('type', $v)) {
            printf('<link rel="stylesheet" href="%s" />', "{$protocol}://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css");
            printf('<script src="%s"></script>', "{$protocol}://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js");
            print "<script>jQuery( document ).ready( function( \$ ) { \$( 'select' ).select2(); } );</script>";
            $select2_enqueued = true;
        }
    }
    // Content field
    if (K::get_var('wrap', $paf_shortcodes[$tag])) {
        K::textarea('content', array('class' => 'large-text', 'id' => 'content'), array('format' => '<p><label><strong>Content:</strong>:textarea</label></p>'));
    }
    // Buttons
    echo '<hr />';
    echo '<p><label><strong>Shortcode:</strong><input type="text" class="large-text" id="shortcode" value=""/></label></p>';
    echo '<hr />';
    echo '<p>';
    K::input('submit', array('class' => 'button button-large button-primary aligncenter	', 'type' => 'submit', 'value' => 'Add shortcode'));
    echo ' ';
    K::wrap('Reset', array('class' => 'button button-large paf-reset', 'href' => '#', 'id' => 'paf-reset'), array('in' => 'a'));
    echo '</p>';
    // Close document
    echo '</form></body></html>';
    // C ya!
    return ob_get_clean();
}