/**
 * Shortcode to display a single video in post/page
 * Usage:
 * 
 * [cvm_video id="video_id_from_wp"]
 * 
 * Complete params:
 * 
 * - id : video ID from WordPress import (post ID) - required
 * - volume : video volume (number between 1 and 100) - optional
 * - width : width of video (number) - optional; works in conjunction with aspect_ratio
 * - aspect_ratio : aspect ratio of video ( 16x9 or 4x3 ) - optional; needed to calculate height
 * - autoplay : play video on page load ( 0 or 1 ) - optional
 * - controls : display controls on video ( 0 or 1 ) - optional
 * 
 * @param array $atts
 * @param string $content
 */
function cvm_single_video($atts = array(), $content = '')
{
    // check if atts is set
    if (!is_array($atts)) {
        return;
    }
    // look for video ID
    if (!array_key_exists('id', $atts)) {
        return;
    }
    // post id from atts
    $post_id = absint($atts['id']);
    // post
    $post = get_post($atts['id']);
    if (!$post) {
        return;
    }
    // check post type
    global $CVM_POST_TYPE;
    if ($CVM_POST_TYPE->get_post_type() !== $post->post_type) {
        return false;
    }
    // get video options attached to post
    $vars = cvm_get_video_settings($post_id);
    // get video data
    $video = get_post_meta($post_id, '__cvm_video_data', true);
    $width = absint($vars['width']);
    $height = cvm_player_height($vars['aspect_ratio'], $vars['width']);
    $settings = $vars;
    $settings['video_id'] = $video['video_id'];
    $video_container = '<div class="cvm_single_video_player" style="width:' . $width . 'px; height:' . $height . 'px; max-width:100%;"><!--' . json_encode($settings) . '--></div>';
    // add JS file
    cvm_enqueue_player();
    return $video_container;
}
/**
 * Outputs video data
 * @param string $before
 * @param string $after
 * @param bool $echo
 */
function cvm_output_video_data($before = " rel='", $after = "'", $echo = true)
{
    global $cvm_video;
    $video_settings = cvm_get_video_settings($cvm_video['ID']);
    $video_id = $cvm_video['video_data']['video_id'];
    $data = array('video_id' => $video_id, 'autoplay' => $video_settings['autoplay'], 'volume' => $video_settings['volume']);
    $output = json_encode($data);
    if ($echo) {
        echo $before . $output . $after;
    }
    return $before . $output . $after;
}
    public function post_video_settings_meta_box()
    {
        global $post;
        $settings = cvm_get_video_settings($post->ID);
        wp_nonce_field('cvm-save-video-settings', 'cvm-video-nonce');
        ?>
<table class="form-table cvm-player-settings-options">
	<tbody>
		<tr>
			<th><label for="cvm_aspect_ratio"><?php 
        _e('Player size', 'cvm_video');
        ?>
:</label></th>
			<td>
				<label for="cvm_aspect_ratio"><?php 
        _e('Aspect ratio');
        ?>
:</label>
				<?php 
        $args = array('options' => array('4x3' => '4x3', '16x9' => '16x9'), 'name' => 'aspect_ratio', 'id' => 'cvm_aspect_ratio', 'class' => 'cvm_aspect_ratio', 'selected' => $settings['aspect_ratio']);
        cvm_select($args);
        ?>
				<label for="cvm_width"><?php 
        _e('Width', 'cvm_video');
        ?>
 :</label>
				<input type="text" name="width" id="cvm_width" class="cvm_width" value="<?php 
        echo $settings['width'];
        ?>
" size="2" />px
				| <?php 
        _e('Height', 'cvm_video');
        ?>
 : <span class="cvm_height" id="cvm_calc_height"><?php 
        echo cvm_player_height($settings['aspect_ratio'], $settings['width']);
        ?>
</span>px
			</td>
		</tr>
				
		<tr>
			<th><label for="cvm_video_position"><?php 
        _e('Video position', 'cvm_video');
        ?>
:</label></th>
			<td>
				<?php 
        $args = array('options' => array('above-content' => __('Above post content', 'cvm_video'), 'below-content' => __('Below post content', 'cvm_video')), 'name' => 'video_position', 'id' => 'cvm_video_position', 'selected' => $settings['video_position']);
        cvm_select($args);
        ?>
			</td>
		</tr>
		<tr>
			<th><label for="cvm_volume"><?php 
        _e('Volume', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input type="text" name="volume" id="cvm_volume" value="<?php 
        echo $settings['volume'];
        ?>
" size="1" maxlength="3" />
				<label for="cvm_volume"><span class="description">( <?php 
        _e('number between 0 (mute) and 100 (max)', 'cvm_video');
        ?>
 )</span></label>
			</td>
		</tr>
		<tr>
			<th><label for="cvm_autoplay"><?php 
        _e('Autoplay', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input name="autoplay" id="cvm_autoplay" type="checkbox" value="1"<?php 
        cvm_check((bool) $settings['autoplay']);
        ?>
 />
				<label for="cvm_autoplay"><span class="description">( <?php 
        _e('when checked, video will start playing once page is loaded', 'cvm_video');
        ?>
 )</span></label>
			</td>
		</tr>
		
		<tr>
			<th><label for="title"><?php 
        _e('Show video title', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input name="title" id="cvm_title" class="cvm_title" type="checkbox" value="1"<?php 
        cvm_check((bool) $settings['title']);
        ?>
 />
				<label for="cvm_title"><span class="description">( <?php 
        _e('when checked, player will display video title', 'cvm_video');
        ?>
 )</span></label>
			</td>
		</tr>
		
		<tr>
			<th><label for="cvm_fullscreen"><?php 
        _e('Allow full screen', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input name="fullscreen" id="cvm_fullscreen" type="checkbox" value="1"<?php 
        cvm_check((bool) $settings['fullscreen']);
        ?>
 />
			</td>
		</tr>
		
		<tr>
			<th><label for="cvm_color"><?php 
        _e('Player color', 'cvm_video');
        ?>
:</label></th>
			<td>#<input type="text" name="color" id="cvm_color" value="<?php 
        echo $settings['color'];
        ?>
" /></td>
		</tr>
		
		<tr valign="top">
			<th scope="row"><label for="byline"><?php 
        _e('Show video author', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input type="checkbox" value="1" id="byline" name="byline"<?php 
        cvm_check((bool) $settings['byline']);
        ?>
 />
				<span class="description"><?php 
        _e('When checked, player will display video uploader.', 'cvm_video');
        ?>
</span>
			</td>
		</tr>
		
		<tr valign="top">
			<th scope="row"><label for="portrait"><?php 
        _e('Author portrait', 'cvm_video');
        ?>
:</label></th>
			<td>
				<input type="checkbox" value="1" id="portrait" name="portrait"<?php 
        cvm_check((bool) $settings['portrait']);
        ?>
 />
				<span class="description"><?php 
        _e('When checked, player will display uploader image.', 'cvm_video');
        ?>
</span>
			</td>
		</tr>
		
	</tbody>
</table>
<?php 
    }