<?php
defined('_JEXEC') or die('Restricted access');
/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/

$fName = $field->db_field_name;
if(!isset($instance)) $instance = $entityInstance;
$cck_type_slider_or_image = isset($layout_params['fields']['cck_type_slider_or_image_'.$field->db_field_name]) ? $layout_params['fields']['cck_type_slider_or_image_'.$field->db_field_name] : 'slider';//slider or image

$cck_lazy_load = isset($layout_params['fields']['cck_lazy_load_'.$field->db_field_name]) ? $layout_params['fields']['cck_lazy_load_'.$field->db_field_name] : 0;
//var_dump($cck_lazy_load);
$data_src = ($cck_type_slider_or_image == 'slider' && $cck_lazy_load == 'on') ? 'data-src' : 'src';
$cck_light_box_in_main_image = isset($layout_params['fields']['cck_light_box_in_main_image_'.$field->db_field_name]) ? $layout_params['fields']['cck_light_box_in_main_image_'.$field->db_field_name] : 0;
if($cck_type_slider_or_image != 'slider' && $cck_light_box_in_main_image == 'on'){
  $gallery_lightbox_selector = 'group-' . $instance->eiid;
}else{
  $gallery_lightbox_selector = 'roadtrip_'.$fName.$field->fid;
}
if($cck_type_slider_or_image == 'slider'){
    // echo '{|style-cck-swiper|}'; //add style with help system plugin
    // echo '{|script-cck-swiper|}';//add script with help system plugin
    includeStylesCCK("components/com_os_cck/assets/css/swiper.css");
    includeJavaScriptCCK( "components/com_os_cck/assets/js/swiper-os.js");
}
if($cck_light_box_in_main_image == 'on'){
    // echo '{|style-cck-lightbox|}';//add style with help system plugin
    // echo '{|script-cck-lightbox|}';//add script with help system plugin
    includeStylesCCK('components/com_os_cck/assets/lightbox/css/lightbox.css');
    includeJavaScriptCCK('components/com_os_cck/assets/lightbox/js/lightbox-2.6.min.js');
}


$icon_alias_prefix = (isset($layout_params['fields'][$fName.'_add_icon_alias_prefix'])) ? $layout_params['fields'][$fName.'_add_icon_alias_prefix'] : '';
if($icon_alias_prefix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_prefix_hidden', 'fa '.$icon_alias_prefix, $layout_html);
}
$icon_alias_suffix = (isset($layout_params['fields'][$fName.'_add_icon_alias_suffix'])) ? $layout_params['fields'][$fName.'_add_icon_alias_suffix'] : '';
if($icon_alias_suffix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_suffix_hidden', 'fa '.$icon_alias_suffix, $layout_html);
}

$rand = rand();
// options_main_img
$width_heigth = (!empty($layout_params["fields"][$fName]["options"]["width"]))? 'width:'.$layout_params["fields"][$fName]["options"]["width"].'px;' : 'width:100px;';
$width_heigth .= (!empty($layout_params["fields"][$fName]["options"]["height"]))? ' height:'.$layout_params["fields"][$fName]["options"]["height"].'px;' : 'height:100px;';
$width_main_img = (!empty($layout_params["fields"][$fName]["options_main_img"]["width"]))? $layout_params["fields"][$fName]["options_main_img"]["width"] : '300';
$height_main_img = (!empty($layout_params["fields"][$fName]["options_main_img"]["height"]))? $layout_params["fields"][$fName]["options_main_img"]["height"] : '200';
// var_dump($width_main_img);
// var_dump($height_main_img);
if(isset($GLOBALS['number_of_plugin'])){
    $number_of_plugin = $GLOBALS['number_of_plugin'];
}else{
    $number_of_plugin = '';
}
//for unique value to each slider


$layout_instanse_id = $layout->lid . '_' . $instance->eiid;

?>

<span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
        {?>
  rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
    <?php } ?> 
>
  <?php

    if(isset($layout_params['fields'][$fName.'_add_icon_prefix_prefix']) && !empty($layout_params['fields'][$fName.'_add_icon_prefix_prefix'])){
        echo '<span class="fa '.$layout_params['fields'][$fName.'_add_icon_prefix_prefix'].'"></span>';
    }
    if(!empty($layout_params['fields'][$fName.'_prefix'])){
      echo '<span class="cck-prefix">'.$layout_params['fields'][$fName.'_prefix'].'</span>';
    }
    if(isset($layout_params['fields'][$fName.'_add_icon_prefix_suffix']) && !empty($layout_params['fields'][$fName.'_add_icon_prefix_suffix'])){
        echo '<span class="fa '.$layout_params['fields'][$fName.'_add_icon_prefix_suffix'].'"></span>';
    }
  
    if(isset($layout_params['fields']['showName_'.$field->db_field_name]) &&
        $layout_params['fields']['showName_'.$field->db_field_name] == 'on'){
        $layout_html = str_replace($field->db_field_name.'-label-hidden', '', $layout_html);
    }

    
    //add settings
    $cck_on_gallery = isset($layout_params['fields']['cck_on_gallery_'.$field->db_field_name]) ? $layout_params['fields']['cck_on_gallery_'.$field->db_field_name] : 0;// 1 or 0
    $cck_on_slider_main_image = isset($layout_params['fields']['cck_on_slider_main_image_'.$field->db_field_name]) ? $layout_params['fields']['cck_on_slider_main_image_'.$field->db_field_name] : 0;//1 or 0
    
    $cck_slider_height = isset($layout_params['fields']['cck_slider_height_'.$field->db_field_name]) ? $layout_params['fields']['cck_slider_height_'.$field->db_field_name]/100 : 57/100; // 57%
    $cck_slider_object_fit = isset($layout_params['fields']['cck_slider_object_fit_'.$field->db_field_name]) ? $layout_params['fields']['cck_slider_object_fit_'.$field->db_field_name] : 'contain'; //contain or cover

    
    
    if($cck_type_slider_or_image == 'image'){
      $allowTouchMove = "false";
    }else{
      $allowTouchMove = "true";
    }
    ?>
    
    <div class="gallery_<?php echo $fName?>" id="gallery_<?php echo $fName?>">
    

    <?php 
    $images = array();
    
    if(!empty($value[0]) && $value[0]->data != '' && $value[0]->data != '[]'){ 
        $images = json_decode($value[0]->data);
        
    } elseif ($layout_params['fields'][$fName.'_def_img'] != '') {
        $images[] = JURI::root().'images/os-cck/'.$layout_params['fields'][$fName.'_def_img'];
    }
    
    $images_from_gallery = $images;
    if($cck_on_slider_main_image == 'on'){?>
      <!-- slider start -->
      <div id="cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?>">
        <!-- Slider main container -->
        <div class="swiper-container">
            <!-- Additional required wrapper -->
            <div class="swiper-wrapper">
                <!-- Slides -->
   
                <?php if($cck_type_slider_or_image == 'image' && $images != '') $images = array_splice($images, 0 ,1); ?>
               <?php if(!empty($value[0]) && $value[0]->data != '' && $value[0]->data != '[]'){
                   
                 foreach ($images as $image){ 

                    if(!isset($image->name)) $image->name = '';
                    if(!isset($image->alt)) $image->alt = '';
                    if(!isset($image->file)) $image->file = '';
                ?>
                
                <div class="swiper-slide">
                  <?php if($cck_light_box_in_main_image == "on") echo '<a href="'.JURI::root().'images/com_os_cck'.$field->fid.'/original/'.$image->file.'" data-lightbox="group-' . $instance->eiid . '" title="' . $image->name . '">'; ?>

                  <?php //echo '<img style="width:100%; height:60%;" '.$data_src.'="'.JURI::root().'images/com_os_cck'.$field->fid.'/original/'.$image->file.'" alt="' . $image->alt . '" class="swiper-lazy"/>' ?>
                  <?php echo '<img style="width:100%" '.$data_src.'="'.JURI::root().show_image_cck('/images/com_os_cck'.$field->fid.'/original/'.$image->file, $width_main_img, $height_main_img).'" alt="' . $image->alt . '" class="swiper-lazy"/>' ?>
                    
                  <?php if($cck_light_box_in_main_image == "on") echo'</a>'; 
                  if($cck_type_slider_or_image == 'slider'){ ?>
                    <div class="swiper-lazy-preloader"></div>
                  <?php } ?>
                </div>
             <?php   }
                 }elseif (isset($layout_params['fields'][$fName.'_def_img']) && $layout_params['fields'][$fName.'_def_img'] != ''){ ?>
                <div class="swiper-slide">
                     <?php if($cck_light_box_in_main_image == "on") echo '<a href="'.JURI::root().'images/os-cck/'.$layout_params['fields'][$fName.'_def_img'].'" data-lightbox="group-<?php echo $instance->eiid; ?>">'; ?>

                  <?php echo '<img style="width:100%" '.$data_src.'="'.JURI::root().'images/os-cck/'.$layout_params['fields'][$fName.'_def_img'].'" class="swiper-lazy"/>' ?>
                    
                  <?php if($cck_light_box_in_main_image == "on") echo'</a>'; ?>
                    
                    </div>
                    <?php if($cck_type_slider_or_image == 'slider'){ ?>
                        <div class="swiper-lazy-preloader"></div>
                    <?php } 
                } ?>
                    
                <?php if($cck_type_slider_or_image == 'slider' && count($images) > 1){ ?>
              <!-- If we need navigation buttons -->
              
              <!-- If we need scrollbar -->
    <?php } ?>
            </div>
            <?php 

            if($cck_type_slider_or_image != 'image' && $images != ''){ ?>
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>
            <?php } ?>
        </div>  
      </div>
      
      <!-- slider end -->
    <?php } ?>

    <script>
      if('<?php echo $cck_type_slider_or_image; ?>' == 'slider'){
          jQuerOs(window).on('load', function(){
          
              var SwipeOs<?php echo $instance->eiid; ?> = new SwipeOs ('#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-container', {
                navigation: {
                  nextEl: '#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-button-next',
                  prevEl: '#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-button-prev',
                },
                allowTouchMove: <?php echo $allowTouchMove; ?>,
                lazy:  true
    
              })
              jQuerOs('#show_request_layout_<?php echo $layout_instanse_id; ?>').on('shown.bs.os_cck_modal', function (e) {
                  var SwipeOs<?php echo $instance->eiid; ?> = new SwipeOs ('#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-container', {
                    navigation: {
                      nextEl: '#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-button-next',
                      prevEl: '#cck-slider-container-<?php echo $fName?>-<?php echo $rand; ?> .swiper-button-prev',
                    },
                    allowTouchMove: <?php echo $allowTouchMove; ?>,
                    lazy:  true

                  })
                  loadGalleryfield('<?php echo $fName?>', '<?php echo $rand; ?>', '<?php echo $cck_slider_height; ?>', '<?php echo $cck_slider_object_fit; ?>', '<?php echo $number_of_plugin; ?>', '<?php echo $layout_instanse_id; ?>');
              })
              
          })
          
      }
      loadGalleryfield('<?php echo $fName?>', '<?php echo $rand; ?>', '<?php echo $cck_slider_height; ?>', '<?php echo $cck_slider_object_fit; ?>', '<?php echo $number_of_plugin; ?>', '<?php echo $layout_instanse_id; ?>');

    </script>

    <?php
      
    if($cck_on_gallery == 'on'){
        if(!empty($value[0]) && $value[0]->data != '' && $value[0]->data != '[]' && is_array($images_from_gallery)){
          
          foreach ($images_from_gallery as $img_num => $image){
            if($img_num == '0' && $cck_type_slider_or_image != 'slider' && $cck_light_box_in_main_image == 'on'){
              continue;
            }
                if(!isset($image->name)) $image->name = '';
                if(!isset($image->alt)) $image->alt = '';
                if(!isset($image->file)) $image->file = '';

                if($cck_light_box_in_main_image == "on") echo '<a href="'.JURI::root().'images/com_os_cck'.$field->fid.'/original/'.$image->file.'" data-lightbox="'.$gallery_lightbox_selector.'" title="' . $image->name . '">';
                echo '<img style="'.$width_heigth.'" src="'.JURI::root().'images/com_os_cck'.$field->fid.'/thumbnail/'.$image->file.'" alt="' . $image->alt . '"/>';
                if($cck_light_box_in_main_image == "on") echo'</a>';
          }
        }elseif (isset($layout_params['fields'][$fName.'_def_img']) && $layout_params['fields'][$fName.'_def_img'] != ''){
            if($cck_light_box_in_main_image == "on") echo '<a href="'.JURI::root().'images/os-cck/'.$layout_params['fields'][$fName.'_def_img'].'" data-lightbox="'.$gallery_lightbox_selector.'" >';
            echo '<img style="'.$width_heigth.'" src="'.JURI::root().'images/os-cck/'.$layout_params['fields'][$fName.'_def_img'].'" />';
            if($cck_light_box_in_main_image == "on") echo'</a>';
        }
    }

    ?>
    </div>
    
    <?php
      if(isset($layout_params['fields'][$fName.'_add_icon_suffix_prefix']) && !empty($layout_params['fields'][$fName.'_add_icon_suffix_prefix'])){
        echo '<span class="fa '.$layout_params['fields'][$fName.'_add_icon_suffix_prefix'].'"></span>';
      }
      if(!empty($layout_params['fields'][$fName.'_suffix'])){
        echo '<span class="cck-suffix">'.$layout_params['fields'][$fName.'_suffix'].'</span>';
      }
      if(isset($layout_params['fields'][$fName.'_add_icon_suffix_suffix']) && !empty($layout_params['fields'][$fName.'_add_icon_suffix_suffix'])){
        echo '<span class="fa '.$layout_params['fields'][$fName.'_add_icon_suffix_suffix'].'"></span>';
      }
    ?>
</span>
