$.tablesorter.addParser({
	id: 'dkamera_score',
	is: function(s) { return false; },
	format: function(s) {
		return - parseFloat(s.replace('%', '').replace(',', '.'));
	},
	type: 'numeric'
});

$.tablesorter.addParser({
	id: 'dkamera_pubdate',
	is: function(s) { return false; },
	format: function(s) {
		var regex = /([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2,4})/g;
		var match;
		var value = 0;
		while (match = regex.exec(s))
		{
			value = parseInt(match[3] + match[2] + match[1]);
		}
		return value;
	},
	type: 'numeric'
});

$.tablesorter.addParser({
	id: 'dkamera_price',
	is: function(s) { return false; },
	format: function(s) {
		var regex = /([0-9]{1,3}(\.[0-9]{3})*(,[0-9]{2})?)/g;
		var match;
		var price = 0;
		while (match = regex.exec(s)) {
			var test_price = match[1];
			if (!test_price) continue;
			test_price = parseFloat(test_price.replace('.', '').replace(',', '.'));
			if (!test_price) continue;
			if (price == 0 || price > test_price)
				price = test_price;
		}
		RegExp.lastIndex = 0;
		return price;
	},
	type: 'numeric'
});

function display_none_available() {
	var testbericht_tables = $('.buecher-uebersicht-table table');
	testbericht_tables.each(function (i, testbericht_table) {
		testbericht_table = $(testbericht_table);
		var table_spezial = testbericht_table.parents('.spezial');
		table_spezial.show();
		var tbody = testbericht_table.find('tbody');
		var trs = tbody.find('tr');
		if (trs.filter(':visible').length)
		{
			tbody.children('tr.none-available').remove();
			if (table_spezial.length) table_spezial.css({height: 'auto'});
		}
		else
		{
			tbody.append($('<tr class="none-available"><td colspan="5">Keine Bücher entsprechen den Kriterien</td></tr>'));
			if (table_spezial.length) table_spezial.css({height: '1px'});
		}
	});
}

function apply_filter() {
	var testbericht_table = $('.buecher-uebersicht-table table');
	var trs = testbericht_table.find('tbody tr');
	apply_type_filter(trs);
	apply_search_filter(trs);
	display_none_available();
	var type = _type;
	if (type == 'alle')
	{
		$('.buecher-uebersicht-table .spezial').show();
	}
	else
	{
		$('.buecher-uebersicht-table .spezial').hide();
	}
}

function apply_type_filter(trs) {
	var type = _type;
	if (type == 'alle')
	{
		trs.show();
	}
	else
	{
		trs.not('.' + type).hide();
		trs.filter('.' + type).show();
	}
}

function apply_search_filter(trs) {
	if (!_search) return;
	var visible_trs = trs.filter(':visible');
	var search = new RegExp(_search.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$0'), 'im');
	visible_trs.each(function (i, tr) {
		tr = $(tr);
		var title = tr.children('.title');
		if (!title.text().match(search)) tr.hide();
	});
}

var _type = 'alle';
function select_type(type) {
	_type = type;
	apply_filter();
}

function select_tab(type) {
	var tabs = $('.tabs li');
	var tab_box = $('.tab');
	tabs.removeClass('ui-tabs-selected');
	tabs.filter('.' + type).addClass('ui-tabs-selected');
	tab_box.find('.tabbing').hide();
	tab_box.find('.tabbing').filter('.' + type).show();
	select_type(type);
}

var _search = '';
var last_search_timeout;
function select_search() {
	_search = $('#buecher-search').val();
	if (last_search_timeout) window.clearTimeout(last_search_timeout);
	last_search_timeout = window.setTimeout(apply_filter, 250);
	//apply_filter();
}

function init_toolstips() {
	var testbericht_table = $('.buecher-uebersicht-table table');
	testbericht_table.find('td.title a[title]').tipsy({gravity: 'w'});
}

function history_handler()
{
	var hash = $.history.getCurrent();
	if (!hash) return;
	if (hash.match(/^tab-([a-z]+)$/g))
	{
		var type = hash.substr(4);;
		select_tab(type);
	}
}

$(document).ready(function() {
	var testbericht_table = $('.buecher-uebersicht-table table');
	testbericht_table.tablesorter({
		/* debug: true, */
		headers: {
			0: {sorter: false},
			1: {sorter: 'text'},
			2: {sorter: 'dkamera_pubdate'},
			3: {sorter: 'dkamera_score'},
			4: {sorter: 'dkamera_price'}
		},
		textExtraction: function (e) {
			return $.trim($(e).text()) + ' ' + $(e).find('img').attr('alt');
		}
	});
	var tabs = $('.tabs li');
	tabs.find('a').bind('click', function (e) {
		e.preventDefault();
		var target = $(e.target);
		var href = target.attr('href');
		var type = href.substr(5);
		$.history.add(href.substr(1));
		select_tab(type);
		if (typeof reload_ads != 'undefined') reload_ads();
		if (pageTracker) pageTracker._trackPageview('ajax:'+window.location.href.replace("#", ":"));
	})
	var tab_box = $('.tab');
	tab_box.find('#buecher-search').bind('change keyup', select_search)
	init_toolstips();
	apply_filter();
	$(document).history(history_handler);
	if (!$.history.getCurrent())
		$.history.add('tab-alle');
	history_handler();
});


