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

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


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

$playId = -1;

$play_N = 1;

if ($params->get('playId') != ''){
    $playId = $params->get('playId');
}
$item_audios = [];
echo '<div class="ml_search_result_page ml_search_result_music" 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_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 (isset($item_audios) && count($item_audios)){
    $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.'.music.css');
    $youtube = 0;
    if ($youtube){
    } else { ?>
        <script type="text/javascript">
        var play_direction = 'next';
        function thisSourceNotSappot() {
            if (play_direction === 'next'){
                new_function_for_next_play();
            }
            if (play_direction === 'prev'){
                audio_back_play();
            }
        }
        //<![CDATA[
        jQuerMedia(document).ready(function(){
            jQuerMedia("#jquery_jplayer_1").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 JURI::root();
                        ?>/components/com_medialibrary/includes/jQuerMedia.jplayer.swf",
                supplied: "webma, m4a, oga, mp3",
                useStateClassSkin: true,
                autoBlur: false,
                cssSelectorAncestor: "#jp_container_<?php echo $play_N;?>",
                smoothPlayBar: true,
                keyEnabled: true,
                audioFullScreen: true,
                wmode: "window",
                nextPlay:  "<?php if ($next_play_list_item > 0){
                                echo sefRelToAbs('index.php?option=com_medialibrary&view=allmusic&Itemid='
                                    .$Itemid.'&playId='.$next_play_list_item.'&playAudio=1');
                            }?>"

            });

        });

        </script>

        <script>
            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 link_items = [];
            var title_items = [];
            <?php
            foreach ($item_audios as $kay => $value) {
                $title_file = $value['srcs'][0]->title;
                $part_of_link_item = 'index.php?option=com_medialibrary&task=view&id='
                    . $value['row']->id . '&catid=' .$catid .'&Itemid='. $Itemid;?>
                link_items[<?php echo $kay;?>] = '<a href="<?php
                    echo sefRelToAbs($part_of_link_item); ?>">'+"<?php echo $title_file;?>"+'</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_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_play_N;
                var old_playId = global_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_playId = playId;
                global_play_N = play_N;
                var theA = document.createElement('a');
                theA.innerHTML = link_items[playId];
                jQuerMedia("#media-lib-item-linc"+play_N).empty().append(theA);

                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 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_AUDIO."</h5> " ; ?>
                </div>
                <div id="jp_container_1" class="jp-media-lib-audio" role="application" aria-label="media player">
                    <div class="jp-media-lib-type-single">
                        <div id="jquery_jplayer_1" class="jp-media-lib-jplayer"></div>
                        <div class="jp-media-lib-gui jp-media-lib-interface">
                            <div class="jp-media-lib-volume-controls volume-controls-music">
                                <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-button_bar">
                                    <div  class="prev-music-button-holder" style="display: inline-block;">
                                        <div class="prev-music jp-control-button" onclick="audio_back_play();" style="display: inline-block;">
                                            <i class="fa fa-backward"></i>
                                        </div>
                                    </div>
                                    <div class="jp-media-lib-controls music-media-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="next-music-button-holder" style="display: inline-block;">
                                        <div  class="next-music jp-control-button" onclick="new_function_for_next_play();"
                                        style="display: inline-block;">
                                            <i class="fa fa-forward"></i>
                                        </div>
                                    </div>
                                </div>
                                <div class="jp-media-lib-time-parametrs-box">
                                    <div class="jp-media-lib-progress-audio">
                                        <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>
                                <!-- <div class="jp-media-lib-toggles">
                                    <button class="jp-media-lib-repeat" role="button" tabindex="0">repeat</button>
                                </div> -->
                            </div>
                        </div>
                        <div class="jp-media-lib-details">
                            <div class="jp-media-lib-title" aria-label="title">&nbsp;</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' && 0){?>
                    <div class="media-lib-change-music-play-default">
                        <div id="media-lib-item-linc<?php echo $play_N;?>"  style="display: inline-block;">
                            <?php
                            $link_item = 'index.php?option=com_medialibrary&task=view&id='. $item_audios[$playId]['row']->id
                                    . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                            <a href="<?php echo sefRelToAbs($link_item); ?>">
                                <?php
                                if(mb_strlen($item_audios[$playId]['srcs'][0]->title)>100){
                                  echo mb_substr($item_audios[$playId]['srcs'][0]->title,0,100),"...";
                                }else{
                                  echo $item_audios[$playId]['srcs'][0]->title;
                                }
                                ?>
                            </a>
                        </div>
                    </div>
                <?php
                }?>

            <div class="media-lib-play-music-list">
                <?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 ) {
                        if  ($layout != 'list'){?>
                            <div class="okno-player <?php echo 'audio-kay-'.$kay.' '; if ($kay == $playId)
                                echo 'media-lib-now-play' ?>"
                                 onclick="change_treck(<?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 (isset($srcs[0]->title)) $title_file = $srcs[0]->title;
                                    else $title_file = $row->title;
                                    if(mb_strlen($title_file)>18) $title_file = mb_substr($title_file,0,16).'...';
                                    $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>
                                <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="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
                        }
                    }?>
                <?php
                }?>

                <?php 
                
    positions_ml($params->get('singleuser04'));
    positions_ml($params->get('singlecategory04'));
                
                if  ($layout == 'list' && 0){?>
                    <div class="media-lib-change-music-play">
                        <div  onclick="audio_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;?>"  style="display: inline-block;">
                            <?php
                            $link_item = 'index.php?option=com_medialibrary&task=view&id='. $item_audios[$playId]['row']->id
                                    . '&catid='.$catid .'&Itemid='. $Itemid; ?>
                            <a href="<?php echo sefRelToAbs($link_item); ?>">
                                <?php
                                if(mb_strlen($item_audios[$playId]['srcs'][0]->title)>100){
                                  echo mb_substr($item_audios[$playId]['srcs'][0]->title,0,100),"...";
                                }else{
                                  echo $item_audios[$playId]['srcs'][0]->title;
                                }
                                ?>
                            </a>

                        </div>
                        <div 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>

        <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>

        </div>

      <?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
    }
}?>
