$(document).ready(function(){

    $("#yield_saved").hide();
    
    //handle the edit recipe tabs
    $(".entrydiv").hide();
    $("#searchdiv").show();
    $(".entrytab").click(function(){
        var tab_for = $(this).attr("id");
        $(this).siblings(".entrydiv").hide();
        $(this).siblings(".entrytab").css('background-color', 'white');
        $(this).css('background-color', '#eeeeff');
        tab_for = "#"+tab_for+"div";
        $(tab_for).show();
    });	
    
    //delete an ingredient when the delete button is clicked
    var deleteingredients = function(){
        $(".ingredientdelete").click(function(){
            var ingredientid = $(this).attr("ingredientid");
            //steal the recipe id from the manual tab
            var recipeid = $("#ing_id").val();
            $.ajax({
   		        type: "POST",
   			    url: "actions/ingredientdelete.php",
   			    data:"ingredientid="+ingredientid,
   			    success: function(msg){
				    $.ajax({
   		                type: "POST",
   			            url: "actions/printingredients.php",
   			            data:"recipeid="+recipeid,
   			            success: function(msg){
				            $("#recipeleft").html(msg);
				            deleteingredients();
   			            }
 		            });
   			    }
 		    });
        });
     }
       

//--------MANUAL TAB-------------------------------------------------------    
    //add an ingredient manually
    $("#manualsubmit").click(function(){
        var name = $("#ing_name").val();
        var qty = $("#ing_qty").val();
        var serving = $("#ing_serving").val();
        var calories = $("#ing_calories").val();
        var fat = $("#ing_fat").val();
        var recipeid = $("#ing_id").val();
        $.ajax({
   		    type: "POST",
   			url: "actions/ingredientadd.php",
   			data: "name="+name+
   			        "&qty="+qty+
   			        "&serving="+serving+
   			        "&calories="+calories+
   			        "&fat="+fat+
   			        "&recipeid="+recipeid,
   			success: function(msg){
				$("#ing_name").val('');
                $("#ing_qty").val('');
                $("#ing_serving").val('');
                $("#ing_calories").val('');
                $("#ing_fat").val('');
                //update the ingredients list
                $.ajax({
   		            type: "POST",
   			        url: "actions/printingredients.php",
   			        data:"recipeid="+recipeid,
   			        success: function(msg){
				        $("#recipeleft").html(msg);
				        deleteingredients();
   			        }
 		        });
   			}
 		});
    });

//--------SEARCH TAB-------------------------------------------------------    
    //perform a food search on calorie king
    $("#recipe_foodsearch").keypress(function(event){
        if(event.keyCode==13){
	        var search = $("#recipe_foodsearch").val(); 
	        if(search != "Search Foods"){
    	        $.ajax({
    		        type: "get",
        		    url: "/foodsearch/recipeindex.php",
        		    data: "search="+search,
        	        success: function(data){
				        $("#search_food_results_container").html(data);
				        displayfood();//we need to bind the display food
				                //function because the ingredient_listing
				                //spans don't exist until we've made this 
				                //AJAX call
			        }
		        });
            }
	    }
    });
    
    //display a particular food from calorie king
    var displayfood = function(){
        $(".ingredient_listing").click(function(){
            var foodid = $(this).attr("rel");
            $.ajax({
                type: "get",
                url: "/foodsearch/recipeindex.php",
                data: "food="+foodid,
                success: function(data){
                    $("#search_food_results_container").html(data);
                    addfood();
                }
            });
        });
    }
    
    //add a food from calorie king
	var addfood = function(){
	    $("#searchsubmit").click(function(){
	        var name = $("#searched_food_name").val();
	        var qty = $("#searched_food_qty").val();
	        var serving = $(".ingredient_servings:checked").attr("serving");
	        var calories = $(".ingredient_servings:checked").attr("calories");
	        var fat = $(".ingredient_servings:checked").attr("fat");
	        //steal the recipe id from the manual tab
	        var recipeid = $("#ing_id").val();
	        $.ajax({
   		        type: "POST",
   			    url: "actions/ingredientadd.php",
   			    data: "name="+name+
   			        "&qty="+qty+
   			        "&serving="+serving+
   			        "&calories="+calories+
   			        "&fat="+fat+
   			        "&recipeid="+recipeid,
   			    success: function(msg){
   			        $("#search_food_results_container").html('');
                    //update the ingredients list
                    $.ajax({
   		                type: "POST",
   			            url: "actions/printingredients.php",
   			            data:"recipeid="+recipeid,
   			            success: function(msg){
				            $("#recipeleft").html(msg);
				            deleteingredients();
   			            }
 		            });
   			    }
 		    });
	        
	    });
	}
    
    
    
//--------PANTRY TAB-------------------------------------------------------
    //pantry navigation based on the letter the food starts with
    $(".pantry_letter").click(function(){
        var letter = $(this).attr("letter");
        $.ajax({
   		    type: "POST",
   		    url: "actions/printpantryitems.php",
   		    data:"letter="+letter,
   		    success: function(msg){
		        $("#searchpantryresponse").html(msg);
		        printpantryitem();
		        pantrypagination();
   		    }
 		});
    });
    
    //when you click a particular pantry item it will
    //print the details of the item and allow you to add it to your 
    //recipe...it passes letter, start, and num (parameters needed
    //to print the link to return to the last page you were at in the pantry
    //before viewing one specific item)
    var printpantryitem = function(){
        $(".recipe_pantry_item").click(function(){
            var id = $(this).attr("itemid")
            var letter = $(this).attr("backtoletter");
            var start = $(this).attr("backtostart");
            var num = $(this).attr("backtonum");
            $.ajax({
   		        type: "POST",
   		        url: "actions/printpantryitem.php",
   		        data:"id="+id
   		            +"&letter="+letter
   		            +"&start="+start
   		            +"&num="+num,
   		        success: function(msg){
		            $("#searchpantryresponse").html(msg);
		            backtopantry();
		            pantrysubmit();
   		        }
 		    });
        });
    }
    
    //when you click the back to pantry link, it takes you back to the 
    //page in the pantry where you were before you viewed one particular item
    var backtopantry = function(){
        $("#back_to_pantry").click(function(){
            var letter = $(this).attr("letter");
            var start = $(this).attr("start");
            var num = $(this).attr("num");
            $.ajax({
   		        type: "POST",
   		        url: "actions/printpantryitems.php",
   		        data:"letter="+letter
   		            +"&start="+start
   		            +"&num="+num,
   		        success: function(msg){
		            $("#searchpantryresponse").html(msg);
		            printpantryitem();
		            pantrypagination();
   		        }
 		    });
        });
    }
    
    
    //add an ingredient from the pantry
    var pantrysubmit = function(){
        $("#pantrysubmit").click(function(){
            var qty = $("#pantry_qty").val();
            var info = $(".serving_radio_button:checked").val();
            var servinginfo = info.split('|');
            var serving = servinginfo[0];
            var calories = servinginfo[1];
            var fat = servinginfo[2];
            var recipeid = $("#ing_id").val();
            var name = $("#pantry_item_name").html();
            if (!qty.match(/^\d*\.?\d*$/)){//match floating point numbers
                alert('QTY must be a number');
            }else{
                $.ajax({
   		            type: "POST",
   			        url: "actions/ingredientadd.php",
   			        data: "name="+name+
   			            "&qty="+qty+
   			            "&serving="+serving+
   			            "&calories="+calories+
   			            "&fat="+fat+
   			            "&recipeid="+recipeid,
   			        success: function(msg){
                        //update the ingredients list
                        $.ajax({
   		                    type: "POST",
   			                url: "actions/printingredients.php",
   			                data:"recipeid="+recipeid,
   			                success: function(msg){
				                $("#recipeleft").html(msg);
				                deleteingredients();
   			                }
 		                });
 		                //print the pantry tab again with all entries
 		                $.ajax({
   		                    type: "POST",
   		                    url: "actions/printpantryitems.php",
   		                    data:"letter=0"
   		                        +"&start=0"
   		                        +"&num=10",
   		                    success: function(msg){
		                        $("#searchpantryresponse").html(msg);
		                        pantrypagination();
		                        printpantryitem();
   		                    }
 		                });
   			        }
 		        });
 		    }
        });
    }
    
    
    var pantrypagination = function(){
        $(".pantry_page_switch").click(function(){
            var letter = $(this).attr("letter");
            var start = $(this).attr("start");
            var num = $(this).attr("num");
            //print the pantry tab again with all entries
 		    $.ajax({
   		        type: "POST",
   		        url: "actions/printpantryitems.php",
   		        data:"letter="+letter
   		        +"&start="+start
   		        +"&num="+num,
   		        success: function(msg){
		            $("#searchpantryresponse").html(msg);
		            pantrypagination();
		            printpantryitem();
   		        }
 		    });
        });
    }





//--------YIELD TAB-------------------------------------------------------
//change the yield of the recipe
    $("#yieldsubmit").click(function(){
        var name = $("#yield_name").val();
        var yield = $("#yield_qty").val();
        //steal the recipe id from the manual tab
        var recipeid = $("#ing_id").val();
        $.ajax({
   		    type: "POST",
   			url: "actions/edityield.php",
   			data: "name="+name+
   			        "&yield="+yield+
   			        "&recipeid="+recipeid,
   			success: function(msg){
   			    //let the user know the yield was saved
   			    $("#yield_saved").fadeIn("slow", function(){
   			        $("#yield_saved").fadeOut("slow");
   			    });
                //update the ingredients list
                $.ajax({
   		            type: "POST",
   			        url: "actions/printingredients.php",
   			        data:"recipeid="+recipeid,
   			        success: function(msg){
				        $("#recipeleft").html(msg);
				        deleteingredients();
   			        }
 		        });
   			}
 		});
    });		



    deleteingredients();
    printpantryitem();
    pantrypagination();	
	
});
