var WebElements;

if(!WebElements) WebElements = {};

WebElements.KeywordFilter = function(input, options) {
	
	this.input = input;				// defines the input element for the keyword
	this.button = null;				// defines the button that fires the filter
	this.form = null;				// defines the form element for the search
	this.container = null;			// defines the element that contains the dataset to be filtered (e.g. an 'ul' or a 'table')
	this.lines = null;				// defines the lines of the dataset (e.g. 'li' or 'tr')
	this.lines_tagname = "";		// defines the tag name of the dataset lines
	this.items_tagname = "";		// defines the tag name of the lines' items that hold information to filter (e.g. 'td' in 'tr' or 'label' in 'li')
									// it can be used to group items into lines to control the displaying of whole lines
	
	this.mode = "onchange";			// how is the filter fired ("onchange" means while typing, "onclick" means by pressing the button)
	
	this.setOptions(options);
	
	this.init();
	
	this.attachBehaviors();
}

WebElements.KeywordFilter.prototype.setOptions = function(options) {
	
	if(options) 
	{
		for(var optionname in options)
		{
			this[optionname] = options[optionname];
		}
		
		return true;
	}
	
	return false;
}

WebElements.KeywordFilter.prototype.init = function() {
	
	if(this.button == null)
	{
		this.mode = "onchange";
	}
	
	if(this.lines == null && this.lines_tagname != "")
	{
		this.lines = this.container.getElementsByTagName(this.lines_tagname);
	}
}

WebElements.KeywordFilter.prototype.attachBehaviors = function() {
	
	var self = this;
	
	switch(this.mode)
	{
		case "onchange":
					WebElements.Utils.addEventListener(this.input, "keyup", function() { self.filter(); });
					break;
					
		case "onclick":
					WebElements.Utils.addEventListener(this.button, "click", function() { self.filter(); });
					break;
	}
}

WebElements.KeywordFilter.prototype.filter = function() {
	
	// fetch keyword from input element
	var keyword = this.input.value;
	
	// check if it's empty - if so then show all lines
	// if not search for the keyword
	if(keyword != null && keyword != "")
	{
		// loop through all the lines
		for(var i = 0; i < this.lines.length; i++)
		{
			var found = false;
			
			// if there are items defined for the list
			if(this.items_tagname != "")
			{
				// fetch all the items of the current list element
				var items = this.lines[i].getElementsByTagName(this.items_tagname);
				
				// loop through the items
				for(var k = 0; k < items.length; k++)
				{
					// ensure to get the real text of the item
					var textnode = items[k].firstChild;
					
					// search for content only if there is something in the item
					if(textnode != null)
					{
						// nodeType is 3 for a text node
						while(textnode.nodeType != 3) textnode = textnode.nextSibling;
						
						// if the keyword is found in an item set found to true
						if(textnode.nodeValue.toLowerCase().indexOf(keyword.toLowerCase()) != -1)
						{
							found = true;
						}
					} 
				}
			}
			// if there are no items defined for the list
			else
			{
				// search for the keyword directly in the list element
				if(this.lines[i].firstChild.nodeValue.toLowerCase().indexOf(keyword.toLowerCase()) != -1)
				{
					found = true; 
				}
			}
			
			// show or hide the current line according to if the keyword was found
			if(found) 	this.showLine(i);
			else		this.hideLine(i);
		}
	}
	else this.showAllLines();
	
	return false;
}

WebElements.KeywordFilter.prototype.hideLine = function(index) {
	
	this.lines[index].style.display = "none";
}

WebElements.KeywordFilter.prototype.showLine = function(index) {
	
	this.lines[index].style.display = "";
}

WebElements.KeywordFilter.prototype.showAllLines = function() {
	
	for(var i = 0; i < this.lines.length; i++)
	{
		this.showLine(i);
	}
}



