/* This pagination plugin is made by puredesign */
/* puredesign.be                                */
$(function(){
    $entries = $(".blog-entry").size();
    $toShow  = 4;
    $curPage = 1;
    /* make first page visible */
    for($i=0;$i<$toShow;$i++){
        $(".blog-entry:eq("+$i+")").fadeIn();
    }
    $pages = Math.ceil($entries/$toShow);
    /* calculate pages */
    for($j=1;$j<=$pages;$j++){
        $(".pagination ul").append('<li><a href="javascript:void(0);" class="pgNr"><span>'+$j+'</span></a></li>');
    }
    /* set current page */
    $(".pagination-vorige").hide();
    $(".pagination ul li:eq(0) a span").attr("id","crPg");
    
    $(".pgNr").click(function(e){
        $(".blog-entry").hide();
        $(".pagination ul li a span").removeAttr("id");
        $toPage = parseInt($(this).text());
        f_check_buttons($toPage);
        $(".pagination ul li:eq("+($toPage-1)+") a span").attr("id","crPg");
        $endBlock = ($toPage*$toShow);
        $beginBlock = ($endBlock-$toShow);
        for($i=$beginBlock;$i<$endBlock;$i++){
            $(".blog-entry:eq("+$i+")").fadeIn();
        }
        $curPage = $toPage;
        e.preventDefault();
        return false;
    });
    $(".pagination-vorige").click(function(e){
        $(".blog-entry").hide();
        $(".pagination ul li a span").removeAttr("id");
        $toPage = parseInt($curPage-1);
        f_check_buttons($toPage);
        $(".pagination ul li:eq("+($toPage-1)+") a span").attr("id","crPg");
        $endBlock = ($toPage*$toShow);
        $beginBlock = ($endBlock-$toShow);
        for($i=$beginBlock;$i<$endBlock;$i++){
            $(".blog-entry:eq("+$i+")").fadeIn();
        }
        $curPage = $toPage;
        e.preventDefault();
        return false;
    });
    $(".pagination-volgende").click(function(e){
        $(".blog-entry").hide();
        $(".pagination ul li a span").removeAttr("id");
        $toPage = parseInt($curPage+1);
        f_check_buttons($toPage);
        $(".pagination ul li:eq("+($toPage-1)+") a span").attr("id","crPg");
        $endBlock = ($toPage*$toShow);
        $beginBlock = ($endBlock-$toShow);
        for($i=$beginBlock;$i<$endBlock;$i++){
            $(".blog-entry:eq("+$i+")").fadeIn();
        }
        $curPage = $toPage;
        e.preventDefault();
        return false;
    });
    $(".pagination-laatste").click(function(e){
        $(".blog-entry").hide();
        $(".pagination ul li a span").removeAttr("id");
        $toPage = parseInt($pages);
        f_check_buttons($toPage);
        $(".pagination ul li:eq("+($toPage-1)+") a span").attr("id","crPg");
        $endBlock = ($toPage*$toShow);
        $beginBlock = ($endBlock-$toShow);
        for($i=$beginBlock;$i<$endBlock;$i++){
            $(".blog-entry:eq("+$i+")").fadeIn();
        }
        $curPage = $toPage;
        e.preventDefault();
        return false;
    });
});
function f_check_buttons($goToPage){
    $(".pagination-vorige").show();
    $(".pagination-volgende").show();
    $(".pagination-laatste").show();
    if($goToPage == 1){
        $(".pagination-vorige").hide();
    }
    if($goToPage == $pages){
        $(".pagination-volgende").hide();
    }
}
