var ElementFilter = Class.create();
ElementFilter.prototype = {
	initialize: function(id, options) {
		this.container = $(id);
		if (!this.container) return;
		this.options = Object.extend({
			tag: 'li',
			searchText: 'Find: ',
			searchCss: ''
		}, options || {});
		
		this.items = this.container.getElementsByTagName(this.options.tag);
		this.text = [];
		// Preload the text of the item for faster comparison
		for (var i=0, item; item = this.items[i]; i++) {
			var t = this.getText(item);
			if (t) {
				this.text.push(t);
			} else {
				return;
			}
		}
		
		// Create search area
		this.search = $element('input', {type: 'text', id: 'search_' + id});
		this.label = $element('label', {'for': this.search.id}, this.options.searchText);
		this.panel = $element('div', {'class': this.options.searchCss});
		this.panel.addChildren(this.label, this.search);
		this.search.observe('keyup', this.filter.bind(this));
		this.panel.addBefore(this.container);
	},
	
	filter: function() {
		var rx = new RegExp(this.search.value, "i");
		for (var i=0, item; item = this.items[i]; i++) {
			if (rx.test(this.text[i])) {
				Element.show(item);
			} else {
				Element.hide(item);
			}
		}
	},
	
	getText: function(node) {
		return node.innerText || node.textContent || node.text || null;
	}
}