// ===============================
// = Supplier search specific JS =
// ===============================
var SupplierSearch = {
  
  toggleFavorite: function(element, supplier_id) {
    
    var t;
    
    if(!(element = $(element))) return;
    
    // Preemptively toggle the icon without waiting for a response from the server
    if(element.readAttribute('selected') == 'true') {
      $$('.supplier_favorite' + supplier_id).each(function(e) {
        
        // Change the icon
        e.setAttribute('src', '/images/layout/bookmark-unchecked.png');
        
        // Change the title
        var tmp_string = e.readAttribute('title');
        e.setAttribute('title', e.readAttribute('alternate_title'));
        e.setAttribute('alternate_title', tmp_string);
        
        // Change the selected property
        e.setAttribute('selected' , 'false');
        
        if(t = $('favorite_text' + supplier_id)) {
          t.firstChild.nodeValue = "Save to My SupplierSource";
        }
          
      });
    } else {
      $$('.supplier_favorite' + supplier_id).each(function(e) {
        
        // Change the icon
        e.setAttribute('src', '/images/layout/bookmark-checked.png');
        
        // Change the title
        var tmp_string = e.readAttribute('title');
        e.setAttribute('title', e.readAttribute('alternate_title'));
        e.setAttribute('alternate_title', tmp_string);
        
        // Change the selected property
        e.setAttribute('selected' , 'true');
        
        if(t = $('favorite_text' + supplier_id)) {
          t.firstChild.nodeValue = "Remove from My SupplierSource";
        }
        
      });
    }
    
    // Make the toggle request
    new Ajax.Request('/suppliers/'+ supplier_id +'/supplier_favorites', {asynchronous:true, evalScripts:true, method:'post'}); 
    return false;
  }
  
}