var sorted = [];
function get_sort_order(e){
	for (i in sorted){
		if (sorted[i].e == e) {
			sorted[i].order = -sorted[i].order;
			return sorted[i].order; 
		}
	} 
	var n = {e: e, order: 1};
	sorted.push(n);
	return n.order;
}

$('document').ready(function(){
	$('table.table_sortable th').css("cursor", "hand").click(function(){
		var elements = [];
		//alert($(this).text());
		// ktora to kolumna?
		col = $('table.table_sortable th').index(this);
		//alert(col);
		// zaznaczamy całą kolumnę
		$('table.table_sortable tbody tr').each(function(){
			elements.push({tr: this, value: $($(this).children().get(col)).text()});
		});
		var sort_order = get_sort_order(this);
		// lista elementow jest, teraz sortowanie
		function sort_string(a,b){
			return a.value == b.value ? 0 : (a.value < b.value ? sort_order : -sort_order);
		};
		function sort_number(a,b){
			if (a.value == b.value){
				return 0;
			}
			else if (a.value == "") {
				return -1;
			}
			else if (b.value == "") {
				return 1;
			}
			return (parseFloat(a.value.replace(',', '.'))-parseFloat(b.value.replace(',', '.')))*(-sort_order);
		};
		elements.sort($(this).hasClass('sort_number') ? sort_number : sort_string);
		tbody = $('table.table_sortable tbody');
		$(elements).each(function(v,e){
			tbody.prepend(e.tr);
		});
		return false;
	});
	$('#kohana-profiler').css('top', $('#switch-content').height()+400);
}); 
