<?php
/**
 * Mobile Joomla! Pro
 * http://www.mobilejoomla.com
 *
 * @version		1.2.13.pro
 * @license		GNU/GPL v2 - http://www.gnu.org/licenses/gpl-2.0.html
 * @copyright	(C) 2008-2015 Kuneri Ltd.
 * @date		February 2015
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgMobileMJPro_Tablet extends JPlugin
{
	function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
	}

	function onMJBeforeSave(&$names, &$MobileJoomla_Settings)
	{
		$tablet_params = array(
			'template', 'homepage', 'gzip', 'domain', 'buffer_width',
			'header1', 'header2', 'header3', 'middle1', 'middle2', 'middle3',
			'componenthome', 'removetags', 'footer1', 'footer2', 'footer3',
			'jfooter', 'img', 'img_addstyles'
		);
		foreach($tablet_params as $param)
			$names[] = 'tablet.'.$param;
	}

	function onMJDisplayConfig(&$config_blobs, &$MobileJoomla_Settings, $lists)
	{
		$advanced = end($config_blobs);
		$advanced_key = key($config_blobs);
		unset($config_blobs[$advanced_key]);

		$templates = $lists['private.templates'];
		$gzip = $lists['private.gzip'];
		$img = $lists['private.img'];
		$modulepositions = $lists['private.modulepositions'];

		$lists['tablet.template'] = JHTML::_('mjconfig.templateparam', 'tablet.template', $templates, $MobileJoomla_Settings);
		$lists['tablet.gzip'] = JHTML::_('mjconfig.g_radioparam', 'tablet.gzip', $gzip, $MobileJoomla_Settings);
		$lists['tablet.header1'] = JHTML::_('mjconfig.positionparam', 'tablet.header1', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.header2'] = JHTML::_('mjconfig.positionparam', 'tablet.header2', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.header3'] = JHTML::_('mjconfig.positionparam', 'tablet.header3', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.middle1'] = JHTML::_('mjconfig.positionparam', 'tablet.middle1', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.middle2'] = JHTML::_('mjconfig.positionparam', 'tablet.middle2', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.middle3'] = JHTML::_('mjconfig.positionparam', 'tablet.middle3', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.componenthome'] = JHTML::_('mjconfig.g_booleanparam', 'tablet.componenthome', $MobileJoomla_Settings);
		$lists['tablet.footer1'] = JHTML::_('mjconfig.positionparam', 'tablet.footer1', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.footer2'] = JHTML::_('mjconfig.positionparam', 'tablet.footer2', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.footer3'] = JHTML::_('mjconfig.positionparam', 'tablet.footer3', $modulepositions, $MobileJoomla_Settings);
		$lists['tablet.jfooter'] = JHTML::_('mjconfig.booleanparam', 'tablet.jfooter', $MobileJoomla_Settings);
		$lists['tablet.img'] = JHTML::_('mjconfig.g_listparam', 'tablet.img', $img, $MobileJoomla_Settings);
		$lists['tablet.img_addstyles'] = JHTML::_('mjconfig.g_booleanparam', 'tablet.img_addstyles', $MobileJoomla_Settings);
		$lists['tablet.removetags'] = JHTML::_('mjconfig.g_booleanparam', 'tablet.removetags', $MobileJoomla_Settings);

		$config_blobs['COM_MJ__TABLET_SETTINGS'] = array(
			//left
			array(
				'COM_MJ__SETTINGS' => array(
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__TEMPLATE_NAME', 'COM_MJ__TEMPLATE_NAME_TABLET_DESC'),
						'input_blob' => $lists['tablet.template']
					)
				),
				'COM_MJ__HOMEPAGE' => array(
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__FORCED_HOMEPAGE', 'COM_MJ__FORCED_HOMEPAGE_DESC', 'tablet.homepage'),
						'input_blob' => JHTML::_('mjconfig.textinput', 'tablet.homepage', $MobileJoomla_Settings['tablet.homepage']),
					),
					array(
						'input_blob' => JHTML::_('mjconfig.menulist', $lists['menuoptions'], 'tablet.homepage', $MobileJoomla_Settings['tablet.homepage'])
					),
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__COMPONENT_ON_HOMEPAGE', 'COM_MJ__COMPONENT_ON_HOMEPAGE_DESC'),
						'input_blob' => $lists['tablet.componenthome']
					)
				),
				'COM_MJ__IMAGE' => array(
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__IMAGE_ADAPTATION_METHOD', 'COM_MJ__IMAGE_ADAPTATION_METHOD_DESC'),
						'input_blob' => $lists['tablet.img']
					),
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__DECREASE_IMAGE_WIDTH', 'COM_MJ__DECREASE_IMAGE_WIDTH_DESC', 'tablet.buffer_width'),
						'input_blob' => JHTML::_('mjconfig.textinput', 'tablet.buffer_width', $MobileJoomla_Settings['tablet.buffer_width'], 5, array('style'=>'text-align:right'))
					),
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__STYLE_IMAGE_SIZE', 'COM_MJ__STYLE_IMAGE_SIZE_DESC'),
						'input_blob' => $lists['tablet.img_addstyles']
					)
				),
				'COM_MJ__ADVANCED' => array(
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__GZIP_COMPRESSION'),
						'input_blob' => $lists['tablet.gzip']
					),
					array(
						'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__REMOVE_UNSUPPORTED_TAGS'),
						'input_blob' => $lists['tablet.removetags']
					)
				)
			),
			//right
			array(
				'COM_MJ__TEMPLATE_MODULES' => array()
			)
		);

		$tplmod_sections = array(
			'COM_MJ__MODULE_ABOVE_PATHWAY' => 'header',
			'COM_MJ__MODULE_BETWEEN_PATHWAY_COMPONENT' => 'middle',
			'COM_MJ__MODULE_BELOW_COMPONENT' => 'footer'
		);
		
		//template modules
		foreach($tplmod_sections as $section=>$sectionconfig)
		{
			for($i=1; $i<=3; ++$i)
			{
				$config_blobs['COM_MJ__TABLET_SETTINGS'][1]['COM_MJ__TEMPLATE_MODULES'][] = array(
					'label_blob' => JHTML::_('mjconfig.label', "{$section}_{$i}"),
					'input_blob' => $lists["tablet.{$sectionconfig}{$i}"]
				);
			}
		}

		$config_blobs[$advanced_key] = $advanced;
		//TODO: add tablet subdomain
		$config_blobs['COM_MJ__ADVANCED_SETTINGS'][1]['COM_MJ__REDIRECT_TO_DOMAIN'][] = array(
				'label_blob' => JHTML::_('mjconfig.label', 'COM_MJ__TABLET_DOMAIN', 'COM_MJ__DOMAIN_NAME_TABLET_DESC', 'tablet.domain'),
				'input_blob' => JHTML::_('mjconfig.textinput', 'tablet.domain', $MobileJoomla_Settings['tablet.domain'])
			);
	}
}
