<?php
/**
 * @version		$Id: record.php 143 2009-06-25 11:37:24Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

// no direct access
defined('_JEXEC') or die;

/**
 * @package		TAOJ.ContentManager
 * @subpackage	com_quanta
 */
class JElementModal_Record extends JElement
{
	/**
	 * Element name
	 *
	 * @var		string
	 */
	var	$_name = 'Modal_Record';

	public function fetchElement($name, $value, &$node, $control_name)
	{
		static $count = 0;
		$db			=& JFactory::getDBO();
		$doc 		=& JFactory::getDocument();
		$fieldName	= $control_name.'['.$name.']';

		// Get the title of the linked chart
		$db->setQuery(
			'SELECT title' .
			' FROM #__taoj_quanta_records' .
			' WHERE id = '.(int) $value
		);
		$title = $db->loadResult();

		if ($error = $db->getErrorMsg()) {
			JError::raiseWarning(500, $error);
		}

		if (empty($title)) {
			$title = JText::_('Quanta_Select_a_record');
		}

		$doc->addScriptDeclaration(
		"function jSelectRecord_".$name."(id, title, object) {
			document.getElementById('".$name."_id').value = id;
			document.getElementById('".$name."_name').value = title;
			document.getElementById('sbox-window').close();
		}"
		);

		$link = 'index.php?option=com_quanta&amp;view=records&amp;layout=modal&amp;tmpl=component&amp;function=jSelectRecord_'.$name;

		JHTML::_('behavior.modal', 'a.modal');
		$html = "\n".'<div style="float: left;"><input style="background: #ffffff;" type="text" id="'.$name.'_name" value="'.htmlspecialchars($title, ENT_QUOTES, 'UTF-8').'" disabled="disabled" /></div>';
		$html .= '<div class="button2-left"><div class="blank"><a class="modal" title="'.JText::_('Quanta_Select_a_record').'"  href="'.$link.'" rel="{handler: \'iframe\', size: {x: 650, y: 375}}">'.JText::_('JButton_select').'</a></div></div>'."\n";
		$html .= "\n".'<input type="hidden" id="'.$name.'_id" name="'.$fieldName.'" value="'.(int)$value.'" />';

		return $html;
	}
}
