Liferay.CHMMotCleSelector = new Class({

	/**
	 * OPTIONS
	 *
	 * Required
	 * instanceVar {string}: The instance variable for this class.
	 * hiddenInput {string}: The hidden input used to pass in the current tags.
	 * textInput {string}: The text input for users to add tags.
	 *
	 * Optional
	 * focus {boolean}: Whether the text input should be focused.
	 * strict {boolean}: Wether the search is strict (=?) or not (LIKE %?%)
	 */

	initialize: function(options) {
		var instance = this;

		instance._curMotCle = '';

		instance.options = options;
		instance._ns = instance.options.instanceVar || '';
		instance._mainContainer = jQuery('<div class="chmeaux-motcle-select-container"></div>');
		instance._container = jQuery('<div class="chmeaux-motcle-container"></div>');

		var hiddenInput = jQuery('#' + options.hiddenInput);

		hiddenInput.attr('name', hiddenInput.attr('id'));

		var textInput = jQuery('#' + options.textInput);

		textInput.autocomplete(
			{
				source: function(term) { return instance._getMotsCles(instance, term) },
				width: textInput.width() + 20,
				formatItem: function(row, i, max, term) {
					return row;
				},
				dataType: 'json',
				delay: 1,
				multiple: false,
				mutipleSeparator: '',
				minChars: 1,
				hide: function(event, ui) {
					jQuery(this).removeClass('showing-list');
				},
				show: function(event, ui) {
					jQuery(this).addClass('showing-list');
					this._LFR_listShowing = true;
				},
				result: function(event, ui) {
					var caretPos = this.value.length;

					if (this.createTextRange) {
						var textRange = this.createTextRange();

						textRange.moveStart('character', caretPos);
						textRange.select();
					}
					else if (this.selectionStart) {
						this.selectionStart = caretPos;
						this.selectionEnd = caretPos;
					}
				}
			}
		);


		var okButton = jQuery('#' + options.instanceVar + 'okMotCle');

		okButton.click(
			function() {
				instance._curMotCle = textInput.val();

				textInput.val('');

				instance._update();

				eval(options.javascript);
			}
		);

		textInput.keypress(
			function(event) {
				if (event.keyCode == 13) {
					if (!this._LFR_listShowing) {
						okButton.trigger('click');
					}

					this._LFR_listShowing = null;

					return false;
				}
			}
		);


		if (options.focus) {
			textInput.focus();
		}

		if (options.curMotCle != '') {
			instance._curMotCle = options.curMotCle;

			instance._update();
		}

		Liferay.Util.actsAsAspect(window);

		window.before(
			'submitForm',
			function() {
				var val = jQuery.trim(textInput.val());

				if (val.length) {
					okButton.trigger('click');
				}
			}
		);
	},

	_getMotsCles: function(instance, term) {
		var limit = 20;
		var offset = 0;

		var data = Liferay.Service.CHMReferentiel.MotCle.searchAutocomplete(
			{
				libelle: term,
				strict: instance.options.strict,
				limit: limit,
				offset: offset
			}
		);

		var ret = jQuery.map(
			data,
			function(row) {
				return {
					data: row.text,
					value: row.value,
					result: row.text
				}
			}
		);

		return ret;
	},

	_update: function() {
		var instance = this;

		instance._updateHiddenInput();
	},

	_updateHiddenInput: function() {
		var instance = this;

		var options = instance.options;
		var curMotCle = instance._curMotCle;

		var hiddenInput = jQuery('#' + options.hiddenInput);

		hiddenInput.val(curMotCle);
	}

});