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

AriKernel::import('Parameters.ParametersHelper');

class plgArismartcontentFancybox extends AriSmartContentPluginContent
{
	private static $_types = array(
		'yt' => 'youtube',
		'ig' => 'instagram',
		'vimeo',
		'metacafe',
		'dailymotion',
		'twitvid',
		'twitpic',
		'module',
		'article',
		'url'
	); 
	var $_optionsMapping = array(
		'padding' => 'opt_padding',
		'margin' => 'opt_margin',
		'width' => 'opt_width',
		'height' => 'opt_height',
		'minWidth' => 'opt_minWidth',
		'minHeight' => 'opt_minHeight',
		'maxWidth' => 'opt_maxWidth',
		'maxHeight' => 'opt_maxHeight',
		'autoWidth' => 'opt_autoWidth',
		'autoHeight' => 'opt_autoHeight',
		'autoResize' => 'opt_autoResize',
		'autoCenter' => 'opt_autoCenter',
		'fitToView' => 'opt_fitToView',
		'aspectRatio' => 'opt_aspectRatio',
		'topRatio' => 'opt_topRatio',
		'leftRatio' => 'opt_leftRatio',
		'scrolling' => 'opt_scrolling',
		'wrapCSS' => 'opt_wrapCSS',
		'closeClick' => 'opt_closeClick',
		'modal' => 'opt_modal',
		'scrollOutside' => 'opt_scrollOutside',
		'titlePos' => 'opt_helpers_title_type',
		'showArrows' => 'opt_arrows',
		'nextClick' => 'opt_nextClick',
		'autoPlay' => 'opt_autoPlay',
		'playSpeed' => 'opt_playSpeed',
		'mouseWheel' => 'opt_mouseWheel',
		'loop' => 'opt_loop',
		'toolbar' => 'opt_toolbarEnable',
		'toolbarSkipSingle' => 'opt_helpers_buttons_skipSingle', 
		'toolbarPos' => 'opt_helpers_buttons_position',
		'closeBtn' => 'opt_closeBtn',
		'closeOnClick' => 'opt_helpers_overlay_closeClick',
		'closeSpeedOut' => 'opt_helpers_overlay_speedOut',
		'overlayShowEarly' => 'opt_helpers_overlay_showEarly',
		'locked' => 'opt_helpers_overlay_locked',
		'openSpeed' => 'opt_openSpeed',
		'openOpacity' => 'opt_openOpacity',
		'openFx' => 'opt_openEffect',
		'openEasing' => 'opt_openEasing',
		'closeSpeed' => 'opt_closeSpeed',
		'closeOpacity' => 'opt_closeOpacity',
		'closeFx' => 'opt_closeEffect',
		'closeEasing' => 'opt_closeEasing',
		'nextSpeed' => 'opt_nextSpeed',
		'nextFx' => 'opt_nextEffect',
		'nextEasing' => 'opt_nextEasing',
		'prevSpeed' => 'opt_prevSpeed',
		'prevFx' => 'opt_prevEffect',
		'prevEasing' => 'opt_prevEasing',
        'iframe_preload' => 'opt_iframe_preload'
	);
	var $_typeOptionsMapping = array(
		'youtube' => array(
			'yt' => 'youtube_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'module' => array(
			'module' => 'module_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'vimeo' => array(
			'vimeo' => 'vimeo_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'metacafe' => array(
			'metacafe' => 'metacafe_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'dailymotion' => array(
			'dailymotion' => 'dailymotion_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'twitvid' => array(
			'twitvid' => 'twitvid_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'twitpic' => array(
			'twitpic' => 'twitpic_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'instagram' => array(
			'ig' => 'instagram_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'url' => array(
			'url' => 'url_url',
            'parseLinks' => 'url_parseLinks',
            'paramSeparator' => 'url_paramSeparator',
            'itemSeparator' => 'url_itemSeparator',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link',
		),
		'article' => array(
			'article' => 'article_id',
			ARISMARTCONTENT_CONTENTPLG_CONTENT_KEY => 'link'
		),
		'inline' => array(
			'content' => 'inline_content'
		)
	);

	function getTag()
	{
		return 'fancybox';
	}

	function contentPluginHandler($params, $content, $sourceContent) 
	{
		jimport('arismartcontent.fancybox.fancybox');		

		if (!empty($params['opt_width']) || !empty($params['width']))
			$params['opt_autoWidth'] = false;
			
		if (!empty($params['opt_height']) || !empty($params['height']))
			$params['opt_autoHeight'] = false;
		
		$contentType = $this->getContentType($params, $content);
		$params = $this->prepareParams($contentType, $params, $content, $sourceContent);
		$mergedParams = $this->getMergedParams($params);
		$mergedParams->set('type', $contentType);

		$fancyBoxWidget = new AriSmartContentWidgetFancybox($mergedParams);
		$retContent = $fancyBoxWidget->getOutput('render');

		return $retContent;
	}

	function getContentType($params, $content)
	{
		$contentType = !empty($params['type']) ? strtolower($params['type']) : '';
		if (!empty($contentType))
			return $contentType;

		$contentType = $this->params->get('type');
		$defContentType = null;
		foreach (self::$_types as $key => $value)
		{
			$paramKey = $key;
			if (is_numeric($key))
				$paramKey = $value;
				
			if (!empty($params[$paramKey]))
			{
				$defContentType = $value;
				break;
			}
		}
		
		if (is_null($defContentType))
		{
			if (strpos($content, '{content}') !== false)
				$defContentType = 'inline';
		}
		
		if (!is_null($defContentType))
			$contentType = $defContentType;

		return $contentType; 
	}
	
	function prepareHandler_inline($params, $content, $sourceContent)
	{
		AriKernel::import('Application.ARISmartContent.Plugin.SimpleContentPlugin');

		$preparedParams = $this->prepareHandler('inline', $params, $content, $sourceContent);

		if (empty($preparedParams['link']))
		{
			$linkPlg = new AriSmartContentPluginSimpleContent('link');
			$preparedParams['link'] = $linkPlg->getContent($content);
		}

		if (empty($preparedParams['inline_content']))
		{
			$contentPlg = new AriSmartContentPluginSimpleContent('content');
			$preparedParams['inline_content'] = $contentPlg->getContent($content);
		}
		
		return $preparedParams;
	}
}