var wacker = wacker || {};
wacker.textTableFilterShowAll = wacker.textTableFilterShowAll || 'alle anzeigen';
wacker.textTableFilterInit = wacker.textTableFilterInit || 'Filter einschalten';
wacker.textTableFilterRemove = wacker.textTableFilterRemove || 'Filter ausschalten';

jQuery().ready(function ($) {
	

	$('table.fn-filter').each(function (numTable) {
		var table = $(this);
		var toggleFilter;
		var thead;
		var initialized = false;
		var firstRow;
		var shown = false;
		var rows;
		if (!table.find('thead').length) {
			firstRow = table.find('tr:eq(0)');
			rows = table.find('tr:gt(0)');
		} else {
			thead = table.find('thead:eq(0)')
			firstRow = thead.children('tr:eq(0)');
			rows = table.find('tbody > tr');
		}
		
		var filterRow = $('<tr style="display:none;" class="space"/>');
		var contents = {};
		var headLinks = [];
		var currentFilterLinks = [];
		var currentFilterLink;
		var currentListItem;
		
		
		firstRow.children().each(function (i) {
			var th = $(this);
			var tds = rows;
			var filterHead = $('<td headers="' + (this.id || (this.id = 'mdm-head-' + numTable + '-' + i)) + '" class="' + this.className + '"/>');
			var filterCell;
			var filterList;
			var text;
			var liFirst = $('<li style="display:none"/>');
			filterRow.append(filterHead);
			if (th.hasClass('fn-filter')) {
				tds = rows.find('td:eq('+ i + ')')
				filterList = $('<ul/>').append(liFirst);
				filterCell = $('<div class="tooltip-navigation"/>').hover(function (e) {
					filterList.show();
				}, function (e) {
					filterList.hide();
				});
				filterHead.append(filterCell);
				var column = contents[i] = {};
				var numFilterValues = 0;
				var headLink = $('<a href="#">' + wacker.textTableFilterShowAll + '</a>').click(function (e) {
					rows.show();
					currentFilterLink && currentFilterLink.returnToInit();
					currentListItem && $(currentListItem).show();
					return false;
				});
				var currentFilter = $('<a href="#" style="display:none" onclick="return false;"/>');
				currentFilter.returnToInit = function () {
					this.before(headLink).hide();
					filterList.hide();
					liFirst.hide();
					$(currentListItem).show();
				};
				headLinks.push(headLink);
				currentFilterLinks.push(currentFilter);
				tds.each(function (k) {
					var text = $(this).text();
					if (text) {
						if (!column.hasOwnProperty(text)) {
							column[text] = [this.parentNode];
							++numFilterValues;
						} else {
							column[text].push(this.parentNode);
						}
					}
				});
				if (numFilterValues > 1) {
					filterCell.append($('<ul class="filter"/>').append($('<li/>').append(headLink).append(currentFilter).append(filterList)));
					function step(text) {
						filterList.append($('<li/>').append($('<a href="#">' + text + '</a>').click(function (e) {
							rows.hide();
							$(column[text]).show();
							currentFilterLink && currentFilterLink.returnToInit();
							currentFilter.html(text).show();
							liFirst.prepend(headLink).show();
							filterList.hide();
							currentFilterLink = currentFilter;
							currentListItem = this.parentNode;
							$(this.parentNode).hide();
							return false;
						})));
					}
					for (text in column) {
						step(text);
					}
				}
			}
		});
		
		toggleFilter = $('<a class="toggleFilter" href="#">' + wacker.textTableFilterInit + '</a>').click(function (e) {
			if (shown = !shown) {
				filterRow.show();
				if (!initialized) {
					initialized = true;
					firstRow.children().each(function (k) {
						//$(this).css({width:this.offsetWidth});						
					});
				}
				$(this).html(wacker.textTableFilterRemove);
			} else {
				filterRow.hide();
				rows.show();
				currentFilterLink && currentFilterLink.returnToInit();
				$(this).html(wacker.textTableFilterInit)
			}
			return false;
		});
		firstRow.after(filterRow);
		//table.before(toggleFilter);
		toggleFilter.click();
		
		$('d*').click(function (e) {
			alert(this.className + ' ' + this.id + ' ' + this.nodeName)
		})
	});

});

