<?php
defined('_JEXEC') or die;

/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 
The user always has the ability to exit fullscreen mode of their own accord; see Things your users want to know. You can also do so programmatically by calling the exitFullscreen() method; this is implemented in Gecko as mozCancelFullScreen(), WebKit as webkitExitFullscreen(), and Trident as msExitFullscreen().
//div.mozRequestFullScreen()
**/


if ($params->get('video_player_skin') != ''){
    $name_skin = $params->get('video_player_skin'); // blue.monday | pink.flag
} else {
    $name_skin = 'blue.monday';
}
$name_skin = 'pink.flag';

$layout = 'list';// list/default

$video_heigth = '300px';
$video_width = '615px';


$play_N = 1;
$playVideo = 1;
$playId = -1;

if ($params->get('playId') != ''){
    $playId = $params->get('playId');
}
$playSub = '';
$playVideo = '';
if ($params->get('playVideo') != ''){
    $playVideo = $params->get('playVideo');
}

$lenSub = '';
if ($params->get('lenSub') != ''){
    $lenSub = $params->get('lenSub');
}

$currTime = '';
if ($params->get('currTime') != ''){
    $currTime = $params->get('currTime');
}
$item_videos = [];

//echo '<div  class="ml_search_result_page ml_search_result_video" id="medialibrary" >';
positions_ml($params->get('singlecategory01'));
positions_ml($params->get('search_mod1'));

if($pageNav->total > 0){
  $song_for = trim(protectInjectionWithoutQuote('searchtext')) ;
  $slash = '';
  if(!empty($song_for))$slash = '/';
  positions_ml($params->get('singleuser02'));
  positions_ml($params->get('singlecategory02'));
  positions_ml($params->get('search_mod2'));
}
positions_ml($params->get('singleuser03'));
positions_ml($params->get('singlecategory03'));
positions_ml($params->get('search_mod3'));


foreach ($rows as $row) {
    if ($row->group_videos) {
        $item_video = [];
        foreach ($row->group_videos as $kay => $value) {
            if ($value[0]->youtube != ''){
                $item_video[$kay]['srcs'] = $value;
                $item_video[$kay]['row'] = $row;
                $item_video[$kay]['youtube'] = 1;
            } else {
                $item_video[$kay]['srcs'] = $value;
                $item_video[$kay]['row'] = $row;
                $item_video[$kay]['youtube'] = 0;
            }
        }
        $item_videos = $item_videos + $item_video;
    }
}
$item_tracks = [];
foreach ($rows as $row) {
    if ($row->group_tracks) {
        $item_track = [];
        foreach ($row->group_tracks as $kay => $value) {
            $item_track[$kay]['sub_srcs'] = $value;
        }
        $item_tracks = $item_tracks + $item_track;
    }
}
if (isset($item_videos) && count($item_videos)){
    $next_play_list_item = 0;
    if (!isset($item_videos[$playId])){$playId = -1;}
    foreach ($item_videos as $kay => $value) {
        if (!isset($item_videos[$playId]) && $playId == -1)  {
            $playId = $kay;
            $playSub = '';
            $currTime = '';
            $playVideo = 0;
        }
        if ($next_play_list_item == -1) $next_play_list_item = $kay;
        if ($kay == $playId) $next_play_list_item = -1;
    }

        if(count($item_videos[$playId]['srcs']) > 0 ){
          //start div with video
          ?>
            <div class="ml-collumn-xs-12 ml-collumn-sm-12 ml-collumn-md-12 ml-collumn-lg-12" >
                        <div id="youtube_player_<?php echo $play_N;?>">
                            <div id="player"></div>
                        </div>

          <?php
        }
        
        if( checkJavaScriptIncluded("jQuerMedia.js") === false ) { 
         $doc->addScript($mosConfig_live_site . '/components/com_medialibrary/includes/jQuerMedia.js');
        }
    $doc->addScript($mosConfig_live_site . '/components/com_medialibrary/includes/jQuerMedia.jplayer.js');
    //$doc->addScript($mosConfig_live_site
    // . '/components/com_medialibrary/includes/jplayer.playlist.min.js');
    $doc->addStyleSheet($mosConfig_live_site
        . '/components/com_medialibrary/includes/skin/'.$name_skin.'/css/jplayer.'.$name_skin.'.video.css');

    $youtube = 0;

        foreach ($item_videos as $key => $video) {
            if ( isset($video['youtube']) && $video['youtube'] != 0 ) {
                $youtube =  $video['youtube'];
                break ;
            }
        }

    ?>
        <script  type="text/javascript"  >
            function FullScreenJP_ON() {
                fullscreen_mode = 1;
                jQuerMedia("#jquery_jplayer_1").jPlayer({'fullScreen':1});
            }
            function FullScreenYT_ON() {
                fullscreen_mode = 1;
                // div = document.getElementById("player");
                // console.log(div);
                // console.log(div.mozRequestFullScreen);
                // if (div.mozRequestFullScreen){
                //     div.mozRequestFullScreen();
                // }
                // if (document.documentElement.requestFullscreen) {
                //   document.documentElement.requestFullscreen();
                // } else if (document.mozRequestFullScreen) {
                //   document.mozRequestFullScreen();
                // } else if (document.documentElement.webkitRequestFullscreen) {
                //   document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
                // }
            }
            function FullScreenOFF() {
                jQuerMedia("#jquery_jplayer_1").jPlayer({'fullScreen':0});
                if (document.cancelFullScreen) {
                  document.cancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                  document.mozCancelFullScreen();
                } else if (document.webkitCancelFullScreen) {
                  document.webkitCancelFullScreen();
                }
            }

            var play_direction = 'next';
            function thisSourceNotSuppot() {
                if (play_direction === 'next'){
                    new_function_for_next_play();
                }
                if (play_direction === 'prev'){
                    new_function_for_back_play();
                }
            }

            // create youtube player
            var player;
            function onYouTubePlayerAPIReady(playId, autoplay) {
                var onReadyPlay = 'onPlayerReady';
                if (!autoplay){onReadyPlay = '';}
                player = new YT.Player('player', {
                  height: '<?php echo $video_heigth;?>',
                  width: '<?php echo $video_width;?>',
                  videoId: playId,
                  allowFullScreen: fullscreen_mode,
                  events: {
                    'onReady': onReadyPlay,
                    'onStateChange': onPlayerStateChange
                  }
                });
            }

            // autoplay video
            function onPlayerReady(event) {
                event.target.playVideo();
                if (fullscreen_mode){
                    setTimeout(function(){FullScreenYT_ON();}, 5000);
                }
            }


            var change_youtube_video = 1;
            // when video ends
            var fullscreen_mode = 0;
            function onPlayerStateChange(event) {
                if(event.data === 0) {
                    if (change_youtube_video){
                        fullscreen_mode = document.mozFullScreen;
                        new_function_for_next_play();
                        change_youtube_video = 0;
                    }
                } else {
                    change_youtube_video = 1;
                }
            }

        </script>


<?php
        //add Youtube palayerif exist youtube video
    if ($youtube){
    
    ?>
        <script src="https://www.youtube.com/iframe_api"  type="text/javascript"></script>


            <script  type="text/javascript"  >
                var now_i_play_type = 'video';

                jQuerMedia(document).ready(function(){
                    setTimeout(function(){
                        jQuerMedia("#jp_container_<?php echo $play_N;?>").hide();

                        var playId = <?php echo $playId;?>;
                        var play_N = <?php echo $play_N;?>;
                        change_video(play_N, playId, 0);
                    }, '1000');

                });
            </script>

            <script type="text/javascript">

            //<![CDATA[
            jQuerMedia(document).ready(function(){

                jQuerMedia("#jquery_jplayer_<?php echo $play_N;?>").jPlayer({
                    ready: function () {
                        jQuerMedia(this).jPlayer("setMedia", {

                        });
                    },
                    swfPath: "<?php echo $mosConfig_live_site
                        ?>/components/com_medialibrary/includes/jQuerMedia.jplayer.swf",
                    supplied: "webmv, ogv, m4v",
                    size: {
                        width: "<?php echo $video_width;?>",
                        height: "<?php echo $video_heigth;?>",
                        cssClass: "jp-media-lib-video-360p"
                    },
                    useStateClassSkin: true,
                    cssSelectorAncestor: "#jp_container_<?php echo $play_N;?>",
                    autoBlur: false,
                    smoothPlayBar: true,
                    keyEnabled: true,
                    remainingDuration: true,
                    toggleDuration: true

                });

            });

            </script>
            <?php
        } else { ?>

        <script type="text/javascript">
        //<![CDATA[
        jQuerMedia(document).ready(function(){

            jQuerMedia("#jquery_jplayer_<?php echo $play_N;?>").jPlayer({
                ready: function () {
                    jQuerMedia(this).jPlayer("setMedia", {
                        <?php $row = $item_videos[$playId]['row'];
                        $srcs = $item_videos[$playId]['srcs'] ;
                        if (isset($item_tracks[$playId])){
                            $srt = $item_tracks[$playId];
                        }
                        if (isset($srcs) && count($srcs) > 0 ) { ?>
                            title:" <?php  if (isset($row->title) && $row->title != '') {
                                if (strlen($row->title) < 50) echo $row->title ;
                                else  echo substr($row->title, 0, 50);
                                }

                            ?> ",
                            artist:" <?php if (isset($row->people_by_types['studio'][0]['name'])
                                                  && $row->people_by_types['studio'][0]['name'] != '')
                                                echo $row->people_by_types['studio'][0]['name'] ?>",
                      <?php foreach ($srcs  as $video){
                                $array_format = explode('.', $video->src);
                                $format = array_pop($array_format);
                                $ormat_lower = strtolower($format);
                                if ($ormat_lower == 'ogv') { ?>
                                        ogv:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['videos']['location']
                                            .$video->src ?>",
                                <?php } elseif ($ormat_lower == 'm4v') {  ?>
                                        m4v:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['videos']['location']
                                            .$video->src ?>",
                                <?php } elseif ($ormat_lower == 'webm') {  ?>
                                        webmv:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['videos']['location']
                                            .$video->src ?>",
                                
                                <?php
                                    }
                                }

                                ?>
                            poster: "<?php // if (isset($row->imageURL) && $row->imageURL != '') {
                                //echo $row->imageURL; }
                                // else { echo $mosConfig_live_site
                                //.'/components/com_medialibrary/images/'._MEDIALIBRARY_NO_PICTURE ;}?>"
                        <?php
                        } ?>

                    })<?php if ($playVideo && $params->get('playVideo')) {echo '.jPlayer("play"';
                            if ($currTime) {echo ', '.$currTime;} echo ')';}?>;
                },
                swfPath: "<?php echo $mosConfig_live_site
                    ?>/components/com_medialibrary/includes/jQuerMedia.jplayer.swf",
                supplied: "webmv, ogv, m4v",
                size: {
                    width: "<?php echo $video_width;?>",
                    height: "<?php echo $video_heigth;?>",
                    cssClass: "jp-media-lib-video-360p"
                },
                useStateClassSkin: true,
                cssSelectorAncestor: "#jp_container_<?php echo $play_N;?>",
                autoBlur: false,
                smoothPlayBar: true,
                keyEnabled: true,
                remainingDuration: true,
                toggleDuration: true,
                nextPlay:  "<?php if ($next_play_list_item > 0){
                               echo sefRelToAbs('index.php?option=com_medialibrary&view=allvideo&Itemid='
                                .$Itemid.'&playId='.$next_play_list_item.'&playVideo=1');
                            }?>"

            });

        });

        </script>
    <?php
    } ?>
        <script  type="text/javascript"  >
        var first_add_subtitles = 1;
        var item_videos = new Array();
        var queue_item_videos = new Array();
        <?php
        foreach ($item_videos as $kay => $value) {
            if ($value['srcs'] != '' && isset($item_videos[$kay])){?>
                queue_item_videos.push(<?php echo $kay ?>);
                item_videos['<?php echo $kay ?>'] = {
                    <?php
                    $row = $value['row'];
                    $srcs = $value['srcs'];
                    if (isset($srcs) && count($srcs) > 0 ) { ?>
                        title:" <?php if (isset($row->title) && $row->title != '') {
                            if (strlen($row->title) < 50) echo $row->title ;
                            else  echo substr($row->title, 0, 50);
                            }
                        ?> ",
                        artist:" <?php if (isset($row->people_by_types['studio'][0]['name'])
                                              && $row->people_by_types['studio'][0]['name'] != '')
                                            echo $row->people_by_types['studio'][0]['name'] ?>",
                        <?php foreach ($srcs  as $video){
                            $array_format = explode('.', $video->src);
                            $format = array_pop($array_format);
                            $ormat_lower = strtolower($format);
                            if ($ormat_lower == 'ogv') { ?>
                                ogv:" <?php echo $mosConfig_live_site.$medialibrary_configuration[
                                    'videos']['location'].$video->src ?>",
                            <?php } elseif ($ormat_lower == 'm4v') {  ?>
                                m4v:" <?php echo $mosConfig_live_site.$medialibrary_configuration[
                                    'videos']['location'].$video->src ?>",
                            <?php } elseif ($ormat_lower == 'webm') {  ?>
                                webmv:" <?php echo $mosConfig_live_site.$medialibrary_configuration[
                                    'videos']['location'].$video->src ?>",
                            <?php
                                }
                            }    ?>
                        poster: "<?php // if (isset($row->imageURL) && $row->imageURL != '') {
                            //echo $row->imageURL; }
                            // else { echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                            //._MEDIALIBRARY_NO_PICTURE ;}?>"
                        <?php

                    }?>
                }
                <?php
                foreach ($value['srcs'] as $value) {
                    if ($value->youtube != ''){?>
                        item_videos[<?php echo $kay ?>] = {youtube: "<?php echo explode('&',
                            str_replace('watch?v=', 'embed/', $value->youtube))[0]; ?>"}
                        <?php
                    }
                }
            }
        } ?>
        var link_items = [];
        <?php

        foreach ($item_videos as $kay => $value) {
            $part_of_link_item = 'index.php?option=com_medialibrary&task=view&id='
                . $value['row']->id . '&catid='.$catid .'&Itemid='. $Itemid;
            if ($item_videos[$kay]['srcs'][0]->title != ''){
                if(strlen($item_videos[$kay]['srcs'][0]->title)>100){
                  $link_title = str_replace("'", "\'", substr($item_videos[$kay]['srcs'][0]->title,0,100)."...");
                }else{
                  $link_title = str_replace("'", "\'", $item_videos[$kay]['srcs'][0]->title);
                }
            } else {
                $name_file = explode('_', $item_videos[$kay]['srcs'][0]->src);
                if(count($name_file) == 2){
                    $link_title = str_replace('-', ' ', explode('.',$name_file[1])[0]);
                } else {
                    $link_title = '';
                }
            }
            ?>
            link_items[<?php echo $kay;?>] = '<a href="<?php echo sefRelToAbs($part_of_link_item);
                ?>">'+'<?php echo $link_title;?>'+'</a>';
        <?php
        }?>

        var global_play_N = <?php echo $play_N;?>;
        var global_playId = <?php echo $playId;?>;
        function new_function_for_next_play (){
            play_direction = 'next';
            var play_N = global_play_N;
            var old_playId = global_playId;
            var playId = '';
            for(var kay in queue_item_videos){
                if (parseInt(queue_item_videos[kay]) == queue_item_videos[kay]){
                    if(playId == -1){
                        playId = queue_item_videos[kay];
                    }
                    if (queue_item_videos[kay] == old_playId){
                        playId = -1;
                    }
                }
            }
            if (playId != -1){
                change_video(play_N, playId);
            }
        } ;
        function new_function_for_back_play (){
            play_direction = 'prev';
            var play_N = global_play_N;
            var old_playId = global_playId;
            var playIdNow = '';
            var playId = '';
            for(var kay in queue_item_videos){
                if (parseInt(queue_item_videos[kay]) == queue_item_videos[kay]){
                    if (queue_item_videos[kay] == old_playId){
                        playIdNow = -1;
                    }
                    if(playIdNow != -1){
                        playId = queue_item_videos[kay];
                    }
                }
            }
            if (playId != ''){
                change_video(play_N, playId);
            }
        } ;
        function change_video(play_N, playId, autoplay = 1){
            jQuerMedia('#div_chuse_add_sub').empty();
            jQuerMedia('#div_chuse_add_sub').append(link_subs[playId]);
            global_playId = playId;
            global_play_N = play_N;
            jQuerMedia("#media-lib-item-linc"+play_N).empty().append('<span>'+link_items[
                playId]+'</span>');
            jQuerMedia(".media-lib-now-play").removeClass('media-lib-now-play');
            jQuerMedia(".kay-"+playId).addClass('media-lib-now-play');
//            console.log(item_videos[playId]);
            FullScreenOFF();
            if (item_videos[playId]['youtube']){
                jQuerMedia("#jp_container_<?php echo $play_N;?>").hide();
                // var theIFrame = '<iframe width="640" height="360" frameborder="0" src="https://www.youtube.com/embed/'+item_videos[
                //     playId]['youtube'];
                // theIFrame+= '"></iframe>';
                // jQuerMedia("#youtube_player_"+play_N).empty().append(theIFrame);
                jQuerMedia("#jquery_jplayer_1").jPlayer("stop");
                jQuerMedia("#youtube_player_"+play_N).empty().append('<div id="player"></div>');

                onYouTubePlayerAPIReady(item_videos[playId]['youtube'], autoplay);
                //console.log('fullscreen_mode'+fullscreen_mode);

            } else {
                jQuerMedia("#youtube_player_"+play_N).empty();
                jQuerMedia("#jp_container_<?php echo $play_N;?>").show();
                jQuerMedia("#jquery_jplayer_"+play_N).jPlayer("setMedia", item_videos[
                    playId]).jPlayer("play");
                //console.log('fullscreen_mode'+fullscreen_mode);
                if (fullscreen_mode){
                    setTimeout(FullScreenJP_ON, 3000);

                }
            }
        }
    </script>
    <div class="row result_search">
    
        <div id="ML-player" class="ml-collumn-xs-12 ml-collumn-sm-12 ml-collumn-md-8 ml-collumn-lg-7">

            <div class="search_result_title">
                <?php 

                echo "<h2 class='search_expression' >" .$song_for . "</h2><span>$slash</span><h5 class='search_media_count' >"._MEDIALIBRARY_FOUND." <strong>". $pageNav->total ."</strong> ".
                _MEDIALIBRARY_LABEL_MEDIA_TYPE_VIDEO."</h5> " ; ?>
            </div>


            <div id="jp_container_<?php echo $play_N;?>" class="jp-media-lib-video jp-media-lib-video-360p<?php
                if($layout == 'list') echo '-list';?>"
                 role="application" aria-label="media player">
                <div class="jp-media-lib-type-single">
                    <div id="jquery_jplayer_<?php echo $play_N;?>" class="jp-media-lib-jplayer"></div>
                    <div class="jp-media-lib-gui">
                        <div class="jp-media-lib-video-play">
                            <button class="jp-media-lib-video-play-icon" role="button" tabindex="0">play
                            </button>
                        </div>
                        <div class="jp-media-lib-interface">
                            <div class="jp-media-lib-progress">
                                <div class="jp-media-lib-seek-bar">
                                    <div class="jp-media-lib-play-bar"></div>
                                </div>
                            </div>
                            <div class="jp-media-lib-current-time" role="timer" aria-label="time">&nbsp;</div>
                            <div class="jp-media-lib-duration" role="timer" aria-label="duration">&nbsp;</div>
                            <div class="jp-media-lib-controls-holder">
                                <div class="jp-media-lib-controls">
                                    <button class="jp-control-button jp-media-lib-play" role="button" tabindex="0">
                                        <i class="fa fa-play"></i>
                                        <i class="fa fa-pause"></i>
                                    </button>
                                    <button class="jp-control-button jp-media-lib-stop" role="button" tabindex="0">
                                        <i class="fa fa-stop"></i>
                                    </button>

                                </div>
                                <div class="jp-media-lib-volume-controls">
                                    <button class="jp-media-lib-mute" role="button" tabindex="0">mute</button>
                                    <button class="jp-media-lib-volume-max" role="button" tabindex="0">
                                        max volume
                                    </button>
                                    <div class="jp-media-lib-volume-bar">
                                        <div class="jp-media-lib-volume-bar-value"></div>
                                    </div>
                                </div>
                                <div class="jp-media-lib-toggles">
                                    <button class="jp-media-lib-repeat" role="button" tabindex="0">
                                        repeat
                                    </button>
                                    <button class="jp-media-lib-full-screen" role="button" tabindex="0">
                                        full screen
                                    </button>
                                    <div id="div_chuse_add_sub">
                                        <?php if (isset($item_tracks[$playId]) &&
                                                  count($item_tracks[$playId]['sub_srcs'])){ ?>
                                            <select name="chuse_add_sub" onchange="ml_add_sub()">
                                                <option value="" <?php if(!$lenSub){ echo 'selected';
                                                                }?> ><?php echo _MEDIALIBRARY_NO;?> </option>
                                                <?php foreach ($item_tracks[$playId]['sub_srcs'] as $subs) {?>
                                                    <option value="<?php echo $subs->src;?>" <?php
                                                        if($subs->scrlang == $lenSub){ echo 'selected';
                                                            $playSub = $subs->src;}?>
                                                        ><?php echo $subs->scrlang;?></option>
                                                <?php
                                                }?>

                                            </select>
                                        <?php
                                        }?>
                                    </div>
                                    <!-- <input type="button" value="sub" onclick="ml_add_sub();"></input> -->
                                    <div id="sub_linck" style="display:none;">
                                        <?php
                                        $part_link = '';
                                        if ($task == 'search'){
                                            foreach ($_GET as $key => $value) {
                                                $part_link .= "&".$key."=".$value;
                                            }
                                            $part_link = substr($part_link, 1);
                                        } else {
                                            $part_link = 'option=com_medialibrary&view=allvideo';
                                        }
                                        $link_item = sefRelToAbs('index.php?'.$part_link
                                            .'&Itemid='.$Itemid .'&limit='.$limit.'&limitstart='
                                            .$limitstart.'&catid=0'.'&playId=Global_playId&playVideo=play'
                                            .'&currTime=CurrTime&lenSub=LenSub')?>
                                        <a href="<?php echo $link_item;?>" ></a>
                                    </div>
                                </div>
                            </div>
                            <div class="jp-media-lib-details">
                                <div class="jp-media-lib-title" aria-label="title"></div>
                            </div>
                        </div>
                    </div>
                    <div class="jp-media-lib-no-solution">
                        <span>Update Required</span>
                            To play the media you will need to either update your
                            browser to a recent version or update your
                        <a href="https://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
                    </div>
                </div>
            </div>

            <?php if  ($layout != 'list'){?>
                <div class="media-lib-change-play-default">
                    <div class="prev-video"  onclick="new_function_for_back_play();" style="display: inline-block;">
                        <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        .'play_back.png';?>" alt="Back">
                    </div>

                    <div id="media-lib-item-linc<?php echo $play_N;?>" class="video-link" >
                        <?php
                        $link_item = 'index.php?option=com_medialibrary&task=view&id='
                            . $item_videos[$playId]['row']->id . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                        <a href="<?php echo sefRelToAbs($link_item); ?>">
                            <?php
                            if(strlen($item_videos[$playId]['srcs'][0]->title)>100){
                              echo substr($item_videos[$playId]['srcs'][0]->title,0,100),"...";
                            }else{
                              echo $item_videos[$playId]['srcs'][0]->title;
                            }
                            ?>
                        </a>

                    </div>

                    <div class="next-video" onclick="new_function_for_next_play();" style="display: inline-block;">
                        <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        .'play_next.png';?>" alt="Next">
                    </div>
                </div>
            <?php
            }?>

            <div class="media-lib-play<?php if($layout == 'list') echo '-list';?>">
                <?php foreach ($item_videos as $kay => $item_video) {
                    $srcs = $item_video['srcs'];
                    $row = $item_video['row'];
                    if ($item_video['youtube']) {
                        $play_icon = 'play_yt.png';
                    } else {
                        $play_icon = 'play.png';
                    }
                    if (isset($srcs) && count($srcs) > 0 ) {
                        if  ($layout != 'list'){?>
                            <div class="okno-player <?php echo 'kay-'.$kay.' '; if ($kay == $playId)
                                echo 'media-lib-now-play' ?>"
                                 onclick="change_video(<?php echo $play_N.', '. $kay;?>)">
                                <div class="play-icon">
                                    <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        .$play_icon ?>" alt="Play">
                                </div>
                                <img src="<?php if (isset($row->imageURL) && $row->imageURL != '') {
                                    echo $row->imageURL; }
                                      else { echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        ._MEDIALIBRARY_NO_PICTURE ;}
                                ?>" alt="<?php echo $row->title;?>" style="
                                      height:<?php echo $medialibrary_configuration['foto']['high'];?>px;
                                      width:<?php echo $medialibrary_configuration['foto']['width'];?>px;">
                                <div class="titlemedia">
                                    <?php
                                    $link_item = 'index.php?option=com_medialibrary&task=view&id='
                                    . $row->id . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                                    <a href="<?php echo sefRelToAbs($link_item); ?>">
                                        <?php if(mb_strlen($srcs[0]->title)>10){
                                            echo mb_substr($srcs[0]->title,0,10).'..';
                                        }else{
                                            echo $srcs[0]->title;
                                        }?>
                                    </a>

                                </div>
                                <div class="hitsmedia">
                                    <?php echo $row->hits.' '._MEDIALIBRARY_LABEL_HITS;?>
                                </div>
                                <div class="datemedia">
                                    <?php
                                    //       PRINT HOW MATCH TIME MEDIA IN THE SITE
                                    // $data_type = explode(';', _MEDIALIBRARY_MANAGER_DATE);
                                    // $date_now = array('Y' => date("Y"), 'm' => date("m"), 'j' => date("j"));
                                    // $date_item = array( 'Y' => substr($row->date,0, 4),
                                    //    'm' => substr($row->date, 5, 2),
                                    //                     'j' => substr($row->date,8, 2) );
                                    // if (($yers = $date_now['Y'] - $date_item['Y']) > 0 ) echo $yers.' '
                                    //    . explode('-', $data_type[0])[1];
                                    // elseif (($month = $date_now['m'] - $date_item['m']) > 0 ) echo $month.' '
                                    //    .explode('-', $data_type[1])[1];
                                    // elseif (($days = $date_now['j'] - $date_item['j']) > 0 ) echo $days.' '
                                    //    .explode('-', $data_type[2])[1];
                                    // else echo '0 '.explode('-', $data_type[2])[1];
                                    ?>
                                </div>
                            </div>
                        <?php
                        } else {?>
                        <div class="media-lib-play-list-item  <?php echo 'kay-'.$kay.' ';
                                 if ($kay == $playId) echo 'media-lib-now-play' ?>"
                                 onclick="change_video(<?php echo $play_N.', '. $kay;?>)">
                          <?php
                          $imageURL = $row->imageURL;
                          ?>
                          <div class = "media-lib-play-list-item-logo" >
                             <?php
                            if ($imageURL != '') {
                                    if($imageURL != '' && substr($imageURL,0,4) != "http"){
                                        $imageURL = $mosConfig_live_site . '/'. $imageURL;
                                    }
                                    echo '<img style="height: 50px; width: 40px;" src="' .$imageURL. '" border="0">';
                            } else {
                                    echo '<img  style="height: 50px;width: 40px;" src="' .
                                     './components/com_medialibrary/images/'._MEDIALIBRARY_NO_PICTURE .
                                     '" alt="no-img_eng_big.gif" border="0"  />&nbsp;';
                            }?>
                          </div>
                          <div class="media-lib-play-list-item-title">
                              <p>
                                <?php
                                if(mb_strlen($srcs[0]->title)>19){
                                    echo mb_substr($srcs[0]->title,0,17).'..';
                                }else{
                                    echo $srcs[0]->title;
                                }?>
                              </p>
                              <?php
                                $link_item = 'index.php?option=com_medialibrary&task=view&id='
                                . $row->id . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                                <a href="<?php echo sefRelToAbs($link_item); ?>">
                                    <?php if(mb_strlen($row->title)>19){
                                        echo mb_substr($row->title,0,17).'..';
                                    }else{
                                        echo $row->title;
                                    }?>
                                </a>
                          </div>
                          </div>
                        <?php
                        }
                    }?>
                <?php
                }?>

            </div>
            <?php if  ($layout == 'list'){?>
                <div class="media-lib-change-play">
                    <div class="prev-video"  onclick="new_function_for_back_play();" style="display: inline-block;">
                        <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        .'play_back.png';?>" alt="Back">
                    </div>

                    <div id="media-lib-item-linc<?php echo $play_N;?>" class="video-link" >
                        <?php
                        $link_item = 'index.php?option=com_medialibrary&task=view&id='
                            . $item_videos[$playId]['row']->id . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                        <a href="<?php echo sefRelToAbs($link_item); ?>">
                            <?php
                            if(strlen($item_videos[$playId]['srcs'][0]->title)>100){
                              echo substr($item_videos[$playId]['srcs'][0]->title,0,100),"...";
                            }else{
                              echo $item_videos[$playId]['srcs'][0]->title;
                            }
                            ?>
                        </a>

                    </div>

                    <div class="next-video" onclick="new_function_for_next_play();" style="display: inline-block;">
                        <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                        .'play_next.png';?>" alt="Next">
                    </div>
                </div>
            <?php
            }?>
        </div>

        <div class="ml-collumn-xs-12 ml-collumn-sm-12 ml-collumn-md-4 ml-collumn-lg-5 music_list_right_content">
            <?php positions_ml($params->get('singleuser01'));?>
        </div>
<?php
positions_ml($params->get('singleuser04'));
positions_ml($params->get('singlecategory04'));
?>
    
    <div style="clear: both;"></div>
    <script type="text/javascript" charset="utf-8">
        var link_subs = [];
        <?php
        $url = JFactory::getURI();
        $url_c = explode('index.php', $url->toString());
        $url_site = $url_c[0];
        foreach ($item_tracks as $kay => $value) {
                $part_of_link_item = $mosConfig_live_site . '/components/com_medialibrary/media/'
                        .'track/'. $value['sub_srcs'][0]->src;?>

            <?php if (count($item_tracks[$kay]['sub_srcs'])){?>
                link_subs[<?php echo $kay;?>] = '<select name="chuse_add_sub" onchange="ml_add_sub()">';
                        link_subs[<?php echo $kay;?>] += '<option value=""><?php echo _MEDIALIBRARY_NO;
                            ?> </option>';
                    <?php
                    foreach ($item_tracks[$kay]['sub_srcs'] as $subs) {
                            ?>link_subs[<?php echo $kay;?>] += '<option value="<?php echo $subs->src;
                                ?>"><?php echo $subs->scrlang;?></option>';
                    <?php
                    }?>

                link_subs[<?php echo $kay;?>] += '</select>';
            <?php
            }?>

        <?php
        } ;?>



        <?php
        if ($playSub){?>
              jQuerMedia(document).ready(function($) {
                  setTimeout('addOneSub()',2000);
              });

        <?php
        };?>
        function addOneSub(){
                jQuerMedia('#jp_video_0').append('<track src="<?php 
                    echo $mosConfig_live_site;?>/components/com_medialibrary/media/track/<?php 
                    echo $playSub;
                ?>" label="English subtitles" kind="subtitles" srclang="en" default="default"/>');

            first_add_subtitles = 0;
        }
        function ml_add_sub(){
            var chuse_sub = jQuerMedia('[name=chuse_add_sub]').val();
            //console.log(chuse_sub);
            //change_video(global_play_N, global_playId);

            var link_item = jQuerMedia('#sub_linck').children().attr('href')
            link_item = link_item.replace('Global_playId', global_playId);
            var currentTime = Math.round($("#jquery_jplayer_<?php echo $play_N;
                ?>").data("jPlayer").status.currentTime);
            link_item = link_item.replace('CurrTime', currentTime);
            var limitstart = <?php echo $limitstart;?>;
            link_item = link_item.replace('Limitstart', limitstart);
            var select = jQuerMedia('[name=chuse_add_sub]').children();
            var lenSub;
            for (var i = 0; select[i]; i++) {
                if (chuse_sub == select[i].value){
                    lenSub = select[i].text;
                }
            }

            link_item = link_item.replace('LenSub', lenSub);
            if (chuse_sub && first_add_subtitles != 1){
                window.location.href = link_item;
            } else {
                first_add_subtitles = 0;
            }


            jQuerMedia('#jp_video_0').empty();
            if (chuse_sub.length) {
                jQuerMedia('#jp_video_0').append('<track src="<?php
                        echo $mosConfig_live_site;?>/components/com_medialibrary/media/track/'
                        +chuse_sub+'" label="English subtitles" kind="subtitles" srclang="en" '
                        +'default="default"/>');

                var track = jQuerMedia('#jp_video_0').children()[0];
                    track.src = "<?php echo $mosConfig_live_site;
                        ?>/components/com_medialibrary/media/track/"+chuse_sub;
            }
        }
        <?php 
        if ($layout == 'list'){?>
            jQuerMedia(document).ready(function(){
                var width_container = jQuerMedia('#ML-player');
                var width_select_list = jQuerMedia('.media-lib-play-list');
                var width_player = +'<?php echo $video_width;?>'.slice(0, -2);
                if ((width_select_list.width() + width_player) > width_container.width()){
                    width_select_list.width(width_player);
                    width_select_list.css('float', 'left');
                }
            });
        <?php 
        }?>
    </script>
       <?php
        if(count($item_videos[$playId]['srcs']) > 0 ){
          //end div with video
        ?>
            </div>
        <?php
        }
        ?>
    <?php 
positions_ml($params->get('singleuser05'));
positions_ml($params->get('singlecategory05'));
      if ($pageNav !=null && $pageNav->total > $pageNav->limit ) { ?>

          <div id="ml-paginator-custom" class="pagination-custom">
              <?php echo $pageNav->getPagesLinks( ); //for 1.6 ?>
          </div>

      <?php
      } 

positions_ml($params->get('singleuser06'));
positions_ml($params->get('singlecategory06'));
       ?>

    <?php if ($params->get('back_button')!= ''): ?>
            <div style="text-align: center;" class="back_button">
                <?php mosHTML::BackButton ( $params, $hide_js ); ?>
            </div>
    <?php endif ?>

      </div>
                

    <style type="text/css" media="screen">
        .media-lib-item-linc{
            margin: 25px;
        }
        .play-icon{
            position: absolute;
        }
    </style>
<?php
}?>
