jQuery(function($) {
	setEqualHeight($("#footer-wrapper").find('.aside'));
	setEqualHeight($(".home #content #main").find('.module'));

	setEqualHeightSeveralRows($("#content #main ul.grid").find('.module'));

	turnBlockElementsToLinks($('#content').find('.block-link'));
	
	/**
	 * This function takes a set of columns (or modules)
	 * and makes sure they are of equal height
	 */
	
	function setEqualHeight(columns) {
		var maxHeight = 0;
		columns.each(function() {
			maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight;
		});
		columns.height(maxHeight);
	}
	
	function setEqualHeightSeveralRows(columns) {
		var colsToSet = [];
		var i = 0;
		var maxHeight = 0;
		columns.each(function(index, val) {
			colsToSet[i] = val;
			i++;
			maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight;

			if (0 == (index+1)%3) {
				$(colsToSet).height(maxHeight);
				maxHeight = 0;
				i = 0;
				colsToSet = [];
			}
		});
		
		$(colsToSet).height(maxHeight);
	}
	
	/**
	 * This function takes a set of modules and wraps them in
	 * a-tags. This makes it possible to make an entire module
	 * clickable while still conforming to XHTML 1.0 Strict.
	 */
	
	function turnBlockElementsToLinks(modules) {
		modules.each(function() {
			var link = $('<div>').append($(this).find('a:first').clone()).find('a');
			if (0 < link.length) {
				link.html('').addClass('block-link');
		
				$(this).find('a').each(function() {
					$(this).replaceWith($(this).html());
				});
			
				link.append($(this).children().clone()).remove();
			
				$(this).html('').append(link);
			}
		});
	}
});
