<?php
/**
 * @package SJ Categories Full for Hikashop
 * @version 1.0.0
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @copyright (c) 2014 YouTech Company. All Rights Reserved.
 * @author YouTech Company http://www.smartaddons.com
 *
 */
defined('_JEXEC') or die;
$tag_id = 'sj_categoriesfull_' . rand() . time();
\Joomla\CMS\HTML\HTMLHelper::stylesheet('modules/' . $module->module . '/assets/css/styles.css');


ob_start();
if ($params->get('border_display') == 0) {
	?>
	#<?php echo $tag_id; ?> .cf-wrap .cf-cat .cf-cat-inner{
	border:0;
	}
<?php
}
$css = ob_get_contents();
ob_end_clean();
$document = \Joomla\CMS\Factory::getDocument();
$document->addStyleDeclaration($css);
$nb_cols = (int)$params->get('nb_cols', 1);
$nb_cols = ($nb_cols <= 0 || $nb_cols > 6) ? 1 : $nb_cols;
$cl1 = $cl2 = '';
switch ($nb_cols) {
	case 6:
		$cl1 = 4;
		$cl2 = 3;
		break;
	case 5:
		$cl1 = 4;
		$cl2 = 3;
		break;
	case 4:
		$cl1 = 4;
		$cl2 = 2;
		break;
	case 3:
		$cl1 = 3;
		$cl2 = 2;
		break;
	case 2:
		$cl1 = 2;
		$cl2 = 2;
		break;
	case 1:
		$cl1 = 1;
		$cl2 = 1;
		break;
	default:
		break;
}

$class_resp = '';
$class_resp .= ' preset01-' . $nb_cols;
$class_resp .= ' preset02-' . $cl1;
$class_resp .= ' preset03-' . $cl2;
$class_resp .= ' preset04-1';

ImageHelper::setDefault($params);

$big_image_config = array(
	'type' => $params->get('imgcfgcat_type'),
	'width' => $params->get('imgcfgcat_width'),
	'height' => $params->get('imgcfgcat_height'),
	'quality' => 90,
	'function' => ($params->get('imgcfgcat_function') == 'none') ? null : 'resize',
	'function_mode' => ($params->get('imgcfgcat_function') == 'none') ? null : substr($params->get('imgcfgcat_function'), 7),
	'transparency' => $params->get('imgcfgcat_transparency', 1) ? true : false,
	'background' => $params->get('imgcfgcat_background'));

$layout_select = ($params->get('layout_select') == 'layout1') ? ' cf-layout1' : ' cf-layout2';
$cat_image_style = ($params->get('cat_image_style') == 'style_full') ? ' cat-full-image' : ' cat-half-image';
$cat_image_style = ($params->get('layout_select') == 'layout1') ? $cat_image_style : '';
$cat_title_style = ($params->get('cat_title_style') == 'style_dark') ? ' style_dark' : ' style_blue';

$pause_hover = ($params->get('pause_hover') == 'hover') ? 'hover' : '';
;
$interval = (int)$params->get('interval', '4000');
$effect = ($params->get('effect') == 1) ? ' slide' : '';

if ($params->get('play') != 1) {
	$interval = 0;
} else {
	$interval = $params->get('interval', 5000);
}
?>

<?php if ($params->get('pretext') != '') { ?>
	<div class="catfull-pretext">
		<?php echo $params->get('pretext'); ?>
	</div>
<?php } ?>
<?php if (!empty($list)) { ?>
	<!--[if lt IE 9]>
	<div class="sj-categoriesfull pre-load msie lt-ie9" id="<?php echo $tag_id; ?>"><![endif]-->
	<!--[if IE 9]>
	<div class="sj-categoriesfull pre-load msie" id="<?php echo $tag_id; ?>"><![endif]-->
	<!--[if gt IE 9]><!-->
	<div class="sj-categoriesfull pre-load" id="<?php echo $tag_id; ?>"><!--<![endif]-->
		<div class="cf-wrap <?php echo $class_resp; ?>"><!-- Begin cf-wrap -->
            <?php $k = 0; ?>
            <?php foreach ($list as $items) { ?>
            
                <?php if ($k % 2 == 0) { ?>
                    <div class="cf-items-two">
                <?php } ?>
            
                <?php
                include \Joomla\CMS\Helper\ModuleHelper::getLayoutPath($module->module, $layout . '_items');
                ?>
            
                <?php $k++; ?>
            
                <?php if ($k % 2 == 0) { ?>
                    </div>
                <?php } ?>
            
            <?php } ?>
            
            <?php // nếu số item lẻ thì đóng div cuối ?>
            <?php if ($k % 2 != 0) { ?>
                </div>
            <?php } ?>
		</div>
		<!-- End cf-wrap -->
	</div> <!-- End sj-categoriesfull -->

	<script type="text/javascript">
		//<![CDATA[
		jQuery(document).ready(function ($) {
			;
			(function (element) {
				var $element = $(element);
				var $loadding = $('.cf-loading', $element);
				var $slide_items = $('.cf-cat', $element);
				$loadding.delay(800).remove();
				$element.delay(800).removeClass('pre-load');
				$slide_items.each(function () {
					var cat_id = $(this).attr('data-id');
					var $items = $('#cf_items_' + cat_id, $element);
					$items.each(function () {
						var $this = $(this), options = options = !$this.data('modal') && $.extend({}, $this.data());
						$this.jcarousel(options);
						$this.bind('jslide', function (e) {
							var index = $(this).find(e.relatedTarget).index();
							// process for nav
							$('[data-jslide]').each(function () {
								var $nav = $(this), $navData = $nav.data(), href, $target = $($nav.attr('data-target') || (href = $nav.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''));
								if (!$target.is($this)) return;
								if (typeof $navData.jslide == 'number' && $navData.jslide == index) {
									$nav.addClass('sel');
								} else {
									$nav.removeClass('sel');
								}

							});
							var $pages = $('#cf_pages_' + cat_id, $element);

							slidingPage($pages, _resize = false);
						});

						<?php
							if($params->get('mouse_enable') == 1) { ?>
						$this.mousewheel(function (event, delta) {
							(delta > 0) ? $this.jcarousel('prev') : $this.jcarousel('next');
							return false;
						});
						<?php } ?>

						<?php if($params->get('swipe_enable') == 1) { ?>
						$this.touchSwipeLeft(function () {
							$this.jcarousel('next');
							return false;
						});

						$this.touchSwipeRight(function () {
							$this.jcarousel('prev');
							return false;
						});
						<?php } ?>

						return;
					});
				});

				function slidingPage(el, _resize) {
					if (el.hasClass('cf-control') === false) {
						return;
					}
					var $page = $('li.cf-page', el);
					var $page_active = $page.filter('.sel');
					var $page_holder = $page.parent();
					var index = $page_active.index();
					var toleft = -$page_active.position().left;
					var $page_container = $page_holder.parent();
					var $page_width = $page.width() * $page.length;
					var viewport = {};
					viewport.left = 0;
					viewport.right = $page_container.width();

					var visible = {};
					visible.left = viewport.left - parseInt($page_holder.position().left);
					visible.right = viewport.right - parseInt($page_holder.position().left);
					var posUpdate = function () {
						visible.left = viewport.left - parseInt($page_holder.position().left);
						visible.right = viewport.right - parseInt($page_holder.position().left);
					};

					var slidingWhenReisize = function (toleft, d) {
						if (!d) {
							d = 500;
						}
						if (toleft <= $page_container.width() - $page_width) {
							toleft = $page_container.width() - $page_width;
						}
						if (toleft > 0) {
							toleft = 0;
						}
						$page_holder.stop(true, true).animate({
							left: toleft
						}, {
							duration: d,
							complete: posUpdate,
							queue: false
						});
					}

					if (_resize == true) {
						slidingWhenReisize(toleft);
					}

					var slidingto = function (index, d) {
						if (!d) {
							d = 500;
						}
						if (index >= 0) {
							if (index == 0) {
								index = 1;
							}
							var prevLeft = $page.eq(index - 1).position().left;
							if (prevLeft <= visible.left) {
								$page_holder.stop(true, true).animate({
									left: '+=' + (visible.left - prevLeft) + 'px'
								}, {
									duration: d,
									complete: posUpdate,
									queue: false
								});
							}
						}
						if (index < $page.length) {
							if (index == $page.length - 1) {
								index = $page.length - 2;
							}
							var nextRight = $page.eq(index + 1).position().left + $page.eq(index + 1).width();
							if (nextRight >= visible.right) {
								$page_holder.stop(true, true).animate({
									left: '-=' + (nextRight - visible.right) + 'px'
								}, {
									duration: d,
									complete: posUpdate,
									queue: false
								});
							}
						}

					}
					slidingto(index);
				}

				$(window).resize(function () {
					$slide_items.each(function () {
						var _cat_id = $(this).attr('data-id');
						var $pages = $('#cf_pages_' + _cat_id, $element);
						slidingPage($pages, true);
					});
				});
			})('#<?php echo $tag_id; ?>')
		});
		//]]>
	</script>

<?php
} else {
	echo \Joomla\CMS\Language\Text::_('Has no content to show!');
}?>
<?php if ($params->get('posttext') != '') { ?>
	<div class="catfull-posttext">
		<?php echo $params->get('posttext'); ?>
	</div>
<?php } ?>