﻿$(document).ready(function() {
    if (window.location.href.toLowerCase().indexOf("article=") == -1) {
        getArticleList();

        $('#btnNext').click(function() {
            var page = Number($('#hdnPage').attr("value"));
            if (page <= Number($('#hdnTotalPages').attr("value"))) {
                $('#hdnPage').val(page + 1);
                getArticleList();
            }
        });

        $('#btnPrevious').click(function() {
            var page = Number($('#hdnPage').attr("value"));
            if (page > 0) {
                $('#hdnPage').val(page - 1);
                getArticleList();
            }
        });
    }
});

/* Get a list of articles */
function getArticleList() {
    var page = $('#hdnPage').attr("value");
    var size = 20;
    var totalPages;

    $('#Container').toggleClass('Loading');
    
    $.ajax({
        cache: false,
        type: "POST",
        url: "Webservices/ArticleService.asmx/GetArticles",
        data: "{'page': " + page + ", 'size': " + size + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        proccessData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            ajaxError(XMLHttpRequest, textStatus, errorThrown);
        },
        success: function(json, success) {
            json = eval(json.d);
            $('#Container').setTemplate($("#tmpArticleList").html());
            $('#Container').processTemplate(json);
            $("#Container").corner("keep bottom");
            $.ajax({
                cache: false,
                type: "POST",
                url: "Webservices/ArticleService.asmx/GetRecordCount",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                proccessData: false,
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    ajaxError(XMLHttpRequest, textStatus, errorThrown);
                },
                success: function(json, success) {
                    totalPages = Math.ceil(json.d / size);
                    $('#hdnTotalPages').val(totalPages);
                    $('#spanCount').text(page + " of " + totalPages.toString() + " pages");
                    $('#Container').toggleClass('Loading');
                    if (totalPages == 0)
                        $('#divArticleList').hide();
                    else
                        $('#divArticleList').show();
                    if (totalPages == 1)
                        $('#spanCount').hide();
                    else
                        $('#spanCount').show();
                    if (page == 1)
                        $('#btnPrevious').hide();
                    else
                        $('#btnPrevious').show();
                    if (page == totalPages)
                        $('#btnNext').hide();
                    else
                        $('#btnNext').show();
                }
            });
        }
    });
}