$(document).ready(function() {
	// handy shortcut function - colorizes table column
	function col_on(element, x) {
		var y, trs, td;

		while (element.tagName.toLowerCase() != 'table' && element.parentNode) {
			element = element.parentNode;
		}

		trs = element.getElementsByTagName('tr');
		for (y = 0; y < trs.length; ++y) {
			if ((td = trs[y].getElementsByTagName('td')[x]) || (td = trs[y].getElementsByTagName('th')[x])) {
				td.className = td.className && td.className.length ? td.className + ' active' : 'active';
			}
		}
	}

	// handy shortcut function - decolorizes table column
	function col_off(element, x) {
		var y, trs, td;

		while (element.tagName.toLowerCase() != 'table' && element.parentNode) {
			element = element.parentNode;
		}

		trs = element.getElementsByTagName('tr');
		for (y = 0; y < trs.length; ++y) {
			if ((td = trs[y].getElementsByTagName('td')[x]) || (td = trs[y].getElementsByTagName('th')[x])) {
				td.className = td.className.replace(/\s*active\s*/, '');
			}
		}
	}

	// colorize everithyng on the row/column
	$('table.program td').hover(function() {
		var x = 0,
			parent = $(this).parent('tr').addClass('active'),
			children = parent.children('td'),
			child = children[x];

		for (; child; child = children[++x]) {
			if (child == this) {
				break;
			}
		}

		if (!this.className || (this.className.indexOf('first') == -1) && this.className.indexOf('common') < 0) {
			col_on(this, x);
		}
	}, function() {
		var x = 0,
			parent = $(this).parent('tr').removeClass('active'),
			children = parent.children('td'),
			child = children[x];

		for (; child; child = children[++x]) {
			if (child == this) {
				break;
			}
		}

		col_off(this, x);
	});

	// special hack for table heads
	$('table.program th.column').hover(function() {
		var x = 0,
			parent = $(this).parent('tr'),
			children = parent.children('th'),
			child = children[x];

		for (; child; child = children[++x]) {
			if (child == this) {
				break;
			}
		}

		col_on(this, x);
	}, function() {
		var x = 0,
			parent = $(this).parent('tr'),
			children = parent.children('th'),
			child = children[x];

		for (; child; child = children[++x]) {
			if (child == this) {
				break;
			}
		}

		col_off(this, x);
	});

	// fix some other cool stuff
	$('table.program td a').hover(function() {
		if (!$(this).hasClass('last')) {
			$(this).parent('td').addClass('not_last');
		}
	}, function() {
		$(this).parent('td').removeClass('not_last');
	});

	// fix some even more cool stuff
	$('table.program td a.last').each(function() {
		var height = this.parentNode.offsetHeight - 30;	// i know for sure the padding is 30px.. don't ask!
		for (var x = 0; x < this.parentNode.childNodes.length; ++x) {
			var node = this.parentNode.childNodes[x];
			if (node.nodeType == 1 && (!node.className || node.className.indexOf('last') == -1)) {
				height -= node.offsetHeight
			}
		}
		this.style.height = height + 'px';
	});
});

