<?php
$prefix = 'r_';

$meta_box_single = array(
	'id' => 'my-meta-box',
	'title' => 'Single post settings',
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
	
	
	    array(
			'name' => 'Post - Variant',
			'desc' => 'Select layout for post',
			'id' => $prefix . 'post_layout',
			'type' => 'select',
            "options" => array('One Sidebar', 'With Small Sidebar', 'Small Sidebar - Left'),
			'std' => ''
		),	
		
	    array(
			'name' => 'Custom post excerpt',
			'desc' => 'Write small text for excerpt of posts',
			'id' => $prefix . 'post_text',
			'type' => 'textarea',
			'std' => ''
		),
	
	    array(
			'name' => 'Thumbnail - Variant',
			'desc' => 'Select layout for post thumbnail',
			'id' => $prefix . 'post_thumb_var',
			'type' => 'select',
            "options" => array('Standard', 'Full width', 'Full width - Parallax'),
			'std' => ''
		),	

		array(
			'name' => 'Hide Thumbnail (Standard)',
			'id' => $prefix . 'post_thumbnail',
			'type' => 'checkbox',
			'desc' => 'Check the box to hide the post thumbnail'
		),
		
		array(
			'name' => 'Show Video',
			'id' => $prefix . 'post_video',
			'type' => 'checkbox',
			'desc' => 'Check the box to show video'
		),

		array(
			'name' => 'Youtube video',
			'desc' => 'Paste the video, only after the symbol "v =" (ex: http://www.youtube.com/watch?v=UJ1MOWg15Ec, UJ1MOWg15Ec - only this code)',
			'id' => $prefix . 'youtube',
			'type' => 'text',
			'std' => ''
		),
		
		array(
			'name' => 'Vimeo video',
			'desc' => 'Insert the video id (ex: http://vimeo.com/76709460, 76709460 - ID)',
			'id' => $prefix . 'vimeo',
			'type' => 'text',
			'std' => ''
		),
		
		array(
			'name' => 'Video time',
			'desc' => 'Write the video time (Only for Home Video posts shortcode)',
			'id' => $prefix . 'video_time',
			'type' => 'text',
			'std' => ''
		),
		
		array(
			'name' => 'Show Slider in post',
			'id' => $prefix . 'post_slider',
			'type' => 'checkbox',
			'desc' => 'Check the box to show Slider in single post'
		),
		
	    array(
			'name' => 'Slider image 1',
			'desc' => 'Insert the full path to your image',
			'id' => $prefix . 'slider_image_1',
			'type' => 'text',
			'std' => ''
		),
	
	    array(
			'name' => 'Slider image 2',
			'desc' => 'Insert the full path to your image',
			'id' => $prefix . 'slider_image_2',
			'type' => 'text',
			'std' => ''
		),
		
		array(
			'name' => 'Slider image 3',
			'desc' => 'Insert the full path to your image',
			'id' => $prefix . 'slider_image_3',
			'type' => 'text',
			'std' => ''
		),
		
		array(
			'name' => 'Slider image 4',
			'desc' => 'Insert the full path to your image',
			'id' => $prefix . 'slider_image_4',
			'type' => 'text',
			'std' => ''
		)
	)
);


function my_editor_style_single() {

    global $current_screen;

    switch ($current_screen->post_type) {

    case 'post':

        break;

    case 'page':

        break;

    case 'acf': // CPT

        //echo '<script>alert("post");</script>';

        break;

    }

}

add_action( 'admin_head', 'my_editor_style_single' );

add_action('admin_menu', 'mytheme_add_box_single');

// Add meta box
function mytheme_add_box_single() {
	global $meta_box_single;
	
	add_meta_box($meta_box_single['id'], $meta_box_single['title'], 'mytheme_show_box_single', $meta_box_single['page'], $meta_box_single['context'], $meta_box_single['priority']);
}

// Callback function to show fields in meta box
function mytheme_show_box_single() {
	global $meta_box_single, $post;
	
	// Use nonce for verification
	echo '<input type="hidden" name="mytheme_meta_box_single_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
	
	echo '<table class="form-table">';

	foreach ($meta_box_single['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);
		
		echo '<tr>',
				'<th style="width:25%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
				'<td>';
		switch ($field['type']) {
			case 'text':
				echo '<input type="text" class="input_style" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:100%" />',
					'<br />', $field['desc'];
				break;
			case 'textarea':
				echo '<textarea name="', $field['id'], '" class="input_style" id="', $field['id'], '" cols="60" rows="4" style="width:100%">', $meta ? $meta : $field['std'], '</textarea>',
					'<br />', $field['desc'];
				break;
			case 'select':
				echo '<select name="', $field['id'], '" id="', $field['id'], '">';
				foreach ($field['options'] as $option) {
					echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
				}
				echo '</select>';
				break;
			case 'radio':
				foreach ($field['options'] as $option) {
					echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
				}
				break;
			case 'checkbox':
				echo '<div class="on_off"><input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /></div>';
				break;
		}
		echo 	'<td>',
			'</tr>';
	}
	
	echo '</table>';
}

add_action('save_post', 'mytheme_save_data_single');

// Save data from meta box
function mytheme_save_data_single($post_id) {
	global $meta_box_single;
	
	// verify nonce
	if ( !isset($_POST['mytheme_meta_box_single_nonce']) || !wp_verify_nonce( $_POST['mytheme_meta_box_single_nonce'], basename(__FILE__) )) {
		return $post_id;
	}

	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}

	// check permissions
	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 ($meta_box_single['fields'] as $field) {
		$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);
		}
	}
}

function single_options_styles() {  
   
wp_register_style( 'single_opt_style', BASE_URL . 'css/single_opt_style.css', null, false );
wp_enqueue_style('single_opt_style');
}
add_action( 'admin_enqueue_scripts', 'single_options_styles' ); 
?>