function findRecipes(featured) {
  ls();
  ss("Loading...");
  cook("recipe_s_name", $("#s_name").val());
  cook("recipe_s_product", $("#s_product").val());
  cook("recipe_s_category", $("#s_category").val());
  $.getJSON("dr.php", {
      a: "FindRecipes",
      name: $("#s_name").val(), 
      product: $("#s_product").val(),
      category: $("#s_category").val(),
      featured: featured
    }, 
    hideLists
  );
}

function ss(msg) { $("#status").slideUp("fast", function() { $(this).html(msg).slideDown("fast"); }); }
function hs() { $("#status").slideUp("fast"); }
function ls() { $("fieldset input, fieldset select").attr("disabled","true"); }
function as() { $("fieldset input, fieldset select").attr("disabled",""); }

function populateStores(list) {
  ref = list;

  if ( list.length == 0 ) {
    ss("No recipes found for the given search criteria.");
    as();
    return false;
  }

  var output  = '';
  var maxColumns = 3;
  var itemsPerRow = Math.floor( list.length / maxColumns );
  var currentPosition = 0;
  for( i = 0; i < list.length; i++ ) {
    if ( currentPosition == 0 ) {
      output += '<ul class="column" style="display:none;">';
    }
    currentPosition++;
    output += '<li><a href="?cid=7&id=' + list[i].id + '">' + list[i].name + '</a></li>';
    if ( currentPosition == itemsPerRow && i < (list.length - itemsPerRow) || (i + 1) == list.length ) {
      output += '</ul>';
      currentPosition = 0;
    }
  }

  $("#results").html(output);
  showLists();
}

function hideLists(list, obj) {
  if ( obj == "success" ) {
    if ( !$("#results").children("ul:last").is("ul") ) {
      populateStores(list);
      return true;
    }
    obj = $("#results").children("ul:last");
  }
  obj.slideUp("slow", function() {
    if ( $(this).prev().is("ul") ) {
      hideLists(list, $(this).prev());
    } else {
      populateStores(list);
      return true;
    }
  });
}

function showLists(obj) {
  if ( !obj )
    obj = $("#results").children("ul:first");
  obj.slideDown("slow", function() {
    if ( $(this).next().is("ul") ) {
      showLists($(this).next());
    } else {
      hs();
      as();
      return true;
    }
  });
}