<?php
/**
 * @version		$Id: rainbow.php 144 2009-06-27 02:19:12Z 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 JElementRainbow extends JElement
{
	/**
	 * @access	protected
	 * @var		string
	 */
	var	$_name = 'Rainbow';

	function fetchElement($name, $value, &$node, $control_name)
	{
		static $cache = array();
		static $count = 0;
		$count++;

		$id = $control_name.$name;

		if (!isset($cache[$id]))
		{
			// Convert existing value to RGB
			$r = 0;
			$g = 0;
			$b = 0;

			$clr = trim($value, '# ');
			if (strlen($clr) == 6)
			{
				$r = hexdec($clr[0].$clr[1]);
				$g = hexdec($clr[2].$clr[3]);
				$b = hexdec($clr[4].$clr[5]);
			}
			else if (strlen($clr) == 3)
			{
				$r = hexdec($clr[0].$clr[0]);
				$g = hexdec($clr[1].$clr[1]);
				$b = hexdec($clr[2].$clr[2]);
			}

	        /*
	         * Required to avoid a cycle of encoding &
	         * html_entity_decode was used in place of htmlspecialchars_decode because
	         * htmlspecialchars_decode is not compatible with PHP 4
	         */
	        $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES), ENT_QUOTES);

			$document = &JFactory::getDocument();

			if ($node->attributes('small'))
			{
				$document->addScriptDeclaration(
					"window.addEvent('domready', function() {
						var rainbow$count = new MooRainbow('img_$id', {
							'startColor':[$r, $g, $b],
							'imgPath':'components/com_quanta/media/images/rainbow_small/',
							'onChange': function(color) {
								\$('$id').value = color.hex;
							}
						});
					});"
				);
				JHtml::stylesheet('rainbow_small.css', 'administrator/components/com_quanta/media/css/');
			}
			else
			{
				$document->addScriptDeclaration(
					"window.addEvent('domready', function() {
						var rainbow$count = new MooRainbow('img_$id', {
							'startColor':[$r, $g, $b],
							'imgPath':'components/com_quanta/media/images/rainbow/',
							'onChange': function(color) {
								\$('$id').value = color.hex;
							}
						});
					});"
				);
				JHtml::stylesheet('rainbow.css', 'administrator/components/com_quanta/media/css/');
			}
			JHtml::script('mooRainbow.js', 'administrator/components/com_quanta/media/js/');

			$size	= ($node->attributes('size') ? ' size="'.$node->attributes('size').'"' : 'size="8"');
			$class	= ($node->attributes('class') ? ' class="'.$node->attributes('class').'"' : 'class="inputbox"');

			$cache[$id] = '';
			$cache[$id] .= '<input type="text" name="'.$control_name.'['.$name.']" id="'.$id.'" value="'.$value.'"'.$class.$size.' />';
			$cache[$id] .= ' <img id="img_'.$id.'" class="rain" width="16" height="16" alt="[r]" src="components/com_quanta/media/images/rainbow/rainbow.png" />';
		}

		return $cache[$id];
	}
}