var towncol = 1;

function index_iefix(tBodies) {
	if (!iefix) return;
	for (var i = 0; i < tBodies.length; i++)
		if (tBodies[i].rows.length > 0)
			tBodies[i].rows[0].className += ' iefix';
}

function table_getsortkey(td, col) {
	var result = 0;
	switch (col) {
	case 1:
		result = td.parentNode.id;
		break;
	case 3:
		result = td.lastChild.firstChild.nodeValue.toLowerCase();
		break;
	case 4:
		do {
			td = td.firstChild;
		} while (td.nodeType != 3 && td.firstChild);
		result = ugly_date(td.nodeValue.substring(0, 20));
		break;
	case 5:
		result = td.className.match(/\bvalue([0-9]+)\b/);
		if (result)
			result = parseInt(result[1], 10);
		else
			result = 0;
		break;
	case 6:
		result = parseInt(td.firstChild.nodeValue, 10);
		break;
	case 7:
		td = td.firstChild.nodeValue.split('~');
		result = parseInt(td[0], 10) * 100 + parseInt(td[1], 10);
		break;
	}
	return result;
}

function index_table_sort() {
	var ary, e, i, j, lasttown;
	var td = this.parentNode;
	var col = td.cellIndex;
	var table = td.parentNode.parentNode.parentNode;
	var sortasc = (table.tHead.sortasc[col] = !table.tHead.sortasc[col]);

	if (col == towncol && sortasc == true) {
		eraseCookie('index_sortby');
		eraseCookie('index_sortdir');
	} else {
		createCookie('index_sortby', col, 7);
		createCookie('index_sortdir', sortasc ? 'asc' : 'desc', 7);
	}
	if (table.sortby == col) {
		// reverse rows in each tbody
		for (i = 0; i < table.tBodies.length; i++) {
			ary = new Array();
			for (j = 0; j < table.tBodies[i].rows.length; j++)
				ary[j] = table.tBodies[i].rows[j];
			for (j = ary.length - 1; j >= 0; j--)
				table.tBodies[i].appendChild(ary[j]);
		}

		// reverse tbodies if grouped by town
		if (table.sortby == towncol) {
			ary = new Array();
			for (i = 0; i < table.tBodies.length; i++)
				ary[i] = table.tBodies[i];
			for (i = ary.length - 1; i >= 0; i--)
				table.appendChild(ary[i]);
		}

		table_alternate(table);
		index_iefix(table.tBodies);
		this.src = this.src.replace(/sort_[^.]+/, 'sort_' + (sortasc ? 'up' : 'down'));
		return;
	}

	ary = new Array();
	for (i = 0; i < table.tBodies.length; i++) {
		for (j = 0; j < table.tBodies[i].rows.length; j++) {
			ary[ary.length] = [table_getsortkey(table.tBodies[i].rows[j].cells[col], col), table.tBodies[i].rows[j]];
			ary.sort(function(a, b) {
				if (a[0] == b[0]) return 0;
				if (a[0] < b[0]) return -1;
				return 1;
			});
			if (!sortasc) ary.reverse();
		}
	}

	if (col == towncol) {
		j = -1;
		lasttown = null;
		for (i = 0; i < ary.length; i++) {
			e = ary[i][0].substring(0, 3);//ary[i][0].lastIndexOf(' '));
			if (e != lasttown) {
				j++;
				lasttown = e;
			}
			table.tBodies[j].appendChild(ary[i][1]);
		}
		// ugly fix for firefox not repainting borders
		for (i = 0; i < table.tBodies.length; i++)
			table.appendChild(table.tBodies[0]);
	} else
		for (i = 0; i < ary.length; i++)
			table.tBodies[0].appendChild(ary[i][1]);

	if (table.sortby != null)
		table.tHead.rows[0].cells[table.sortby].lastChild.src =
			table.tHead.rows[0].cells[table.sortby].lastChild.src.replace(/sort_[^.]+/, 'sort_none');
	table.sortby = col;
	this.src = this.src.replace(/sort_[^.]+/, 'sort_' + (sortasc ? 'up' : 'down'));
	table_alternate(table);
	index_iefix(table.tBodies);
}

function exit() {
	var blah, i, e;
	e = document.getElementById('overview').tHead.rows[0].cells;
	for (i = 0; i < e.length; i++) {
		blah = e[i].getElementsByTagName('img')[0];
		if (blah) blah.onclick = null;
	}
}

function init() {
	var blah, e, i, table;

	// make table sortable
	table = document.getElementById('overview');
	table.sortby = null;
	if (!table.tHead) table.tHead = table.getElementsByTagName('thead')[0];
	e = table.tHead.rows[0].cells;
	table.tHead.sortasc = new Array();
	for (i = 0; i < e.length; i++) {
		blah = e[i].getElementsByTagName('img')[0];
		if (blah) {
			table.tHead.sortasc[i] = false;
			blah.src = blah.src.replace(/sort_[^.]+/, 'sort_none');
			blah.onclick = index_table_sort;
			blah.style.cursor = 'pointer';
		}
	}
	e = readCookie('index_sortdir');
	blah = ((e == 'asc') ? false : true);
	e = readCookie('index_sortby');
	if (e) {
		e = parseInt(e, 10);
		if (typeof table.tHead.sortasc[e] == 'boolean') {
			table.tHead.sortasc[e] = blah;
			table.tHead.rows[0].cells[e].getElementsByTagName('img')[0].onclick();
		}
	}
	index_iefix(table.tBodies);
}

