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

function apply_filters($func, $param1, $param2 = false)
{
	switch ($func)
	{
		case 'ls_slider_title':
			return ls_filter_slider_title($param1, $param2);
		case 'ls_get_preview_for_slider':
			return ls_filter_get_perview_for_slider($param1);
		case 'ls_get_thumbnail':
			return ls_get_thumbnail($param1, $param2);
		case 'ls_get_image':
			return ls_get_image($param1);
		case 'ls_parse_defaults':
			return ls_parse_defaults($param1, $param2);
		default:
			break;
	}
}

function ls_filter_slider_title($slider_name = '', $max_length = 50)
{
	$name = empty($slider_name) ? 'Unnamed' : call_user_func('stripslashes', $slider_name);
	return isset($name[$max_length]) ? Tools::substr($name, 0, $max_length).' ...' : $name;
}

function ls_filter_get_perview_for_slider($slider_obj = array())
{
	if (isset($slider_obj['data']['layers']))
	{
		foreach ($slider_obj['data']['layers'] as $layer)
		{
			if (!empty($layer['properties']['background']))
			{
				$image = $layer['properties']['background'];
				break;
			}
		}
	}
	if (isset($image))
		return $image;
	else
		return __PS_BASE_URI__.'modules/layerslider/views/img/base/slider_preview.jpg';
}

function ls_get_thumbnail($id = null, $url = null)
{
	if (!empty($url))
	{
		$thumb = substr_replace($url, '-150x150.', strrpos($url, '.'), 1);
		$file = LS_IMG_PATH.'/sampleslider/'.basename($thumb);

		if (file_exists($file))
			return $thumb;
		else
			return $url;
	}

	return __PS_BASE_URI__.'modules/layerslider/views/img/base/not_set.png';
}

function ls_get_image($id = null, $url = null)
{
	return $url;
}

function ls_parse_defaults($defaults = array(), $raw = array())
{
	$ret = array();
	foreach ($defaults as $default)
	{
		$php_key = is_string($default['keys']) ? $default['keys'] : $default['keys'][0];
		$js_key = is_string($default['keys']) ? $default['keys'] : $default['keys'][1];
		$ret_key = isset($default['props']['meta']) ? 'props' : 'attrs';

		if (isset($raw[$php_key]) && is_array($raw[$php_key]))
			$ret[$ret_key][$js_key] = $raw[$php_key];
		elseif (is_bool($default['value']))
		{
			if ($default['value'] == true && empty($raw[$php_key]))
				$ret[$ret_key][$js_key] = false;
			elseif ($default['value'] == false && !empty($raw[$php_key]))
				$ret[$ret_key][$js_key] = true;

		}
		elseif (isset($raw[$php_key]))

			if (isset($default['props']['meta']) || $default['value'] != $raw[$php_key])
				$ret[$ret_key][$js_key] = is_numeric($raw[$php_key]) ? (float)$raw[$php_key] : call_user_func('stripslashes', $raw[$php_key]);

	}

	return $ret;
}

function ls_array_to_attr($arr, $mode = '')
{
	if (!empty($arr) && is_array($arr))
	{
		$ret = array();
		foreach ($arr as $key => $val)
		{
			if ($mode == 'css' && is_numeric($val))
				$ret[] = ''.$key.':'.layerslider_check_unit($val).';';

			elseif (is_bool($val))
			{
				$bool = $val ? 'true' : 'false';
				$ret[] = "$key:$bool;";
			}
			else
				$ret[] = "$key:$val;";
		}
		return implode('', $ret);
	}
	}

?>
