﻿function updatePage(thisPage) {
    //hide all pages and show only the selected one
    var nwHeight = $("#page_" + thisPage).height();
    $(".paginator").hide();
    $("div#loader").show().animate({
        height: nwHeight
    }, 900);
    $(this).oneTime(1000, function() {
        $("div#loader").stop().hide();
        $("#page_" + thisPage).fadeIn(300);
        //$('body').scrollTo($("div#cntReacties"), { duration: 0, axis: 'y' });
        window.location.hash = '#page' + thisPage;
    });

    $("#navGeneralMenu").animate({ opacity: 1.0 }, 500);

    //update html and click events
    updatePaginator(thisPage);

    //update url
    //document.location = "#reacties?page=" + thisPage;
    //window.location.hash = '#bottom';
    
}

function updatePaginator(page) {
    //get nr of pages
    var nrPages = ($(".paginator").size());
    var htmlPages = "";

    //update paginator
    for (i = 1; i <= nrPages; i++) {
        if (i == page)
            htmlPages += "<li class=\"current\">" + i + "</li>";
        else
            htmlPages += "<li><a href=\"#\" style=\"position:static;\">" + i + "</a></li>";
    }

    //replace html
    $("#jQueryPaginator").find("ul").html(htmlPages);

    //handle previous and next
    var nrOfPages = ($(".paginator").size());
    if (nrOfPages > 1) {
        if (page == 1) {
            $("#jQueryPaginator").find("span.prevPage").html("&laquo; vorige");
        }
        else {
            $("#jQueryPaginator").find("span.prevPage").html("<a href=\"#\" id=\"previousPage\" title=\"vorige\">&laquo; vorige</a>");
            $("#previousPage").live("click",function() {
                updatePage(parseInt($("#jQueryPaginator").find("li.current").text()) - 1);
                return false;
            });
        }
        if (page == nrOfPages) {
            $("#jQueryPaginator").find("span.nextPage").html("volgende &raquo;");
        }
        else {
            $("#jQueryPaginator").find("span.nextPage").html("<a href=\"#\" id=\"nextPage\" title=\"volgende\" style=\"position:static;\">volgende &raquo;</a>");
            $("#nextPage").live("click",function() {
                updatePage(parseInt($("#jQueryPaginator").find("li.current").text()) + 1);
                return false;
            });
        }
    }
    else {
        $("#jQueryPaginator").find("span.prevPage").html("");
        $("#jQueryPaginator").find("span.nextPage").html("");
    }

    //add click function for nrs.
    $("#jQueryPaginator").find("li").find("a").live("click",function() {
        var thisPage = $(this).text();
        updatePage(thisPage);
        return false;
    });
}
