/* This script will enable paging for the specified "container" div
 * There should be one nested div which contains the "pages" 
 * a paging div will be generated at the bottom of the "container" div
 */

var debug=true;

PagingObj = function(args) {
    this.container = document.getElementById(args.container);

    this.enabled = args.enabled;
this.pagenum = document.location.pathname.replace(/.*-page(\d+).*/, "$1");
    //find pagecontainer 
    for (var i = 0; i < this.container.childNodes.length; i++) {
        if (this.container.childNodes[i].className) {
            if (this.container.childNodes[i].className.indexOf("pagedata") == 0) {
                this.pagedatadiv = this.container.childNodes[i];
                break;
            }
        }
    }

    if ((this.enabled != "True" && this.enabled != "1") || this.pagenum==0 || this.pagedatadiv.innerHTML.length<(parseInt(args.pagesize) + 1500)) {
        this.pagedatadiv.style.display = "block";
        return;
    }


    this.pagedata = this.pagedatadiv.innerHTML;
    this.pages = {};
    this.numpages = 0;

    if (typeof (args.pagesize) !== 'undefined') {
        this.pagesize = args.pagesize;
    }
    if (this.pagesize <= 0) {
        this.pagesize = 2000;
    }
    this.splitPages();
    this.decoratePages();
    //check for current page in url
    
    if (isNaN(this.pagenum) || this.pagenum > this.numpages) {
        this.pagenum = 1;
    }

    //add fade out to pagecontainer
    if (this.pagenum != 1) {
        var topfader = document.createElement("div");
        topfader.className = "topfader";
        this.container.insertBefore(topfader, this.container.firstChild);
    }
    if (this.pagenum != this.numpages) {
        var bottomfader = document.createElement("div");
        bottomfader.className = "bottomfader";
        this.container.appendChild(bottomfader);
    }

    this.generatePageNums();

    //jump to the selected page
    this.showPage(this.pagenum);
}
PagingObj.prototype.decoratePages = function()
{

var url = document.location.pathname.replace(/(--page\d+)/, "");
var query = document.location.search;
if(this.pages[this.numpages-1].length<1500)
{
this.pages[this.numpages-2]=this.pages[this.numpages-2]+this.pages[this.numpages-1];
delete this.pages[this.numpages-1];
this.numpages--;
//this.pages.splice(this.numpages-1,1);
}
for(var i=0;i<this.numpages;i++)
{
var prepend="...";
var next_link=url + "--page" + (parseInt(i) + 2) + query;
var postpend="...[<b><a href='"+next_link+"'>Page suivante</a></b>]";
if(i== 0)
{prepend="";}
if((i+1)+""==this.numpages)
{ postpend=""; }

this.pages[i]=prepend+this.pages[i]+postpend;

}


}
PagingObj.prototype.splitPages = function() {

    var splitpoint = 0;

    //first scan it for the split tag if thats found then ignore the pagesize and split on the tag
    splitpoint = this.pagedata.indexOf("<split>");
    if (splitpoint != -1) {
        do {
            this.pages[this.numpages++] = this.pagedata.slice(0, splitpoint);
            this.pagedata = this.pagedata.slice(splitpoint + 7);
            splitpoint = this.pagedata.indexOf("<split>");

        } while (splitpoint != -1);
        this.pages[this.numpages++] = this.pagedata;
        return;
    }

    while (this.pagedata.length > 0) {
        splitpoint = this.pagesize;

        if (splitpoint > this.pagedata.length) {
            this.pages[this.numpages++] = this.pagedata;
            this.pagedata = "";
            break;
        }
        while (this.isAlphaChar(this.pagedata.charAt(splitpoint)) && splitpoint > (this.pagesize - 50)) {
            splitpoint--;
        }
        if (splitpoint <= this.pagesize - 50) {
            //alert("Couldnt split page");
            return;
        }
        this.pages[this.numpages++] = this.pagedata.slice(0, splitpoint);
        this.pagedata = this.pagedata.slice(splitpoint);
    }
}

PagingObj.prototype.isAlphaChar = function(xChar) {
    var regEx = /^[a-zA-Z0-9]$/;
    return xChar.match(regEx);
}  

PagingObj.prototype.generatePageNums = function()
{
  var pagenums = document.createElement("div");
  var pagelink;
  var pagelinktext;
  var url = document.location.pathname.replace(/(--page\d+)/, "");
  var query = document.location.search;

  pagenums.className = "pageNumbers";

//Pages link
  if (this.pagenum >= 1) {
      pagelink = document.createTextNode("Pages");
      
      //pagelink.href = url + "--page" + (this.pagenum - 1) + query;
      //pagelinktext = document.createTextNode("prev");
      //pagelink.appendChild(pagelinktext);
      pagenums.appendChild(pagelink);
  }
  for(var i=1;i<=this.numpages;i++) {
    if (i == this.pagenum) {
      pagelink = document.createElement("span");
      pagelink.className="active";
    } else {
      pagelink = document.createElement("a");
      pagelink.href = url + "--page" + i + query;
    }
    pagelinktext = document.createTextNode(i);
    pagelink.appendChild(pagelinktext);
    pagenums.appendChild(pagelink);
  }
  if (this.pagenum <= this.numpages) {
      pagelink = document.createElement("a");
      //pagelink.href = url + "--page" + (parseInt(this.pagenum) + 1) + query;
       pagelink.href = url + "--page0" + query;
      pagelink.className="next";
      pagelinktext = document.createTextNode("Voir en entier");
      pagelink.appendChild(pagelinktext);
      pagenums.appendChild(pagelink);
  }
  this.container.appendChild(pagenums);
}


PagingObj.prototype.showPage = function(pagenum) {
    this.displaypage = document.createElement("div");
    this.displaypage.innerHTML = this.pages[pagenum - 1];
    this.container.insertBefore(this.displaypage, this.container.firstChild);
}
