<?php

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * Renders a imagethumbnails element
 *
 * @package 	Captcha
 */

class JElementImagethumbnails extends JElement
{
	/**
	* Element name
	*
	* @access	protected
	* @var		string
	*/
	var	$_name = 'Imagethumbnails';

	function fetchElement($name, $value, &$node, $control_name)
	{
		jimport( 'joomla.filesystem.folder' );
		jimport( 'joomla.filesystem.file' );

		// path to images directory
		$path		= JPATH_ROOT.DS.$node->attributes('directory');
		//$filter		= $node->attributes('filter');
		$filter = '\.png$|\.gif$|\.jpg$|\.bmp$|\.ico$';
		$exclude	= $node->attributes('exclude');
		$stripExt	= $node->attributes('stripext');
		$files		= JFolder::files($path, $filter);

		$options = array ();

		if (!$node->attributes('hide_none'))
		{
			$options[] = JHTML::_('select.option', '-1', '- '.JText::_('Do not use').' -');
		}

		if (!$node->attributes('hide_default'))
		{
			$options[] = JHTML::_('select.option', '', '- '.JText::_('Use default').' -');
		}

		if ( is_array($files) )
		{
			foreach ($files as $file)
			{
				if ($exclude)
				{
					if (preg_match( chr( 1 ) . $exclude . chr( 1 ), $file ))
					{
						continue;
					}
				}
				if ($stripExt)
				{
					$file = JFile::stripExt( $file );
				}
				$options[] = JHTML::_('select.option', $file, $file);
			}
		}
		$thumbImage		= $name.'-thumbnail-image';
		$thumbContainer	= $name.'-thumbnail-container';
		$javascript=" onchange=\"$('".$thumbImage."').src='".JURI::root().$node->attributes('directory')."'+this.value;\" ";
		return JHTML::_('select.genericlist',  $options, ''.$control_name.'['.$name.']', 'class="inputbox"'.$javascript, 'value', 'text', $value, $control_name.$name)
				.'<span id="'.$thumbContainer.'"><img id="'.$thumbImage.'" src="'.JURI::root().$node->attributes('directory').$value.'" /></span>'
				;
	}
}
