//javascript object to represent a pectin recipe
function Recipe(id, name, jelly, jam, classiclemon, lowlemon, instantlemon, type, typeid, pectin, pectinid, juice_classic_ingredients, juice_low_ingredients, juice_simmer, jam_instructions){
	this.id = id;
	this.name = name;
	this.jelly = jelly;
	this.jam = jam;
	this.classic_lemon = classiclemon;
	this.low_lemon = lowlemon;
	this.instant_lemon = instantlemon;
	this.type = type;
	this.typeid = typeid;
	this.pectin = pectin;
	this.pectinid = pectinid;
	this.juice_classic_ingredients = juice_classic_ingredients;
	this.juice_low_ingredients = juice_low_ingredients;
	this.juice_simmer = juice_simmer;
	this.jam_instructions = jam_instructions;
}

//instantiate a recipe for each fruit
var recipes = [
	new Recipe("apple", "Apple", true, false, false, false, false, "Jam", "jam", "classic", "classic", "1 1/4 lbs (5 medium) apples and 1 1/2 cups water", "1 2/3 lbs (7 medium) apples and 1 1/2 cups water", "10"),
	new Recipe("apricot", "Apricot", false, true, false, false, false, "Jam", "jam", "classic", "classic", "", "", "", "Fresh Apricots - Pitted, Peeled and Finely Chopped"),
	new Recipe("blackberry", "Blackberry", true, true, false, false, false, "Jam", "jam", "classic", "classic", "2 lbs blackberries", "2 2/3 lbs blackberries", "5", "Blackberries-wash; crush 1 layer at a time with potato masher"),
	new Recipe("blueberry", "Blueberry", false, true, true, true, false, "Jam", "jam", "classic", "classic", "", "", "", "Blueberries-wash; crush 1 layer at a time with potato masher"),
  	new Recipe("boysenberry", "Boysenberry", true, true, false, false, false, "Jam", "jam", "classic", "classic", "2 lbs boysenberries", "2 2/3 lbs boysenberries", "5", "Boysenberries-wash; crush 1 layer at a time with potato masher"),
	new Recipe("sweetcherry", "Sweet Cherry", true, true, true, true, false, "Jam", "jam", "classic", "classic", "1 1/2 lbs cherries and 3 Tbsp water", "2 lbs cherries and 1/4 cup water", "10", "Sweet Cherries - Remove pits and stems, finely chop"),
	new Recipe("sourcherry", "Sour Cherry", true, true, false, false, false, "Jam", "jam", "classic", "classic", "1 1/2 lbs cherries and 3 Tbsp water", "2 lbs cherries and 1/4 cup water", "10", "Sour Cherries - Remove pits and stems,  finely chop"),
	new Recipe("crabapple", "Crabapple", true, false, false, false, false, "Jam", "jam", "classic", "classic", "1 1/4 lbs (17 medium) crabapples and 1 cup + 2 Tbsp water", "1 2/3 lbs (23 medium) crabapples and 1 1/2 cups water", "10"),
	new Recipe("currant", "Currant", true, false, false, false, false, "Jam", "jam", "classic", "classic", "1 2/3 lbs currants and 1/4 cup + 1 Tbsp water", "2 1/4 lbs currants and 1/3 cup water", "10"),
	new Recipe("grape", "Grape", true, true, false, false, false, "Jam", "jam", "classic", "classic", "1 1/4 lbs grapes and 1/4 cup + 1 Tbsp water", "1 2/3 lbs grapes and 1/3 cup water", "10", "Concord Grapes - Prepared per below"),
	new Recipe("peach", "Peach", false, true, true, true, true, "Jam", "jam", "classic", "classic", "", "", "", "Peaches - Pitted, Peeled and Finely Chopped"),
	new Recipe("pear", "Pear", false, true, false, false, false, "Jam", "jam", "classic", "classic", "", "", "", "Pears - Pitted, Peeled and Finely Chopped"),
   	new Recipe("plum", "Plum", true, true, false, false, false, "Jam", "jam", "classic", "classic", "1 1/3 lbs (14 medium) plums and 1/4 cup + 1 Tbsp water", "1 3/4 lbs (18 medium) plums and 1/3 cup water", "10", "Plums - Pit, chop, add 1/2 cup water and  simmer for 5 minutes"),
	new Recipe("raspberry", "Raspberry", true, true, false, false, false, "Jam", "jam", "classic", "classic", "2 lbs raspberries", "2 23 lbs raspberries", "5", "Raspberries-wash; crush 1 layer at a time with potato masher"),
	new Recipe("strawberry", "Strawberry", true, true, false, false, false, "Jam", "jam", "classic", "classic", "4 lbs strawberries", "5 lbs strawberries", "5", "Strawberries - Wash, hull, then crush one layer at a time")
];

var selectedRecipe;

$(document).ready(function() {
	//include the recipe html that we broke out to make the html more manageable
	$("#recipe_container").load("recipes.html");
	
	var fruitList = "";
	var fruitHover = "";
	//build the list of fruits and their hover divs
	for(var i=0; i<recipes.length; i++){
		var recipe = recipes[i];
		fruitList += "<li id=\"" + recipe.id + "\" class=\"fruit hover\">" + recipe.name +"</li>";
		fruitHover += "<div id=\""+ recipe.id + "_hover\" style=\"display:none;\"><div class=\"hover_surround shadow\">" + recipe.name + "</div><div class=\"pointer\"></div></div>";
	}
	
	$('#fruits').append(fruitList);
	$('#fruit_hovers').append(fruitHover);
	
	initializeHover();
	intializeFruitClick();
	
	//add a click function to all of the close buttons
	$('a.close').click(function(){
		disableCalc();
		$('li.fruit').addClass("hover").removeClass("disabled");
		$('div.fruit_click').hide();
		$("div#step1").removeClass();
		$("div#step1").addClass("active");
		intializeFruitClick();
		resetStepTwo();
	});
	
	//add a click function to the calculate button	
	$("#calculate").click(function(){
		initRecipeContainer();
		$('#container').hide();
		$('#recipe_container').show();
	});
});

//initialize the click function on the fruit images
function intializeFruitClick(){
	$("li.fruit").click(function(){
		disableCalc();
		$('li.fruit').addClass("disabled").removeClass("hover");
		$('li.fruit').unbind();
		var div = "#" + $(this).attr("id") + "_hover";
		$(div).hide();
		
		$(this).removeClass("disabled");
		
		$("div#step1").removeClass();
		$("div#step1").addClass($(this).attr("id"));
		
		var div = "#" + $(this).attr("id") + "_click";
		$(div).show();
		
		var name = $('li.fruit').index($(this));
		selectedRecipe = recipes[name];
		resetStepThree();
		enableStepTwo(selectedRecipe.jelly, selectedRecipe.jam);
	});
}

//initialize the recipes.  this is all the logic to build the recipes
function initRecipeContainer(){
	$("#change-combo").unbind();
	$("#change-combo").click(function(){
		$('#recipe_container').hide();
		$('#container').show();
	});
	
	$("#methods").show();
	$("#dropper1 .accordion").unbind();
	$("#dropper1 .accordion").click(function () {
		$("#dropper1 p").toggle();
		$("#dropper1 .accordion").toggle();
	});
	
	$("#dropper2 .accordion").unbind();
	$("#dropper2 .accordion").click(function () {
		$("#dropper2 p").toggle();
		$("#dropper2 .accordion").toggle();
	});
	
	$('.recipe_name').html(selectedRecipe.name);
	$('.recipe_type').html(selectedRecipe.type);
	$('.recipe_pectin').html(selectedRecipe.pectin);
	$('#print_image').attr("src", "/images/print-" + selectedRecipe.typeid + "-" + selectedRecipe.pectinid + ".png");
	$('#header_fruit').html(selectedRecipe.name);
	$('#header_type').html(selectedRecipe.type);
	
	$('div.recipe_header').hide();
	$('div.instructions').hide();
	$('div.reps').hide();
	$('ol.prep').hide();
	$('span.yield').hide();
	
	//logic for the jam recipes
	if(selectedRecipe.typeid == "jam"){
		$(".jam_instruct").html(selectedRecipe.jam_instructions);
		if(selectedRecipe.id == "grape"){
			$("#grape_instructions").show();
		}
		//jam + classic pectin
		if(selectedRecipe.pectinid == "classic"){
			if(selectedRecipe.classic_lemon){
				$('.lemons').show();
			}else{
				$('.lemons').hide();
			}
			$('#classic_jam_recipe_header').show();
			$('#classic_jam_instructions').show();
			$('#classic_jam_recipe').show();
		}
		//jam + instant pectin
		else if(selectedRecipe.pectinid == "instant"){
			$("#methods").hide();
			if(selectedRecipe.instant_lemon){
				$('.lemons').show();
			}else{
				$('.lemons').hide();
			}

			$('#instant_jam_recipe').show();
			if(selectedRecipe.id == "peach"){
				$('#peach_jam_recipe_header').show();
				$('#peach_jam_instructions').show();
			}else{
				$('#instant_jam_recipe_header').show();
				$('#instant_jam_instructions').show();
			}
		}
		//jam + low sugar pectin
		else{
			if(selectedRecipe.low_lemon){
				$('.lemons').show();
			}else{
				$('.lemons').hide();
			}
			$('#low_jam_recipe_header').show();
			$('#low_jam_instructions').show();
			$('#low_jam_recipe').show();
		}
	}
	//logic for jelly recipes
	else{
		$('#juice_prep').show();
		var cls = "." + selectedRecipe.id;
		$(cls).show();
		$('.juice_yield').html(selectedRecipe.name);
		$('.recipe_juice').html(selectedRecipe.name);
		$('span.simmer').html(selectedRecipe.juice_simmer);
		
		//jelly + classic pectin
		if(selectedRecipe.pectinid == "classic"){
			if(selectedRecipe.classic_lemon){
				$('.lemons').show();
			}else{
				$('.lemons').hide();
			}
			$('#classic_yield').show();
			$('.juice_ingredients').html(selectedRecipe.juice_classic_ingredients);
			$('#classic_jelly_recipe_header').show();
			$('#classic_jelly_instructions').show();
			$('#classic_jelly_recipe').show();
		}
		//jelly + low sugar pectin
		else{
			if(selectedRecipe.low_lemon){
				$('.lemons').show();
			}else{
				$('.lemons').hide();
			}
			$('#low_yield').show();
			$('.juice_ingredients').html(selectedRecipe.juice_low_ingredients);
			$('#low_jelly_recipe_header').show();
			$('#low_jelly_instructions').show();
			$('#low_jelly_recipe').show();
		}
	}	
}

//initialize the hovers
function initializeHover(){
	$("li.hover").hover(
  		function () {
    		var div = "#" + $(this).attr("id") + "_hover";
			$(div).show();
  		}, 
  		function () {
    		var div = "#" + $(this).attr("id") + "_hover";
			$(div).hide();
  		}
	);
}

//enable step two whenever a fruit is selected
function enableStepTwo(showJelly, showJam){
	$("div#step2").removeClass();
	$("div#step2").addClass("active");
	
	$("li.recipe").removeClass("disabled").addClass("hover").click(function(){
		disableCalc();
		$('li.recipe').addClass("disabled");
		$(this).removeClass("disabled");
		
		$("div#step2").removeClass();
		$("div#step2").addClass($(this).attr("id"));
		
		selectedRecipe.type = $(this).attr("rel");
		selectedRecipe.typeid = $(this).attr("id");
		
		if($(this).attr("id") == "jam")
			enableStepThree(true);
		else
			enableStepThree(false);
	});
	
	initializeHover();
	
	if(!showJelly){
		$("#jelly").addClass("disabled").unbind();
	}
	if(!showJam){
		$("#jam").addClass("disabled").unbind();
	}
}

//enable step three whenever jelly or jam is selected
function enableStepThree(showInstant){
	$("div#step3").removeClass();
	$("div#step3").addClass("active");
	
	$("li.pectin").removeClass("disabled").addClass("hover").click(function(){
		$('li.pectin').addClass("disabled");
		$(this).removeClass("disabled");
		
		$("div#step3").removeClass();
		$("div#step3").addClass($(this).attr("id"));
		
		selectedRecipe.pectin = $(this).attr("rel");
		selectedRecipe.pectinid = $(this).attr("id");
		enableCalc();
	});
	
	initializeHover();
	
	if(!showInstant){
		$("#instant").addClass("disabled").unbind();
	}
}

//reset set two when a fruit is changed
function resetStepTwo(){
	$("div#step2").removeClass();
	$("li.recipe").removeClass("hover").addClass("disabled").unbind();
	resetStepThree();
}

//reset set three whenever a fruit is changed or jam/jelly is changed
function resetStepThree(){
	$("div#step3").removeClass();
	$("li.pectin").removeClass("hover").addClass("disabled").unbind();
	initializeHover();
}


function enableCalc(){
	$("a#calculate").removeClass("removed");
}

function disableCalc(){
	$("a#calculate").addClass("removed");
}




