var recipeObj;

Event.observe(window,'load',function() {
   Event.observe('myform','submit',getRecipe);
});

function getRecipe(evnt) {
   Event.stop(evnt);
   if (recipeObj) {
      printRecipe();
      return;
   }
   var url = 'query1.php';
   var myAjax = new Ajax.Request( url, 
{
    method:'get',
    onSuccess: function(transport){
      eval("recipeObj = {" + transport.responseText + "}");
      printRecipe();
    },
    onFailure: function(){ alert('Something went wrong...') }
  });

}

function printRecipe() {

      // Pobieranie nazwy drinka i obiektu przeznaczonego na przepis
      var drinkName = $F('drink');

      // Wyszukiwanie nazwy drinka 
      var drinkObj = recipeObj.drinks.find(function(drink) {
           if (drink.drink == drinkName) {
             return drink;
           }
      });
        
      if (!drinkObj) return;

      var str = "<h3>" + drinkObj.makings.title + "</h3>";
      var ingredients = drinkObj.makings.ingredients.collect(function(ingredient) {
            return ingredient.ingredient;
      });
      str += ingredients.join('<br />');
      str += "<br /><br />" + drinkObj.makings.instruction;

      if ($('recipe')) 
          $('recipe').remove('recipe');
      var recipe = document.createElement('div');
      Element.extend(recipe);
      recipe.id = 'recipe';
      recipe.addClassName('recipe');
      recipe = recipe.update(str);
      document.body.appendChild(recipe);
      new Effect.Shake(recipe);
}
