<?php
/*
 * ------------------------------------------------------------------------
 * JA Blockk Template
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - Copyrighted Commercial Software
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites:  http://www.joomlart.com -  http://www.joomlancers.com
 * This file may not be redistributed in whole or significant part.
 * ------------------------------------------------------------------------
*/

defined('_JEXEC') or die;
use Joomla\CMS\Uri\Uri;
$item  			= $displayData;
$params  		= json_decode($item->attribs, true);
$desc 			= '';
//var_dump($item);
if (!isset($params['ctm_gallery'])) return;
$gallery = $params['ctm_gallery'];

if(is_array($gallery) && isset($gallery['src'])) {
	$thumbnail = $gallery['src'][0];
	$desc = $gallery['caption'][0];
}

if(!$thumbnail) {
  $images = json_decode($item->images);
  $thumbnail = @$images->image_intro;
}

$galleryId = 'ja-gallery-detail-'.$item->id;
?>

<?php if(is_array($gallery) && count($gallery['src']) > 1): ?>
  <div id="<?php echo $galleryId; ?>" class="ja-gallery-wrap">
    <?php $cnt = -1; ?>
    <?php foreach($gallery['src'] as $index => $src): $cnt++; ?>
    <div class="gallery-item">
      <div class="item-image">
        <a href="<?php echo htmlspecialchars(Uri::root(true).'/'.$src); ?>" data-toggle="lightbox" data-gallery="gallery-<?php echo $item->id ;?>" data-title="
          <div class='number-item'>
            <span><?php echo ($cnt + 1) ;?>/</span>
            <span class='total'><?php echo count($gallery['src']) ;?></span>
          </div>

          <?php if ($gallery['caption'][$index] !='') : ?>
            <div class='image-caption h5'>
              <?php echo $gallery['caption'][$index]; ?>
            </div>
          <?php endif; ?>

          <?php if ($gallery['desc'][$index] !='') : ?>
          <div class='image-desc'>
            <?php echo $gallery['desc'][$index]; ?>
          </div>
          <?php endif; ?>
          ">
          <img src="<?php echo htmlspecialchars(Uri::root(true).'/'.$src); ?>" alt="<?php echo htmlspecialchars($gallery['caption'][$index]); ?>" itemprop="thumbnailUrl"/>
        </a>
      </div>

      <div class="number-item">
        <span><?php echo ($cnt + 1) ;?></span>
        <span class="total">/<?php echo count($gallery['src']) ;?></span>
      </div>

      <?php if ($gallery['caption'][$index] !='') : ?>
      <div class="image-caption h5">
        <?php echo $gallery['caption'][$index]; ?>
      </div>
      <?php endif; ?>

      <?php if ($gallery['desc'][$index] !='') : ?>
      <div class="image-desc">
        <?php echo $gallery['desc'][$index]; ?>
      </div>
      <?php endif; ?>
    </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<script>
(function($){
  $(document).ready(function(){
    if($('.ja-gallery').length > 0) {
      $('.ja-gallery-wrap').appendTo('.ja-gallery');
    }

    $(document).on('click', '[data-toggle="lightbox"]', function(event) {
      event.preventDefault();
      $(this).ekkoLightbox();
    });
  });
})(jQuery);
</script>