function pager( container )
    {
    container = $(container);
    var current_page_ix = 0;
    var is_animating = false;
    var pages, k_pages, mask, pagelist;
    var navigation, dots, next_link, prev_link;
    
    var initialize = function ()
      {
      var page_id;
      pages = $('.page',container);
      k_pages = pages.length;
      
      if ( document.location.hash && document.location.hash.length > 1 )
        {
        page_id = document.location.hash.substring(1);
        pages.each(function (ix, p)
          {
          if ( p.id == page_id )
            {
            current_page_ix = ix;
            }
          });
        }
      };
    
    var calc_pagelist_offset = function (ix)
      {
      var offset = 0;
      for ( var i = 0;  i < ix;  i++ )
        {
        offset -= pages.eq(i).height();
        }
      return offset;
      };
    
    var scroll_to_page = function (ix)
      {
      if ( current_page_ix === ix || is_animating )
        { return;  }
        
      var duration = 666 * Math.abs(ix - current_page_ix);
      
      var after = function ()
        {
        current_page_ix = ix;

        is_animating = false;
        $('a',dots).eq(ix).addClass('current');
        };
      
      $('a.current',dots).removeClass('current');
      is_animating = true;

//      next_link.hide('fast');


      pagelist.animate({'top': calc_pagelist_offset(ix)}, duration, 'swing', after);
      mask.animate({'height': pages.eq(ix).height()}, duration / 3.0, 'swing');      
      
      return false;
      };

    var render = function ()
        {
        var render_mask = function ()
          {
          mask = $('<div class="pagelist-mask"></div>');
          container.append(mask);
          };
        
        var render_pages = function ()
          {
          pagelist = $('<ol class="pagelist-list"></ol>');
          pagelist.append(pages);
          pages.wrap('<li></li>');
          
          mask.append(pagelist);
          mask.height($(pages[ current_page_ix ]).height());
          pagelist.css({'top': calc_pagelist_offset(current_page_ix)});
          };
        
        var render_navigation = function ()
          {
          navigation = $('<div class="pagelist-navigation"></div>');
          container.append(navigation);

/*
          next_link = $('<a class="pagelist-navigation-next"></a>');
          prev_link = $('<a class="pagelist-navigation-prev"></a>');

          if ( 0 == current_page_ix )
            {
            prev_link.hide();
            }
          else
            {
            prev_link.text(pages[ current_page_ix - 1 ].title);
            }

          if ( current_page_ix < k_pages )
            {
            next_link.text('Continue reading “' + pages[ current_page_ix + 1 ].title + '”');
            }
          else
            {
            next_link.hide();
            }

          navigation.append(prev_link).append(next_link);
*/
          render_dots();
          };
        
        var render_dots = function ()
          {
          dots = $('<ol class="pagelist-dots"></ul>');
          
          var dot = function ( ix )
            {
            var title = $(pages[ ix ]).attr('title');
            var link = $('<a></a>');

            dots.append(link);
            
            link.wrap('<li></li>').
                 attr({'title': title,
                       'href': document.location.pathname + '#' + pages[ix].id}).
                 text(title).
                 click(bind(scroll_to_page, this, ix));
            
            if ( ix === current_page_ix )
              {
              link.addClass('current');
              }            
            };

          foreach(dot, range(k_pages));
          dots.css('margin-left', -1 * dots.width() / 2);
          navigation.append(dots);
          };

        container.empty();
        
        render_mask();
        render_pages();
        render_navigation();
        };

    initialize();
    render();
    return scroll_to_page;
    }
