// JavaScript Document
var $j = jQuery.noConflict();
var recipe_id = 0; //global 
var group_id = 0; 
var recipeFetch = 1; //for cache clearing...
var sel_tag=0;
var seg_id =0; 
var gt =0;
var gbi = 0;
var gti = 0;
var sets_i = 0; 
var audi_a = 0;
var open_l  = 0; 
var groupText; 
var pager = ""; 




function setNav(on,off) {
	//turn off
	if(on!=off) {
	$j("#lnk_"+off).removeClass("onthistab");
	$j("#lnk_"+on).addClass("onthistab");
	}
}


function getTags() { //all Tags... to Filter Recipe List ...
	gt++; 
	var url = "surfer/tag_pool.php?gt="+gt;
    $j("#B_1").load(url); 	
}

function loadRecipesAll_(taggie) {
	setNav("RecipesAll",pager); 
	pager = "RecipesAll"; 	
    sel_tag = taggie; //global..

  var url = "surfer/recipe_list_all.php?";
  //alert(url);
	recipeFetch++;
	url += "recipefetchie="+recipeFetch;
	if(taggie!=0) {
	  url += "&taggie="+taggie;	
	}
	//alert(url); 
  $j("#A_1a").empty(); 
  $('A_2').innerHTML = '';
  $('A_3').innerHTML = ''; 
  
  $j.post(url,
      function(data) {
		$j("#B_1").load("surfer/tag_pool.php?pager="+pager);   
		$j("#A_1").html(data);  
	  }
     ); 
  }  
  
  
////////////////// new version of load recipes all ////////  

function loadRecipesAll(taggie) {
 	setNav("RecipesAll",pager); 
	pager = "RecipesAll"; 	
    sel_tag = taggie; //global..

  var url = "surfer/recipe_list_all.php?";
  //alert(url);
	recipeFetch++;
	url += "recipefetchie="+recipeFetch;
	if(taggie!=0) {
	  url += "&taggie="+taggie;	
	}
	//alert(url); 
  $j("#A_1a").empty(); 
  $('A_2').innerHTML = '';
  $('A_3').innerHTML = ''; 
  
  $j.post(url,
      function(data) {
		$j("#B_1").load("surfer/tag_pool.php?pager="+pager);   
		$j("#A_1").html(data);  
		/// great. a list has loaded. now, hows about if a recipe is selected? 
		$j(".recipestodisplay").click( function() {
												   recipe_id = this.id;
												   var url = "surfer/recipe_view.php?recipe_id="+recipe_id;
												   $j.get(url, {pager:pager},function(data) {
																		  $j("#B_1").html("TIP: FRESH IS BEST"); 
																		  $j("#A_1").html(data); 
																		  
																		}); 
												   
												}); 
	  }
     );  	
	
}


/// END OF NEW VERSION RECIPE LOADER ////


function loadRecipes(taggie) {
	
	setNav("RecipesOnly",pager); 
	pager = "RecipesOnly"; 		
    sel_tag = taggie; //global..

  var url = "surfer/recipe_list.php?";
  //alert(url);
	recipeFetch++;
	url += "recipefetchie="+recipeFetch;
	if(taggie!=0) {
	  url += "&taggie="+taggie;	
	}
	
  $('A_2').innerHTML = '';
  $('A_3').innerHTML = ''; 
  
  $j.post(url,
      function(data) {
		$j("#A_1a").load("forms/recipe_new.php");  
		$j("#B_1").load("surfer/tag_pool.php?pager="+pager);   
		$j("#A_1").html(data);  
	  }
     ); 
  }  


function loadBinder() {
	setNav("Binder",pager); 
	pager = "Binder"; 	
  var url = "surfer/binder.php?";

   $j("#A_1a").empty();
   $j("#A_2").empty();
    $j("#A_3").empty();
  
  $j.post(url,
      function(data) {
		$j("#A_1").html(data);  
	  }
     ); 
  } 


/// based on Session, stored recipes October 30
function shopperCheck() {

	  $j("#shopListCheckie").load("surfer/shopListCheckie.php" ); 
	  $('shopListCheckie').style.display = "block";	
	  $('overlay').style.display = "block";	

	
} //end of function



function print_shopList() { 
		
		//get the values ....
		var groups = document.groupSelect.groups; 
		var lister = ''; 
	  for(var aa=0;aa<groups.length;aa++) {
		  if(parseInt(groups[aa].value)) {
							   //alert(groups[aa].id+" is "+groups[aa].value); 
							   idboy = groups[aa].id; 
							   lister += "@"+idboy+"|"+groups[aa].value; 
							 }
	  }
	  if(lister!='') {
		lister = lister.substring(1); 
		//alert("lister is \n"+lister); 
	   var okit = confirm("This will open a new window and prompt the print command.");
       if(okit==true) 
	   {
		var url = "surfer/popup_shopList.php?lister="+lister; 
		window.open(''+url,'newwin','left=20,top=20,width=750,height=800,toolbar=0,resizable=1,scrollbars=1');   
	   }		
	  }
}
 
 
 
 function csv_shopList() { 
		
		//get the values ....
		var groups = document.groupSelect.groups; 
		var lister = ''; 
	  for(var aa=0;aa<groups.length;aa++) {
		  if(parseInt(groups[aa].value)) {
							   //alert(groups[aa].id+" is "+groups[aa].value); 
							   idboy = groups[aa].id; 
							   lister += "@"+idboy+"|"+groups[aa].value; 
							 }
	  }
	  if(lister!='') {
		lister = lister.substring(1); 
        var url = "surfer/csv_shopList.php?lister="+lister; 
		$j("#csvOutput").load(url); 
		
	  }
}




/// new version of packetMake based on session storage of recipes (in a "Binder" ) ///
function packetMake() {
     
	   var okit = confirm("This will open a new window which will show a PDF or prompt download.");
       if(okit==true) 
	   {
		var url = "surfer/pdf_gen.php";
		window.open(''+url,'newwin','left=20,top=20,width=750,height=800,toolbar=0,resizable=1,scrollbars=1');   
	   }		
	
} //end of function


/* new feature Oct 30 */

   function bagger(recipe) {
	 //given a bit.. determine if checked or not
	 if($('recipe_binder_'+recipe).checked) {
		 //add to gig bag
		 //alert("add to Binder"); 
		 addBinder(recipe,'list');
	 } else {
		//remove from gig bag  
		//alert("will remove from Binder"); 
		removeBinder(recipe,'list' ); 
	 }
  }


function addBinder(id,from) {
//  alert("bit is "+	id); 
  $j.post("surfer/binder_insert.php?recipe_id="+id,
		function(data) { 
		if(data!="no") {
				$j("#messageBox").html("You just put a recipe in your binder! "); 
				$('messageBox').style.display = "block";
				setTimeout("emptyDiv('messageBox')",2000);
			
		} else {        
		  alert("Trouble adding recipe!"); 	
		}}
		); 
	
}

function removeBinder(id,from) {
	var url = "surfer/binder_delete.php?recipe_id="+id;
	 
  $j.post(url,
	function(data) { 
	$j("#messageBox").html(data).show(); 
    setTimeout("emptyDiv('messageBox')",2000);
	if(from!='list') {
	loadBinder(); //reload the binder  Note: change to just remove row (html) 
    
	}
	
	}); 	
}

/////////////////////////////////




function addRecipe() {

  var recName = $('recipeNew').value;
  if(recName.length<6) {
	return false;
  } else {
  var url = "surfer/recipe_insert.php";
	$j.post(url, { recName:recName},
			function(data) {
				if(data!=0) {
					      $('recipeNew').value = '';
						  loadRecipe(data); //data is returned new id
				} else {
				   alert("Trouble adding recipe"); 	
				}
			} 
			); 
	
	}
  }

function removeRecipe(recipe) {
  var confirmer = confirm("Sure about deleting it?"); 
  if(confirmer) {
	$j.post("surfer/recipe_delete.php?recipe_id="+recipe,
			      function(data) {
					  alert(data);   
					   loadRecipes(sel_tag); 
				  }); 
  }
	
}

  var rec_i = 0; 
  function loadRecipe(recipe) {  //for editing
	rec_i++; 
    recipe_id = recipe; //set global
	//first, load the basics
	$j("#A_1").load("surfer/recipe_edit.php?recipe_id="+recipe_id+"&sel_tag="+sel_tag); 
	getIngredients_recipe(); //get the ingredients
	getPreps_recipe();    //get the preps
	loadReci_tags(); 
	//get the Tags ....
	
    $('A_1a').innerHTML = ''; //don't display add Recipe form

  }
  
  function recipeSave() {
	var name=$('recipe_name').value;
	var footNotes = $('footNotes').value;
	var output = $('output').value; 
	if(name.length<8) {
	  alert("Recipes need a name."); 	
	  return false;
	} else {
		var url = "surfer/recipe_save.php?recipe_id="+recipe_id;
	  $j.post(url,{name:name,footNotes:footNotes,output:output }, function(data) { alert(data) }); 	
	}
	  
  }
  
  function displayRecipe(recipe,screener) { //for Read Only Recipe.. screener is how view (might want print/popup.
		rec_i++;
		recipe_id = recipe; 
		$("A_1").innerHTML = ''; 
		  $j("#A_1").load("surfer/recipe_view.php?recipe_id="+recipe_id+"&sel_tag="+sel_tag+"&screener="+screener+"&pager="+pager); 																	

	  $('A_1a').innerHTML = ''; //don't display add Recipe form
  }
  
  function print_recipe(recipe) { 
		var url = "surfer/popup_printRecipe.php?recipe_id="+recipe; 
		//alert(url);      
	 var okit = confirm("This will open a new window and prompt the print command.");
     if(okit==true) 
	   {
				//open new window...
		window.open(''+url,'newwin',
	'left=20,top=20,width=750,height=800,toolbar=0,resizable=1,scrollbars=1');   
	   }

}

 function getIngredients_recipe() {
   gbi++;
	var url = "surfer/recipe_ingredients_list.php?gbi="+gbi+"&recipe_id="+recipe_id;
	$j.get(url,
		     function(data) { 
			   $('A_2').innerHTML = data; 
			   $j("#newGroup").focus( function() { $j(this).val(''); }); 
			   $j("#ag1").click( function() { addGroup(); }); 
			   $j(".lnk_itemAdd").click( function() {
									  var idboy = this.id; 
									  groupText = $j("#g_"+this.id).html();
									  loadGroup_newInputs(idboy); 
									  });
			   $j(".lnk_groupRevise").click(
											function() {
											  var idboy = this.id;
											  loadGroup(idboy);  
											}
											); 
				   $j(".lnk_groupReorder").click(
											 function() {
												 groupText = $j("#g_"+this.id).html();
												 reorderGroup(this.id);  
											 }
											 );		   
			   
			   
			   }
			   
			   
		   );  
 } 
 

 
 
 
/* pertaining to Ingredient Groups */

 function addGroup() {
  var groupName = $('newGroup').value;
  if(groupName.length<3) {
	var confirmer = confirm("You want to make an Untitled group?");
	if(!confirmer) {
	  return false; 	
	}
  }
  var url = "surfer/group_insert.php?recipe_id="+recipe_id;
	$j.post(url, { groupName:groupName},
			function(data) {
				if(data!=0) {
					       
					      getIngredients_recipe();
						  groupText = groupName; //the following function looks for groupText var to put in h3
						   loadGroup_newInputs(data); //load the insert items form ...		

				} else {
				   alert("Trouble adding group"); 	
				}
			} 
			); 

  }


function loadGroup(id) {
	  group_id = id; //set global 
	  var url = "forms/group_edit_.php?group_id="+id;
	  groupText = $j("#g_"+group_id).html();
	  $j("#group_edit").load(url, 
						   function() {
	                             $j("#groupName").val(groupText); 
								$j("#sgn").click(
												 function() {
													var newname = $j("#groupName").val(); 
													$j.post("surfer/group_nameSave.php", { name:newname,group_id:group_id},
															function(data) {
															  $j("#g_"+group_id).html(newname);
															  groupText = newname; 
															  //$j("#mssg_editing").html("Saved the Changes!");
															  alert("Saved the Changes!");
															}
															);  
												 }
												 );
								 $j(".itemDelete").click(
														function() {
														  //alert("will delte "+this.id); 	
														  $j("#row_"+this.id).remove(); 
														  $j.post("surfer/group_itemDelete.php?id="+this.id, function(data) {
																													    alert(data); 
																													  }
																													  ); 
														}
														); 
								 $j(".itemSave").click(
													     function() {
														
															 var quanter = $j("#quantity_"+this.id).val(); 
															 var texter = $j("#itemText_"+this.id).val(); 
															// alert("item "+quanter+" text is "+texter); 
															 $j.post("surfer/group_itemSave.php?id="+this.id, 
																	 { quantity:quanter,recipeText:texter},
																	 function(data) {
																		 alert(data); 
																	 }); 
														 }
													   ); 
                                $j("#s3").click( function() {  loadGroup_newInputs(group_id)}); 
								$j("#s4").click( function() {  reorderGroup(group_id)}); 
						   }//close function from loaded file
						   );

  
  $j('#group_edit').show();	
  $j('#overlay').show();
} 



function loadGroup_newInputs(group) {
	group_id = group; //set global
	$j.get("forms/group_newItems.php?group_id="+group_id, 
						   function(data) {
							$j("#group_edit").html(data)
							$j(".newInputs").hide();  
							var $numRow = 1;
							$j("#grouper").html(groupText); 
							$j("#newInputs_1").show(); 
							$j("#rowAdder").click( function() { 
															$numRow++; 
															$j("#newInputs_"+$numRow).show(); 
															}); 
							 $j(".new_ingredient").attr("disabled","disabled"); 
							 $j(".category").change( function() { 
															     var idboy = this.id.substring(9);  
																 var catval = $j(this).val(); 
																 if(catval!=0) {
																	 $j("#new_itemSel_"+idboy).removeAttr("disabled");
																	doSuggest(idboy,catval);   
																 } else {
																	$j("#new_itemSel_"+idboy).attr("disabled","disabled"); 
																 }
																 } ); 
                              $j("#s1").click(
											   function() {
												   //alert("save for "); 
												     saveNewItems($numRow); 
											   }
											  );
						   
						    }//close function from loaded file
						   );
     $j('#group_edit').show();
     $j('#overlay').show();
}

function doSuggest(id,category) {
							   $j("#new_itemSel_"+id).suggest("surfer/ingredients.php?cat="+category);	
}
	

///// new group ////

function saveNewItems(numNew) { 
  	//alert("will save for "+numRows+ "inputs");  
  var lister_new = '';

  var newItemsCat = document.groupEdit.new_itemCat; 
  var newItems = document.groupEdit.new_itemSel;
  var newItemsQuant = document.groupEdit.new_itemQuant;
  var newItemsText = document.groupEdit.new_itemText;

  for(var b=0;b<numNew;b++) {
		var thisItem = newItems[b].value; //look at value of item...  
	if(thisItem.length>2) {
		var quanty = newItemsQuant[b].value;
		var texty = newItemsText[b].value; 
		var thisCat = newItemsCat[b].value;
		lister_new += "@"+thisCat+"|"+thisItem+"|"+quanty+"|"+texty;
	}
  } //end of foreach new items
  
  	  if(lister_new!='') {
	  lister_new = lister_new.substring(1); 
	  }
    //alert("lister is \n"+lister_new); 

  var url = "surfer/group_insertItems.php?group_id="+group_id;
  //alert("url is "+url); 
  
 $j.post(url, {lister_new:lister_new},
		  function(data) {
			   //alert(data); 
			   //alert("received \r\n "+data); 
			  
			  var data_arr = data.split("@");
			  
			  if(parseInt(data_arr[0])!=1) {
				alert("Trouble adding group!");   
			  } else {
				 closer("group_edit");
				$j("#messageBox").html(data_arr[1]); 
				$('messageBox').style.display = "block";
				setTimeout("emptyDiv('messageBox')",2000);		
			  }
			 
		  }
		  );


} //end of save new group


function reorderGroup(group) {
	group_id=group; 
	$j.get("surfer/group_itemsReorder.php?id="+group, function(data) {
																	   $j("#group_edit").html(data).show(); 
																	   $j("#grouper").html(groupText); 
																	 }); 
	
}									 
	



/// REORDER A GROUP
function moveUp(bitter) {
  var bits = $('lister').childElements();
  for(var i=0;i<bits.length;i++) {
    //alert(bits[i].id); 
   if(bits[i].id ==bitter) {
      var bitterSlot = i;
	  }
   }
   if(bitterSlot==0) {
     alert("Can't get any higher."); 
	 return false;
	 //make it the Last Slot ... using method of moveDown	 
	 
   } else {
   //find the slot Above it...
   var beforeSlot = bits[bitterSlot - 1].id; 
   	target = $(beforeSlot);
	src = $(bitter);
	//alert("target: "+target+ ":: src: "+src); 
	$('lister').insertBefore(src, target);
	//send to database
	
	$j.post("surfer/item_moveIt.php?doThis=move",
			{target:beforeSlot, source:bitter,group_id:group_id}
			);
	
	}
} //end of function Move Up

function moveDown(bitter) {  //set list 
 // alert("bitter is "+bitter); 
  var bits = $('lister').childElements();
  for(var i=0;i<bits.length;i++) {
   if(bits[i].id ==bitter) {
      var bitterSlot = i;
	  }
   }
  
  if(bitterSlot==bits.length-2) { //length includes extra LI that totals. 
     alert("Can't get any lower."); 
	
   } else {
	var source = bits[bitterSlot + 1].id; 
	target = $(bitter);
	src = $(source);
	$('lister').insertBefore(src, target);
	//send to database
	
	$j.post("surfer/item_moveIt.php?doThis=move",
			{target:bitter, source:source,group_id:group_id}
			);	

  }
}
//end of function Move Down


/* end of groups */ 

function getPreps_recipe() 
 {
	gbi++;
	var url = "surfer/recipe_prep_segs.php?gbi="+gbi+"&recipe_id="+recipe_id;
	$j.get(url,
		     function(data) { $('A_3').innerHTML = data; }
		   ); 	
 }
 
 function loadSeg(type,id) {
	 gbi++;
	 var url = "forms/seg_"+type+".php";
	 if(type=='edit') {
		url += "?seg_id="+id;
		seg_id = id; //global if needed...
	 }
	 $j("#seg_edit").load(url); 
	 $('seg_edit').style.display = "block";	
     $('overlay').style.display = "block";
 }
 
 function saveSegment(type) 
 {
	var name=$('seg_name').value;
	var instructs = $('seg_instructions').value;
	var url = "surfer/seg_save.php";
	//alert("yes hitting JS"); 
	$j.post(
			  url,
			  {type:type,name:name,seg_instructions:instructs,recipe_id:recipe_id,seg_id:seg_id },
			  function(data) {
				  data_arr = data.split("@"); 
				  if(parseInt(data_arr[0])!=0) {
					 closer("seg_edit");
					$j("#messageBox").html(data_arr[1]); 
					$('messageBox').style.display = "block";
					setTimeout("emptyDiv('messageBox')",2000);	
					getPreps_recipe();
				  } else {
					  alert(data_arr[1]); //put problem in alert   
				  }
			  }
			);
 }


 
function loadReci_tags() { //populate the Form's Tag Realm with ... IT's tags
  	audi_a++;
	
	var url = "forms/tag_realm.php?recipe_id="+recipe_id+"&audi="+audi_a;
	$j("#B_1").load(url,
					function(data) {
					  //now get the existing tags ....
					  getTags_recipe(); 
					}
					); 
}

function getTags_recipe() {
	gbi++; 
  //get the existing tags per this recipe and place in a realm which loads via Above function
  $j("#tags_ex").load("surfer/tag_r.php?gbi="+gbi+"&recipe_id="+recipe_id); 
}

function addTag() { //creating new tags via Text Input and assign to a recipe...

  var tagNew = $('tagNew').value;
  if(tagNew.length<2) {
 
	return false;
  } else {
    var url = "surfer/tags_insert.php?wordie="+tagNew+"&recipe_id="+recipe_id;
	 $j.post(url,
			 function(data) {
				 //alert(data);
				 if(data==1) {
					getTags_recipe(); //reload this recipe's tags... 
					$('tagNew').value = ''; 
				 }
			 });
	
  } 
 
}//end of function

function loadTagSel() {
  //load a mod to select tags for the recipe 
	//alert("okay add"); 
	$j("#tagger").load("surfer/recipeTags.php?recipe_id="+recipe_id
	 ); 
	  $('tagger').style.display = "block";	
	  $('overlay').style.display = "block";	  
	  
}
  function reciTag(tag) { //given an existing tag.. add or remove it per this recipe
	 var url = "surfer/reciTag_change.php?tag="+tag+"&recipe_id="+recipe_id;
	 if($('tagg_'+tag).checked) {
		 //insert it..
		 url+="&doThis=add";
	 } else {
		//delete it ....  
		url+="&doThis=rem"; 
	 }
	 $j.post(url,function(data) { 
						    if(data!=1) {
							  alert("Problem updating record"); 	
							}
						  } ); 
  } 
   	
	
	



function closer(div) {
  //empty the form // 
  $(div).style.display = "none";	
  $(div).innerHTML = '';
    $('overlay').style.display = "none";
}
  function emptyDiv(id) {
	$(id).innerHTML = '';
	$(id).style.display='none'
}


/* common Functions */ 
/*
 function submitenter(myfield,e,action)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
    //do What?
	
	if(action=='addTag') {
		//alert("yes hit");
		addTag();
	}
   return false;
   }
else
   return true;
}

}

*/


