	$(document).ready(function() {									
		$('table table:first').addClass('tablesorter');		
		$('table.tablesorter').prepend('<thead>');
		$("table.tablesorter tbody tr:first").clone().appendTo('table table thead').addClass('headFlag');
		$("table.tablesorter tbody tr:first").remove();
		$('table.tablesorter thead td').each(function(){ $(this).replaceWith('<th>' + $(this).html() + '</th>'); });		
		$('table.tablesorter tbody tr').each(function(){ 
			$(this).children('td:first').addClass('first'); 			
			$(this).prepend('<td><input type="checkbox" class="rowSelect" /></td>');				
		});
		$('table.tablesorter thead tr').each(function(){ 
			$(this).prepend('<th>&nbsp;</th>');										
		});
		var cC = 0;
		var tblHeaders = new Object();
		$('table.tablesorter th').each(function(){
			var colSum = sumOfColumns('table.tablesorter',(cC +1),true);
			if (colSum > 0) { var type = 'digit'; }
			else { var type = 'text'; }
			tblHeaders[cC] = {sorter : type};
			cC++;
			});	
		$('table.tablesorter').tablesorter({
            indexing: true,
            sortInitialOrder: 'desc',
            debug: false,
            widgets: ['zebra'],
            textExtraction: 'complex',
			headers: tblHeaders
        	}); 
		$('table table tbody td[bgcolor="#dddddd"]').addClass('hl');
		$('table.tablesorter thead tr th:first').removeClass('header');
		var buttons = '<div class="chartButtons"><input type="button" class="removeRows" value="Show Only Checked" /><input type="button" class="showRows" value="Show All" /><input type="button" class="uncheckRows" value="Uncheck All" /></div>';
		$(buttons).insertBefore("table.tablesorter");
		$(buttons).insertAfter("table.tablesorter");
		$("input.removeRows").bind("click", function(e){
			var checkCount = $('input.rowSelect:checked').length;
			if (checkCount > 1) {			
				$('table.tablesorter tbody tr').hide();									
				$('input.rowSelect:checked').each(function(){
					$(this).parents('tr').show();
				   });
				}
			else {alert('Please select at least two supplements to show and compare.');}	
			});		
		$("input.showRows").bind("click", function(e){
			$('table.tablesorter tbody tr').show();
			});			
		$("input.uncheckRows").bind("click", function(e){
			$('table.tablesorter tbody tr').show();
			$('input.rowSelect:checked').each(function(){
				$(this).click();
			   });			
			});					
		//$('body').prepend(sumOfColumns('table.tablesorter',7,true));
		});
	function sumOfColumns(tableID, columnIndex, hasHeader) {
		var tot = 0;
		var tests = '';
		$("" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""))
		.children("td:nth-child(" + columnIndex + ")")
		.each(function() {
			var valTest = $(this).text().replace(/[-|,|*|√|\s|mg|√+]+/g,'');
			
			tot += parseInt(eval(valTest*1));
			tests += valTest+" ";
		});
		//return tot+": "+tests;
		return tot;
		}