//Configuration
customPaginationStartPage = 10; //0 - disable; avalible - 1...


customPaginationTable = ''; //do not change

function checkIfIsNumber(evnt){
    var chCode = (evnt.which) ? evnt.which : evnt.keyCode;
    return !(chCode > 31 && (chCode < 48 || chCode > 57));
}

function DNSJumpToPage(){
    var page = jQuery('#DNSJumpToPage').val();
    if(page == 0){
        jQuery('#DNSJumpToPage').val(1);
        page = 1;
    }
    if(page == 1){
        jQuery('#DNSCustomPaginPrev').addClass('disabled');
    }else{
        jQuery('#DNSCustomPaginPrev').removeClass('disabled');                
    }
    var dataTable = jQuery('#DNSJumpToPage').parents().find('.dataTable').first();
    var dataTableId = dataTable.prop('id');
    var dTableVars = jQuery('#'+dataTableId).DataTable().page.info();
    if(page > dTableVars.pages){
        jQuery('#DNSJumpToPage').val(dTableVars.pages);                    
        page = dTableVars.pages;
    }
    if(page == dTableVars.pages){
        jQuery('#DNSCustomPaginNext').addClass('disabled');
    }else{
        jQuery('#DNSCustomPaginNext').removeClass('disabled');                
    }            
    jQuery('#'+dataTableId).DataTable().page(page-1).ajax.reload(null, false);
    DNSReloadCustomPageNumbers(page, dTableVars.pages);
}
        
function DNSGoToPrevPage(event){
    event.preventDefault();
    if(!jQuery('#DNSCustomPaginPrev').hasClass('disabled')){
        var page = jQuery('#DNSJumpToPage').val();
        jQuery('#DNSJumpToPage').val(parseInt(page) - 1);
        DNSJumpToPage();    
    }
}
function DNSGoToNextPage(event){
    if(!jQuery('#DNSCustomPaginNext').hasClass('disabled')){            
        event.preventDefault();
        var page = jQuery('#DNSJumpToPage').val();
        jQuery('#DNSJumpToPage').val(parseInt(page) + 1);
        DNSJumpToPage();
    }
}        
function DNSReloadCustomPageNumbers(page, maxPage){
    var tempPage = parseInt(page);
    var tempMaxPage = parseInt(maxPage); 
    jQuery('#DNSCustomPrevButton2').children().first().html(tempPage-2);
    jQuery('#DNSCustomPrevButton1').children().first().html(tempPage-1);
    jQuery('#DNSCustomNextButton1').children().first().html(tempPage+1);
    jQuery('#DNSCustomNextButton2').children().first().html(tempPage+2);
    if(tempPage == 1){
        jQuery('#DNSCustomPrevButton1').addClass('hidden');
        jQuery('#DNSCustomPrevButton2').addClass('hidden');                
    }else if(tempPage == 2){  
        jQuery('#DNSCustomPrevButton2').addClass('hidden');
        jQuery('#DNSCustomPrevButton1').removeClass('hidden');
    }else{
        jQuery('#DNSCustomPrevButton1').removeClass('hidden');
        jQuery('#DNSCustomPrevButton2').removeClass('hidden');                
    }          
    if(tempPage == tempMaxPage){
        jQuery('#DNSCustomNextButton1').addClass('hidden');
        jQuery('#DNSCustomNextButton2').addClass('hidden');                
    }else if(tempPage == tempMaxPage -1){
        jQuery('#DNSCustomNextButton2').addClass('hidden');
        jQuery('#DNSCustomNextButton1').removeClass('hidden');
    }else{
        jQuery('#DNSCustomNextButton1').removeClass('hidden');
        jQuery('#DNSCustomNextButton2').removeClass('hidden');                   
    }            
}

function DNSJumpToButtonPage(item){
    var id = jQuery(item).prop('id');
    var curPage = jQuery('#DNSJumpToPage').val();
    if(id.indexOf('Next') == 9)
    {
        var pageNr = id.replace('DNSCustomNextButton', '');
        jQuery('#DNSJumpToPage').val(parseInt(curPage) + parseInt(pageNr));
    }
    if(id.indexOf('Prev') == 9)
    {
        var pageNr = id.replace('DNSCustomPrevButton', ''); 
        jQuery('#DNSJumpToPage').val(parseInt(curPage) - parseInt(pageNr));
    }            
    DNSJumpToPage();            
}        

function addCustomPagination(tableId, startPage){
    if(startPage === undefined){
        startPage = customPaginationStartPage;
    }
    if(startPage == 0){
        return false;
    }
    addPaginationBlock(tableId);
    var dtInfo = jQuery('#'+tableId).DataTable().page.info();
    jQuery('#DNSJumpToPage').val(dtInfo.page + 1);
    if(dtInfo.pages >= startPage){
        jQuery('#'+tableId+'_paginate').addClass('hidden')
        jQuery('#DNSJumpToPageBlock').removeClass('hidden');
        DNSReloadCustomPageNumbers(dtInfo.page + 1, dtInfo.pages);
    }    
}

function addEventsDeclarations(tableId){
    $(document).delegate('#DNSJumpToPage', 'change', function() {
        var page = jQuery('#DNSJumpToPage').val();
        if(page == 0){
            jQuery('#DNSJumpToPage').val(1);
            page = 1;
        }
        var dTableVars = jQuery('#'+tableId).DataTable().page.info();
        if(page > dTableVars.pages){
            jQuery('#DNSJumpToPage').val(dTableVars.pages);                    
            page = dTableVars.pages;
        }
        //jQuery('#'+tableId).DataTable().page(page-1).ajax.reload(null, false); 
        DNSJumpToPage();
    });
    
    $('#'+tableId).on( 'length.dt', function(){
        DNSJumpToPage();
    });    
}

function addPaginationBlock(tableId){
    if($('#DNSJumpToPageBlock').length == 0){
        var jumpToPageContent = '<div class="hidden pull-right" id="DNSJumpToPageBlock" style="display:inline-block;">'
            +'<ul class="pagination">'
            +'<li id="DNSCustomPaginPrev" class="paginate_button previous disabled"><a href="#" onclick="DNSGoToPrevPage(event);">Previous</a></li>'
            +'<li class="paginate_button" id="DNSCustomPrevButton2" onclick="DNSJumpToButtonPage(this);"> <a href="#"></a></li>'
            +'<li class="paginate_button" id="DNSCustomPrevButton1" onclick="DNSJumpToButtonPage(this);"> <a href="#"></a></li>'        
            +'<li class="paginate_button"><a style="padding: 0px 0px;"><input id="DNSJumpToPage" type="text" maxlength="5" size="4" onkeypress="return checkIfIsNumber(event);" style="padding: 6px 0px; text-align: center; border:none; color:#fff; background-color:#337ab7; border-color:#337ab7;"></input></a></li>'
            +'<li class="paginate_button" id="DNSCustomNextButton1" onclick="DNSJumpToButtonPage(this);"> <a href="#"></a></li>'
            +'<li class="paginate_button" id="DNSCustomNextButton2" onclick="DNSJumpToButtonPage(this);"> <a href="#"></a></li>' 
            +'<li id="DNSCustomPaginNext" class="paginate_button next"><a href="#" onclick="DNSGoToNextPage(event);">Next</a></li>'
            +'</ul></div>';
            jQuery('#'+tableId+'_paginate').parent().append(jumpToPageContent);   
            addEventsDeclarations(tableId);
    }
}

function DNSgetCurrentPageNumber(){
    return $('#DNSJumpToPage').val();
}

function DNSJumpToSpecificPageNumber(number){
    $('#DNSJumpToPage').val(number);
    DNSJumpToPage();
}
