
/*
 * ParamController class, collects parameters and their values
 *
 * @package classes
 * @copyright Copyright 2009, Design for Delight
 * @version 1.0
 */
var ParamController = new Class(
{
	Implements: [Options, Events],

	options:
	{
		disabled: false,
		saveURL: 'params.ajax.php'
	},

	/*
	 * Constructor
	 */
	initialize: function (id)
	{
		this.id = id;
		this.params = new Array();
		this.values = new Array();
		this.observers = new Array();
		this.disabled = this.options.disabled;
	},
	
	/*
	 * Disables firing of events
	 */
	disable: function ()
	{
		this.disabled = true;
	},
	
	/*
	 * Enables notification of observers
	 *
	 * @param boolean notify
	 */
	enable: function (notify)
	{
		this.disabled = false;
		
		if (notify)
			this.fireEvent('onUpdate', this.getValues());
	},
	
	/*
	 * Registers parameter(s)
	 *
	 * @param mixed param
	 */
	register: function (param)
	{
		if ($type(param) == 'string')
		{
			if (this.params.indexOf(param) > -1)
				return;
			
			this.params.push(param);
			this.values.push(new Array());
		}
		else if ($type(param) == 'array')
		{
			param.each(function (val)
			{
				this.register(val);
			}.bind(this));
		}
	},
	
	/*
	 * Returns true if the specified parameter holds the specified value
	 *
	 * @param string param
	 * @param string value
	 * @return boolean
	 */
	hasValue: function (param, value)
	{
		if (this.params.indexOf(param) == -1)
			return false;
			
		var values = this.getValues(param);
		if (values.indexOf(value) > -1)
			return true;
		return false;
	},
	
	/*
	 * Add a value to the specified parameter
	 *
	 * @param string param
	 * @param string value
	 */
	addValue: function (param, value)
	{
		if (this.params.indexOf(param) == -1)
			return false;
			
		var values = this.getValues(param);
		if (values.indexOf(value) == -1)
		{
			this.values[this.params.indexOf(param)].push(value);
			this.save();
			
			if (!this.disabled)			
				this.fireEvent('onUpdate', this.getValues());
		}
	},
	
	/*
	 * Replaces a value for the specified parameter
	 *
	 * @param string param
	 * @param string value1
	 * @param string value2
	 */
	replaceValue: function (param, value1, value2)
	{
		if (this.params.indexOf(param) == -1)
			return false;
			
		var values = this.getValues(param);
		if (values.indexOf(value1) > -1 && values.indexOf(value2) == -1)
		{
			this.values[this.params.indexOf(param)].erase(value1);
			this.values[this.params.indexOf(param)].push(value2);
			this.save();
			
			if (!this.disabled)
				this.fireEvent('onUpdate', this.getValues());
		}
	},
	
	/*
	 * Removes a value belonging to the specified parameter
	 *
	 * @param string param
	 * @param string value
	 */
	removeValue: function (param, value)
	{
		if (this.params.indexOf(param) == -1)
			return false;
			
		var values = this.getValues(param);
		if (values.indexOf(value) > -1)
		{
			this.values[this.params.indexOf(param)].erase(value);
			this.save();
			
			if (!this.disabled)
				this.fireEvent('onUpdate', this.getValues());
		}
	},
	
	/*
	 * Removes more than one value belonging to a parameter
	 *
	 * @param string param
	 * @param Array values
	 */
	removeValues: function (param, values)
	{
		if (this.params.indexOf(param) == -1 || $type(values) != 'array')
			return false;
			
		values.each(function (item)
		{
			if (this.getValues(param).indexOf(item) > -1)
				this.values[this.params.indexOf(param)].erase(item);
		}.bind(this));
		this.save();
		
		if (!this.disabled)
			this.fireEvent('onUpdate', this.getValues());
	},
	
	/*
	 * Clears all values (belonging to the specified parameter)
	 *
	 * [@param string param]
	 */
	clear: function (param)
	{
		// If parameter is specified we clear it
		if ($type(param) == 'string')
		{
			var index = this.params.indexOf(param);
			if (index == -1)
				return false;
				
			this.values[index].empty();
		}
		// Else we clear all the parameters
		else
		{
			this.values.each(function (item)
			{
				item = new Array();
			}.bind(this));
		}
		this.save();
		
		if (!this.disabled)
			this.fireEvent('onUpdate', this.getValues());
	},
	
	/*
	 * Returns the parameters and values registered with this controller
	 *
	 * [@param string param]
	 * @return mixed
	 */
	getValues: function (param)
	{
		if (param)
		{
			var index = this.params.indexOf(param);
			if (index > -1)
				return this.values[index];
		}
		else
		{
			var result = new Hash();
			this.params.each(function (item, index)
			{
				if (this.values[index].length > 0)
					result.set(item, $A(this.values[index]));
			}.bind(this));
			return result;
		}
	},
	
	/*
	 * Returns the ID
	 *
	 * @return string id
	 */
	getId: function ()
	{
		return this.id;
	},
	
	/*
	 * This function saves the contents of this controller by sending the vars to the server
	 */
	save: function ()
	{
	}
});
