$(document).ready(function(){
	// Fix the google checkout button
	watchForElement('googlecart-checkout-button', function(){
		var row = $("<tr></tr>");
		$('#googlecart-checkout-button').parent().parent().parent().append(row);
		row.append($('#googlecart-checkout-button').parent());
	});
	
	if($.cookie('hide_descriptions') == 'true'){
		$("td.product_description div").hide();
	}
	
	/* Make the show/hide description links work */
	$("#show_hide_descriptions").click(function (){
		if(this.checked){
			$.cookie('hide_descriptions', null, { path: '/', expires: 100 });
			$("td.product_description div").slideDown(500, function(){
				$("td.product_description div:hidden").css({'display':'block'});
			});
		}else{
			$.cookie('hide_descriptions', 'true', { path: '/', expires: 100 });
			$("td.product_description div").slideUp(500, function(){
				$("td.product_description div:hidden").css({'display':'none'});
			});
		}
	});
	$("#show_hide_descriptions").triggerHandler('click');
	
	
	
	/* Add keydown events to all of the text filters */
	$("#filter .filter_between input").bind('keyup',function(e){
		filterParts();
	});
	
	/* Figure out which checkboxes to check by default by parsing the filter hash
	Also figure out which part to higlight if p_### is specified */
	if(window.location.hash.length > 1){
		var filterOnLoad = false;
		$.each(window.location.hash.split(','), function(k,v){
			v = v.replace('#','').split(':');
			if(v.length == 2){
				filterOnLoad = true;
				// $('#'+v[0]+' input:checkbox[value='+v[1].replace('%20',' ')+']').attr('checked', true); // ' in Men's & Women's was causing issues with selector, known JQ bug
				$('#'+v[0]+' input:checkbox').filter(function(){ return $(this).val() === v[1].replace('%20',' '); }).attr('checked',true); // This is a less efficient work around
			}else if(v.length == 1 && v[0].indexOf("p_") == 0){
				$("#"+v[0]).animate({'backgroundColor':'#0095D7'}, 'fast')
					.animate({'backgroundColor':'#FFF'}, 'fast')
					.animate({'backgroundColor':'#0095D7'}, 'fast')
					.animate({'backgroundColor':'#FFF'}, 'fast')
					.animate({'backgroundColor':'#0095D7'}, 'fast');
				// addClass("highlighted");
			}
		});
		
		if(filterOnLoad){
			filterParts();
		}
	}
	
	/* Add click events to all of the filter checkboxes */
	$("#filter .filter_eq").bind('click',function(e){
		var at_hash = $(this).parent().parent().attr('id') + ':' + $(this).val() + ',';
		at_hash = at_hash.replace(' ','%20');
		
		if(window.location.hash.length > 0){
			window.location.hash = window.location.hash.replace(at_hash,'');
		}
		
		if($(this).is(':checked')){
			window.location.hash = window.location.hash + (window.location.hash.charAt(window.location.hash.length-1) != ',' ? ',' : '') + at_hash;
		}
		
		filterParts();

		/* enable the checkbox that was clicked */
		$(this).parent().removeClass('disabled');
	});
	
});