<?php
/**
 * Catch Flames Pro Custom meta box
 *
 * @package Catch Themes
 * @subpackage Catch Flames Pro
 * @since Catch Flames Pro 1.0
 */
 
 // Add the Meta Box  
function catchflames_add_custom_box() {
	add_meta_box(
		'siderbar-layout',									//Unique ID
       __( 'Catch Flames Options', 'catch-flames' ),	//Title
        'catchflames_sidebar_layout',					//Callback function
        'page'												//show metabox in pages
    ); 
	add_meta_box(
		'siderbar-layout',									//Unique ID
       __( 'Catch Flames Options', 'catch-flames' ),	//Title
        'catchflames_sidebar_layout',					//Callback function
        'post'												//show metabox in pages
    ); 
}

add_action( 'add_meta_boxes', 'catchflames_add_custom_box' );


//Header Featured Image Options
global $header_image_options;
$header_image_options = array(
	'default' => array(
		'id'		=> 'catchflames-header-image',
		'value' 	=> 'default',
		'label' 	=> __( 'Default', 'catch-flames' ),
	),
	'enable' => array(
		'id'		=> 'catchflames-header-image',
		'value' 	=> 'enable',
		'label' 	=> __( 'Enable', 'catch-flames' ),
	),	
	'disable' => array(
		'id'		=> 'catchflames-header-image',
		'value' 	=> 'disable',
		'label' 	=> __( 'Disable', 'catch-flames' )
	)
);


global $sidebar_layout;
$sidebar_layout = array(
	'default-sidebar'		=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'default',
		'label' 			=> sprintf( __( 'Default Layout Set in <a href="%s">Theme Settings</a>', 'catch-flames' ), esc_url( admin_url('admin.php?page=theme_options' ) ) ),
		'thumbnail'			=> ' '
	),
	'three-columns'			=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'three-columns',
		'label'				=> __( 'Three Columns (Style 1)', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/three-columns.png'
	),
	'three-columns-sidebar'	=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'three-columns-sidebar',
		'label'				=> __( 'Three Columns (Style 2)', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/three-columns-sidebar.png'
	),
	'three-columns-equal'	=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'three-columns-equal',
		'label'				=> __( 'Three Columns (Style 3)', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/three-columns-equal.png'
	),	
	'right-sidebar'			=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'right-sidebar',
		'label'				=> __( 'Right sidebar', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/right-sidebar.png'
	),
	'left-sidebar'			=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'left-sidebar',
		'label'				=> __( 'Left sidebar', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/left-sidebar.png'
	),	
	'no-sidebar-one-column'	=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'no-sidebar-one-column',
		'label'				=> __( 'No Sidebar, One Column', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/one-column.png'
	),	
	'no-sidebar-full-width' => array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'no-sidebar-full-width',
		'label'				=> __( 'No sidebar, Full Width', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/no-sidebar-fullwidth.png'
	),	
	'no-sidebar'			=> array(
		'id'				=> 'catchflames-sidebarlayout',
		'value'				=> 'no-sidebar',
		'label'				=> __( 'No sidebar', 'catch-flames' ),
		'thumbnail'			=> get_template_directory_uri() . '/inc/panel/images/no-sidebar.png'
	)
	
);
	

//Sidebar Options
global $sidebar_options;
$sidebar_options = array(
	'main-sidebar'				=> array(
		'id'					=> 'catchflames-sidebar-options',
		'value'					=> 'default-sidebar',
		'label'					=> __( 'Default Sidebar', 'catch-flames' )
	),
	'optional-sidebar-one'		=> array(
		'id'					=> 'catchflames-sidebar-options',
		'value'					=> 'optional-sidebar-one',
		'label'					=> __( 'Optional Sidebar One', 'catch-flames' )
	),
	'optional-sidebar-two'		=> array(
		'id'					=> 'catchflames-sidebar-options',
		'value'					=> 'optional-sidebar-two',
		'label'					=> __( 'Optional Sidebar Two', 'catch-flames' )
	),
	'optional-sidebar-three'	=> array(
		'id'					=> 'catchflames-sidebar-options',
		'value'					=> 'optional-sidebar-three',
		'label'					=> __( 'Optional Sidebar three', 'catch-flames' )
	)	
);


/**
 * @renders metabox to for sidebar layout
 */
function catchflames_sidebar_layout() {  
    global $header_image_options, $sidebar_layout, $sidebar_options, $post;   
    // Use nonce for verification  
    wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_nonce' );

    // Begin the field table and loop  ?>
    <div class="catchflames-meta sidebar-layout">
    	<div class="layout-css">
    		<h4 class="title"><?php _e('Sidebar Layout Options', 'catch-flames'); ?></h4>    
			<?php  
            foreach ($sidebar_layout as $field) {  
                $meta = get_post_meta( $post->ID, $field['id'], true );
                if(empty( $meta ) ){
                    $meta='default';
                }
                if( $field['thumbnail']==' ' ): ?>
                    <label class="description box-default">
                        <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $meta ); ?>/>&nbsp;&nbsp;<?php echo $field['label']; ?>
                    </label>
                <?php else: ?>
                    <label class="description box">
                        <span><img src="<?php echo esc_url( $field['thumbnail'] ); ?>" width="100" height="90" alt="" /></span></br>
                        <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $meta ); ?>/>&nbsp;&nbsp;<?php echo $field['label']; ?>
                    </label>
                <?php endif;
            } // end foreach 
            ?>
     	</div><!-- .layout-css -->
  	</div><!-- .catchflames-meta --> 
    
    <div class="catchflames-meta select-sidebar">
    	<h4 class="title"><?php _e('Select Sidebar', 'catch-flames'); ?></h4>   
        <table id="sidebar-select" class="form-table" width="100%">
            <tbody> 
                <tr>
                    <?php  
                    foreach ($sidebar_options as $field) {  
                        
                        $metasidebar = get_post_meta( $post->ID, $field['id'], true );
                        
                        if (empty( $metasidebar ) ){
                            $metasidebar='default-sidebar';
                        } ?>
                        <td style="max-width: 100px; vertical-align: top;">
                            <label class="description">
                                <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $metasidebar ); ?>/>&nbsp;&nbsp;<?php echo $field['label']; ?>
                            </label>
                        </td>
                        
                        <?php
                    } // end foreach 
                    ?>
                </tr>
            </tbody>
        </table>        
	</div><!-- .catchflames-meta -->  
    
     <div class="catchflames-meta" style="border-bottom: 2px solid #dfdfdf; margin-bottom: 10px; padding-bottom: 10px;">
    	<h4 class="title"><?php _e('Header Featured Image Options', 'catch-flames'); ?></h4>  
        <table id="featuedimage-metabox" class="form-table" width="100%">
            <tbody> 
                <tr>                
                    <?php  
                    foreach ($header_image_options as $field) { 
					
					 	$metaheader = get_post_meta( $post->ID, $field['id'], true );
                        
                        if (empty( $metaheader ) ){
                            $metaheader='default';
                        } ?>
                        
                        <td style="width: 100px;">
                            <label class="description">
                                <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $metaheader ); ?>/>&nbsp;&nbsp;<?php echo $field['label']; ?>
                            </label>
                        </td>
                        
                        <?php
                    } // end foreach 
                    ?>
                </tr>
            </tbody>
        </table>          
	</div><!-- .catchflames-meta -->     
             
<?php 
}


/**
 * save the custom metabox data
 * @hooked to save_post hook
 */
function catchflames_save_custom_meta( $post_id ) {  
	global $header_image_options, $sidebar_layout, $sidebar_options, $post;
	
	// Verify the nonce before proceeding.
    if ( !isset( $_POST[ 'custom_meta_box_nonce' ] ) || !wp_verify_nonce( $_POST[ 'custom_meta_box_nonce' ], basename( __FILE__ ) ) )
        return;
		
	// Stop WP from clearing custom fields on autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE)  
        return;
		
	if ('page' == $_POST['post_type']) {  
        if (!current_user_can( 'edit_page', $post_id ) )  
            return $post_id;  
    } elseif (!current_user_can( 'edit_post', $post_id ) ) {  
            return $post_id;  
    }  
	
	foreach ( $header_image_options as $field ) {  
		//Execute this saving function
		$old = get_post_meta( $post_id, $field['id'], true); 
		$new = $_POST[$field['id']];
		if ($new && $new != $old) {  
			update_post_meta($post_id, $field['id'], $new);  
		} elseif ('' == $new && $old) {  
			delete_post_meta($post_id, $field['id'], $old);  
		} 
	 } // end foreach 
	 
	foreach ($sidebar_layout as $field) {  
		//Execute this saving function
		$old = get_post_meta( $post_id, $field['id'], true); 
		$new = $_POST[$field['id']];
		if ($new && $new != $old) {  
			update_post_meta($post_id, $field['id'], $new);  
		} elseif ('' == $new && $old) {  
			delete_post_meta($post_id, $field['id'], $old);  
		} 
	 } // end foreach   
	 
	foreach ( $sidebar_options as $field ) {  
		//Execute this saving function
		$old = get_post_meta( $post_id, $field['id'], true); 
		$new = $_POST[$field['id']];
		if ($new && $new != $old) {  
			update_post_meta($post_id, $field['id'], $new);  
		} elseif ('' == $new && $old) {  
			delete_post_meta($post_id, $field['id'], $old);  
		} 
	 } // end foreach 
	 
}
add_action('save_post', 'catchflames_save_custom_meta'); 