var maxDirectionStep = 1;
var maxIngredientStep = 1;
function directionsInputUpdate() {
	if(this.value != '') {
		
		var stepNum = this.id.substring(14);
		stepNum++;
		if(stepNum > maxDirectionStep) {
			maxDirectionStep = stepNum;
		}
		if($("#direction"+stepNum).length > 0) {
			
			nextInput = $("#direction"+stepNum).value;
			if(nextInput != '') {
				
			}	
		} else {
			$("#recipeDirections").append('<div class="directionDiv" id="direction'+stepNum+'"><label>Step '+stepNum+': <input type="text" name="direction[]" class="directionInput" id="directionInput'+stepNum+'" size="80" /></label></div>');
			$(".directionInput").keyup(directionsInputUpdate);
			$(".directionInput").blur(clearDirectionsInput);
		}
		
	}
}
function clearDirectionsInput() {
	if(this.value == '' && this) {
		var stepNum = this.id.substring(14);
		if(stepNum != maxDirectionStep) {
			$("#direction"+stepNum).slideUp().remove();
			startingStep = stepNum;
			for(i = startingStep; i<=maxDirectionStep; i++) {
				if($("#direction"+i).attr("id") != "undefined") {
					currentVal = $("#directionInput"+i).val();
					newStepNum = i -1;
					$("#direction"+i).replaceWith('<div class="directionDiv" id="direction'+newStepNum+'"><label>Step '+newStepNum+': <input type="text" name="direction[]" class="directionInput" id="directionInput'+newStepNum+'" size="80" value="'+currentVal+'" /></label></div>');
					
				}
			}
			$(".directionInput").keyup(directionsInputUpdate);
			$(".directionInput").blur(clearDirectionsInput);
		}
	}
	
}
function ingredientsInputUpdate() {
	
	var newInput = false;
	if(this.type=='text') {
		var stepNum = this.id.substring(15);
		if(this.value != '') {
			$("#ingredientProduct"+stepNum).attr("disabled",'disabled');
			newInput = true;
		} else {
			$("#ingredientProduct"+stepNum).removeAttr("disabled");
			deleteIngredient(stepNum);
		}
	} else if(this.type=='select-one'){
		stepNum = this.id.substring(17);
		if(this.value != 'none') {
			$("#ingredientInput"+stepNum).attr("disabled",'disabled');
			newInput = true;
		} else {
			$("#ingredientInput"+stepNum).removeAttr("disabled");
			deleteIngredient(stepNum);
		}
	}
	if(newInput) {
		stepNum++;
		
		if($("#ingredient"+stepNum).length == 0) {
			if(stepNum > maxIngredientStep) {
				maxIngredientStep = stepNum;
			}
//			$("#recipeIngredients").append('<div class="ingredientDiv" id="ingredient'+stepNum+'"> Quantity: <input type="text" name="quantity['+stepNum+']" class="quantityInput" id="quantityInput'+stepNum+'" /> Ingredient: <input type="text" name="ingredient['+stepNum+']" class="ingredientInput" id="ingredientInput'+stepNum+'">	or <select class="ingredientProduct" id="ingredientProduct'+stepNum+'" name="ingredientProduct['+stepNum+']"><option value="none"></option>'+ingredientOptions+'</select><a href="#" onclick="deleteIngredient('+stepNum+'); return false;"><img src="'+((cms == true) ? "../" : "" ) +'img/cms/delete_btn.png" alt="Delete this ingredient" /></a></div>')
			$('#recipeIngredients').append('<div class="ingredientDiv" id="ingredient'+stepNum+'"><span style="float:left;">Quantity: <input type="text" name="quantity['+stepNum+']" class="quantityInput" id="quantityInput'+stepNum+'" /></span><span style="float:left;">Ingredient: <input type="text" name="ingredient['+stepNum+']" class="ingredientInput" id="ingredientInput'+stepNum+'"><br /><span style="visibility:hidden;">Ingredient: </span><select class="ingredientProduct" id="ingredientProduct'+stepNum+'" name="ingredientProduct['+stepNum+']"><option value="none"></option>'+ingredientOptions+'</select></span><span style="float:left;"><a href="#" onclick="deleteIngredient('+stepNum+'); return false;"><img src="'+((cms == true) ? "../" : "" ) +'img/cms/delete_btn.png" alt="Delete this ingredient" /></a></span><br clear="all" /></div>');
			
			$(".ingredientInput").keyup(ingredientsInputUpdate);
			$(".ingredientProduct").change(ingredientsInputUpdate);
		}
	}
}
function deleteIngredient(stepNum) {
	if(stepNum != maxIngredientStep) {
		$("#ingredient"+stepNum).slideUp("normal",function() {
			$("#ingredient"+stepNum).remove();	
		});
	}
}
