<?php
/**
* LayerSlider v5.1.2 - Responsive Slideshow Module http://layerslider.webshopworks.com
*
*  @author    WebshopWorks <info@webshopworks.com>
*  @copyright 2015 WebshopWorks
*  @license   One Domain Licence
*/

if (!defined('_PS_VERSION_'))
	exit;

if (!defined('LS_INCLUDE'))
{
	$id = 0;
	$slides = array();
	$data = array();
	$slider = array();
}

if (isset($slides['properties']['props']['forceresponsive']))
{
	$data[] = '<div class="ls-wp-fullwidth-container">';
	$data[] = '<div class="ls-wp-fullwidth-helper">';
}

$slider_style_attr = array();
$slider_style_attr[] = 'width:'.layerslider_check_unit($slides['properties']['props']['width']).';';
$slider_style_attr[] = 'height:'.layerslider_check_unit($slides['properties']['props']['height']).';';

if (!empty($slides['properties']['props']['maxwidth']))
	$slider_style_attr[] = 'max-width:'.layerslider_check_unit($slides['properties']['props']['maxwidth']).';';

$slider_style_attr[] = 'margin:0 auto;';
if (isset($slides['properties']['props']['sliderStyle']))
	$slider_style_attr[] = $slides['properties']['props']['sliderStyle'];

$data[] = '<div id="layerslider_'.$id.'" class="ls-wp-container" style="'.implode('', $slider_style_attr).'">';

if (!empty($slider['slides']) && is_array($slider['slides']))
{
	foreach ($slider['slides'] as $slidekey => $slide)
	{
		if (isset($slide['props']['skip']))
			continue;

		$slide_id = !empty($slide['props']['id']) ? ' id="'.$slide['props']['id'].'"' : '';
		$slide_attrs = !empty($slide['attrs']) ? ls_array_to_attr($slide['attrs']) : '';
		$post_content = false;

		if (empty($slide['attrs']['transition2d']) && empty($slide['attrs']['transition3d']))
			if (empty($slide['attrs']['customtransition2d']) && empty($slide['attrs']['customtransition3d']))
				$slide_attrs .= ' transition2d: all;';

		$slide_attrs = !empty($slide_attrs) ? 'data-ls="'.$slide_attrs.'"' : '';
		$data[] = '<div class="ls-slide"'.$slide_id.' '.$slide_attrs.'>';

		if (!empty($slide['props']['background']))
		{
			$src = !empty($slide['props']['backgroundId']) ? apply_filters('ls_get_image', $slide['props']['backgroundId'], $slide['props']['background']) : $slide['props']['background'];
			$data[] = '<img src="'.__PS_BASE_URI__.'modules/layerslider/views/img/base/blank.gif" data-src="'.$src.'" class="ls-bg" alt="Slide background" />';
		}

		if (isset($slides['properties']['attrs']['thumbnailNavigation']))
			if ($slides['properties']['attrs']['thumbnailNavigation'] != 'disabled')
			{
				if (!empty($slide['props']['thumbnail']))
				{
					$src = !empty($slide['props']['thumbnailId']) ? apply_filters('ls_get_image', $slide['props']['thumbnailId'], $slide['props']['thumbnail']) : $slide['props']['thumbnail'];
					$data[] = '<img src="'.__PS_BASE_URI__.'modules/layerslider/views/img/base/blank.gif" data-src="'.$src.'" class="ls-tn" alt="Slide thumbnail" />';
				}
			}

		if (!empty($slide['layers']) && is_array($slide['layers']))
		{
			foreach ($slide['layers'] as $layerkey => $layer)
			{
				if (isset($layer['props']['skip']))
					continue;

				$layer['props']['media'] = !empty($layer['props']['media']) ? $layer['props']['media'] : '';
				if (!empty($layer['props']['media']))
					switch ($layer['props']['media'])
					{
						case 'img':
							$layer['props']['type'] = 'img';
							break;
						case 'html':
							$layer['props']['type'] = 'div';
							break;
						case 'post':
							$layer['props']['type'] = 'div';
							break;
					}

				if ($layer['props']['type'] == 'img' && empty($layer['props']['image']))
					continue;

				$first = Tools::substr($layer['props']['html'], 0, 1);
				$last = Tools::substr($layer['props']['html'], Tools::strlen($layer['props']['html']) - 1, 1);

				if ($layer['props']['media'] == 'post' && ($first == '<' && $last == '>'))
					$type = $layer['props']['html'];
				else
					$type = '<'.$layer['props']['type'].'>';

				if (!empty($layer['props']['url']) && !preg_match('/^\#[0-9]/', $layer['props']['url']))
				{
					$el = phpQuery::newDocument('<a>')->children();
					$el->attr('href', $layer['props']['url']);
					if (!empty($layer['props']['target']))
						$el->attr('target', $layer['props']['target']);
					$inner = $el->append($type)->children();
				}
				else
					$el = $inner = phpQuery::newDocument($type)->children();

				$level = isset($layer['props']['level']) ? $layer['props']['level'] : '';
				$el->addClass('ls-l'.$level);
				if (!empty($layer['props']['id']))
					$inner->attr('id', $layer['props']['id']);
				if (!empty($layer['props']['class']))
					$inner->addClass($layer['props']['class']);
				if (!empty($layer['props']['url']))
				{
					if (!empty($layer['props']['rel']))
						$el->attr('rel', $layer['props']['rel']);
					if (!empty($layer['props']['title']))
						$el->attr('title', $layer['props']['title']);
				}
				else
					if (!empty($layer['props']['title']))
						$inner->attr('title', $layer['props']['title']);

				$el->attr('style', 'top:'.layerslider_check_unit($layer['props']['top']).';');
				$el->attr('style', $el->attr('style').'left:'.layerslider_check_unit($layer['props']['left']).';');

				if (isset($layer['attrs']) && isset($layer['props']['transition']))
					$el->attr('data-ls', ls_array_to_attr($layer['attrs']));
				elseif (isset($layer['attrs']))
					$el->attr('style', $el->attr('style').ls_array_to_attr($layer['attrs']));

				if (!empty($layer['props']['style']))
				{
					if (Tools::substr($layer['props']['style'], -1) != ';')
						$layer['props']['style'] .= ';';

					$inner->attr('style', $inner->attr('style').preg_replace('/\s\s+/', ' ', $layer['props']['style']));
				}

				if (!empty($layer['props']['styles']))
					$inner->attr('style', $inner->attr('style').ls_array_to_attr(Tools::jsonDecode($layer['props']['styles'], true), 'css'));
				if (empty($layer['props']['wordwrap']))
					$inner->attr('style', $inner->attr('style').'white-space: nowrap;');

				if (!empty($layer['props']['url']) && preg_match('/^\#[0-9]/', $layer['props']['url']))
					$el->addClass('ls-linkto-'.Tools::substr($layer['props']['url'], 1));

				if ($layer['props']['type'] == 'img')
				{
					$inner->attr('src', __PS_BASE_URI__.'modules/layerslider/views/img/base/blank.gif');
					$inner->attr('src', __PS_BASE_URI__.'modules/layerslider/views/img/base/blank.gif');
					$src = !empty($layer['props']['imageId']) ? apply_filters('ls_get_image', $layer['props']['imageId'], $layer['props']['image']) : $layer['props']['image'];
					$inner->attr('data-src', $src);

					if (!empty($layer['props']['alt']))
						$el->attr('alt', $layer['props']['alt']);
					else
						$el->attr('alt', '');
				}
				elseif ($layer['props']['media'] != 'post' || ($first != '<' && $last != '>'))
					$inner->html(call_user_func('stripslashes', $layer['props']['html']));

				$data[] = $el;
			}
		}

		if (!empty($slide['props']['linkUrl']))
		{
			if (!empty($slide['props']['linkTarget']))
				$target = ' target="'.$slide['props']['linkTarget'].'"';
			else
				$target = '';

			$data[] = '<a href="'.$slide['props']['linkUrl'].'"'.$target.' class="ls-link"></a>';
		}

		$data[] = '</div>';
	}
}

$data[] = '</div>';
if (isset($slides['properties']['props']['forceresponsive']))
	$data[] = '</div></div>';
