//jQuery won't conflict with other libraries that might use $
jQuery.noConflict();

jQuery(function($) {
	//jQuery Document Ready
	console.log('oh heck. you are doing me an inspect');

	// http://isotope.metafizzy.co/
	// recipe isotope init
	var $isotoped = $('#recipe_tiles').imagesLoaded(function() {

		$isotoped.isotope({
			itemSelector: '.recipe_tile',
			containerStyle: null,
			layoutMode: 'masonry'
		});
	});
	// isotope filtering
	function updateRecipeFilter() {
		// prefixes are
		// prod-
		// prodcat-
		// type-
		// this is for use with hash so we can tell what's what, to update the filter dropdowns/tiles
		// if the user comes to the page already with a hash

		var productFilter = $('#recipe_product option:selected').attr('value');
		// console.log('product filter: ' + productFilter);

		if ($('#recipe_product_cat').length) {
		var catFilter = $('#recipe_product_cat option:selected').attr('value');

		} else {
			var catFilter = '';
		}

		var tagClasses = '';

		$('.type-tile:not(.off)').each(function() {
			tagClasses += catFilter + productFilter + $(this).data('tagslug') + ', ';
		});

		if (tagClasses == '') {
			tagClasses = catFilter + productFilter;
		}
		else {
			// clip off the trailing ", "
			var tagClasses = tagClasses.substring(0, tagClasses.length - 2);
		}


		$isotoped.isotope({filter: tagClasses});
		console.log(tagClasses);


		// show results count (if enabled)
		if ($('#results_count').length) {
			var itemCount = $('.recipe-tile' + tagClasses).length;
			if (itemCount == 1) { var itemWord = 'recipe'; }
			else { var itemWord = 'recipes'; }
			$('#results_count').html('Showing ' + itemCount + ' ' + itemWord);
		}

		// update the hash (if enabled)
		if ($('#recipes_filters').data('usefilterhash') === 'please') {
			// var hashit = tagClasses.replace(/\./g, '+').replace(/ /g, '').replace('+','');
			var hashit = tagClasses
				.replace(/\./g, '+')
				.replace(/ /g, '')
				.replace(/,/g, '')
				.replace('+',''); // dumps the first + that shows up (at the beginning)
			// I'm using this instead of escape() so the url doesn't look completely unreadable
			window.location.hash = hashit;
		}


	}

	// if there's already a hash... use it to pre-set the filters
	if (window.location.hash.length > 0) {
		// if we are actually USING the hash, pre-set the filters
		if ($('#recipes_filters').data('usefilterhash') === 'please') {
			var hashthing = window.location.hash.replace('#', '');

			// if there's more than one, break it apart
			if (hashthing.indexOf('+') !== -1) {
				var selections = hashthing.split("+");
			} else {
				// if there's just one, put it into an array by itself
				var selections = Array(hashthing);
			}
			console.log(selections);

			selections.forEach(function(onething) {
				if (/^prod-/.test(onething)) {
					$('#recipe_product').val('.' + onething);
				} else if (/^prodcat-/.test(onething)) {
					$('#recipe_product_cat').val('.' + onething);
				} else if (/^type-/.test(onething)) {
					$(".type-tile[data-tagslug='." + onething + "']").removeClass("off");
				}
			});
		} else {
			// if there is a hash but we're not using it, clean it off
			if ("pushState" in history) {
				// cleans it off including the hash mark
				var scrollV, scrollH, loc = window.location;
				history.pushState("", document.title, loc.pathname + loc.search);
			} else {
				// if browser isn't capable of this (IE 9 and below *shakes fist*)
				// just do this, which leaves the hash but doesn't cause the page to reload
				window.location.hash = '';
			}



		}
	}
	// in case they later want to be able to link in with a category already selected
	// this needs to be run when the page loads
	updateRecipeFilter();

	//region on change of any filters
	$('.filter-dropdown').on('change', function() {
		// console.log('you picked a thing!');
		updateRecipeFilter();
	});
	$('.type-tile').on('click', function() {
		$(this).toggleClass('off');
		updateRecipeFilter();
	});

	$('#view_all').on('click', function() {
		$('.type-tile').addClass('off');
		$('#recipe_product, #recipe_product_cat').val('');

		updateRecipeFilter();
	});
	//endregion

	// on single recipe view, make any Products Used tiles the same height
	if ($('#recipe_single').length) {
		$('#apicius .rec-product').imagesLoaded(function() {
			var tallest_gray = 0;
			$('#apicius .rec-product').each(function() {
				if ($(this).height() > tallest_gray) {
					tallest_gray = $(this).height();
				}
			});
			$('#apicius .rec-product').css('height', tallest_gray + 'px');
			$('#apicius .rec-product .rec-product-name').addClass('final-pos');
		});
	}


});

