/*** QuickFilter ~ (C) 2005  Simon Bünzli  <zeniko@gmx.ch> ***/
/*** Altered 2005 by Zuber Sven ***/

var QuickFilter = {
	onLoad: function()
	{	
		
		this.tables = document.getElementsByTagName("TABLE");
		for (var i = 0; i < this.tables.length; i++)
		{
			var table = this.tables[i];
			
			if (!/\bquickfilter\b/.test(table.className))
			{ // nur Tabellen von der CSS-Klasse "quickfilter" werden berücksichtigt
				continue;
			}
			
			var thead = table.createTHead();
			if (!thead.firstChild && table.tBodies[0].rows.length)
			{ // falls keine Kopfzeile vorhanden ist, verwende dafür die erste Datenzeile
				thead.appendChild(table.tBodies[0].rows[0]);
			}
			if (!table.tBodies[0] || table.tBodies[0].rows.length < 1)
			{ // Tabellen ohne Datenzeilen werden ignoriert
				continue;
			}
			
			var rowsLength = 0;
			var columns = table.rows[0].cells;
			for (var j = 0; j < columns.length; j++)
			{
				rowsLength += parseInt(columns[j].getAttribute("colspan")) || 1;
			}
			
			
			var quickfilterBar = document.createElement("span");
			quickfilterBar.setAttribute("class", "search");
			quickfilterBar.innerHTML = 'Search: <input type="text" value="" onkeyup="QuickFilter.onUpdate(' + i + ', event.keyCode==13);" onchange="QuickFilter.onUpdate(' + i + ')">&nbsp;<img src="images/reset.gif" align="middle" width="89" height="18" alt="reset" onclick="var filter=this.previousSibling.previousSibling; filter.value=\'\'; QuickFilter.onUpdate(' + i + ', true); filter.focus();"/></a>';
			
			
			document.getElementById("find").appendChild(quickfilterBar);
		}
	},

	onUpdate: function(aTableNo, aAtOnce)
	{
		var table = this.tables[aTableNo];
		
		if (table._qs_timeout && table._qs_timeout == "filtering")
		{
			return;
		}
		else if (table._qs_timeout)
		{
			clearTimeout(table._qs_timeout);
		}
		if (!aAtOnce)
		{
			table._qs_timeout = setTimeout(function() { QuickFilter.doFilter(table); }, 500);
		}
		else
		{
			this.doFilter(table);
		}
	},

	doFilter: function(aTable)
	{
		aTable._qs_timeout = "filtering";
		
		var tbody = aTable.tBodies[0];
		// Input[16] is the search bar, which was created by quickfilter
		var query = document.getElementsByTagName("INPUT")[16].value.replace(/[ \t]+/g, " ").toLowerCase();
		var searchString = document.getElementsByTagName("INPUT")[16].value.replace(/[ \t]+/g, " ");
		var filterStrings = query.replace(/(-)?(?:"([^"]+)(?:"|$)|(\S+))\s*/g, "\n$1 $2$3").split("\n").slice(1);
		var negative = filterStrings.map(function(aQuery) { return aQuery.charAt(0) == "-"; });
		
		filterStrings = filterStrings.map(function(aQuery, aIdx) { return aQuery.substr((negative[aIdx])?2:1); });
		
		var count = 0;
		for (var i = 0; i < tbody.rows.length; i++){
			var visible = true;
			var x = tbody.rows[i].getAttribute('font-size','false');
			if(x == "10"){
				var rowText = tbody.rows[i].innerHTML.replace(/(<\/?).*?>/g, "$1>").replace(/[ \t]+/g, " ").toLowerCase();
				// Search for the first entry, that should be "hidden"
				var result = rowText.search("___")
				if (result != -1){
					rowText = rowText.substring(0,result)
				}
				for (var j = 0; visible && (j < filterStrings.length); j++){
					visible = visible && (rowText.indexOf(filterStrings[j]) > -1) != negative[j];
				}
				if(visible){
					tbody.rows[i].style.display = "";
					count++;
				}
				else{
					tbody.rows[i].style.display = "none";
				}
			}
			
		}
		var resultCount = document.createElement("span");
		if (searchString==""){
			resultCount.innerHTML = count+' results';
		}
		else {
			resultCount.innerHTML = count+' results for "'+searchString+'"';
		}
		document.getElementById("searchResult").replaceChild(resultCount,document.getElementById("searchResult").firstChild);
		
		
		aTable._qs_timeout = null;
	}
};

Array.prototype.map = function(aFunc){
	var mapped = new Array();
	
	for (var i = 0; i < this.length; i++){
		mapped[i] = aFunc(this[i], i);
	}
	
	return mapped;
}

QuickFilter.__onload = window.onload;

window.onload = function(aEvent){
	if (QuickFilter.__onload){
		QuickFilter.__onload(aEvent);
	}
	if (document.createElement && document.getElementsByTagName && document.body.innerHTML){
		QuickFilter.onLoad();
	}
}

