<?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') && defined('ARI_FRAMEWORK_LOADED')) or die;

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

require_once JPATH_ADMINISTRATOR . '/components/com_arismartcontent/defines.php';

AriKernel::import('Web.HtmlHelper');
AriKernel::import('Template.Template');

class AriSmartContentWidgetMediaGalleryHelper
{
	static function getLightboxPlugin($lightboxParams)
	{
		$plg = null;
		
		$grouping = (bool)AriUtils::getParam($lightboxParams, 'grouping');
		$plgName = AriUtils::getParam($lightboxParams, 'type');
		if (empty($plgName))
			return $plg;
			
		$helper = AriSmartContentApplicationHelper::getInstance();
		$plg = $helper->getPlugin(ARISMARTCONTENT_INTEGRATIONPLUGIN_TYPE, $plgName);

		if ($plg && $grouping)
			$plg->setGroup($grouping);		

		return $plg;
	}
	
	static function saveParams($ticket, $params)
	{
		$app = JFactory::getApplication();
		$app->setUserState($ticket, $params);

		return true;
	}

	static function getParams($ticket)
	{
		$app = JFactory::getApplication();

		$params = $app->getUserState($ticket);

		return $params;
	}

	static function getContent($data, $params)
	{
		$mainParams = $params['_default'];
		$data = self::prepareData($data, $params, true);

		@ob_start();

		AriTemplate::display(
			dirname(__FILE__) . '/../tmpl/items.php', 
			array(
				'data' => $data,
				'isCurrentPage' => true
			)
		);

		$content = ob_get_contents();
		
		@ob_end_clean();
		
		return $content;
	}
	
	static function prepareData($data, $params, $lazyLoad = false)
	{
		$preparedData = array();
		if (!is_array($data) || count($data) == 0)
			return $preparedData;
			
		$bgImg = JURI::root() . '/media/arismartcontent/mediagallery/css/images/bg.png';
		$mainParams = $params['_default'];
		$lightboxParams = AriUtils::getParam($params, 'lightbox');
		$linkTarget = AriUtils::getParam($mainParams, 'linkTarget'); 

		$lightboxPlugin = self::getLightboxPlugin($lightboxParams);

		foreach ($data as $dataItem)
		{
            $title = AriUtils::getParam($dataItem, 'Title');
            $description = AriUtils::getParam($dataItem, 'Description');
			$aAttrs = null;
			$imgAttrs = array('class' => 'ari-media-gallery-img');
            if (!empty($title))
                $imgAttrs['title'] = $title;

			if (!empty($dataItem['Preview_Url']))
			{
				$imgAttrs['src'] = $dataItem['Preview_Url'];

				if (!empty($dataItem['Preview_Width']))
					$imgAttrs['width'] = $dataItem['Preview_Width'];
				
				if (!empty($dataItem['Preview_Height']))
					$imgAttrs['height'] = $dataItem['Preview_Height'];
					
				$aAttrs = array(
					'href' => $dataItem['Item_Url']
				);
				
				if (!empty($dataItem['Item_Width']))
					$aAttrs['width'] = $dataItem['Item_Width'];
					
				if (!empty($dataItem['Item_Height']))
					$aAttrs['height'] = $dataItem['Item_Height'];

				if ($linkTarget)
					$aAttrs['target'] = $linkTarget;

                if (!empty($description))
                    $aAttrs['title'] = $description;
                else if (!empty($title))
                    $aAttrs['title'] = $title;
			}
			else
			{
				$imgAttrs['src'] = $dataItem['Item_Url'];

				if (!empty($dataItem['Item_Width']))
					$imgAttrs['width'] = $dataItem['Item_Width'];

				if (!empty($dataItem['Item_Height']))
					$imgAttrs['height'] = $dataItem['Item_Height'];				
			}
			
			if (!empty($dataItem['Link']))
				$aAttrs['href'] = $dataItem['Link'];
			
			if (!empty($dataItem['Nav_Url']))
			{
				$imgAttrs['data-navthumb'] = $dataItem['Nav_Url'];
			}
			
			if ($lazyLoad)
			{
				$preloadSize = null;				
				if (isset($imgAttrs['width']) && isset($imgAttrs['height']))
					$preloadSize = $imgAttrs['width'] . 'x' . $imgAttrs['height'];

				$src = $imgAttrs['src'];
				if ($preloadSize)
					$imgAttrs['data-src'] = 'holder.js/' . $preloadSize . '/mediagallery';

				$imgAttrs['data-original-src'] = $src;
				$imgAttrs['src'] = $bgImg;
				$imgAttrs['class'] .= ' lazyload';
			}

			if ($lightboxPlugin)
				$lightboxPlugin->processLink($aAttrs, null, $lightboxParams);
			
			$item = sprintf(
				'<img%1$s/>',
				AriHtmlHelper::getAttrStr($imgAttrs)
			);
			
			if ($aAttrs)
				$item = sprintf(
					'<a%1$s>%2$s</a>',
					AriHtmlHelper::getAttrStr($aAttrs),
					$item
				);
				
			$dataItem['__item'] = $item;

			$preparedData[] = $dataItem;
		}
		
		return $preparedData;
	}
	
	static function prepareHiddenData($data, $params)
	{
		$preparedData = array();
		if (!is_array($data) || count($data) == 0)
			return $preparedData;
			
		$mainParams = $params['_default'];
		$lightboxParams = AriUtils::getParam($params, 'lightbox');
		$linkTarget = AriUtils::getParam($mainParams, 'linkTarget');

		$lightboxPlugin = self::getLightboxPlugin($lightboxParams);

		foreach ($data as $dataItem)
		{
			$aAttrs = array();			
			if (!empty($dataItem['Preview_Url']))
			{
				$aAttrs['href'] = $dataItem['Item_Url'];

				if ($linkTarget)
					$aAttrs['target'] = $linkTarget;
			}
			else
			{
				$aAttrs['href'] = $dataItem['Item_Url'];
			}
			
			if ($lightboxPlugin) {
				$lightboxPlugin->processLink($aAttrs, null, $lightboxParams);
			}

			$item = sprintf(
				'<a%1$s></a>',
				AriHtmlHelper::getAttrStr($aAttrs)
			);
				
			$dataItem['__hidden_item'] = $item;

			$preparedData[] = $dataItem;
		}

		return $preparedData;
	}
}