// version 1.0
// http://macbruker.com/2009/07/min-forste-jquery-plugin/
// created by: Martin Berglund

(function($){  
	$.fn.tableTweaks = function(options) {


		var defaults = {
			oddClass: 'tt-alt', 
			firstClass: 'tt-first',
			lastClass: 'tt-last',
			highlightHeaders: 'tt-trace',
			overRow: 'tt-over-row',
			overCol: 'tt-over-col',
			overCell: 'tt-over-cell'
		};

		settings = $.extend({}, defaults, options);  

		return this.each(function () {
	
			$(this).each(function () {
	
				var table = $(this);

				/* Adds a class to every odd tr */
				$('tr:odd', table).addClass(settings.oddClass);
	
				/* Adds a class to first tr/th/td */
				$('tr:first, th:first-child, td:first-child').addClass(settings.firstClass);
	
				/* Adds a class to last tr/th/td */
				$('tr:last, th:last-child, td:last-child').addClass(settings.lastClass);
	
				/* Adds a class to highlight th in own tr and matching th in thead tr */
				$('tbody tr', table).each(function() {
					$(this).children('td').each(function(index) {
						$(this).hover(
							function() {
								$('tbody tr', table).find('td:eq(' + (index) + ')').addClass(settings.overCol);
								if ($(this).parent().children(':first-child').is('th')) {
									$('thead tr', table).children('th:eq(' + (index + 1) + ')').not(':empty').addClass(settings.highlightHeaders);
								} else {
									$('thead tr', table).children('th:eq(' + (index) + ')').not(':empty').addClass(settings.highlightHeaders);
								}

								$(this).prevAll('th').not(':empty, thead th').addClass(settings.highlightHeaders);
								$(this).addClass(settings.overCell).parent('tr').addClass(settings.overRow);
							}, function() {
								$('tbody tr', table).find('td:eq(' + (index) + ')').removeClass(settings.overCol);

								if ($(this).parent().children(':first-child').is('th')) {
									$('thead tr', table).children('th:eq(' + (index + 1) + ')').not(':empty').removeClass(settings.highlightHeaders);
								} else {
									$('thead tr', table).children('th:eq(' + (index) + ')').not(':empty').removeClass(settings.highlightHeaders);
								}

								$(this).prevAll('th').not(':empty, thead th').removeClass(settings.highlightHeaders);
								$(this).removeClass(settings.overCell).parent('tr').removeClass(settings.overRow);
							}
						);
					});
				});
	
			});
	
		});
	};
})(jQuery);