/**
 * Direct message text command class
 * @author Andrey Lugovtsov
 */
var groupMessageTextCommand = 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]*[gG][\\s]+(\'.*)[\\s]*(.*)', 'i'), el, indicator);
		this.autoCompleter = autoCompleter;
		this.lastAutoComplete = null;
		this.matches = null;
		this.groupNameRegExp = new RegExp('^[\\s]*[gG][\\s]+\'(.+)\'[\\s]*.*', 'i'); 
	},
	
	/**
	 * Покажем элемент с надписью Direct message to
	 */
	onFirstSuccessTest: function(matches) {
//		window.console.log(matches);
		this.indicator.update(this.getIndicatorText());
		this.showIndicator();
		this.autoCompleterQueryWrapper(matches);
	},
	
	/**
	 * Спрячем элемент с подписью команды
	 */
	onFirstFailureTest: function() {
//		window.console.log('onFirstFailureTest');
		this.hideIndicator();
		this.matches = null;
		if (this.autoCompleter) {
			this.autoCompleter.hideResults();
		}
	},
	
	getIndicatorText: function() {
		var group = this.getGroupName();
		return 'Group message to ' + (group ? group + ' group' : '');
	},
	
	getGroupName: function() {
		var s = new String(this.el.getValue());
		var matches = s.match(this.groupNameRegExp);
		var group = false;
		if (matches) {
			group = matches[1];
		}
		return group;
	},
	
	onSuccessTest: function(matches) {
		this.indicator.update(this.getIndicatorText());
		if (! this.indicator.visible()) {
			this.showIndicator();
		}
		this.matches = matches;
		this.autoCompleterQueryWrapper(matches);
	},
	
	autoCompleterQueryWrapper: function(matches) {
		if (this.getGroupName()) {
			return this.autoCompleter.hideResults();
		}
//		window.console.log(matches)
		if (this.autoCompleter) {
			if (this.lastAutoComplete === null || this.lastAutoComplete != matches[1]) {
				this.autoCompleter.query(this.lastAutoComplete = matches[1]);
			}
			else if (this.lastAutoComplete == matches[1]) {
				this.autoCompleter.showResults();
			}
		}
	}
});
