/*************************************************************************************************\
|* @author Unknown                                                                               *|
|* @version 1.0                                                                                  *|
\*************************************************************************************************/

/*************************************************************************************************\
|* @module Search                                                                                *|
|*                                                                                               *|
|* @description This class contains everything needed for the list searches.                     *|
|*                                                                                               *|
|* @version 1.0                                                                                  *|
\*************************************************************************************************/
if (!CLUBNICK.Modules) {
	CLUBNICK.Modules = {};
}

CLUBNICK.Modules.Search = (new function () {
	var $searchElement;
	var $searchForm;
	
	this.DEFAULT_VALUE = 'Suchen...';

	/*********************************************************************************************\
	|* @method init                                                                              *|
	|*                                                                                           *|
	|* @description Initializes the Search, creates the Tooltip and starts binding the           *|
	|* submit-observers.                                                                         *|
	\*********************************************************************************************/
	this.init = function () {
	    $searchElement = $('list_query');
	    
	    if (!$searchElement) {
	        return;
	    }
	
		$searchForm = $searchElement.up('form');
	
		$searchElement.defaultValueActsAsHint(CLUBNICK.Modules.Search.DEFAULT_VALUE);    

	    createToolTip();
		addObserver();
	};

	/*********************************************************************************************\
	|* @method createToolTip                                                                     *|
	|*                                                                                           *|
	|* @description Creates a tooltip for the searchForm. This will be displayed, if someone     *|
	|* tries to start an empty search.                                                           *|
	\*********************************************************************************************/
	function createToolTip () {
	    var options = {
	        stem: 'bottomLeft',
	        hideOthers: true,
	        className: 'small',
	        offset: {
	            x: - $searchElement.getDimensions().width / 2 ,
	            y: 0
	        },
	        hook: {
	            target: 'topRight',
	            tip: 'bottomLeft'   
	         },
	        border: 5,
	        borderColor: '#e56a17',
	        radius: 5,
	        images: 'styles/clubnick/',
	        showOn: false,
	        hideAfter: 3
	    };

	    new Tip($searchElement, 'Gib einen Suchbegriff ein!', options);
	}

	/*********************************************************************************************\
	|* @method valueIsDefaultValue                                                               *|
	|*                                                                                           *|
	|* @description Indicates, wether the current value equals the searchFields defaultValue.    *|
	|*                                                                                           *|
	|* @return {Boolean} value equals defaultValue?                                              *|
	\*********************************************************************************************/
	function valueIsDefaultValue () {
		return ($searchElement.value === CLUBNICK.Modules.Search.DEFAULT_VALUE);
	}

	/*********************************************************************************************\
	|* @method addObserver                                                                       *|
	|*                                                                                           *|
	|* @description Binds the submit event to a callback. Here is checked, if the                *|
	|* valueIsDefaultValue, then show the Tooltip.                                               *|
	\*********************************************************************************************/
	function addObserver () {
		$searchForm.observe('submit', function (event) {
	        if (!valueIsDefaultValue()) {
	            return;
	        }

            Event.stop(event);
            $searchElement.prototip.show();
            $searchElement.activate();
	    });
	
		bindHandlingForCharacterCategorySelect();
	}

	/*********************************************************************************************\
	|* @method bindHandlingForCharacterCategorySelect                                            *|
	|*                                                                                           *|
	|* @description Binds the change event to a callback, if a CharacterSelectBox is there.      *|
	|* If the valueIsDefaultValue, remove the value, before auto-submitting the form.            *|
	\*********************************************************************************************/
	function bindHandlingForCharacterCategorySelect () {
		var $characterCategorySelect = $('character_category_id');
		if (!$characterCategorySelect) {
			return;
		}

		$characterCategorySelect.observe('change', function (event) {

			// Clear search value, if value is default value. So no string search is triggered, if category was changed. 
			if (valueIsDefaultValue()) {
			    $searchElement.value = '';
			}

			$searchForm.submit();
		});
	}
});