/**
 * @class	W_ChangeMultipleSelectbox
 * @author	Marco Troost
 */
var W_ChangeMultipleSelectbox = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	string	listener_node_id
	 * @return	void
	 */
	initialize: function(root_node_id, listener_node_id, input_node_id, add_node_id, remove_node_id, add_node_all_id, remove_node_all_id)
	{
		// nodes
		this.root_node				= $(root_node_id);
		this.listener_node			= $(listener_node_id);		
		this.input_node				= $(input_node_id);		
		this.add_node				= $(add_node_id);		
		this.remove_node			= $(remove_node_id);		
		this.add_all_node			= $(add_node_all_id);		
		this.remove_all_node		= $(remove_node_all_id);		
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		// set vars
		var _this	= this;

		if (this.root_node && this.listener_node && this.input_node && this.add_node && this.remove_node)
		{
			var add 		= 1;
			var remove 		= 2;
			var add_all 	= 3;
			var remove_all 	= 4;
			
			this.add_node.addEvent('click',function()			
			{
				_this.moveSelection(add);
			}); 
			
			this.remove_node.addEvent('click',function()			
			{
				_this.moveSelection(remove);
			});
			
			this.add_all_node.addEvent('click',function()			
			{
				_this.moveSelection(add_all);
			});
			
			this.remove_all_node.addEvent('click',function()			
			{
				_this.moveSelection(remove_all);
			});				
			
			this.root_node.addEvent('dblclick',function()
			{
				_this.moveSelection(add);
			});
			
			this.listener_node.addEvent('dblclick',function()
			{
				_this.moveSelection(remove);
			});			
			
		}
	},

	/**
	 * move selection
	 * @param	object handler_node
	 * @return	void
	 */
	moveSelection: function(action)
	{
		// set vars
		var _this	= this;
		
		if (action == 1 || action == 3)
		{
			// add items to listener_node box 
			var source_node			= this.root_node;
			var destination_node	= this.listener_node;			

		}
		else if (action == 2 || action == 4)
		{
			// remove items from listener_node box
			var source_node			= this.listener_node;
			var destination_node	= this.root_node;
		}
		
		if (action == 1 || action == 2)
		{
			// get selected items
			var selected_option = source_node.getSelected();	
		   	var option_values 	= selected_option.get('value').toString();
		   	var option_id		= selected_option.get('id').toString();			
		}
		else
		{
			// get all the items
			var option_nodes	= source_node.getElements('option');
		   	var option_values 	= option_nodes.get('value').toString();
		   	var option_id		= option_nodes.get('id').toString();
			selected_option 	= option_nodes;
		}

	   	if (selected_option)
	   	{
	   		// copy node_id to hidden inputbox
	   		_this.handleInputbox(action, option_values);

	   		// inject option in destination
	   		selected_option.inject(destination_node);		   		
	   	}		
	},
	
	/**
	 * handleInputBox: copy selection values to (hidden) inputbox
	 * @param	action
	 * @return	void
	 */	
	handleInputbox: function(action, option_values)
	{	
		// set vars
		var _this	= this;
		
   		// add item to input_node
   		if (action == 1 || action == 3)
   		{
	   		var value_input_node = this.input_node.get('value');
	   		if (value_input_node)
	   		{
	   			var new_input_value = value_input_node+','+option_values; // add comma and selection to new input value
	   		}
	   		else 
	   		{ // inputbox is empty
	   			var new_input_value = option_values; // add selection to input value
	   		}
   			this.input_node.set('value', new_input_value);
   		}
   		
   		// delete item from input_node
		else if (action == 2 || action == 4) 
		{				
			var value_input_node = this.input_node.get('value');
			
			if (value_input_node.contains(','))
			{ 
				if (value_input_node.contains(','+option_values)) // multiple items in input node
				{
					var remove 			= ','+option_values;
				}
				else
				{
					var remove 			= option_values;						
				}
				var new_input_value = value_input_node.replace(remove,'');
				this.input_node.set('value', new_input_value);
			}
			else 
			{	// there is only one selection in inputbox
				var new_input_value = value_input_node.replace(option_values,'');
				this.input_node.set('value', new_input_value);					
			}
		}		
	}
});
