marqui.ps.defineNs("marqui.ps.controls");

marqui.ps.controls.TocPagers = {};

marqui.ps.controls.TocPager = function(containerId, pageSize, toolbarContainerId, id)
{
    try
    {
        this.container = $(containerId);
        this.toolbarContainer = $(toolbarContainerId);
        if (null == this.container || null == this.toolbarContainer) 
        {
            throw ("marqui.ps.controls.TocPager requires all four ctor parameters.");
        }
        this.id = id;
        this.pageSize = pageSize;
        this.setup();
    }
    catch(err)
    {
        throw ("The Marqui TOC Pager could not be created properly");
    }
}

marqui.ps.controls.TocPager.prototype.setup = function()
{
    var nextButtonId = this.id + "Next";
    var prevButtonId = this.id + "Previous";
    var pageLabelId = this.id + "PageLabel";
    var nextPageEventString = "marqui.ps.controls.TocPagers['"+this.id+"'].nextPage()";
    var prevPageEventString = "marqui.ps.controls.TocPagers['"+this.id+"'].prevPage()";
    var toolbar =   "<table class='tocpager-toolbartable'><tr><td class='tocpager-prevbuttoncontainer' id='"+prevButtonId+"' onclick=\"" + prevPageEventString + "\" ondblclick=\"" + prevPageEventString + "\"><a class='tocpager-prevbutton' href='javascript:void(0)'>Previous</a></td>" +
                    "<td class='tocpager-pagelabel' id='"+pageLabelId+"'></td>" + 
                    "<td class='tocpager-nextbuttoncontainer' id='"+nextButtonId+"' onclick=\"" + nextPageEventString + "\" ondblclick=\"" + nextPageEventString + "\"><a class='tocpager-nextbutton' href='javascript:void(0)'>Next</a></td></tr></table>";
    this.toolbarContainer.innerHTML = toolbar;  
    this.nextButton = $(nextButtonId);                      
    this.prevButton = $(prevButtonId);
    if (this.nextButton == null || this.prevButton == null)
    {
        throw ("The Marqui TOC Pager could not create the pager toolbar.");
    }
    this.pageLabel = $(pageLabelId);
    this.visibleElements = marqui.ps.html.stripWhiteSpaceNodes(this.container);
    this.numPages = Math.ceil(this.visibleElements.length/this.pageSize);
    this.showFirstPage();
}


marqui.ps.controls.TocPager.prototype.showFirstPage = function()
{
    this.currentPage = 0;
    var len = this.visibleElements.length;
    for (var i=this.pageSize;i < len; i++)
    {
        this.toggleVisibility(this.visibleElements[i]);
    }
    this.updatePageLabel();
}

marqui.ps.controls.TocPager.prototype.updatePageLabel = function()
{
    var start = (this.currentPage < 5 || this.numPages < 10 ? 0 : (this.currentPage > this.numPages - 10 ? this.numPages - 10 : (this.currentPage-4)));
    var s = "" ; // "<div>" + (this.currentPage+1) + "/" + this.numPages + "</div><div>";
    if (start > 0)
    {
        var ev = "marqui.ps.controls.TocPagers['" + this.id + "'].showPage(0);";
            s += "<a class='tocpager-pagebutton' href='javascript:void(0)' onclick=\"" + ev + "\">1</a>...";
    }
    var space="";
    for(var i=start;i<(this.numPages) && i < start+10 ;i++)
    {
        if (i == this.currentPage)
        {
            s += (space + "<b>" + (i+1) + "</b>"); 
        }
        else
        {
            var ev = "marqui.ps.controls.TocPagers['" + this.id + "'].showPage(" + (i) + ");";
            s += space + "<a class='tocpager-pagebutton' href='javascript:void(0)' onclick=\"" + ev + "\">" + (i+1) + "</a>";
        }
        space=" ";
    }
    if (start < this.numPages - 10)
    {
        var ev = "marqui.ps.controls.TocPagers['" + this.id + "'].showPage("+(this.numPages-1)+");";
            s += "...<a class='tocpager-pagebutton' href='javascript:void(0)' onclick=\"" + ev + "\">"+this.numPages+"</a>";
    }
    this.pageLabel.innerHTML = s;
}

marqui.ps.controls.TocPager.prototype.toggleVisibility = function(node)
{
    if (node.style.display == "none")
    {
        node.style.display = "block"; //node.style.previousDisplay;
    }
    else
    {
        node.style.previousDisplay = "block"; //node.style.display;
        node.style.display="none";
    }
        
    
}

marqui.ps.controls.TocPager.prototype.showPage = function(pageNumber)
{
    try
    {
        if (pageNumber < 0 || pageNumber >= this.numPages)
        {
            return;
        }
        for (var i = 0;i < this.pageSize; i++)
        {
            var hideIndex = (this.currentPage * this.pageSize) + i;
            var showIndex = (pageNumber * this.pageSize) + i;
            if (hideIndex >= 0 && hideIndex < this.visibleElements.length)
            {
                this.toggleVisibility(this.visibleElements[hideIndex]);
            }
            if (showIndex >= 0 && showIndex < this.visibleElements.length)
            {
                this.toggleVisibility(this.visibleElements[showIndex]);
            }
        }
        this.currentPage = pageNumber;
        this.updatePageLabel();
    }
    catch(err)
    {
        throw ("The Marqui TOC Pager could not show page " + pageNumber);
    }
}

marqui.ps.controls.TocPager.prototype.nextPage = function()
{
    this.showPage(this.currentPage+1);
}

marqui.ps.controls.TocPager.prototype.prevPage = function()
{
    this.showPage(this.currentPage-1);
}