function attach_quantity_controls(){
	$('div.nutritiondata:not(.javascript-activated)').each(function(i) {
	    var data = {},
			nutritiondata = $(this),
			nutrition_result = nutritiondata.parent(), 
			orig_ratio = $('span.serving_grams', nutritiondata).text(), 
			small_height, 
			big_height;
	
		if (!nutritiondata.length) return;

	    $('<div class="see-all">See all nutrition information</div>').insertAfter(nutritiondata).toggle(function(){
	        // change 'see all' to 'hide extra'
	        $(this).text('Hide extra nutrition information');
	        
	        // fix the height and set overflow
	        small_height = nutritiondata.height();
	        nutritiondata.height(small_height).css('overflow', 'hidden');
	        
	        // find the width of the first <td> in the previous nutrition table
	        var width = $(this).prev().find('td:first').width();
	        
	        // show the extra rows, and fix the width of the <td>s which are all wrong for some reason
	        $('tr.extra-nutrition', nutrition_result).show().find('td').width(width);
	        
	        big_height = nutritiondata[0].scrollHeight;
	        nutritiondata.animate({
	            height: big_height
	        }, 'normal', function(){
	            $(this).css('overflow', 'visible').height('auto');
	        });
	        
	        showing_all = true;
	        
	    }, function(){
	        nutritiondata.animate({
	            height: small_height
	        }, 'normal', function(){
	            $('tr.extra-nutrition').hide();
	            $(this).css('overflow', 'visible').height('auto');
	            
	        });
	        
	        
	        $(this).text('See all nutrition information');
	        
	        showing_all = false;
	    });

		// map the serving-size changing stuff, if applicable
		if ($('form.serving-size', nutrition_result).length) {	

		    $('tr', nutritiondata).each(function(i){
		        var value_element = $('div.nutrientvalue', this), value = value_element.text().split(' ');
		        
		        if (!value_element[0]) 
		            return;
		        
		        value_element[0].row_num = i;
		        
		        data[i] = [parseFloat(value[0]) / orig_ratio, value[1]];
		    });
	
		    function update_serving(){
		        var quantity = $('form.serving-size :text', nutrition_result).val(), serving = $('form.serving-size select', nutrition_result).val(), ratio = quantity * serving;
		        
		        $("#ratio").val(ratio);
		        
		        $('div.nutrientvalue', nutritiondata).each(function(i){
		            var row_num = this.row_num;
		            
		            if (!data[row_num]) 
		                return;
		            
		            var oldval = data[row_num][0], newval = Math.round(oldval * ratio * 1000) / 1000;
		            
		            this.innerHTML = newval + ' ' + data[row_num][1];
		            
		            if ($("#input_" + this.id).length > 0) {
		                $("#input_" + this.id).val(newval);
		            }
		            
		        });
		        
		        
		        $('div.serving_current').html(quantity + ' x ' + $('form.serving-size option:selected', nutrition_result).text());
		
		        $("#input_measure").val($('form.serving-size option:selected', nutrition_result).text());
		    }
		    
		    $('form.serving-size', nutrition_result)
				.find(':text')
					.keyup(update_serving)
				.end()
				.find('select', nutrition_result)
					.change(update_serving)
					.ready(update_serving);
	    
		
		    $(".updateservingbutton2", nutrition_result).click(update_serving);

			update_serving();
		}

	    nutritiondata.addClass('javascript-activated');
	});
}

$(attach_quantity_controls);
