var maxPageCount = 5;
var currentPage = 0;
var lastPage;

function rearrangePager(showAll) // ADDED: 20080606-FERRY: Shows only 5 pager, instead of all
{
  var minPagerShown, maxPagerShown;
  var isLeftTurn, turnsCount;
  var stuckCount = 0;
  
  $('#pageNavi span.elipse').remove();
  $('#pageNavi span.separator').remove();
  $('#pageNavi a.pager').hide();
  
  if(showAll == 0)
  {
    minPagerShown = currentPage;
    maxPagerShown = currentPage;
    
    turnsCount = maxPageCount - 1;
    isLeftTurn = 1;
    
    while(turnsCount > 0 && stuckCount < 3)
    {
      if(isLeftTurn == 1 && minPagerShown > 0)
      {
        minPagerShown--;
        turnsCount--;
        stuckCount = 0;
      }
      else if(isLeftTurn == 0 && maxPagerShown < lastPage - 1)
      {
        maxPagerShown++;
        turnsCount--;
        stuckCount = 0;
      }
      else
      {
        stuckCount++;
      }
      
      isLeftTurn = 1 - isLeftTurn;
    }    
  }
  else
  {
    minPagerShown = 0;
    maxPagerShown = lastPage;
  }
  
  // Remove all other 'active' pager (Remove 'active' class from other pager)
  $.each(
    $('#pageNavi a.activePager'),
    function()
    {
      $(this).removeClass('activePager');
    }
  );
  
  
  $.each(
    $('#pageNavi a.pager'), 
    function(i, item)
    {
      if(i == currentPage)
        $(item).addClass('activePager');
        
      if(i >= minPagerShown && i <= maxPagerShown)
      {        
        $(item).show();
        if(i < maxPagerShown)
          $('<span class="separator"></span>').insertAfter(item);
      }
      else if(i == 0)
      {
        $(item).show();
        
        // Show head-elipse only if it's needed. Otherwise, show separator
        if(minPagerShown > 1)
          $('<span class="elipse">...</span>').insertAfter(item);
        else
          $('<span class="separator"></span>').insertAfter(item);
      }
      else if(i == lastPage)
      {
        $(item).show();
        
        // Show tail-elipse only if it's needed, otherwise show separator
        if(maxPagerShown < lastPage - 1) 
          $('<span class="elipse">...</span>').insertBefore(item);
        else
          $('<span class="separator"></span>').insertBefore(item);
      }
    }
  );
}

$(document).ready(function() {
  if($('#textContent')){
    //Look for occurences of any element which contains ###
    var pCount = $('#textContent > *:contains(###)').length;
    //Return immediately when none is found
    if(pCount == 0) return;
    
    var maxLoop =  pCount + 1;    
    lastPage = pCount;
    $('#pageNavi').append('Page: ');
    
    //Construct the Paging Numbers
	$('#textPaged').empty();
    var i = 0;
    for(i = 0; i < maxLoop; i++)
    {
       //Construct the Container Div for each Page
       $('#textPaged').append('<div class="page-text" id="page'+(i+1)+'" ' + (i > 0 ? 'style="display:none"' : '')+ '></div>');
       //Construct the associated Link for each Page
       var link = ' <a class="pager" href="#title" title="Page '+(i+1)+'">'+(i+1)+'</a> ';
       
       // if(i != maxLoop-1) link += ' |';       // FIX: 20080606-FERRY: Moved the separator generation to rearrangePager()
       $('#pageNavi').append(link);
    }
    
    $('#pageNavi').append('<br/><a class="show-all" href="#title" title="READ ALL ON 1 PAGE">READ ALL ON 1 PAGE</a>&nbsp; ')
    
    $('#pageNavi a').bind("click", function(e){
      if($(this)[0].firstChild.nodeValue == "READ ALL ON 1 PAGE")
      {
        for(i = 0; i < maxLoop; i++){
          $('#textPaged #page'+(i+1)).show();  
        }
        
        rearrangePager(1);
      }
      else
      {
        //Hide everything then show the intended
        $('#textPaged .page-text').hide();
        $('#textPaged #page'+this.firstChild.nodeValue).show();
        
        currentPage = eval(this.firstChild.nodeValue) - 1;
        rearrangePager(0);
      }
    });
    
    var j= 1;
    //TODO: Right now it can't move element #text (ones without container)
    var contentChildren = $('#textContent').children();
    contentChildren.each(
        function()
        {
            if($.trim($(this).text()) == '###')
            {
                j = j + 1;
            }
            else            
            {
                var currentPage = $('#textPaged #page'+j);
                $(this).appendTo(currentPage);
            }
        }
    );
    $('#textContent').hide(); 
    
    rearrangePager(0);
  }
});