//filtering is based on the productNutrition json object embedded in the page.
//diet setup
jQuery(function() {
	if (jQuery('#dietSearchForm input.dsFilter:checked').length == 0) {
		jQuery('#dietSearchForm .dsBoxInner').hide().parent().addClass('dsBoxClose');
		
	}
	jQuery('#dietSearchForm .dsTitle').click(function() {
		var inner = jQuery(this).siblings('.dsInner,.dsBoxInner');
		if (inner.is(':visible')) {
			inner.slideUp();
			inner.parent().addClass('dsBoxClose');
		} else {
			inner.slideDown();
			inner.parent().removeClass('dsBoxClose');
		}
		return false;
	});
	//switch out the labels for custom A tags
	jQuery('.dsOptions label').hide();
	//setup A tags to trigger the checkbox
	jQuery('.dsOptions li>a').css({ display: 'block' }).click(function() {
		var chkBox = jQuery(this).parent().find('input[type=checkbox]');
		chkBox.attr('checked', !chkBox.attr('checked')).triggerHandler('click');
		return false;
	});
	if (typeof (productNutrition) == undefined) return;
	jQuery('#filterButton').hide();
	jQuery('#dietSearchForm input.dsFilter').click(function() {
		if (jQuery(this).closest('li').is('.dsItemDisable'))
			return false;
		jQuery('.productList > li').stop(true).fadeTo(600, .7);
		//if checked, we are narrowing the filter so only consider visible. If unchecked, we are expanding the filter so only consider hidden
		//evaluate each product that could change
		var isChecked = jQuery(this).attr('checked');
		jQuery('.productList > li.productListing:' + (isChecked ? 'visible' : 'hidden')).each(function() {
			var id = jQuery(this).find('input[name=pid]').val();
			//assume the product will not be hidden
			var hide = false;
			//loop over the filters
			jQuery('#dietSearchForm input.dsFilter').each(function() {
				if (jQuery(this).attr('checked')) {
					if (!eval('productNutrition[id].' + jQuery(this).attr('name')))
						hide = true;
				}
			});
			jQuery(this).toggleClass('filtered', hide);
		});
		//message if no results
		if (jQuery('.productList > li.productListing:visible').length == 0) {
			jQuery('#nodietresults').show();
		}
		else {
			jQuery('#nodietresults').hide();
		}
		//persistance
		headscapeDOM.setCookie('dietFilter', jQuery('#dietSearchForm').serialize(), 30);
		//add clear to every 3rd visible product item (grid of 3 wide)
		var i = 0;
		jQuery('.productList > li.productListing:visible').each(function() {
			jQuery(this).toggleClass('clear', ((i++ % 3) == 0));
			if (jQuery(this).hasClass('featuredListing')) i++;
		});
		//updateDietNumbers();disabled temporarily
		//special styles / classes on checkboxes
		jQuery(this).closest('li').toggleClass('dsItemSelect', isChecked).toggleClass('dsItem', !isChecked);
		jQuery('.productList > li').fadeTo(150, 1);
	});
	jQuery('#dietSearchForm .headerIcons').click(function() {
		var name = jQuery(this).attr('class').replace(/.* /g, '');
		jQuery('#dietSearchForm .dsBoxInner').show();
		jQuery('#dietSearchForm li:not(.dsItemDisable) input.dsFilter[name=' + name + ']').attr('checked', 'checked').triggerHandler('click');
	});
	//updateDietNumbers();disabled temporarily
});
function updateDietNumbers() {
	//add numbers to unchecked filters
	//loop over unchecked filters
	jQuery('#dietSearchForm input.dsFilter').each(function() {
		var count = 0;
		var name = jQuery(this).attr('name');
		var isChecked = jQuery(this).attr('checked');
		//loop over the products
		if (!isChecked) {
			jQuery('.productList > li.productListing:visible').each(function() {
				var id = jQuery(this).find('input[name=pid]').val();
				if (eval('productNutrition[id].' + name))
					count++;
			});
			jQuery(this).closest('li').find('.filterNumber').text(' (' + count + ')');
		}
		else {
			jQuery(this).closest('li').find('.filterNumber').text('');
		}

	});
};
