// Create global variables to store the draggables
// These are needed so that we can create and destroy them later on
var store_items=new Array();
var cabinet_items=new Array();

// When the document is loaded, set everything up
document.observe('dom:loaded' , function() {
  
  // Make each item in the store draggable
  $$('div.store_item').each(function(s)   { 
    var s_drag = new Draggable(s, { revert:true, scroll:window }); 
    store_items.push(s_drag);
    });
  
  // Make all visible items in the cabinet draggable
  $$('div.cabinet_item').each(function(s) { 
    if(s.visible()){
      var s_drag = new Draggable(s, { revert:true, scroll:window }); 
      var array_ptr = s.readAttribute('id').substring(4);
      cabinet_items[array_ptr] = s_drag;
      }
    });

  // Create droppable region in the cabinet where items from the store will be dropped
  Droppables.add('cabinet', {
    accept: 'store_item',                 // Only take store items
    hoverclass: 'hover',                  // When a store item is over the cabinet, apply the 'hover' class

    onDrop: function(item){               // When a store item is dropped
      var s_drag;                          
      while(store_items.length > 0){      // Delete all of the current draggables
        s_drag = store_items.pop();
        s_drag.destroy();
      };

      // Hide the item which was dropped and show the it in the user's cabinet
      $('empty_cab').hide();  
      $('ing_'+item.readAttribute('id').substring(4)).hide();  
      $('cab_'+item.readAttribute('id').substring(4)).show();
      // Update the store, removing the dropped item
      new Ajax.Updater($($(item).up('div')), 'stock.process.php?add_ing&id='+item.readAttribute('id').substring(4),
        {
          evalScripts: true,
          // After the AJAX update completes and passes
          onSuccess: function(){
            // Create a new draggable for the new item in the cabinet (was invisible before)
            var array_ptr = item.readAttribute('id').substring(4);
            cabinet_items[array_ptr] = new Draggable($('cab_'+item.readAttribute('id').substring(4)), { revert:true, scroll:window }); 
          },
          // If the AJAX update failed
          onFailure: function(){
            // Don't show the element in the cabinet and re-enable it in the store
            $('ing_'+item.readAttribute('id').substring(4)).show();
            $('cab_'+item.readAttribute('id').substring(4)).hide(); 
          }
        }
      );
    }
  });

  // Create droppable region in the recycle bin where items from the cabinet will revert to the store
  Droppables.add('recycle_bin', {
    accept: 'cabinet_item',               // Only take store items
    hoverclass: 'hover',                  // When a store item is over the cabinet, apply the 'hover' class
    onDrop: function(item){               // When a store item is dropped
      var s_drag;
      while(store_items.length > 0){      // Delete all of the current draggables
        s_drag = store_items.pop();
        s_drag.destroy();
      };

      // Hide the item which was dropped in the recycle bin
      $('cab_'+item.readAttribute('id').substring(4)).hide();
      // Update the store, re-enabling the dropped item
      new Ajax.Updater($('store'+$(item.down('span')).readAttribute('id').substr(7)), 'stock.process.php?del_ing&id='+item.readAttribute('id').substring(4),
        {
          evalScripts: true,
          // After the AJAX update completes and passes, destroy the draggable item which was dropped
          onSuccess: function(){
            var array_ptr = item.readAttribute('id').substring(4);
            cabinet_items[array_ptr].destroy();
            // Also, show the store page which just had an item added back to it
            toggleStore('store'+$(item.down('span')).readAttribute('id').substr(7));
          },
          // If the AJAX update failed
          onFailure: function(){
            // Don't re-enable it in the cabinet
            $('cab_'+item.readAttribute('id').substring(4)).show();
            $$('div.store_item').each(function(s)   { 
              s_drag = new Draggable(s, { revert:true, scroll:window }); 
              store_items.push(s_drag);
            });
          }
        }
      );
    }
  });
});

function loadRecipes(alpha, number, type, show_missing, num_change, first_id){
  if(alpha == "#")
    alpha = "0";
  link = 'recipes.process.php?option='+type+'&num='+number+'&show_missing='+show_missing+'&num_change='+num_change+'&first='+first_id+'&alpha='+alpha;
  new Ajax.Updater($('recipe_page'), link, {
    evalScripts: true,
      onSuccess: function(){
        $('missing_ings_td').removeClassName('recipe_option_en');
        $('missing_ings_link').removeClassName('recipe_option_en');
        $('missing_ings_link').update('Show Missing Ingredients');
      }    
    }
  );
  $$('td.recipe_top_option').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $$('a.recipe_top_option_link').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $('recipe_top_option_'+type).addClassName('recipe_option_en');
  $('recipe_top_option_link_'+type).addClassName('recipe_option_en');
}; 
  
function toggleRecipes(glass, garnish, ignore, alpha, number, show_missing, num_change, first_id){
  new Ajax.Updater($('recipe_page'), 'recipes.process.php?option=make&nogarnish='+garnish+'&noglass='+glass+'&num_ignore='+ignore+'&num='+number+'&num_change='+num_change+'&first='+first_id+'&alpha='+alpha, {
      evalScripts: true,
      onSuccess: function(){
        $('missing_ings_td').addClassName('recipe_option_en');
        $('missing_ings_link').addClassName('recipe_option_en');
        $('missing_ings_link').update('Hide Missing Ingredients');
      }
    }
  );
};

function loadContains(){
  alpha = "0";
  
  selected_id = $F('contains');

  link = 'recipes.process.php?option=contains&id='+selected_id;
  
  new Ajax.Updater($('recipe_page'), link, {
    evalScripts: true,
      onSuccess: function(){
        $('missing_ings_td').removeClassName('recipe_option_en');
        $('missing_ings_link').removeClassName('recipe_option_en');
        $('missing_ings_link').update('Show Missing Ingredients');
      }    
    }
  );
  $$('td.recipe_top_option').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $$('a.recipe_top_option_link').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $('recipe_top_option_all').addClassName('recipe_option_en');
  $('recipe_top_option_link_all').addClassName('recipe_option_en');
};

function loadSearch(){
  alpha = "0";
  
  search_string = $F('search_string');
  
  link = 'recipes.process.php?option=search&string='+search_string;
  
  new Ajax.Updater($('recipe_page'), link, {
    evalScripts: true,
      onSuccess: function(){
        $('missing_ings_td').removeClassName('recipe_option_en');
        $('missing_ings_link').removeClassName('recipe_option_en');
        $('missing_ings_link').update('Show Missing Ingredients');
      }    
    }
  );
  $$('td.recipe_top_option').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $$('a.recipe_top_option_link').each(function(s) {
    s.removeClassName('recipe_option_en');
    s.addClassName('recipe_option');
    }
  );  
  $('recipe_top_option_all').addClassName('recipe_option_en');
  $('recipe_top_option_link_all').addClassName('recipe_option_en');
}; 
  
function toggleRecipes(glass, garnish, ignore, alpha, number, show_missing, num_change, first_id){
  new Ajax.Updater($('recipe_page'), 'recipes.process.php?option=make&nogarnish='+garnish+'&noglass='+glass+'&num_ignore='+ignore+'&num='+number+'&num_change='+num_change+'&first='+first_id+'&alpha='+alpha, {
      evalScripts: true,
      onSuccess: function(){
        $('missing_ings_td').addClassName('recipe_option_en');
        $('missing_ings_link').addClassName('recipe_option_en');
        $('missing_ings_link').update('Hide Missing Ingredients');
      }
    }
  );
};

function showMissing(){
  $$('table.missing_ings').each(function(s)   {
    s.toggle();
    }
  );
  if($('missing_ings_td').hasClassName('recipe_option_en')){
    $('missing_ings_td').removeClassName('recipe_option_en');
    $('missing_ings_link').removeClassName('recipe_option_en');
    $('missing_ings_link').update('Show Missing Ingredients');
  }
  else{
    $('missing_ings_td').addClassName('recipe_option_en');
    $('missing_ings_link').addClassName('recipe_option_en');
    $('missing_ings_link').update('Hide Missing Ingredients');
  }
};

function loadNext(type, last, glass, garnish, ignore, number, show_missing){
  new Ajax.Updater($('recipe_page'), 'recipes.process.php?option='+type+'&next=1&last='+last+'&num='+number+'&show_missing='+show_missing+'&nogarnish='+garnish+'&noglass='+glass+'&num_ignore='+ignore, {
    evalScripts: true
    }
  );
}    
function loadPrev(type, first, glass, garnish, ignore, number, show_missing){
  new Ajax.Updater($('recipe_page'), 'recipes.process.php?option='+type+'&prev=1&first='+first+'&num='+number+'&show_missing='+show_missing+'&nogarnish='+garnish+'&noglass='+glass+'&num_ignore='+ignore, {
    evalScripts: true
    }
  );
}    

function loadArticle(id){
  new Ajax.Updater($('article'), 'articles.process.php?load_art=1&id='+id);
};