<?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
 *
 */

function play_one_video($row, $params){
    $play_N = 1;
    if ($row->group_videos != [] && $params != []){        
        global $doc, $mosConfig_live_site, $catid, $Itemid, $medialibrary_configuration;

        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 = '';

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

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

        $item_videos = [];
        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 = [];
        if (isset($row->group_tracks)) {
            $item_track = [];
            foreach ($row->group_tracks as $kay => $value) { 
                $item_track[$kay]['sub_srcs'] = $value;
            }
            $item_tracks = $item_tracks + $item_track;
        }
        
        $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.'.css');
        $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 ;
            }
        }
        // print_r(":1111111111111111:");
        // print_r($item_videos);
        // print_r(":1111111111111111:");
        // print_r($youtube);
        //exit ;        

        //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 play_direction = 'next';
            function thisSourceNotSuppot() {

                if (play_direction === 'next'){
                    new_function_for_next_play();
                } 
                if (play_direction === 'prev'){
                    new_function_for_prev_play();
                }
            }

            // create youtube player
            var player ; 
            function onYouTubePlayerAPIReady(playId,  autoplay) {

                if( !jQuerMedia("#player") ) return ;

                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>



            <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" >     
            var now_i_play_type = 'video';
            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($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 ($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&id='
                                    .$row->global_id.'&task=view&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 title_video_items = [];
            <?php
            foreach ($item_videos as $kay => $value) {
                $title_file = $value['srcs'][0]->title;
                if($title_file != ''){?>
                    title_video_items[<?php echo $kay;?>] =  '<?php echo $title_file;?>';
                <?php
                }else {?>
                    title_video_items[<?php echo $kay;?>] =  'youtube';    
                <?php  
                }
            }?>
            function new_function_for_next_play (){
                if (now_i_play_type == 'video'){
                    next_video_play();
                }
                if (now_i_play_type == 'audio'){
                    next_audio_play();
                }

            }
            function new_function_for_prev_play (){
                    if (now_i_play_type == 'video'){
                        new_function_for_back_play();
                    }
                    if (now_i_play_type == 'audio'){
                        audio_back_play();
                    }
                }
            var global_video_play_N = <?php echo $play_N;?>;
            var global_video_playId = <?php echo $playId;?>;

            function next_video_play(){
                play_direction = 'next';
                var play_N = global_video_play_N;
                var old_playId = global_video_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_video_play_N;
                var old_playId = global_video_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_video_playId = playId;
                global_playId = playId;
                global_play_N = play_N;
                now_i_play_type = 'video';
                var theSpan = document.createElement('span');
                theSpan.innerHTML = title_video_items[playId];
                jQuerMedia("#media-lib-item-linc"+play_N).empty().append(theSpan);
                jQuerMedia(".media-lib-now-play").removeClass('media-lib-now-play');
                jQuerMedia(".kay-"+playId).addClass('media-lib-now-play');
                if (item_videos[playId]['youtube']){ 
                    jQuerMedia("#jp_container_<?php echo $play_N;?>").hide();
                    // var theIFrame = '<iframe width="540" height="320" frameborder="0" ';
                    // var theIFrame+= 'src="https://www.youtube.com/embed/'+item_videos[playId]['youtube'];
                    // theIFrame+= '"></iframe>';
                    // jQuerMedia("#youtube_player_"+play_N).empty().append(theIFrame);
                    jQuerMedia("#youtube_player_"+play_N).empty().append('<div id="player"></div>');
                    jQuerMedia("#jquery_jplayer_"+play_N).jPlayer("stop");
    //                onYouTubePlayerAPIReady(item_videos[playId]['youtube'], "player", autoplay);
                    onYouTubePlayerAPIReady(item_videos[playId]['youtube'], autoplay);
                } else {
                    jQuerMedia("#youtube_player_"+play_N).empty();
                    jQuerMedia("#jp_container_<?php echo $play_N;?>").show();
                    var promise = jQuerMedia("#jquery_jplayer_"+play_N).jPlayer("setMedia", item_videos[
                        playId]).jPlayer("play");

                    //$medialibrary_configuration['tracks']['location'] .
                }
            }
        </script>
        <noscript>
            Javascript is required to use MediaLibrary 
            <a href="https://ordasoft.com/media-library-joomla-extension">
                Media Library Software for create modern library .
            </a>,
            <a href="https://ordasoft.com/media-library-joomla-extension">
                MediaLibrary create website for rent, sell media files
            </a>
        </noscript>

        <div id="ML-player" class="ml-collumn-xs-12 ml-collumn-sm-12 ml-collumn-md-8 ml-collumn-lg-7">



            <div id="jp_container_<?php echo $play_N;?>" 
                class="jp-media-lib-video jp-media-lib-video-360p" 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>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="jp-media-lib-no-solution">
                        <div id="sub_linck" style="display:none;">
                            <?php $link_item = sefRelToAbs('index.php?option=com_medialibrary&'
                            .'task=view&Itemid='.$Itemid.'&catid=0&id='.$row->global_id
                            .'&playId=Global_playId&playVideo=1&currTime=CurrTime&lenSub=LenSub')
                            ?><a href="<?php echo $link_item;?>" ></a>
                        </div>
                        <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>
                
            
            <!-- <div id="media-lib-item-linc<?php echo $play_N;?>">
                <?php 
                if(strlen($item_videos[$playId]['row']->title)>100){
                  echo substr($item_videos[$playId]['row']->title,0,100),"...";
                }else{
                  echo $item_videos[$playId]['row']->title;
                }?>
            </div> -->
            <div class="media-lib-play-next-prev">   
                <div  onclick="new_function_for_back_play();" style="display: inline-block;" 
                class="media-lib-play-prev-video">
                    <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;?>"  style="display: inline-block;">
                    <?php
                    $video_title = $item_videos[$playId]['srcs'][0]->title;
                    if($video_title != ''){
                      echo $video_title;
                    }
                    ?>
                    
                </div>

                <div onclick="next_video_play();" style="display: inline-block;" 
                class="media-lib-play-next-video">
                    <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                    .'play_next.png';?>" alt="Next">
                </div>
                <!-- <input type="button" onclick="new_function_for_back_play();" value="back"></input> 
                <input type="button" onclick="new_function_for_next_play();" value="next"></input>  -->
            </div>
            <div class="media-lib-play">
                <?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 ) {

                    ?>
                        <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 if(mb_strlen ($srcs[0]->title)>10) echo mb_substr($srcs[0]->title,0,10),'...';
                                else {
                                    echo $srcs[0]->title;
                                }?>
                            </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 
                    }?>
                <?php 
                }?>
            </div>
        </div>

        <script type="text/javascript" charset="utf-8">
            var link_subs = [];
            <?php 
            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 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;
                }
            }
        </script>

       <?php
        if(count($item_videos[$playId]['srcs']) > 0 ){
          //end div with video
        ?>
            </div>
        <?php
        }
        ?>

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

        </style>


  
           
    <?php
    }
}
function play_one_audio($row, $params){
  
    $play_N = 2;
    if ($row->group_audios != [] && $params != []){        
        global $doc, $mosConfig_live_site, $catid, $Itemid, $medialibrary_configuration;
        if ($params->get('audio_player_skin') != ''){
            $name_skin = $params->get('audio_player_skin'); // blue.monday | pink.flag
        } else {
            $name_skin = 'blue.monday';
        }
        $name_skin = 'pink.flag';

        $playId = -1;

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

        $item_audios = [];
        if ($row->group_audios) {
            $item_audio = [];
            foreach ($row->group_audios as $kay => $value) {
                $item_audio[$kay]['srcs'] = $value;
                $item_audio[$kay]['row'] = $row;
            }
            $item_audios = $item_audios + $item_audio;
        }
        
        if(count($item_audios) > 0 ){
          //start div with audio
          ?>
            <div class="ml-collumn-xs-12 ml-collumn-sm-12 ml-collumn-md-12 ml-collumn-lg-12">
          <?php
        }
                
        $next_play_list_item = 0; 
        if (!isset($item_audios[$playId])){$playId = -1;}
        foreach ($item_audios as $kay => $value) {
            if (!isset($item_audios[$playId]) && $playId == -1)  $playId = $kay; 
            if ($next_play_list_item == -1) $next_play_list_item = $kay;
            if ($kay == $playId) $next_play_list_item = -1;
        }
        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.'.css');
        $youtube = 0;
        if ($youtube){
        } else { ?>
            <script type="text/javascript">
            var play_direction = 'next';
            function thisSourceNotSuppot() {
                if (play_direction === 'next'){
                    new_function_for_next_play();
                } 
                if (play_direction === 'prev'){
                    new_function_for_prev_play();
                }
                    
            }
            //<![CDATA[
            jQuerMedia(document).ready(function(){
                jQuerMedia("#jquery_jplayer_<?php echo $play_N;?>").jPlayer({
                    ready: function () {
                        jQuerMedia(this).jPlayer("setMedia", {
                        <?php $row = $item_audios[$playId]['row'];
                            $srcs = $item_audios[$playId]['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 $audio){
                                    $array_format = explode('.', $audio->src);
                                    $format = array_pop($array_format);
                                    $ormat_lower = strtolower($format);  
                                    if ($ormat_lower == 'ogg') { ?>
                                        oga:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'mp3') {  ?>
                                        mp3:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'm4a') {  ?>
                                        m4a:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'webm') {  ?>
                                        webma:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->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 ($params->get('playAudio')) echo '.jPlayer("play")'?>;
                    },
                    swfPath: "<?php echo $mosConfig_live_site 
                        ?>/components/com_medialibrary/includes/jQuerMedia.jplayer.swf",
                    supplied: "webma, m4a, oga, mp3",
                    useStateClassSkin: true,
                    autoBlur: false,
                    smoothPlayBar: true,
                    keyEnabled: true,
                    audioFullScreen: true,
                    wmode: "window",
                    cssSelectorAncestor: "#jp_container_<?php echo $play_N;?>",
                    nextPlay:  "<?php if ($next_play_list_item > 0){
                                   echo sefRelToAbs('index.php?option=com_medialibrary&id='
                                        .$row->global_id.'&task=view&Itemid='
                                        .$Itemid.'&playId='.$next_play_list_item.'&playAudio=1'); 
                                }?>"
                        
                });

            });

            </script>
            
            <script  type="text/javascript"  >
                var item_audios = new Array();
                var queue_item_audios = new Array();
                <?php
                foreach ($item_audios as $kay => $value) {
                    if ($value['srcs'] != ''){?>
                        queue_item_audios.push(<?php echo $kay ?>);
                        item_audios[<?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 $audio){
                                    $array_format = explode('.', $audio->src);
                                    $format = array_pop($array_format);
                                    $ormat_lower = strtolower($format);  
                                    if ($ormat_lower == 'ogg') { ?>
                                        oga:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'mp3') {  ?>
                                        mp3:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'm4a') {  ?>
                                        m4a:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->src ?>",
                                    <?php } elseif ($ormat_lower == 'webm') {  ?>
                                        webma:" <?php echo $mosConfig_live_site
                                            .$medialibrary_configuration['audios']['location']
                                            .$audio->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 
                    }
                } ?> 
                var title_audio_items = [];
                <?php 
                foreach ($item_audios as $kay => $value) {?>
                    title_audio_items[<?php echo $kay;?>] =  "<?php echo 
                    str_replace('"', "'", $value['srcs'][0]->title);?>";
                <?php
                }?>
                var now_i_play_type = '';
                function new_function_for_next_play (){
                    if (now_i_play_type == 'video'){
                        next_video_play();
                    }
                    if (now_i_play_type == 'audio'){
                        next_audio_play();
                    }

                }
                function new_function_for_prev_play (){
                    if (now_i_play_type == 'video'){
                        new_function_for_back_play();
                    }
                    if (now_i_play_type == 'audio'){
                        audio_back_play();
                    }
                }

                var global_audio_play_N = <?php echo $play_N;?>;
                var global_audio_playId = <?php echo $playId;?>;
                function next_audio_play(){
                    play_direction = 'next';
                    var play_N = global_audio_play_N;
                    var old_playId = global_audio_playId;
                    var playId = ''; 
                    for(var kay in queue_item_audios){
                        if (parseInt(queue_item_audios[kay]) == queue_item_audios[kay]){
                            if(playId == -1){
                                playId = queue_item_audios[kay];
                            }
                            if (queue_item_audios[kay] == old_playId){
                                playId = -1;
                            }
                        }
                    }
                    if (playId != -1){
                        change_treck(play_N, playId);
                    }
                } ;
                function audio_back_play (){
                    play_direction = 'prev';
                    var play_N = global_audio_play_N;
                    var old_playId = global_audio_playId;
                    var playIdNow = ''; 
                    var playId = ''; 
                    for(var kay in queue_item_audios){
                        if (parseInt(queue_item_audios[kay]) == queue_item_audios[kay]){
                            if (queue_item_audios[kay] == old_playId){
                                playIdNow = -1;
                            }
                            if(playIdNow != -1){
                                playId = queue_item_audios[kay];
                            }
                        }
                    }
                    if (playId != ''){
                        change_treck(play_N, playId);
                    }
                } ;
                function change_treck(play_N, playId){
                    global_audio_playId = playId;
                    now_i_play_type = 'audio';
                    var theSpan = document.createElement('span');
                    theSpan.innerHTML = title_audio_items[playId];
                    jQuerMedia("#media-lib-item-linc"+play_N).empty().append(theSpan);

                    jQuerMedia(".media-lib-now-play").removeClass('media-lib-now-play');
                    jQuerMedia(".audio-kay-"+playId).addClass('media-lib-now-play');

                    jQuerMedia("#jquery_jplayer_"+play_N).jPlayer("setMedia", 
                            item_audios[playId]).jPlayer("play");
                }
            </script>

            <div id="jp_container_<?php echo $play_N;
                ?>" class="jp-media-lib-audio" 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 jp-media-lib-interface">
                        <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-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-time-parametrs-box">
                                <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-toggles">
                                    <button class="jp-media-lib-repeat" role="button" tabindex="0">
                                        repeat
                                    </button>
                                </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>
            <div class="media-lib-play-next-prev">   
                <div  onclick="audio_back_play();" style="display: inline-block;" class="media-lib-play-prev-audio">
                    <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;?>"  style="display: inline-block;">
                    <?php
                    $title_file = $item_audios[$playId]['srcs'][0]->title;
                    if($title_file != ''){
                      echo $title_file;
                    }
                    ?>
                    
                </div>

                <div onclick="next_audio_play();" style="display: inline-block;" class="media-lib-play-next-audio">
                    <img src="<?php echo $mosConfig_live_site.'/components/com_medialibrary/images/'
                                    .'play_next.png';?>" alt="Next">
                </div>
                <!-- <input type="button" onclick="new_function_for_back_play();" value="back"></input> 
                <input type="button" onclick="new_function_for_next_play();" value="next"></input>  -->
            </div>
        <!-- <div id="media-lib-item-linc<?php echo $play_N;?>">
            <?php 
            if(strlen($item_audios[$playId]['row']->title)>100){
              echo substr($item_audios[$playId]['row']->title,0,100),"...";
            }else{
              echo $item_audios[$playId]['row']->title;
            }?>
        </div> -->

        <div class="media-lib-play">
            <?php foreach ($item_audios as $kay => $item_audio) {
                $srcs = $item_audio['srcs'];
                $row = $item_audio['row'];
                $play_icon = 'play.png';
                if (isset($srcs) && count($srcs) > 0 ) {
                    //$item_audio_link = 'index.php?option=com_medialibrary&task=view&id='
                    // .$row->global_id.'&Itemid='.$Itemid.'&playId='.$kay.'&playAudio=1';
                    if ($kay != $playId){?>
                       <!--  <a href="<?php  echo sefRelToAbs($item_audio_link); ?>" > -->
                    <?php 
                    }?>
                    <div 
                     class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?> 
                        media-lib-music-list-item <?php echo 'audio-kay-'.$kay.' '; if ($kay == $playId) 
                        echo 'media-lib-now-play' ?>"  onclick="change_treck(<?php echo $play_N.', '. $kay;?>)">
                            <i class="fa fa-microphone"></i>
                            <?php
                            if (isset($srcs[0]->title)) echo $srcs[0]->title;
                            ?>
                            <?php
                            $title_file = $row->title;
                            $part_of_link_item = 'index.php?option=com_medialibrary&task=view&id='
                                . $row->id . '&catid=' .$catid .'&Itemid='. $Itemid;?>
                            - <a href="<?php echo sefRelToAbs($part_of_link_item); 
                                ?>"><?php //echo $title_file;?></a>
                    </div>

                    <?php 
                    // if ($kay != $playId){
                    //     echo '</a>';
                    // }
                }?>
            <?php 
            }?>
            
        </div>

       <?php
        if(count($item_audios) > 0 ){
          //end div with audio
       ?>
            </div>
       <?php
        }
       ?>


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

        </style>

           
    <?php
        }
    }
}
 ?>
