(function($) {
	var counter = 0;
	
	function isIgnored(array, text) {
		if ($.inArray(text, array) == -1) {
			return false;
		} else {
			return true;
		}
	}
	
	$.fn.tablesearcherSearch = function() {
		$(this).children('tbody').children(':hidden').css('display', 'table-row');
		var searchers = $('#'+$.fn.settings.searchers).children(':input');
		for (var i=1; i <= counter; i++) {
			if ($('#'+$.fn.settings.searcherid+i).val()) {
				var index = $.inArray($('#'+$.fn.settings.kriteriumid+i).val(), $.fn.cells);
				$.each($(this).children('tbody').children('tr'), function() {
					var cell = this.cells.item(index);
					if (cell.innerHTML.toLowerCase().indexOf($('#'+$.fn.settings.searcherid+i).val().toLowerCase()) == -1) {
						cell.parentNode.style.display = 'none';
					}
				});
			}
		}
	}
	$.fn.tablesearcherAddSearcher = function() {
		var optiontext = '';
		$.each($.fn.cells, function() {
			optiontext += '<option value="'+this+'">'+this+'</option>';
		});
		counter++;
		$('#'+$.fn.settings.searchers).append($.fn.settings.description+' <input type="text" id="'+$.fn.settings.searcherid+counter+'" onchange="jQuery(\'#'+this.attr('id')+'\').tablesearcherSearch()"> <select id="'+$.fn.settings.kriteriumid+counter+'" onchange="jQuery(\'#'+this.attr('id')+'\').tablesearcherSearch()">'+optiontext+'</select><br><br>');
	}
	$.fn.tablesearcher = function(options) {
		counter = 0;
		$.fn.settings = $.extend({
			ignore: [],
			description: 'Suche:',
			button: '',
			buttontext: 'Kriterium hinzuf&uuml;gen',
			searchers: '',
			searcherid: 'searcher',
			kriteriumid: 'kriterium'
		}, options);
		
		if ($.fn.settings.button == '') {
			$(this).before('<div id="searchbutton"><button onclick="jQuery(\'#'+this.attr('id')+'\').tablesearcherAddSearcher()">'+$.fn.settings.buttontext+'</button></div>');
			$.fn.settings.button = 'searchbutton';
		} else {
			$('#'+$.fn.settings.button).html('<button onClick="jQuery(\'#'+this.attr('id')+'\').tablesearcherAddSearcher()">'+$.fn.settings.buttontext+'</button>');
		}
		if ($.fn.settings.searchers == '') {
			$('#'+$.fn.settings.button).before('<div id="searchers"> </div><br>');
			$.fn.settings.searchers = 'searchers';
		}
		$.fn.cells = [];
		$.each($(this).children('thead').children('tr').children('th'), function() {
			if (!isIgnored($.fn.settings.ignore, this.innerHTML)) {
				$.fn.cells.push(this.innerHTML);
			}
		});
		var optiontext = '';
		$.each($.fn.cells, function() {
			optiontext += '<option value="'+this+'">'+this+'</option>';
		});
		counter++;
		$('#'+$.fn.settings.searchers).append($.fn.settings.description+' <input type="text" id="'+$.fn.settings.searcherid+counter+'" onchange="jQuery(\'#'+this.attr('id')+'\').tablesearcherSearch()"> <select id="'+$.fn.settings.kriteriumid+counter+'" onchange="jQuery(\'#'+this.attr('id')+'\').tablesearcherSearch()">'+optiontext+'</select><br><br>');
	}
})(jQuery);