//
//	Given an amount that is a decimal value, converts this to a displayed amount
//
function computeDisplayedAmount(amount) {
	//	Break the amount into the integer and decimal components
	var intAmount = Math.floor(amount)
	var decAmount = amount - intAmount;
	
	//
	//	Now we need to convert this into a user-displayed string. If either
	//	amount is 0, we don't show it. For the decimal amount, we need to 
	//	convert the value we have to the nearest fraction that we support
	//	(feel free to update this part of the code for the decimal values
	//	you want to supports). As things are, this supports fractions of 
	//	1/8. If it's within 1/16 of the target fraction, it gets assigned
	//	that value. That means values < 1/16 will become 0 and values greater
	//	than 15/16 will be come 1, thus incrementing the integer amount.
	//
	//	I got the unicode characters from this site:
	//	http://unicode-search.net/unicode-namesearch.pl?term=fraction
	//	in case you want to change the increment, you can find more fractional
	//	values there.
	//
	//	Hopefully that all makes sense
	//
	
	//	Initialize the string result
	var result = '';
	
	//	Check to see if the decimal amount is so large that it will result in
	//	needing to increment the integer amount
	if ( decAmount >= 0.9375 ) {
		//	Increment the integer value
		intAmount += 1;
		
		//	Reset the decimal value to 0
		decAmount = 0;
	}
	
	//	If we have an integer amount, update the result to contain this
	if ( intAmount > 0 ) {
		result += intAmount;	
	}
	
	//	If there is a decimal amount, represent it with a unicode character
	if ( decAmount >= 0.0625 ) {
		//	If the result includes an integer amount, add a space between it
		//	and the fractional value yet to come
		if ( intAmount > 0 ) {
			result += '&nbsp;';
		}
		
		//	Determine the correct character based on the decimal amount and
		//	add it to the result
		if ( decAmount >= 0.0625 && decAmount < 0.1875 ) {
			//	1/8
			result += '&#x215b;';
		} else if ( decAmount >= 0.1875 && decAmount < 0.3125 ) {
			//	1/4
			result += '&#x00bc;';
		} else if ( decAmount >= 0.3125 && decAmount < 0.4375 ) {
			//	3/8
			result += '&#x215c;';
		} else if ( decAmount >= 0.4375 && decAmount < 0.5625 ) {
			//	1/2
			result += '&#x00bd;';
		} else if ( decAmount >= 0.5625 && decAmount < 0.6875 ) {
			//	5/8
			result += '&#x215d;';
		} else if ( decAmount >= 0.6875 && decAmount < 0.8125 ) {
			//	3/4
			result += '&#x00be;';
		} else if ( decAmount >= 0.8125 && decAmount < 0.9375 ) {
			//	7/8
			result += '&#x215e;';
		}
		
	}
	
	//	Return what, if anything, is in the result now
	return result
}

//
//	Call this with a known scale factor (or, if the scale factor exists
//	elsewhere on the page, you can update this function to get the value
//	from within the page) to scale the amounts in a recipe.
//
function scaleRecipe(scaleFactor) {
	//	Declare some variables that may be used
	var qty;
	var scaledQty;
	var displayEl;
	
	//	This selector will get all of the elements that have the 'ingr-qty-dec' 
	//	class and it will then loop through these elements, processing each
	//	amount as it loops
	$('span.ingr-qty-dec').each( function() {
		//	Get the existing amount and scale it
		qty = $(this).html();
		
		// 	If the amount is non-zero, we'll need to update the display
		//	of the ingredients as well as the decimal amount (so that 
		//	if we call this again later, things still scale based on 
		//	what it shown on the page
		if ( qty != '' && qty > 0 ) {
			//	Scale the quantity
			scaledQty = qty * scaleFactor;
			
			//	Get a handle to the corresponding display element. If we can't
			//	find this, we are stuffed so we won't process this ingredient
			//	any further
			displayEl = $(this).next('span.ingr-qty');
			if ( displayEl ) {
				//	Update the decimal amount
				// $(this).html(scaledQty);
				
				//	Update the displayed amount
				$(displayEl).html(computeDisplayedAmount(scaledQty));
				
				//	Note that this does NOT update the units of the ingredient
				//	to take care of pluralization. I see that there is a span
				//	with the units in there. If you could hide the plural and
				//	singulare units in the page, you could write a simple 
				//	function to determine which value should be shown based 
				//	on the current decimal value.
			}
		}
	});
}


