/**
 * Sets up the paginator (replace buttons with "More..." and attach the click listener)
 * */
function initPaginator(appendTo, replaceThis, linkLabel, allUrls, name) {
  // Replace the old pagination widget with a more link
  // $newWidget is the new clickable link, we need it in a variable to be able to destroy it later (when no more pages are available)
  var $newWidget = $(replaceThis).html("<span id='isrmorepager-morewidget-"+name+"' class='isrmorepager-morewidget'><a href='#'>"+linkLabel+"</a></span>");

  $('span#isrmorepager-morewidget-'+name+' a').click(function(){
    runPaginator(allUrls, appendTo, $newWidget);
    return false;
  });
}

function runPaginator(allUrls, appendTo, $newWidget)
{
  // load the next page with get()
  var nextUrl = allUrls.shift();
  $.get(nextUrl, function(data){
    $(appendTo).after(data);
    $.scrollTo($(appendTo).offset().top, 1000);
    if(allUrls.length == 0) $newWidget.remove();
  });
  // If we have loaded the last page than we can kill the more widget
}

