/**
 * Direct message text command class
 * @author Andrey Lugovtsov
 */
var dmTextCommand = Class.create(baseTextCommand, {
	
	/**
	 * @param mixed el Элемент, в котором происходит набирание букв
	 * @param mixed indicator Элемент, которому в innterHTML будут записываться результаты парсинга
	 * @param object autocomplete Объект autoСomplete. Если null, то autocomplete=off.
	 * 
	 */
	initialize: function($super, el, indicator, autoCompleter) {
		$super(new RegExp('^[\\s]*[dD][\\s]+([-\\w\\d]*)([\\s]*.*)', 'i'), el, indicator);
		this.autoCompleter = autoCompleter;
		this.lastAutoComplete = null;
		this.matches = null;
	},
	
	/**
	 * Покажем элемент с надписью Direct message to
	 */
	onFirstSuccessTest: function(matches) {
//		window.console.log(matches);
		this.indicator.update(this.getIndicatorText(matches[1]));
		this.showIndicator();
		this.autoCompleterQueryWrapper(matches);
	},
	
	/**
	 * Спрячем элемент с подписью команды
	 */
	onFirstFailureTest: function() {
//		window.console.log('onFirstFailureTest');
		this.hideIndicator();
		this.matches = null;
		if (this.autoCompleter) {
			this.autoCompleter.hideResults();
		}
	},
	
	getIndicatorText: function(userName) {
		return 'Direct message to ' + userName;
	},
	
	onSuccessTest: function(matches) {
		this.indicator.update(this.getIndicatorText(matches[1]));
		if (! this.indicator.visible()) {
			this.showIndicator();
		}
		this.matches = matches;
		this.autoCompleterQueryWrapper(matches);
	},
	
	autoCompleterQueryWrapper: function(matches) {
		if (this.autoCompleter) {
			if (matches[2].length < 1) {
				if (this.lastAutoComplete === null || this.lastAutoComplete != matches[1]) {
					this.autoCompleter.query(this.lastAutoComplete = matches[1]);
				}
				else if (this.lastAutoComplete == matches[1]) {
					this.autoCompleter.showResults();
				}
			}
			else {
				this.autoCompleter.hideResults();
			}
		}
	}
});
