/**
 * @class	OnClickInputbox
 * @author	Marco Troost
 */
var OnClickInputbox = new Class({

	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	string	search_value
	 * @return	void
	 */
	initialize: function(root_node_id, search_value)
	{
		// nodes
		this.root_node					= !root_node_id ? document.getElement('body') : $(root_node_id);
		
		// classes
		this.input_selected_class		= 'selected';
		
		// values
		this.search_value				= search_value;
	},

	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// set events
			this.setEvents();
		}
	},

	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set vars
		var _this = this;

		if (this.root_node)
		{
			// loop trough forms on page
			var nodes		= this.root_node.getElements('form');
			var total_nodes	= nodes.length;
			if (total_nodes)
			{
				nodes.each(function(node, index)
				{
					node.removeEvents();
					
					_this.checkElements(node, 'input');
					_this.checkElements(node, 'textarea');
				});
			}			
		}
	},
	
	
	/**
	 * check input field
	 * @return	void
	 */
	checkElements: function(form_node, element)
	{
		// set vars
		var _this = this;

		if (form_node && element)
		{
			// loop trough inputboxes on form
			var nodes		= form_node.getElements(element);
			var total_nodes	= nodes.length;
			if (total_nodes)
			{
				nodes.each(function(node, index)
				{
					node.removeEvents();

					var input_value = node.get('value');
					
					// check if form is from component search
					if (!input_value)
					{
						var form_id = form_node.get('id');
						
						if (form_id == 'cs_search_pages_form')
						{
							input_value = _this.search_value;
						}							
					}
					
					
					if (input_value)
					{
						node.set('value', input_value);

						node.addEvent('click',function()
						{
							var get_value = node.get('value');
							if (get_value == input_value)
							{
								node.set('value', '');
								node.toggleClass(_this.input_selected_class);
							}
						});

						node.addEvent('blur',function()
						{
							var get_value = node.get('value');

							if (!get_value)
							{
								node.set('value', input_value);
								node.toggleClass(_this.input_selected_class);
							}
						});						
					}
				});
			}		
		}
	}

});
