<?php
$latest_previews = Thememakers_Entity_Preview::get_latest_previews($instance['previews_count']);


?>


<div class="latest-video widget">
	
    <div class="title-caption-dark">
		
        <h3><?php echo $instance['title'] ?></h3>
		
        <!--<a href="/pre-view" class="more-all"><?php _e('See all', THEMEMAKERS_THEME_FOLDER_NAME) ?></a>-->
		
    </div><!--/ .title-caption-dark-->
	
	
    <?php
		$video_width=$instance['width'];
		$video_height=$instance['height'];
	
       // $video_width = 314;
       // $video_height = 270;
						
        $videos = Thememakers_Entity_Preview::get_preview_videos($latest_previews['0']->ID);
		
		?>
		
		<?php
		
		
        $video_url = "";
        if (!empty($videos)) {
            $video_url = $videos[0];
        }

        if (!empty($video_url)) {
            $video_type = 'youtube.com';
            $allows_array = array('youtube.com', 'vimeo.com', '.mp4');

            foreach ($allows_array as $key => $needle) {
                $count = substr_count($video_url, $needle);
                if ($count) {
                    $video_type = $allows_array[$key];
                }
            }

            switch ($video_type) {
                case $allows_array[0]:
                    $video_url = explode("?v=", $video_url);
                    $video_url = explode("&", $video_url[1]);
                    if (is_array($video_url)) {
                        $video_url = $video_url[0];
                    }
                    echo do_shortcode('[video type="youtube" html5_poster="" html5_video_url="" src_webm="" src_ogg="" width="' . $video_width . '" height="' . $video_height . '"]' . $video_url . '[/video]');
                    break;
                case $allows_array[1]:
                    $video_url = explode("/", $video_url);
                    if (is_array($video_url)) {
                        $video_url = $video_url[count($video_url) - 1];
                    }
                    echo do_shortcode('[video type="vimeo" html5_poster="" html5_video_url="" src_webm="" src_ogg="" width="' . $video_width . '" height="' . $video_height . '"]' . $video_url . '[/video]');
                    break;

                default:
                    $html5_poster = THEMEMAKERS_THEME_URI . "/images/video_poster.jpg";
                    if (has_post_thumbnail($first_preview->ID)) {
                        $html5_poster = ThemeMakersHelper::get_post_featured_image($first_preview->ID, 'slider_showcase');
                    }
                    echo do_shortcode('[video type="html5" html5_poster="' . $html5_poster . '" html5_video_url="' . $video_url . '" src_webm="" src_ogg="" width="' . $video_width . '" height="' . $video_height . '"][/video]');
                    break;
            }
        }


  
    ?>

    <ul class="video-list">

        <?php if (!empty($latest_previews)): ?>
            <?php foreach ($latest_previews as $post) : ?>
                <li>
                    <?php
                    $featured_image = THEMEMAKERS_THEME_URI . '/images/no-image.jpg';
                    $permalink = get_permalink($post->ID);
                    $featured_image_width = 94;
                    $featured_image_height = 60;
                    $featured_image = "";
                    if (has_post_thumbnail($post->ID)) {
                        $featured_image = ThemeMakersHelper::get_post_featured_image($post->ID, 'gallery_thumb');
                    }
                    ?>
                   

                    <div class="teaser-content">
                        <h6><a class="title" href="<?php echo $permalink ?>"><?php echo $post->post_title ?></a></h6>
                        <?php echo Thememakers_Entity_Rating::draw_rating($post->ID) ?>
                    </div><!--/ .teaser-conent-->
                    <div class="clear"></div>
                </li>
            <?php endforeach; ?>
        <?php endif; ?>

    </ul><!--/ .video-list-->

</div><!--/ .latest-video-->

