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

$.tablesorter.addParser({
	id: 'dkamera_date',
	is: function(s) { return false; },
	format: function(s) {
		return - parseInt(s.substring(3,7) + s.substring(0,2));
	},
	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;
		}
		return price;
	},
	type: 'numeric'
});

function display_none_available() {
	var testbericht_table = $('.testbericht-uebersicht-table table');
	var tbody = testbericht_table.find('tbody');
	var trs = tbody.find('tr');
	if (trs.filter(':visible').length)
	{
		tbody.children('tr.none-available').remove();
	}
	else
	{
		tbody.append($('<tr class="none-available"><td colspan="5">Keine Kameras entsprechen den Kriterien</td></tr>'));
	}
}

function apply_filter() {
	var testbericht_table = $('.testbericht-uebersicht-table table');
	var trs = testbericht_table.find('tbody tr');
	apply_type_filter(trs);
	apply_kategorie_filter(trs);
	apply_search_filter(trs);
	display_none_available();
}

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

function apply_kategorie_filter(trs) {
	if (_type != 'dslr') return;
	var visible_trs = trs.filter(':visible');
	if (_kategorien.length == 0) return;
	visible_trs.hide();
	$.each(_kategorien, function (i, kat) {
		visible_trs.filter('.' + kat).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('.camera');
		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 _kategorien = [];
function select_kategorie() {
	_kategorien = [];
	$('.kategorie-select').find('input:checked').each(function (i, el) {
		el = $(el);
		_kategorien.push(el.val());
	});
	apply_filter();
}

var _search = '';
var last_search_timeout;
function select_search() {
	_search = $('#testbericht-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 = $('.testbericht-uebersicht-table table');
	testbericht_table.find('td.camera a').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 = $('.testbericht-uebersicht-table table');
	testbericht_table.tablesorter({
		/* debug: true, */
		headers: {
			0: {sorter: false},
			1: {sorter: 'text'},
			2: {sorter: 'dkamera_score'},
			3: {sorter: 'dkamera_date'},
			4: {sorter: 'dkamera_price'}
		},
		textExtraction: function (e) { return $.trim($(e).text()); }
	});
	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('.kategorie-select input').bind('click keypress change', select_kategorie)
	tab_box.find('#testbericht-search').bind('change keyup', select_search)
	init_toolstips();
	select_kategorie();
	apply_filter();
	$(document).history(history_handler);
	if (!$.history.getCurrent())
		$.history.add('tab-alle');
	history_handler();
});

