$(document).ready(function (){
var replacement = '';
var url = window.location.toString();
var limit = 5;
var currentPage = (url.indexOf('page') == -1) ? 1 : parseInt(url.split('?page=')[1]);
var lastPage = getLastPage(currentPage+1);
if(currentPage == 1){
replacement += '<<First1';
for(var i = 1; i < limit; i++){
var nextPage = currentPage + i;
if(nextPage <= lastPage){
replacement += '' + nextPage + '';
}
}
}
else if(currentPage > (Math.ceil(limit/2)) && currentPage < lastPage - (Math.floor(limit/2))){
replacement += '<<First. . .';
for(var i = (currentPage - (Math.floor(limit/2))); i <= (currentPage + (Math.floor(limit/2))); i++){
replacement += (i == currentPage) ? ('' + i + '') : ('' + i + '');
}
}
else if(currentPage <= (Math.ceil(limit/2))){
replacement += '<<First';
for(var i = 1; i <= limit; i++){
if(i <= lastPage){
replacement += (i == currentPage) ? ('' + i + '') : ('' + i + '');
}
}
}
else if(currentPage >= lastPage - (Math.floor(limit/2))){
replacement += '<<First. . .';
for(var i = lastPage - limit + 1; i <= lastPage; i++){
if(i <= lastPage){
replacement += (i == currentPage) ? ('' + i + '') : ('' + i + '');
}
}
}
if((currentPage + (Math.floor(limit/2))) < lastPage){
replacement += '. . .';
}
replacement += (currentPage == lastPage) ? 'Last>>' : 'Last>>';
$('article:nth-last-child(' + ((currentPage == lastPage) ? 1 : 2) + ') .entry-injection div').html(replacement);
alert("ok!");
});
function getLastPage(nextPage) {
var result = null;
var scriptUrl = '/?page=' + nextPage;
$.ajax({
url: scriptUrl,
type: 'get',
dataType: 'html',
async: false,
success: function(data) {
result = (data.indexOf('article-index-1') != -1) ? getLastPage(nextPage + 1) : (nextPage - 1);
}
});
return result;
alert("I am an alert box!");
}