$(document).ready(function(){
	
	if ($("#serving_number").val() !== '4') {
		update_amounts($("#serving_number").val());
	}
	
	$("#serving_number").change(function(){
		user_choice = $(this).val();
		update_amounts(user_choice);
		return false;
	});
	
	$(".add_to_list").click(function(){
		var the_url = $(".add_to_list").attr("href");
		$.ajax({
			url: the_url,
			complete: function(response) {
				$("#shopping_list_wrapper").html(response.responseText);
				$("#add_remove_controls").removeClass('add_on');
				$("#add_remove_controls").addClass('remove_on');
			}
		});
		return false;
	});
	
	$(".remove_from_list").click(function(){
		var the_url = $(".remove_from_list").attr("href");
		$.ajax({
			url: the_url,
			complete: function(response) {
				$("#shopping_list_wrapper").html(response.responseText);
				$("#add_remove_controls").addClass('add_on');
				$("#add_remove_controls").removeClass('remove_on');
			}
		});
		return false;
	});
	
	$('#share_img_cf').hover(function() {
		$(this).attr('src', '/img/btns/share_btn_cf_o.gif');
	}, function() {
		$(this).attr('src', '/img/btns/share_btn_cf.gif');
	});
	
	// hide shopping list links
	$('.shoppingList .links').addClass('hide');
	// show links when hovering over item
	$('.shoppingList .block_item').hover(
		function () {
	    	$(this).find('.links').removeClass('hide');
	  	},
	  	function () {
	    	$(this).find('.links').addClass('hide');
	  	}
	);
	
});

function update_amounts(user_choice) {
	multiplier = user_choice / 4;

	$(".ing_amount").each(function(){
		original_amt = $(this).siblings(".ing_amount_original").html();
		var multiplied = original_amt * multiplier;
		var base_number = Math.floor(multiplied);
		var the_fraction = (multiplied - base_number);
		if (base_number == 0) {
			base_number = '';
		}
		if (the_fraction > 0) {
			entity = '';
			if (the_fraction >= .25) {
				entity = '&frac14;';
			}
			if (the_fraction >= .3) {
				entity = ' 1/3';
			}
			if (the_fraction >= .5) {
				entity = '&frac12;';
			}
			if (the_fraction >= .6) {
				entity = ' 2/3';
			}
			if (the_fraction >= .75) {
				entity = '&frac34;';
			}
			if (the_fraction >= .9) {
				entity = '';
				base_number++;
			}
			final_number = base_number+entity;
		} else {
			final_number = multiplied;
		}
		$(this).html(final_number);
	});
	
	if ($("#print_link").length != 0) {
		var original_url = $("#print_link").attr("href");
		var url_no_id = original_url.split('=')[0];
		$("#print_link").attr("href",url_no_id+"="+user_choice);
	}
/*
*/
}

var profiles =
{
	printShoppingList:
	{
		height:600,
		width:840,
		center:1,
		scrollbars: 1
	}
}

$(function(){
  	$(".popupwindow").popupwindow(profiles);
});
