/**
* Output HTML of FooterNavigation();
<div class="paging">
    <span class="previous"><span>vorige</span></span>
    <span class="moreItemsIndicator">...</span>
    <span class="resultInfo">
        <a href="#" class="pageCurrent">1</a>
        <a href="#" class="pageNum">2</a>
        <a href="#" class="pageNum">3</a>
        <a href="#" class="pageNum">4</a>
        <a href="#" class="pageNum">5</a>
        <a href="#" class="pageNum">6</a>
        <a href="#" class="pageNum">7</a>
        <a href="#" class="pageNum">8</a>
        <a href="#" class="pageNum">9</a>
        <a href="#" class="pageEnd">10</a>
    </span>
    <span class="moreItemsIndicator">...</span>
    <span class="next"><a href="#">volgende</a></span>
</div>
*/

/**
* @param itemsOnPage:Number (number of items per Page)
* @param itemsTotal:Number (Total number of view items )
* @param pagingNumbersToShow:Number (amount of pagingNumbers to show in Paging)
* @param currentPage:Number (The number of the page currently visible;)
* @param pagingURL:String
* @param pagingID:String id of div to insert paging into. For example ""
* @return  
*/
function FooterNavigation(itemsOnPage,itemsTotal,pagingNumbersToShow,currentPage,pagingURL,pagingID)
{
    //Check if DOM is available;
    if(!document.getElementById)
    {
        return;
    }
     
    /*
    * Paging settings:
    */
    this._itemsOnPage = itemsOnPage; // number of items per Page;
    this._itemsTotal = itemsTotal; // total number of items;
    this._pagingNumbersToShow = pagingNumbersToShow; // amount of pagingNumbers to show in Paging -> number must be odd in order to place active paging number in the paging center;    
    this._intCurrentPage = currentPage; // The number of the page currently visible;
    this._currentUrl = pagingURL; // URL of the current Page
    this._pagingUrlPrefix = 'overzichtPage'; // URL variable prefix to redirect to other paging Number.
    this._pagingID = pagingID;
    this._pagingLinkUrlPrefix = this._currentUrl + '&' + this._pagingUrlPrefix + '=';
    /*
    * End paging Settings
    */
    
    this._arrPagingNrs = this._createPagingArr();
    this._previousLink = this._createPreviousLink();
    this._previousItemsIndicator = this._createPreviousItemIndicator();
    this._objPagingLinks = this._createPagingLinks();
    this._nextItemsIndicator = this._createNextItemIndicator();
    this._nextLink = this._createNextLink();
       
    this._pagingContainer = document.getElementById(this._pagingID);
    
    //Delete Childnodes of this._pagingContainer
    while (this._pagingContainer.childNodes.length)
    {
        this._pagingContainer.removeChild(this._pagingContainer.childNodes[0]);        
    }
    
    this._pagingChildNodes = this._pagingContainer.childNodes
    
    this._pagingContainer.appendChild(this._previousLink);
    if(this._previousItemsIndicator !== null)
    {
        this._pagingContainer.appendChild(this._previousItemsIndicator);
    }
    this._pagingContainer.appendChild(this._objPagingLinks);
    if(this._nextItemsIndicator !== null)
    {
        this._pagingContainer.appendChild(this._nextItemsIndicator);
    }
    this._pagingContainer.appendChild(this._nextLink);
}

FooterNavigation.prototype._createPagingArr = function()
{
    var pagesTotal = Math.ceil(this._itemsTotal / this._itemsOnPage);
    var intShiftPositions = Math.floor((this._pagingNumbersToShow - 1)/2); // the difference between center and first/last paging displayed
       
    var intStartItem;
    
    var returnValue = new Array();
    // Fill pagingLinks with correct linkNumbers
    /* if currentPage is one of the first few paging Numbers:
    * start paging at Number 1 and display max. 9 paging numbers
    */
    if ((this._intCurrentPage - intShiftPositions) <= 1)
    {
        
        if (pagesTotal < this._pagingNumbersToShow)
        {
                    
            for (var i=1 ; i<=pagesTotal ; i++)
            {
                returnValue.push(i);
            }
            //console.debug('1a arrPagingNrs: '+arrPagingNrs);
            //return arrPagingNrs;
        }
        else if (pagesTotal != this._pagingNumbersToShow)
        {
            
            for (var i=1 ; i<=this._pagingNumbersToShow ; i++)
            {
                returnValue.push(i);
            }
            //console.debug('1b arrPagingNrs: '+arrPagingNrs);
            
        }
    }
    
    /* if currentPage is not a beginning or starting Number:
    * start paging a few numbers left of CurrentPage
    */
    if(((this._intCurrentPage - intShiftPositions) > 1) && ((this._intCurrentPage + intShiftPositions) < pagesTotal))
    {
        intStartItem = (this._intCurrentPage - intShiftPositions);
        var intStopItem = (intStartItem + this._pagingNumbersToShow);
        //console.debug('intStartItem: '+intStartItem);    
        //console.debug('intStopItem: '+intStopItem);    
        
        for (var i=intStartItem ; i<intStopItem ; i++)
        {
            returnValue.push(i);
        }
        //console.debug('2 returnValue: '+returnValue);        
    }
    
    /* if currentPage is one of the last few paging Number:
    * start paging at itemsTotal - _pagingNumbersToShow
    * check if startItem is not smaller then 1
    */    
    if(((this._intCurrentPage + intShiftPositions) >= pagesTotal) && (pagesTotal >= this._pagingNumbersToShow))
    {
        intStartItem = (pagesTotal - this._pagingNumbersToShow + 1);       
        for (var i=intStartItem ; i<=pagesTotal ; i++)
        {
            returnValue.push(i);
        }
        //console.debug('3 arrPagingNrs: '+arrPagingNrs);        
    }
    
    return returnValue;
};

/**
* Output HTML
* Returns HTML object containing set of links according to arrPagingNrs values;
*   Structure of HTML output:  
*   <span class="resultInfo">
*       <a href="#" class="pageCurrent">1</a>
*       <a href="#" class="pageNum">2</a>
*       <a href="#" class="pageNum">3</a>
*       <a href="#" class="pageNum">4</a>
*       <a href="#" class="pageNum">5</a>
*       <a href="#" class="pageNum">6</a>
*       <a href="#" class="pageNum">7</a>
*       <a href="#" class="pageNum">8</a>
*       <a href="#" class="pageNum">9</a>
*       <a href="#" class="pageEnd">10</a>
*   </span>
*/         
FooterNavigation.prototype._createPagingLinks = function()
{
    var linkCollection = document.createElement('span');
    linkCollection.className='resultInfo';
    
    var pagingLink;
    var pagingLinkTextHolder;
    var pagingLinkText;
    
    for (var i=0 ; i<this._arrPagingNrs.length ; i++)
    {
        pagingLink = document.createElement('a');
        pagingLinkTextHolder = document.createElement('span');
        pagingLink.setAttribute('href',(this._pagingLinkUrlPrefix + this._arrPagingNrs[i])); 
        
        if(this._arrPagingNrs[i] == this._intCurrentPage)
        {
            pagingLink.className='pageCurrent';
            
        }            
        else if(this._arrPagingNrs[i] == (this._arrPagingNrs.length - 1))
        {
            pagingLink.className='pageEnd';            
        }
        else
        {
            pagingLink.className='pageNum';
        }       
           
        pagingLinkText = document.createTextNode(this._arrPagingNrs[i]);
        pagingLinkTextHolder.appendChild(pagingLinkText);
        pagingLink.appendChild(pagingLinkTextHolder);
        linkCollection.appendChild(pagingLink);         
    }
    
    return linkCollection;        
};

/**
* Output HTML: <span class="previous"><span>vorige</span></span>    
*/
FooterNavigation.prototype._createPreviousLink = function()
{
    var returnValue = document.createElement('span');    
    returnValue.className='previous';
    
    var previousLinkText = document.createTextNode('vorige');
    
    if (this._intCurrentPage == 1)
    {
        //span maken i.p.v. link;    
        var previousLinkReplacer = document.createElement('span');       
        previousLinkReplacer.appendChild(previousLinkText);
        returnValue.appendChild(previousLinkReplacer);
    }
    else
    {
        //previous link maken;
        var previousLinkContent = document.createElement('a');
        previousLinkContent.setAttribute('href',(this._pagingLinkUrlPrefix + (this._intCurrentPage - 1)));
        previousLinkContent.appendChild(previousLinkText);
        returnValue.appendChild(previousLinkContent);        
    }
    return returnValue;
};

/**
* Output HTML: <span class="moreItemsIndicator">...</span>    
*/
FooterNavigation.prototype._createPreviousItemIndicator = function()
{
    var returnValue = null;    
    
    if (this._arrPagingNrs[0] != 1)
    {
        returnValue = document.createElement('span');
        returnValue.className='moreItemsIndicator';
        var moreItemsText = document.createTextNode('...');
        returnValue.appendChild(moreItemsText);
    } 
       
    return returnValue;
};

/**
* Output HTML: <span class="moreItemsIndicator">...</span>    
*/
FooterNavigation.prototype._createNextItemIndicator = function()
{
    var pagesTotal = Math.ceil(this._itemsTotal / this._itemsOnPage);
    var returnValue = null;    
    
    if (this._arrPagingNrs[(this._arrPagingNrs.length-1)] != pagesTotal)
    {
        returnValue = document.createElement('span');
        returnValue.className='moreItemsIndicator';
        var moreItemsText = document.createTextNode('...');
        returnValue.appendChild(moreItemsText);
    } 
       
    return returnValue;
};

/**
* Output HTML: <span class="next"><a href="#">volgende</a></span>   
*/
FooterNavigation.prototype._createNextLink = function()
{
    var pagesTotal = Math.ceil(this._itemsTotal / this._itemsOnPage);
        
    var returnValue = document.createElement('span');
    returnValue.className='next';
    
    var nextLinkText = document.createTextNode('volgende');
    
    if (this._intCurrentPage == pagesTotal)
    {
        var nextLinkReplacer = document.createElement('span');       
        nextLinkReplacer.appendChild(nextLinkText);
        returnValue.appendChild(nextLinkReplacer);        
    }
    else
    {
        var nextLinkContent = document.createElement('a');
        nextLinkContent.setAttribute('href',(this._pagingLinkUrlPrefix + (this._intCurrentPage + 1)));
        nextLinkContent.appendChild(nextLinkText);
        returnValue.appendChild(nextLinkContent);     
    }
    
    return returnValue;
};


