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

//shuffle($latest_previews);
//$first_preview = "";
//if (count($latest_previews) > 0) {
//    $first_preview = $latest_previews[0];
//    unset($latest_previews[0]);
//}
?>


	

<div class="latest-video widget">
	
    <div class="title-caption-dark">
		
        <h3><?php echo $instance['title'] ?></h3>
		
        		
    </div><!--/ .title-caption-dark-->

	
	<?php 
	$last_item_date=0;
	$games_users = get_users();

	foreach ($games_users as $games_user){
	
	$youtube_items = get_the_author_meta('youtube_items', $games_user->ID);
	?>
    <?php
		
	//$video_width = 314;
	//$video_height = 270;
	$video_width=$instance['width'];
	$video_height=$instance['height'];
		 
	?>
	
	
	
	<?php $last_item_date_user=array_pop($youtube_items['date']);
			
			$last_url_user=array_pop($youtube_items['link']);
	?>
	
		<?php if ($last_item_date_user>$last_item_date){

			$last_item_date=$last_item_date_user;
			$last_url=$last_url_user;

		} ?>
	
	
	<?php
      
	}
	
	$video_url=$last_url;
	
        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;
            }
        }
		
	
	
    ?>
	<?php 
	
	$games_users = get_users();

	foreach ($games_users as $games_user){
		
		$youtube_items = get_the_author_meta('youtube_items', $games_user->ID);
		
		if (!empty($youtube_items)) {
			
			$last_url_user=array_pop($youtube_items['link']);
			$last_video_title=array_pop($youtube_items['title']);
			
		
		
		
		
	?>
    <ul class="video-list">
       
                <li>
                                  
                        <a class="zoom" href="<?php echo ThemeMakersUser::get_profile_url($games_user->ID) ?>">
							
							<?php 
							
							$video_width = 94;
							$video_height = 60;
							
                            $video_url=$last_url_user;
	
							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;
								}
							} 
							?>
							<span class="curtain"> </span>
                        </a>
                    
					<?php 
						$rated_users = Thememakers_Entity_Rating::get_users_rating();
						$max_rating = Thememakers_Entity_Rating::get_max_rating_number(); 
						
						?>
					
                    <div class="teaser-content">
						
                        <h6 class="title"><?php echo $last_video_title; ?></h6>
						
						 <?php $unique_id = uniqid(); ?>
					
						<a class="vtop" href="<?php echo ThemeMakersUser::get_profile_url($games_user->ID) ?>">
							<?php echo $games_user->display_name ?>
						</a>

						&nbsp;<span class="star rating_<?php echo $unique_id ?>"></span>

						<script type="text/javascript">

							jQuery(function(){
								jQuery('.rating_<?php echo $unique_id ?>').raty({
									half:  true,
									readOnly: true,
									number:<?php echo $max_rating ?>,
									score: parseFloat(<?php echo $rated_users[$games_user->ID] ?>)
								});
							});

						</script>
						
						
                    </div><!--/ .teaser-conent-->
                    <div class="clear"></div>
                </li>
           

    </ul><!--/ .video-list-->
	
		<?php } ?>
	
	<?php } ?>
	
</div><!--/ .latest-video-->

