<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

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

class JElementCategories extends JElement {

  var   $_name = 'Categories';

	function fetchElement($name, $value, &$node, $control_name)
	{
		$db = &JFactory::getDBO();

		$db->setQuery("SELECT IdCategory,CategoryName FROM #__rsevents_categories");
		$options = $db->loadObjectList();
		$result = '<select name="'.$control_name.'['.$name.'][]" id="'.$name.'" class="inputbox" multiple="multiple">';
		
		foreach( $options as $option ) {
			if(is_array( $value) ) {
				if( in_array( $option->IdCategory, $value ) ) {
					$result .= '<option selected="true" value="'.$option->IdCategory.'" >'.$option->CategoryName.'</option>';
				} else {
					$result .= '<option value="'.$option->IdCategory.'" >'.$option->CategoryName.'</option>';
				}
			} elseif ( $value ) {
				if( $value == $option->IdCategory ) {
					$result .= '<option selected="true" value="'.$option->IdCategory.'" >'.$option->CategoryName.'</option>';
				} else {
					$result .= '<option value="'.$option->IdCategory.'" >'.$option->CategoryName.'</option>';
				}
			} elseif ( !( $value ) ) {
				$result .= '<option value="'.$option->IdCategory.'" >'.$option->CategoryName.'</option>';
			}
		}
		$result .= '</select>';
		return $result;

	}
}
