function sidenote( fudge, sn_container, fade_time, do_textile, do_markdown )
{
  fudge        = typeof(fudge)        != 'undefined' ? fudge        : 0;
  sn_container = typeof(sn_container) != 'undefined' ? sn_container : '#sidebar';
  fade_time    = typeof(fade_time)    != 'undefined' ? fade_time    : 5;
  do_textile   = typeof(do_textile)   != 'undefined' ? do_textile   : true;
  do_markdown  = typeof(do_markdown)  != 'undefined' ? do_markdown  : true;
  
//  $(document).ready( 
  $(window).load(
  function()
  {
    if( do_textile )  sidenote_textile(  fudge, sn_container, fade_time );
    if( do_markdown ) sidenote_markdown( fudge, sn_container, fade_time );
  });
}

// ---------------------------------------------------------------------------

// textile reference selector
// $('sup.footnote a[href=#fn1]')
// textile footnote selector
// $('p.footnote#fn1')
function sidenote_textile( fudge, sn_container, fade_time )
{
  // Make transparent and move to sidenotes column
  $('p.footnote').each( function( intIndex )
  {
    $(this).css( 'opacity', 0 );
    $(this).appendTo( $(sn_container) );
  });
  
  $('p.footnote').each( function( intIndex )
  {
    // Compute the new position
    refloc = $('sup.footnote a[href=#fn'+(intIndex+1)+']').parent()[0].offsetTop;
    myloc  = $(this)[0].offsetTop;
    newloc = refloc - myloc + fudge;
  
    // Move to appropriate position if we need to push it down
    // console.dir( String(refloc) + " " + String(myloc) + " " + String(newloc) );
    // console.dir( $(this) );
    if( newloc > 0 )
      $(this).animate( {paddingTop: "+=" + newloc + "px"}, 0 );
  });
    
  // Fade those suckers in
  $('p.footnote').animate( {opacity: 100}, fade_time * 1000 );
  
  // Disable clicks on superscripts
  $('sup.footnote a')  .click( function( event ){ event.preventDefault(); });
  $('p.footnote a sup').click( function( event ){ event.preventDefault(); });
}

// ---------------------------------------------------------------------------

// markdown reference selector
// $('sup#fnref:1')
// markdown footnote selector
// $('.footnotes li#fn:1')
function sidenote_markdown( fudge, sn_container, fade_time )
{
  // Make transparent and move to sidenotes column
  $('.footnotes').css( 'opacity', 0 );
  $('.footnotes').appendTo( $(sn_container) );
  
  $('.footnotes li').each( function( intIndex )
  {
    // Append a number to each footnote
    // $('.footnotes li#fn\\:'+(intIndex+1)+' p')
    //   .first().prepend( '<sup>'+(intIndex+1)+'</sup> ' );
    
    // Compute the new position
    refloc = $('sup#fnref\\:'+(intIndex+1))[0].offsetTop;
    myloc  = $('.footnotes li#fn\\:'+(intIndex+1))[0].offsetTop;
    newloc = refloc - myloc + fudge;
    
    // Move to appropriate position if we need to push it down
    if( newloc > 0 )
      $(this).animate( {paddingTop: "+=" + newloc + "px"}, 0 );
  });
  
  // Fade those suckers in
  $('.footnotes').animate( {opacity: 100}, fade_time * 1000 );
  
  // Disable clicks on superscripts
  $('sup a[rel="footnote"]').click( function( event ){ event.preventDefault(); });
}

