// FUNCTION: productDisplayInit - Controls productDisplay
var productDisplayInit = function () {
	// check that productDisplay exists
	if(!$('.productDisplay').length) {
		return;
	}
	// Get all productDisplay items that exist
	$('.productDisplay').each(function() {
		// Set ingredientTable display to none
		$(this).find('.ingredientTable').css('display','none');
		// Add margin-top to first item
		$(this).css('margin-top','58px');
	});
	// Hide border of last item
	$('#ingredients').find('div.ingredientBorder:last').hide();
	// Check URL for query to display selected Product Ingredient Table
	var urlParam = [];
	urlParam.product = null;
	// FUNCTION: getQueryString - Check url for query
	var getQueryString = function () {
		var query = window.location.search.substring(1);
		var params = query.split('&');
		for (var i = 0; i < params.length; i += 1) {
			var pos = params[i].indexOf('=');
			if (pos > 0) {
				var key = params[i].substring(0, pos);
				var val = params[i].substring(pos + 1);
				urlParam[key] = val;
			}
		}
	};
	getQueryString();
	// If "product=SOMENAME" exists, jump to ingredient table and display
	if (urlParam.product) {
		$paramJump = '#' + urlParam.product
		// Jump to IngredientTable
		window.location.hash = $paramJump;
		// Display IngredientTable
		$($paramJump).find('.ingredientTable').show();
	} else {
		// Show first Ingredient Table
		$('#ingredients').find('.ingredientTable:first').show();
	}
	// Event listener for click on View and Close buttons
	$('.viewIngredientsButton').click(function() {
		var $table = $(this).parent().parent().find('.ingredientTable');
		if ($table.css('display') !== 'block') {
			$table.slideDown('normal');
		} else {
			$table.slideUp('normal');
		}
		// Nofollow anchors
		this.blur();
		return false;
	});
};
$(document).ready(function() {
	productDisplayInit();
});
