/*****************************************
* BOF conversions js
******************************************/

/**
 * @param float quantity (imperial)
 * @param string unit (imperial)
 *
 * @return normalized metric and imperial value
 */
function getNormalizedValue(quantity, unit){
	var normalized = new Object();
	normalized.metricValue = null;
	ml=null;

	//get exact metric value 
	switch (unit){
	  case "tsp":
		ml = quantity * 4.92892159;	
		break;
	  case "tbsp":
		ml = quantity * 14.7867648;	
		break;
	  case "cup":
	  case "cups":
		ml = quantity * 236.588237;	
		break;
	  case "oz":
		gr = quantity * 28.3495231;
		//Return rounded value
		normalized.metricValue = Math.round(gr) + " g"; //round to 1 decimal;
		normalized.quantity = get_fractional_form(quantity);
		normalized.unit = unit;
		return normalized;
		break;	
	  case "lb":
		gr = quantity * 453.59237;

		if (gr > 1000)
		{
			//Return kg
			kg = gr / 1000;
			normalized.metricValue = Math.round(kg*10)/10 + " kg"; //round to 1 decimal;
			normalized.quantity = get_fractional_form(quantity);
			normalized.unit = unit;
			return normalized;
		}
		else{
			//Return gr
			normalized.metricValue = Math.round(gr) + " g"; //round to 1 decimal;
			normalized.quantity = get_fractional_form(quantity);
			normalized.unit = unit;
			return normalized;
		}
			
		break;	
	  case "pinch":
		normalized.metricValue = null;
		normalized.quantity = get_fractional_form(quantity);
		if (quantity > 1)
		{
			normalized.unit = "pinches";
		}
		else{
			normalized.unit = "pinch";
		}
		return normalized;
			break;
	}
	
	if (ml != null){
		//define normalized values
		var lookup = new Array();
		lookup.push([0.616115,	0.000000,	0.925000,	  "0.5 mL",	 "1/8",	 "tsp"]);
		lookup.push([1.232230,	0.925000,	1.848346,	  "1 mL",	 "1/4",	 "tsp"]);
		lookup.push([2.464461,	1.848346,	3.080576,	  "2 mL",	 "1/2",	 "tsp"]);
		lookup.push([3.696691,	3.080576,	4.312806,	  "4 mL",	 "3/4",	 "tsp"]);
		lookup.push([4.928922,	4.312806,	5.545037,	  "5 mL",	 "1",	 "tsp"]);
		lookup.push([6.161152,	5.545037,	6.777267,	  "6 mL",	 "1 1/4",	 "tsp"]);
		lookup.push([7.393382,	6.777267,	8.009498,	  "7 mL",	 "1 1/2",	 "tsp"]);
		lookup.push([8.625613,	8.009498,	9.241728,	  "9 mL",	 "1 3/4",	 "tsp"]);
		lookup.push([9.857843,	9.241728,	12.322304,	  "10 mL",	 "2",	 "tsp"]);
		lookup.push([14.786765,	12.322304,	16.635110,	  "15 mL",	 "1",	 "tbsp"]);
		lookup.push([18.483456,	16.635110,	20.331802,	  "18 mL",	 "1 1/4",	 "tbsp"]);
		lookup.push([22.180147,	20.331802,	24.028493,	  "21 mL",	 "1 1/2",	 "tbsp"]);
		lookup.push([25.876838,	24.028493,	27.725184,	  "24 mL",	 "1 3/4",	 "tbsp"]);
		lookup.push([29.573530,	27.725184,	31.421875,	  "30 mL",	 "2",	 "tbsp"]);
		lookup.push([33.270221,	31.421875,	35.118566,	  "28 mL",	 "2 1/4",	 "tbsp"]);
		lookup.push([36.966912,	35.118566,	38.815258,	  "31 mL",	 "2 1/2",	 "tbsp"]);
		lookup.push([40.663603,	38.815258,	42.511949,	  "34 mL",	 "2 3/4",	 "tbsp"]);
		lookup.push([44.360294,	42.511949,	51.753677,	  "45 mL",	 "3",	 "tbsp"]);
		lookup.push([59.147059,	51.753677,	69.004902,	  "60 mL",	 "1/4",	 "cup"]);
		lookup.push([78.862746,	69.004902,	98.578432,	  "75 mL",	 "1/3",	 "cup"]);
		lookup.push([118.294119,	98.578432,	138.009805,	  "125 mL",	 "1/2",	 "cup"]);
		lookup.push([157.725491,	138.009805,	167.583335,	  "150 mL",	 "2/3",	 "cup"]);
		lookup.push([177.441178,	167.583335,	207.014707,	  "175 mL",	 "3/4",	 "cup"]);
		lookup.push([236.588237,	207.014707,	266.161767,	  "250 mL",	 "1",	 "cup"]);
		lookup.push([295.735296,	266.161767,	305.593139,	  "300 mL",	 "1 1/4",	 "cup"]);
		lookup.push([315.450983,	305.593139,	335.166669,	  "325 mL",	 "1 1/3",	 "cup"]);
		lookup.push([354.882356,	335.166669,	374.598042,	  "375 mL",	 "1 1/2",	 "cup"]);
		lookup.push([394.313728,	374.598042,	404.171572,	  "400 mL",	 "1 2/3",	 "cup"]);
		lookup.push([414.029415,	404.171572,	443.602944,	  "425 mL",	 "1 3/4",	 "cup"]);
		lookup.push([473.176474,	443.602944,	502.750004,	  "500 mL",	 "2",	 "cups"]);
		lookup.push([532.323533,	502.750004,	542.181376,	  "550 mL",	 "2 1/4 ",	 "cups"]);
		lookup.push([552.039220,	542.181376,	571.754906,	  "575 mL",	 "2 1/3",	 "cups"]);
		lookup.push([591.470593,	571.754906,	611.186279,	  "625 mL",	 "2 1/2",	 "cups"]);
		lookup.push([630.901965,	611.186279,	640.759809,	  "650 mL",	 "2 2/3",	 "cups"]);
		lookup.push([650.617652,	640.759809,	680.191181,	  "675 mL",	 "2 3/4",	 "cups"]);
		lookup.push([709.764711,	680.191181,	739.338241,	  "750 mL",	 "3",	 "cups"]);
		lookup.push([768.911770,	739.338241,	778.769613,	  "800 mL",	 "3 1/4",	 "cups"]);
		lookup.push([788.627457,	778.769613,	808.343143,	  "825 mL",	 "3 1/3",	 "cups"]);
		lookup.push([828.058830,	808.343143,	847.774516,	  "875 mL",	 "3 1/2",	 "cups"]);
		lookup.push([867.490202,	847.774516,	877.348046,	  "900 mL",	 "3 2/3",	 "cups"]);
		lookup.push([887.205889,	877.348046,	916.779418,	  "925 mL",	 "3 3/4",	 "cups"]);
		lookup.push([946.352948,	916.779418,	975.926478,	  "1 L",	 "4",	 "cups"]);
		lookup.push([1005.500007,	975.926478,	1015.357851,	  "1.05 L",	 "4 1/4",	 "cups"]);
		lookup.push([1025.215694,	1015.357851,	1044.931380,	  "1.075 L",	 "4 1/3",	 "cups"]);
		lookup.push([1064.647067,	1044.931380,	1084.362753,	  "1.125 L",	 "4 1/2",	 "cups"]);
		lookup.push([1104.078439,	1084.362753,	1113.936283,	  "1.15 L",	 "4 2/3",	 "cups"]);
		lookup.push([1123.794126,	1113.936283,	1153.367655,	  "1.175 L",	 "4 3/4",	 "cups"]);
		lookup.push([1182.941185,	1153.367655,	1212.514715,	  "1.25 L",	 "5",	 "cups"]);
		lookup.push([1242.088244,	1212.514715,	1251.946088,	  "1.3 L",	 "5 1/4",	 "cups"]);
		lookup.push([1261.803931,	1251.946088,	1281.519617,	  "1.325 L",	 "5 1/3",	 "cups"]);
		lookup.push([1301.235304,	1281.519617,	1320.950990,	  "1.375 L",	 "5 1/2",	 "cups"]);
		lookup.push([1340.666676,	1320.950990,	1350.524520,	  "1.4 L",	 "5 2/3",	 "cups"]);
		lookup.push([1360.382363,	1350.524520,	1389.955892,	  "1.425 L",	 "5 3/4",	 "cups"]);
		lookup.push([1419.529422,	1389.955892,	1449.102952,	  "1.5 L",	 "6",	 "cups"]);
		lookup.push([1478.676481,	1449.102952,	1488.534325,	  "1.55 L",	 "6 1/4",	 "cups"]);
		lookup.push([1498.392168,	1488.534325,	1518.107854,	  "1.575 L",	 "6 1/3",	 "cups"]);
		lookup.push([1537.823541,	1518.107854,	1557.539227,	  "1.625 L",	 "6 1/2",	 "cups"]);
		lookup.push([1577.254913,	1557.539227,	1587.112757,	  "1.65 L",	 "6 2/3",	 "cups"]);
		lookup.push([1596.970600,	1587.112757,	1626.544129,	  "1.675 L",	 "6 3/4",	 "cups"]);
		lookup.push([1656.117659,	1626.544129,	1685.691189,	  "1.75L",	 "7",	 "cups"]);
		lookup.push([1715.264718,	1685.691189,	1725.122562,	  "1.8 L",	 "7 1/4",	 "cups"]);
		lookup.push([1734.980405,	1725.122562,	1754.696091,	  "1.825 L",	 "7 1/3",	 "cups"]);
		lookup.push([1774.411778,	1754.696091,	1794.127464,	  "1.875 L",	 "7 1/2",	 "cups"]);
		lookup.push([1813.843150,	1794.127464,	1892.705890,	  "1.9 L",	 "7 2/3",	 "cups"]);
		lookup.push([1892.705890,	1892.705890,	2000,	  "2 L",	 "8",	 "cups"]);
		//get normalized value
		lookup.each (function  (v){
			if (ml >= v[1] && ml < v[2]) {
				normalized.metricValue = v[3];
				normalized.quantity = v[4];
				normalized.unit = v[5];
				throw $break;	
			}
		});	
		//return normalized value
		if (normalized.metricValue !=  null){
			return normalized;
		}
		//we did not find a normalized value, return rounded value
		ml = ml / 1000 ;//ml to L
		normalized.metricValue = (Math.round(ml*10)/10) + " L"; //round to 1 decimal;
		normalized.quantity = get_fractional_form(quantity);
		normalized.unit = unit;
		return normalized;
	}

	else { //unit is not convertible
		normalized.metricValue = null;
		normalized.quantity = get_fractional_form(quantity);
		normalized.unit = unit;
		return normalized;
	}	
}


//return fractional form (to 1/32th) of the given number
function get_fractional_form(number)
{
	var fraction = '';
	var integerPart = parseInt(number);
	var decimalPart = parseInt(100 * (number - integerPart));
	var lookup = new Array();
	lookup.push([0,  0,  0]); // 00
	lookup.push([0,  0,  0]); // 01
	lookup.push([0,  0,  0]); // 02
	lookup.push([0,  1, 32]); // 03
	lookup.push([0,  1, 32]); // 04
	lookup.push([0,  1, 16]); // 05
	lookup.push([0,  1, 16]); // 06
	lookup.push([0,  1, 16]); // 07
	lookup.push([0,  1, 16]); // 08
	lookup.push([0,  3, 32]); // 09
	lookup.push([0,  3, 32]); // 10
	lookup.push([0,  1,  8]); // 11
	lookup.push([0,  1,  8]); // 12
	lookup.push([0,  1,  8]); // 13
	lookup.push([0,  1,  8]); // 14
	lookup.push([0,  5, 32]); // 15
	lookup.push([0,  5, 32]); // 16
	lookup.push([0,  3, 16]); // 17
	lookup.push([0,  3, 16]); // 18
	lookup.push([0,  3, 16]); // 19
	lookup.push([0,  3, 16]); // 20
	lookup.push([0,  7, 32]); // 21
	lookup.push([0,  7, 32]); // 22
	lookup.push([0,  1,  4]); // 23
	lookup.push([0,  1,  4]); // 24
	lookup.push([0,  1,  4]); // 25
	lookup.push([0,  1,  4]); // 26
	lookup.push([0,  1,  4]); // 27
	lookup.push([0,  9, 32]); // 28
	lookup.push([0,  9, 32]); // 29
	lookup.push([0,  5, 16]); // 30
	lookup.push([0,  5, 16]); // 31
	lookup.push([0,  1,  3]); // 32
	lookup.push([0,  1,  3]); // 33
	lookup.push([0,  1,  3]); // 34
	lookup.push([0,  1,  3]); // 35
	lookup.push([0,  3,  8]); // 36
	lookup.push([0,  3,  8]); // 37
	lookup.push([0,  3,  8]); // 38
	lookup.push([0,  3,  8]); // 39
	lookup.push([0, 13, 32]); // 40
	lookup.push([0, 13, 32]); // 41
	lookup.push([0,  7, 16]); // 42
	lookup.push([0,  7, 16]); // 43
	lookup.push([0,  7, 16]); // 44
	lookup.push([0,  7, 16]); // 45
	lookup.push([0, 15, 32]); // 46
	lookup.push([0, 15, 32]); // 47
	lookup.push([0,  1,  2]); // 48
	lookup.push([0,  1,  2]); // 49
	lookup.push([0,  1,  2]); // 50
	lookup.push([0,  1,  2]); // 51
	lookup.push([0,  1,  2]); // 52
	lookup.push([0, 17, 32]); // 53
	lookup.push([0, 17, 32]); // 54
	lookup.push([0,  9, 16]); // 55
	lookup.push([0,  9, 16]); // 56
	lookup.push([0,  9, 16]); // 57
	lookup.push([0,  9, 16]); // 58
	lookup.push([0, 19, 32]); // 59
	lookup.push([0, 19, 32]); // 60
	lookup.push([0,  5,  8]); // 61
	lookup.push([0,  5,  8]); // 62
	lookup.push([0,  5,  8]); // 63
	lookup.push([0,  5,  8]); // 64
	lookup.push([0,  2,  3]); // 65
	lookup.push([0,  2,  3]); // 66
	lookup.push([0,  2,  3]); // 67
	lookup.push([0,  2,  3]); // 68
	lookup.push([0, 11, 16]); // 69
	lookup.push([0, 11, 16]); // 70
	lookup.push([0, 23, 32]); // 71
	lookup.push([0, 23, 32]); // 72
	lookup.push([0,  3,  4]); // 73
	lookup.push([0,  3,  4]); // 74
	lookup.push([0,  3,  4]); // 75
	lookup.push([0,  3,  4]); // 76
	lookup.push([0,  3,  4]); // 77
	lookup.push([0, 25, 32]); // 78
	lookup.push([0, 25, 32]); // 79
	lookup.push([0, 13, 16]); // 80
	lookup.push([0, 13, 16]); // 81
	lookup.push([0, 13, 16]); // 82
	lookup.push([0, 13, 16]); // 83
	lookup.push([0, 27, 32]); // 84
	lookup.push([0, 27, 32]); // 85
	lookup.push([0,  7,  8]); // 86
	lookup.push([0,  7,  8]); // 87
	lookup.push([0,  7,  8]); // 88
	lookup.push([0,  7,  8]); // 89
	lookup.push([0, 29, 32]); // 90
	lookup.push([0, 29, 32]); // 91
	lookup.push([0, 15, 16]); // 92
	lookup.push([0, 15, 16]); // 93
	lookup.push([0, 15, 16]); // 94
	lookup.push([0, 15, 16]); // 95
	lookup.push([0, 31, 32]); // 96
	lookup.push([0, 31, 32]); // 97
	lookup.push([1,  0,  0]); // 98
	lookup.push([1,  0,  0]); // 99

	var item = lookup[decimalPart];

	item[0] += integerPart;
	
	if (item[1] == 0)
		fraction = item[0];
	else if ((item[0] == 0))
		fraction = item[1]+'/'+item[2]; 
	else 
		fraction = item[0] +'-'+item[1]+'/'+item[2]; 
	return fraction;
}



function fraction_to_float(quantity){
	//parse whole and fraction part from the quantity field  
	var whole_part='0';
	var fraction_part='';
	var float_value='';

	//quantity contains only a whole number
	if (quantity.match(/^[0-9]+$/) != null)
	{
		whole_part = quantity.match(/^[0-9]+$/);
		float_value = parseInt(whole_part);
	}
	//quantity contains only a fraction
	else if(quantity.match(/^[0-9]+\/[0-9]+$/) != null){
		var values = /^([0-9]+\/[0-9]+)$/.exec(quantity);
		fraction_part = values[1];
		var float_value = (fraction_part.split('/')[0] / fraction_part.split('/')[1]);
	}
	//quantity contains a whole number and a fraction
	else if(quantity.match(/^[0-9]+-[0-9]+\/[0-9]+$/) != null){
		var values = /^([0-9]+)-([0-9]+\/[0-9]+)$/.exec(quantity);
		whole_part = values[1];
		fraction_part = values[2];
		float_value = (parseInt(whole_part) + (fraction_part.split('/')[0] / fraction_part.split('/')[1]));
	}
	return float_value;
}

//updates all ingredients line with their metric value
function convert_imperial_to_metric(){
	//get all units
	quantities = jQuery('span.quantity');
	quantities.each(function(q){

		//get float value
		var float_value = fraction_to_float(this.innerHTML);
		if (float_value  != '')
		{
			//get normalized values
			normalized = getNormalizedValue (float_value, jQuery(this).parent().children('.unit').html());
			if (normalized.metricValue != null){
				jQuery(this).parent().children('.metricUnit').html("(" + normalized.metricValue + ") ");	
				jQuery(this).parent().children('.metricUnitOriginal').html("(" + normalized.metricValue + ") ");
			}
		}		
	});
}



function multiply_units(multiplicator){
	//get all units
	quantities = jQuery('span.quantity');
	//multiply each unit
	quantities.each(function(q){
		quantity = this.innerHTML;
		//parse whole and fraction part from the quantity field  
		var whole_part='0';
		var fraction_part='';
		var multiplied;

		//quantity contains only a whole number
		if (quantity.match(/^[0-9]+$/) != null)
		{
			whole_part = quantity.match(/^[0-9]+$/);
			multiplied = parseInt(whole_part) * multiplicator;
		}

		//quantity contains only a fraction
		else if(quantity.match(/^[0-9]+\/[0-9]+$/) != null){
			var values = /^([0-9]+\/[0-9]+)$/.exec(quantity);
			fraction_part = values[1];
			var multiplied = (fraction_part.split('/')[0] / fraction_part.split('/')[1]) * multiplicator;
		}

		//quantity contains a whole number and a fraction
		else if(quantity.match(/^[0-9]+-[0-9]+\/[0-9]+$/) != null){
			var values = /^([0-9]+)-([0-9]+\/[0-9]+)$/.exec(quantity);
			whole_part = values[1];
			fraction_part = values[2];
			multiplied = (parseInt(whole_part) + (fraction_part.split('/')[0] / fraction_part.split('/')[1])) * multiplicator;
		}
		
		//get normalized values
		normalized = getNormalizedValue (multiplied, jQuery(this).parent().children('.unit').html());

		//update normalized quantity
		this.innerHTML=normalized.quantity;
		jQuery(this).parent().children('.unit').html(normalized.unit);	

		//update metric value (if applicable)
		if (jQuery(this).parent().children('.metricUnit').html() != '')
		{
			jQuery(this).parent().children('.metricUnit').html("(" + normalized.metricValue + ") ");	
		}
		
		//show plural or singular ingredient
		if ( (jQuery(this).parent().children('.unit').html() != '' || multiplied > 1) && jQuery(this).parent().children('.ingredientPlural').html() != '')
		{
			jQuery(this).parent().children('.ingredientSingular').hide();
			jQuery(this).parent().children('.ingredientPlural').show();
		}
		else{
			jQuery(this).parent().children('.ingredientSingular').show();
			jQuery(this).parent().children('.ingredientPlural').hide();
		}
	 });
}

/*****************************************
* EOF conversions js
******************************************/

