<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

defined('_JEXEC') or die;

require_once JPATH_ADMINISTRATOR . '/components/com_arismartcontent/kernel/class.AriKernel.php';

AriKernel::import('Parameters.ParametersHelper');
AriKernel::import('Application.ARISmartContent.Utils.Widget');
AriKernel::import('Application.ARISmartContent.Utils.ApplicationHelper');
AriKernel::import('Application.ARISmartContent.Providers.ProvidersHelper');
AriKernel::import('Web.JSON.JSON');
AriKernel::import('Document.DocumentHelper');
AriKernel::import('Web.HtmlHelper');
ARiKernel::import('SimpleTemplate.SimpleTemplate');

class AriSmartContentWidgetTabs extends AriSmartContentWidget
{
	var $_params;
	var $_id;

	function __construct($id, $params)
	{
		$this->_id = $id;
		$this->_params = AriParametersHelper::flatParametersToArray($params);
	}

	function getId()
	{
		return $this->_id;
	}

	function loadAssets()
	{
		$appHelper = AriSmartContentApplicationHelper::getInstance();
		$appHelper->loadJQuery();
		$appHelper->loadJQueryUILib('widget');
		$appHelper->loadJQueryUILib('tabs');
        $appHelper->loadJQueryUILib('helper');
	}

	function registerClientInstance()
	{
		$params = $this->_params;
		$mainParams = AriUtils::getParam($params, '_default');
		$theme = AriUtils::getParam($mainParams, 'theme');
		$appHelper = AriSmartContentApplicationHelper::getInstance();

		$appHelper->loadJQueryUITheme($theme);

		$loadMethod = AriUtils::getParam($mainParams, 'loadMethod', 'ready');

		$id = $this->getId();
		$jsOptions = $this->getJsOptions();

		$initCode = sprintf('jqueryui_fixLinks($, "#%1$s>UL>LI>A");$("#%1$s").tabs(%2$s);',
			$id,
			!empty($jsOptions) ? json_encode($jsOptions) : ''
		);

		$doc = JFactory::getDocument();
		if ($loadMethod == 'load')
			$doc->addScriptDeclaration(';(window["ascJQuery"] || jQuery)(window).load(function() { var $ = window["ascJQuery"] || jQuery;' . $initCode . '});');
		else
			$doc->addScriptDeclaration(';(window["ascJQuery"] || jQuery)(document).ready(function($) { ' . $initCode . '});');

		$width = intval(AriUtils::getParam($mainParams, 'width', 300), 10);

		$width = AriUtils::getParam($mainParams, 'width');
		if (strpos($width, '%') === false)
			$width .= 'px'; 

		$styleDec = sprintf('#%1$s{width:%2$s;}',
			$id,
			$width
		);

		$customStyles = trim(AriUtils::getParam($mainParams, 'customstyle', ''));
		if ($customStyles)
		{
			$customStyles = str_replace('{$id}', $id, $customStyles);
			if (!empty($customStyles))
				$styleDec .= $customStyles;
		}
		
		$doc->addStyleDeclaration($styleDec); 
	}

	function getJsOptions()
	{
		$params = $this->_params;
		$options = AriUtils::getParam($params, 'widget');

		$defOptions = array(
			'active' => 0,
			'hide' => array(
				'easing' => '',
				'duration' => 0
			),
			'show' => array(
				'easing' => '',
				'duration' => 0
			),
			'collapsible' => false,
			'event' => 'click',
			'heightStyle' => 'content'
		);

		$jsOptions = AriParametersHelper::getUniqueOverrideParameters($defOptions, $options);
		
		if (isset($jsOptions['active']) && $jsOptions['active'] == -1)
			$jsOptions['active'] = false;

		return $jsOptions;
	}

	function getItems($data)
	{
		$params = $this->_params;
		$mainParams = AriUtils::getParam($params, '_default');
		
		$titleTemplate = AriUtils::getParam($mainParams, 'titleTemplate');
		$itemTemplate = AriUtils::getParam($mainParams, 'itemTemplate');

		$items = array();

		if (!empty($data))
		{
			foreach ($data as $dataItem)
			{
				$title = AriSimpleTemplate::parse($titleTemplate, $dataItem);
				$content = AriSimpleTemplate::parse($itemTemplate, $dataItem);
				
				$item = array(
					'title' => $title,
					'content' => $content
				);
	
				$items[] = $item;
			}
		}

		return $items;
	}

	function getTheme()
	{
		$params = $this->_params;
		$mainParams = AriUtils::getParam($params, '_default');
		$theme = AriUtils::getParam($mainParams, 'theme');
		$appHelper = AriSmartContentApplicationHelper::getInstance();

		return $appHelper->resolveJQueryUITheme($theme);
	}
	
	function getActiveIndex()
	{
		$params = $this->_params;
		$options = AriUtils::getParam($params, 'widget');
		
		$idx = intval(AriUtils::getParam($options, 'active', 0), 10);
		
		return $idx;
	}

	function render()
	{
		$params = $this->_params;
		$mainParams = AriUtils::getParam($params, '_default');

		$source = AriUtils::getParam($mainParams, 'source');
		$sourceData = AriSmartContentProvidersHelper::resolveSource($source);
		$data = AriSmartContentProvidersHelper::getData($sourceData, null);

		$items = $this->getItems($data);

		$this->renderItems($items);
	}
	
	function renderItems($items)
	{
		$this->loadAssets();
		$this->registerClientInstance();

		$this->renderTemplate($items);		
	}
	
	private function renderTemplate($items)
	{
		$params = $this->_params;
		$tabsId = $this->getId();

		require dirname(__FILE__) . '/tmpl/tabs.php';
	}
}