<?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 lsGetInput($default, $current, $attrs = array())
{
	$el = phpQuery::newDocument('<input>')->children();
	$type = is_string($default['value']) ? 'text' : 'number';
	$name = is_string($default['keys']) ? $default['keys'] : $default['keys'][0];
	$el->attr('type', $type);
	$el->attr('name', $name);
	$el->val($default['value']);

	$attrs = isset($default['attrs']) ? array_merge($default['attrs'], $attrs) : $attrs;
	if (isset($attrs) && is_array($attrs))

		foreach ($attrs as $attr => $val)
			$el->attr($attr, $val);

	if (isset($default['tooltip']))
		$el->attr('data-help', $default['tooltip']);

	if (isset($current[$name]) && $current[$name] !== '')
		$el->val(call_user_func('stripslashes', $current[$name]));

	echo $el;
}

function lsGetCheckbox($default, $current, $attrs = array())
{
	$el = phpQuery::newDocument('<input>')->children();
	$name = is_string($default['keys']) ? $default['keys'] : $default['keys'][0];
	$el->attr('type', 'checkbox');
	$el->attr('name', $name);

	$attrs = isset($default['attrs']) ? array_merge($default['attrs'], $attrs) : $attrs;
	if (isset($attrs) && is_array($attrs))

		foreach ($attrs as $attr => $val)
			$el->attr($attr, $val);

	if ($default['value'] === true && count($current) < 3)
		$el->attr('checked', 'checked');
	elseif (isset($current[$name]) && $current[$name] != false && $current[$name] !== 'false')
		$el->attr('checked', 'checked');

	echo $el;
}

function lsGetSelect($default, $current, $attrs = array())
{
	$el = phpQuery::newDocument('<select>')->children();
	$name = is_string($default['keys']) ? $default['keys'] : $default['keys'][0];
	$el->attr('name', $name);
	$value = $default['value'];
	$options = array();

	$attrs = isset($default['attrs']) ? array_merge($default['attrs'], $attrs) : $attrs;
	if (isset($attrs) && is_array($attrs))

		foreach ($attrs as $attr => $val)

			if (!is_array($val))
				$el->attr($attr, $val);

	if (isset($default['options']) && is_array($default['options']))
		$options = $default['options'];
	elseif (isset($attrs['options']) && is_array($attrs['options']))
		$options = $attrs['options'];

	if (isset($current[$name]) && $current[$name] !== '')
		$value = $current[$name];

	if (isset($default['tooltip']))
		$el->attr('data-help', $default['tooltip']);

	$opts = '';
	foreach ($options as $val => $opt)
	{
		if (!is_string($opt))
		{
			$val = $opt['value'];
			$opt = $opt['option'];
		}
		else
			$val = is_string($val) ? $val : $opt;

		$checked = ($val == $value) ? ' selected="selected"' : '';
		$opts .= "<option value=\"$val\"$checked>$opt</option>";
	}
	$el->html($opts);

	echo $el;
}

function lsGetMultiSelect($default, $current, $attrs = array())
{
	$el = phpQuery::newDocument('<select>')->children();
	$name = is_string($default['keys']) ? $default['keys'] : $default['keys'][0];
	$el->attr('name', $name);
	$value = $default['value'];
	$options = array();

	$attrs = isset($default['attrs']) ? array_merge($default['attrs'], $attrs) : $attrs;
	if (isset($attrs) && is_array($attrs))

		foreach ($attrs as $attr => $val)

			if (!is_array($val))
				$el->attr($attr, $val);

	if (isset($default['options']) && is_array($default['options']))
		$options = $default['options'];
	elseif (isset($attrs['options']) && is_array($attrs['options']))
		$options = $attrs['options'];

	if (isset($current[$name]) && $current[$name] !== '')
		$value = $current[$name];

	$value = Tools::jsonDecode($value);

	if (isset($default['tooltip']))
		$el->attr('data-help', $default['tooltip']);

	$opts = '';
	foreach ($options as $val => $opt)
	{
		if (!is_string($opt))
		{
			$val = $opt['value'];
			$opt = $opt['option'];
		}
		else
			$val = is_string($val) ? $val : $opt;

		$checked = in_array($val, $value) ? ' selected="selected"' : '';
		$opts .= "<option value=\"$val\"$checked>$opt</option>";
	}
	$el->html($opts);

	echo $el;
}

?>
