/**
 * От этого класса следует наследовать все текстовые комманды (ТК)
 * @author Andrey Lugovtsov <alugovtsov@craton.by>
 * 
 * Внимание! Класс слушает кастомное событие text-command:test и запускает по нему тест содержимого 
 */
var baseTextCommand = Class.create({
	/**
	 * Конструктор, в нем происходит навешивание на el события keyup
	 * 
	 * @param object regularExpression Регулярное выражение, по которому вычисляется ТК
	 * @param mixed el Элемент, на keyup у которого и будут происходить все события
	 * @param mixed indicator Элемент, которому в innterHTML будут записываться результаты парсинга
	 */
	initialize: function(regularExpression, el, indicator) {
		this.regExp = regularExpression;
		this.el = $(el);
		this.indicator = $(indicator);
		this._testSuccessfull = null;
		this._t = -1;
		
		this.el.observe('keyup', function(e) {
			if (e.keyCode != 27) { // не реагировать на ESC
				this._testWrapper();
			}
		}.bind(this));
		
		this.el.observe('text-command:test', function() {
			this._testWrapper();
		}.bind(this));
		
	},
	
	/**
	 * @private
	 */
	_testWrapper: function() {
		window.clearTimeout(this._t);
		this._t = this.test.bind(this).delay(0.3);
	},
	
	/**
	 * Вызывается на onKeyUp элемента
	 */
	test: function() {
		var s = new String(this.el.getValue()); //.strip();
		var matches = s.match(this.regExp);
		if (matches) { // выражение успешно найдено в строке
			if (this._testSuccessfull === false || this._testSuccessfull == null) {
				// первый успешный тест
				// вызвать onFirstSuccessTest
				// в качестве параметров передаем совпадения
				this.onFirstSuccessTest.call(this, matches);
			}
			this._testSuccessfull = true;
			this.onSuccessTest.call(this, matches);
		}
		else { // выражение не найдено
			// если уже был хотя бы один успешный тест		
			if (this._testSuccessfull === true) {
				this.onFirstFailureTest.call(this);
			}
			this._testSuccessfull = false;
			this.onFailureTest.call(this);
		}
	},
	
	/**
	 * Вызовется, когда результат регулярного выражения переходит из true в false (меняет истинность) 
	 * @param array matches Массив найденных совпадений
	 */
	onFirstSuccessTest: function(matches) {
//		window.console.log('onFirstSuccessTest');
	},
	
	/**
	 * Вызовется, когда результат регулярного выражения переходит из false в true (меняет истинность)
	 */
	onFirstFailureTest: function() {
//		window.console.log('onFirstFailureTest');
	},
	
	/**
	 * Вызывается когда проверка выражения дает false 
	 */
	onFailureTest: function() {
//		window.console.log('onFailureTest');
	},
	
	/**
	 * Вызывается когда проверка выражения дает true 
	 * * @param array matches Массив найденных совпадений
	 */
	onSuccessTest: function(matches) {
//		window.console.log('onSuccessTest');
	},
	
	hideIndicator: function() {
		new Effect.Fade(this.indicator);
//		this.indicator.hide();
	},
	
	showIndicator: function(text) {
		new Effect.Appear(this.indicator);
//		this.indicator.show();
	}
});
