var olSortableTable = sortableTable.extend({
	options: {
		altRowCls: 'altRow',
		categoryCls: false,// groups
		inputKey: false,
		clearKey: false,
		frequency: 0.4
	},
	initialize: function(table, options){
		this.parent(table, options);
		this.category= false; 
		this.elements.each(function(el,i){
			if (this.options.categoryCls) {
				if (el.isCategory=el.hasClass(this.options.categoryCls)) this.category= el;
			} else el.isCategory= false;
			el.category= this.category;
		}, this);
		if (options.inputKey) {
			this.inputKey= $(options.inputKey);
		    this.inputKey.addEvent('blur', this.onBlur.bind(this));
			this.inputKey.addEvent('keydown', this.onKeyPress.bind(this));
			var frm = $(this.inputKey.form);
			frm.onsubmit= function() { return false; }
			if (options.clearKey) {
				var clr= $(options.clearKey); clr.addEvent('click',this.clearFilter.bind(this));
			} else frm.getChildren().each(function(el,i){
				if (el.type == 'reset') el.addEvent('click',this.clearFilter.bind(this));
			}, this);
			this.filterKey();
		} else this.altRow();
	},
	altRow: function(){
		this.elements.each(function(el,i){
			if (el.isCategory) return ;
			if(i % 2){
				el.removeClass(this.options.altRowCls);
			}else{
				el.addClass(this.options.altRowCls);
			}
		}, this);
	},
	showRow: function (el, visible) {
		if (visible){
			el.addClass(this.options.filterSelectedCls);
			if(this.options.filterHide){
				el.removeClass(this.options.filterHideCls);
			}
		}else{
			el.removeClass(this.options.filterSelectedCls);
			if(this.options.filterHide){
				el.addClass(this.options.filterHideCls);
			}
		}
	},
	filterKey: function () { 
		var key = this.inputKey.value.toLowerCase();
		if(key){
			this.category= false;
			this.elements.each(function(el,i){
				if(this.options.filterHide) el.removeClass(this.options.altRowCls);
				if (this.category && (el.category==this.category) && this.category.visibleBlock) el.visibleBlock= true;
				else {
					el.visibleBlock= false;
//					if (el.get('text').toLowerCase().indexOf(key) > -1) { // moo 1.2
					if (el.getText().toLowerCase().indexOf(key) > -1) {
						el.visibleBlock= true;
						if (el.category) {
							if (el.category!=el) this.showRow(el.category, true);
							else this.category=el;
						}
					}
				}
				this.showRow(el, el.visibleBlock);
			}, this);
			if(this.options.filterHide){
				this.filteredAltRow();
				this.filtered = true;
			}
		} else this.clearFilter();
	},
	onKeyPress: function(event) {
/*		switch(event.key) {	// moo 1.2
		case Event.KEY_LEFT:
		case Event.KEY_RIGHT:
		case Event.KEY_UP:
		case Event.KEY_DOWN:
		 return;
		case Event.KEY_TAB:
		case Event.KEY_RETURN:
		//         this.selectEntry();
		//         Event.stop(event);
		case Event.KEY_ESC:
		}*/
		if(this.observer) clearTimeout(this.observer);
		this.observer= setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
	},
	onObserverEvent: function() { this.filterKey(); },
	onBlur: function() { this.filterKey(); return false;}

});