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

defined('_JEXEC') or die ('Restricted access');

require_once dirname(__FILE__) . '/../kernel/class.AriKernel.php';

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

jimport('joomla.html.html');
jimport('joomla.form.formfield');

JHtml::_('behavior.modal', 'a.modal');

class JFormFieldSource extends JFormField
{
	protected $type = 'Source';

	function getInput()
	{	
		return $this->fetchElement($this->element['name'], $this->value, $this->element, $this->name);
	}

	function fetchElement($name, $value, &$node, $control_name)
	{
        $lang = JFactory::getLanguage();
        $lang->load('com_arismartcontent.sys', JPATH_ADMINISTRATOR);

		$category = (string)$node['category'];
		$sourceType = (string)$node['sourcetype'];
		$id = str_replace(array('[', ']'), array('_', ''), $control_name);
		$lblId = $id . '_label';
		$lbl = '';
		$btnLbl = !empty($node['btn_label']) ? $node['btn_label'] : 'COM_ARISMARTCONTENT_LABEL_SELECT';
		$btnLbl = JText::_($btnLbl);
        $btnEditLbl = !empty($node['btnedit_label']) ? $node['btnedit_label'] : 'COM_ARISMARTCONTENT_LABEL_EDIT';
        $btnEditLbl = JText::_($btnEditLbl);
		
		$sourceId = 0;
		if ($value)
		{
			$sourceId = intval($value, 10);
			$model = AriModel::getInstance('Providersource', 'AriSmartContentModel');
			$source = $model->getSource($sourceId);
			if ($source)
			{
				$sourceType = $source->SourceType->SystemName;
				$lbl = $source->Name;
			}
			else 
			{
				$sourceId = 0;
			}
		}

		$this->registerScripts($name, $id, $lblId);

		if (J3_0)
			return sprintf(
				'<span class="input-append"><input class="input-medium" type="text" id="%5$s" value="%6$s" readonly="readonly" disabled="disabled" />%10$s<a target="blank" class="modal btn" rel="{handler: \'iframe\',size: {x: 700, y: 400}}" href="index.php?option=com_arismartcontent&view=sources&tmpl=component&cat=%7$s&callback=selectSource_%1$s_init&sourceType=%8$s"><i class="icon-edit"></i> %9$s</a></span>
				<input type="hidden" name="%2$s" id="%3$s" value="%4$s" />
				',
				$name,
				$control_name,
				$id,
				$sourceId,
				$lblId,
				$lbl,
				$category,
				$sourceType,
				$btnLbl,
                sprintf(
                    '<a target="_blank"%4$s id="%3$s_editBtn" class="modal btn btn-primary" href="#" onclick="editSource_%2$s(\'%3$s\');return false;"><i class="icon-edit"></i> %1$s</a>',
                    $btnEditLbl,
                    $name,
                    $id,
                    empty($sourceId) ? ' style="display:none"' : ''
                )
            );
		else
			return sprintf(
				'<div class="fltlft"><input type="text" id="%5$s" value="%6$s" readonly="readonly" disabled="disabled" /></div>
				<div class="button2-left">
					<div class="blank">
						<a target="blank" class="modal btn" rel="{handler: \'iframe\',size: {x: 700, y: 400}}" href="index.php?option=com_arismartcontent&view=sources&tmpl=component&cat=%7$s&callback=selectSource_%1$s_init&sourceType=%8$s"><i class="icon-edit"></i> %9$s</a>
					</div>
				</div>
				<input type="hidden" name="%2$s" id="%3$s" value="%4$s" />
				',
				$name,
				$control_name,
				$id,
				$sourceId,
				$lblId,
				$lbl,
				$category,
				$sourceType,
				$btnLbl	
			);
	}

	function registerScripts($name, $ctrlId, $lblId)
	{
		$doc = JFactory::getDocument();
		
		$doc->addScriptDeclaration(
			sprintf(
				';function selectSource_%1$s_init(context) {
					var sourcesManager = context.YAHOO.ARISoft.page.pageManager.sourcesManager;
					
					sourcesManager.sourceSelectedEvent.subscribe(function(event, data) {
						var sourceData = data[0];
						
						document.getElementById("%2$s").value = sourceData.Id;
						document.getElementById("%3$s").value = sourceData.Name;

						document.getElementById("%2$s_editBtn").style.display = "";
						
						SqueezeBox.close();
					});
				};
                function editSource_%1$s_init(context) {
					var sourceManager = context.YAHOO.ARISoft.page.pageManager.sourceManager;

					sourceManager.sourceCloseEvent.subscribe(function(event, data) {
						SqueezeBox.close();
					});

                    sourceManager.sourceSavedEvent.subscribe(function(event, data) {
                        var sourceData = data[0];

						document.getElementById("%3$s").value = sourceData.Name;

						SqueezeBox.close();
					});
				};
				function editSource_%1$s(srcEl) {
				    var src = document.getElementById(srcEl),
				        srcId = src.value;

                    SqueezeBox.open("index.php?option=com_arismartcontent&view=source&task=edit&Id=" + srcId + "&direct=1&callback=editSource_%1$s_init&tmpl=component", {handler: "iframe", size: {x: 700, y: 400}});
				};',
				$name,
				$ctrlId,
				$lblId
			)
		);
	}
}