var inlineFavorite = function(favoritesSaveMode, identifier, urn, nRank, nFavoriteId)
{
  /* Add a spinny and progress message */
  var oSelectorDiv = document.getElementById('UserFavoriteRankControl'+identifier);
  var spinner = document.createElement('img');
  spinner.id = 'spinner_'+ identifier;
  spinner.src = '/images/loading.gif';
  spinner.align = 'absbottom';
  oSelectorDiv.appendChild(spinner);
  
  var oActionMessage = document.createElement("span");
  oActionMessage.id = 'ActionMessage_'+ identifier;
  if (favoritesSaveMode == 'SET_RANK')
  {
    oActionMessage.innerHTML = "&nbsp;Ranking ...";
  }
  else if (favoritesSaveMode == 'DELETE')
  {
    oActionMessage.innerHTML = "&nbsp;No longer Following...";
  }
  else
  {
    oActionMessage.innerHTML = "&nbsp;You are now following...";
  }    
  oSelectorDiv.appendChild(oActionMessage);
        
  document.getElementById('removeFavorite'+identifier).style.display = 'none';
  document.getElementById('addFavorite'+identifier).style.display = 'none';
  
  var manageFavoritesForm = document.getElementById('manageFavoritesForm');
  
  manageFavoritesForm.favoritesSaveMode.value = favoritesSaveMode;
  manageFavoritesForm.identifier.value = identifier;
  manageFavoritesForm.urn.value = urn;
  
  /* Why are we checking for null? */
  if (manageFavoritesForm.nRank != null)
  {
    manageFavoritesForm.nRank.value = nRank;
  }
  manageFavoritesForm.nFavoriteId.value = nFavoriteId;
    
  var local_favoritesSaveMode = favoritesSaveMode;
  $( manageFavoritesForm ).ajaxSubmit( {
    dataType: 'json',
    success: function( returnObj )
    {
      if( (typeof returnObj === 'object') && (typeof returnObj.bIsSuccess === 'boolean') )
      {
        if(returnObj.bIsSuccess==true)
        {
          oSelectorDiv.removeChild(document.getElementById('ActionMessage_' + identifier));
          oSelectorDiv.removeChild(document.getElementById('spinner_' + identifier));
          if(returnObj.sMode === 'DELETE')
          {
            document.getElementById('addFavorite'+returnObj.identifier).style.display = 'block';
            document.getElementById('removeFavorite'+returnObj.identifier).style.display = 'none';
          }
          else /* if(returnObj.sMode === 'ADD') */
          {
            document.getElementById('addFavorite'+returnObj.identifier).style.display = 'none';
            document.getElementById('removeFavorite'+returnObj.identifier).style.display = 'block';
          }
          if(local_favoritesSaveMode == 'SET_RANK')
          {
            favoriteRank[identifier] = nRank;
            document.getElementById('removeFavorite'+identifier).style.display = 'block';
            highlightBars('UserFavoriteRankControl'+identifier, 'imgBar'+identifier+nRank);
          }
        }
        else
        {
          alert('Error occurred during attempt to manage list:\n' + returnObj.sMessage);
        }
      }
      else
      {
        alert('Server returned unknown status on attempt to manage list.');
      }
    },
    error: function()
    {
      alert('An error occured during processing.');
    }
  } );
}

function highlightBars( sContainerId, iUpToIdx )
{
  var bOn = true,
      aBarOnImages = highlightBars.aBarOnImages,
      aBarOffImages = highlightBars.aBarOffImages;

  /**
   * I would prefer the find('img') below to find only those which have the ID format we want,
   * but the use of an array of images above and access of items by index on the loop requires
   * we get them all and check ID in the each(). Some day we can change that, but it's really
   * not in scope for the performance adjustments I'm making
   */
  $( '[id="' + sContainerId + '"]' )
    .find( 'img' )
      .each( function( index, Element )
      {
        if( this.id.indexOf( 'Bar' ) >= 0 )
        {
          if( bOn )
          {
            Element.src = aBarOnImages[index];
            bOn = ! ( this.id == iUpToIdx );
          }
          else
          {
            Element.src = aBarOffImages[index];
          }
        }
      } );
}
highlightBars.aBarOnImages = [
  '/images/menuicon/add.gif',
  '/images/intness/intness_clear_hover.gif',
  '/images/intness/intness_20_hover.gif',
  '/images/intness/intness_40_hover.gif',
  '/images/intness/intness_60_hover.gif',
  '/images/intness/intness_80_hover.gif',
  '/images/intness/intness_100_hover.gif'
];
highlightBars.aBarOffImages = [
  '/images/menuicon/add.gif',
  '/images/intness/intness_clear.gif',
  '/images/intness/intness_20.gif',
  '/images/intness/intness_40.gif',
  '/images/intness/intness_60.gif',
  '/images/intness/intness_80.gif',
  '/images/intness/intness_100.gif'
];

function setEmailFavorite(urn, nFavoriteId)
{
  var userFavoritesEmailForm = document.getElementById('userFavoritesEmailForm');
  var oFavoriteCheckbox = document.getElementById('fav_'+nFavoriteId);

  if ( oFavoriteCheckbox )
  {
    userFavoritesEmailForm.urn.value = urn;
    userFavoritesEmailForm.nFavoriteId.value = nFavoriteId;

    if (oFavoriteCheckbox.checked === true)
    {
      userFavoritesEmailForm.sSendMessage.value = 'E';
    }
    else
    {
      userFavoritesEmailForm.sSendMessage.value = '';
    }

    $( userFavoritesEmailForm ).ajaxSubmit( {
      dataType: 'json',
      success: function( returnObj )
      {
        if( (typeof returnObj === 'object') && (typeof returnObj.bIsSuccess === 'boolean') )
        {
          if (returnObj.bIsSuccess === true)
          {
            /*alert('Success');*/
          }
          else
          {
            alert('Error occurred during attempt to manage list:\n' + returnObj.sMessage);
          }
        }
        else
        {
          alert('Server returned unknown status on attempt to manage list.');
        }
      },
      error: function()
      {
        alert('An error occured during processing.');
      }
    } );
  }
};
