var decArray =  {
	.000 : "0"  ,
	.063 : "1/16",
	.100 : "1/10" ,
	.125 : "1/8" ,
	.200 : "2/5", 
	.250 : "1/4", 
	.300 : "3/10", 
	.333 : "1/3", 
	.375 : "3/8", 
	.400 : "2/5", 
	.429 : "3/7", 
	.500 : "1/2",
	.571 : "4/7",
	.600 : "3/5",
	.625 : "5/8",
	.666 : "2/3", 
	.700 : "7/10",
	.714 : "5/7",
	.750 : "3/4",
	.800 : "8/10", 
	.833 : "5/6",
	.875 : "7/8",
	.900 : "9/10",
	.938 : "15/16"
};
//This variable is added for the fix of issue DLFDC-197.
var initial = "true";

function changeServingSize(){
	var currentSize = jQuery('#serv').val();
        var ingredientNumbers  = [];
      //  alert ('value is ' + currentSize);
//	var printLink = document.getElementById('printLink');
	if (isNaN(currentSize) || currentSize == 0){
			alert('Please only use numbers greater than 0 for servings.');
			document.getElementById('serv').value = originalSize;
	}else{
		//If condition is added for the fix of issue DLFDC-197.
        if (initial == "false") {		
            document.getElementById("recalculate_text").innerHTML = "The ingredient amounts have been scaled to " +
                            "       the new number of servings. Any ingredient amounts, cooking times, " +
                            "and/or temperatures referenced in the directions still refer to the original recipe yield.";

    }

            var multiple = currentSize / originalSize;
            jQuery('span.ingredientNumber').each(function(i,e) {
                 ingredientNumbers[i] = jQuery(e).html();
            });
            //ingredientNumbers  = jQuery('span.ingredientNumber').val();
       //     alert('size is ' + ingredientNumbers.length);
        //    alert('0 is ' + ingredientNumbers[0].data);

            var x = 0;
	    if(recAmtArray.length < 1){ 
                jQuery.each(ingredientNumbers, function(index, value){
                    recAmtArray[index] = value;
                });
	    }   // if the first run.... populate our array.
            var y = 0;
                jQuery('span.ingredientNumber').each(function(index, value){
                    var val = recAmtArray[index] * multiple;
                    //  val = val * multiple;
                    jQuery(this).html(closestFrac(val));
                    //element.childNodes[0].nodeValue= closestFrac(val);
                    // alert('val is ');// = 0;
                });
            jQuery('span.makes_servings').text(currentSize);
		/** jQuery.each(servingsNames, function(index, value){
              //  var val = element.childNodes[0].nodeValue;
               // val = val * multiple;
                value= currentSize;
               // alert('val is ');// = 0;
            }); **/

            initial = "false";
	}
}

function closestFrac(dec){
    var lastNum;
	var remainder = dec % 1;
	var wholePart = dec - remainder;
	
	for(var i in decArray){
		if(Math.abs(remainder - lastNum) < Math.abs(remainder - i))
			if(wholePart == 0)
				return decArray[lastNum];
			else
				if(decArray[lastNum] == 0)
					return wholePart;
				else
					return wholePart + ' ' + decArray[lastNum]; // the last was closer than this one so use the last as our fraction
		else
			lastNum = i;
	}
	return Math.round(dec); // they keep getting closer and are hence above .938 so round to the next whole number
}
