$(document).ready(function() {
	// Check for empty TDs
	$(".productGrid td:not(:has('a'))", this).addClass('empty');
	
	// Equalize the heights of the TDs/wraps
	var i = 0;
	var trcount = $('.productGrid tr').size();
	for (i=0; i < trcount; i++) {
		$('.productGrid tr:eq(' + i + ') td .wrap').height(
			Math.max.apply(
				this, $('.productGrid tr:eq(' + i + ') td').map(
					function(i,e){
						return $(e).height() 
					}
				).get()
			) - parseInt($(".productGrid td .wrap").css('padding-bottom'))
		);		
	}
	
	// Learn more left positioning
	$(".productGrid td .learnMore").css({
		"left": parseInt(($(".productGrid td .img").outerWidth() / 2 - $(".productGrid td .learnMore").outerWidth() / 2) + "px")
	});
	
	// Activate product anchor tag when the TD is clicked
	var tds = $('.productGrid td');
	
	tds.each(
		function(index) {
			var td = $(this);
			if (td.hasClass('empty')) return;
			var w = td.children('.wrap');
			var a = w.children('a:eq(0)');
			a.click(function(e){
				e.preventDefault();
			});
			td.click(function(e){
				
				if ($(e.target).is('input') || $(e.target).is('label') || $(e.target).is('span.compare')) return;
				window.location = a.attr('href');
			});
		}
	);
	
	// Apply the hover class to tds
	function hoverClass() {
		var hc = 'hover';
		
		if ($(this).hasClass('empty')) {
			$('.' + hc).removeClass(hc);
			return;
		}
		
		if ($(this).hasClass(hc) === false) {
			$('.' + hc).removeClass(hc);
			$(this).addClass(hc);
		} else {
			$('.' + hc).removeClass(hc);	
		}
	}
	
	$(".productGrid td").hover(hoverClass,hoverClass);
});
