/* Modified version of pagination.js for use in Demo site */

//-- Global Default Variables --/
var _PageSize = 10;
var _ContainerName = 'dvContainerPage';
var _NumPages = 16;

// Must Call this function whenever using pagination
function InitPagination(pageSize, numPages, containerName)
{
    _PageSize = pageSize;
    _NumPages = numPages;
    _ContainerName = containerName;
}

// Gets the page number which the given question resides on
// (uses serverside pagesize variable)
function getPageNum(questionNum)
{
    return Math.floor(((questionNum-1) / _PageSize) + 1);
}

// Sets the active page link's class to "active" to show the
//  user which page is currently being displayed
function setActiveLink(pageNum)
{   
    for(var i = 1; i <= _NumPages; i++) {
        // iterate over each set of jspagers (there may be one or more)
        var lnks = document.getElementsByName('lnkPage' + i);
        for(var k = 0; k < lnks.length; k++) {
            var lnk = lnks.item(k);
            if(lnk) {
                if(i==pageNum) {
                    lnk.className = 'active';
                } else {
                    lnk.className = '';
                }
            }
        }
    }
}

// Pagination for assessments
// hides all div's accept for the div corresponding to the passed in page number
function showPage(pageNum)
{
    showPanel(Math.floor((pageNum-1) / 10) + 1);

    // style the pagination links to show which page is active
    setActiveLink(pageNum);

    //Hide all pages but pageNum
    for(var i = 1; i <= _NumPages; i++) {
        if(i==pageNum) {
            show(_ContainerName + i)
        } else {
            hide(_ContainerName + i);
        }
    }

    //Scroll to top
    scroll(0,0);
   
    //Set "You are viewing page x of numpages" label
    var lblCurPage = getObject('lblCurPage');
    if(lblCurPage) {
        lblCurPage.innerHTML = pageNum;
    }
}

function showPanel(panelNum)
{
    //Show/Hide Panels
    var numPanels = Math.floor(_NumPages / 10) + 1;
    for(var i = 1; i <= numPanels; i++) {
        var pnls = document.getElementsByName('dvPanel' + i);
        for(var k = 0; k < pnls.length; k++) {
            if(i == panelNum) {
                showElem(pnls.item(k));
            } else {
                hideElem(pnls.item(k));
            }
        }
    }
    
    //Show/Hide First & Last links
    var arr;
    if(panelNum == 1) {
        //hide("dvFirst");
        arr = document.getElementsByName('dvFirst');
        for(var k = 0; k < arr.length; k++) {
            hideElem(arr.item(k));
        }
        
        if(numPanels>1) {
            //show("dvLast");
            arr = document.getElementsByName('dvLast');
            for(var k = 0; k < arr.length; k++) {
                showElem(arr.item(k));
            }
        }
    } else if(panelNum == numPanels) {
        //hide("dvLast");
        arr = document.getElementsByName('dvLast');
        for(var k = 0; k < arr.length; k++) {
            hideElem(arr.item(k));
        }
        
        //show("dvFirst");
        arr = document.getElementsByName('dvFirst');
        for(var k = 0; k < arr.length; k++) {
            showElem(arr.item(k));
        }
    }
}
