﻿/// <reference path="jquery-1.4.1.js" />

//--------------------------------------------------------
//
//      Post Pagination Renderer
//
//--------------------------------------------------------
var PostPaginationRenderer;
if (!PostPaginationRenderer) {
    PostPaginationRenderer = new Object();
}

PostPaginationRenderer.render = function (container) {
    var categoryId = container.attr("categoryId");
    var contentId = container.attr("contentId");
    var pageCount = container.attr("pageCount");
    var page = container.attr("page");

    var helper = PostPaginationRenderer.createHelper(null, categoryId, contentId);

    var cultureCode = "";

    $.ajax({
        type: "POST",
        url: "/ws/PostCategoryService.asmx/GetPaginationTemplate",
        dataType: "json",
        data: "",
        async: false,
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            cultureCode = data.d;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            culture = "";
        }
    });

    var templatePath = "/Template/Post/PostPagination" + cultureCode + ".htm";
    container.load(templatePath, function () {
        var html = container.html();
        html = html.replace(/categoryId/g, helper.postCategoryId);
        html = html.replace(/contentId/g, helper.contentId);

        container.html(html);

        $(helper.pageSelector).html(page);
        $(helper.inputSelector.replace("#", ".")).val(page);
        $(helper.pageCountSelector).html(pageCount);
    });

}

PostPaginationRenderer.createHelper = function (target, categoryId, contentId) {
    var helper = new Object();
    helper.contentId = contentId;
    helper.postCategoryId = categoryId;
    helper.targetSelector = "#pnlTarget" + categoryId;
    helper.inputSelector = "#inputPage" + categoryId;
    helper.pageSelector = ".page" + categoryId;
    helper.pageCountSelector = ".pageCount" + categoryId;
    helper.getPage = function () {
        return $(helper.pageSelector).val();
    }

    return helper;
}

PostPaginationRenderer.requestPage = function (helper, pageCommand) {
    var params = "{'contentId':" + helper.contentId + ", 'postCategoryId':" + helper.postCategoryId + ", 'pageCommand':'" + pageCommand + "'}";

    $.ajax({
        type: "POST",
        url: "/ws/PostCategoryService.asmx/GetPosts",
        dataType: "json",
        data: params,
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var result = JSON.parse(data.d);
            PostPaginationRenderer.onPaginationComplete(result, helper);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

PostPaginationRenderer.onPaginationComplete = function (result, helper) {
    $(helper.pageSelector).html(result.Page);
    $(helper.targetSelector).html(result.Html);
    $(helper.targetSelector).children("#pnlPagination").remove();

    $(helper.inputSelector.replace("#", ".")).val(result.Page);
}

//--------------------------------------------------------
//
//      Post Pagination Methods
//
//--------------------------------------------------------

$(document).ready(function () {
    AttachPostPagination();
});

function AttachPostPagination() {
    $(".postPagination").each(function (index) {
        PostPaginationRenderer.render($(this));
    });
}

function GoFirstPage(target, categoryId, contentId) {

    var helper = PostPaginationRenderer.createHelper(target, categoryId, contentId);
    PostPaginationRenderer.requestPage(helper, 'first');

    return false;
}

function GoLastPage(target, categoryId, contentId) {

    var helper = PostPaginationRenderer.createHelper(target, categoryId, contentId);
    PostPaginationRenderer.requestPage(helper, 'last');

    return false;
}

function GoPreviousPage(target, categoryId, contentId) {

    var helper = PostPaginationRenderer.createHelper(target, categoryId, contentId);
    var pageCommand = parseInt($(helper.inputSelector).val()) - 1;
    PostPaginationRenderer.requestPage(helper, pageCommand);

    return false;
}

function GoNextPage(target, categoryId, contentId) {

    var helper = PostPaginationRenderer.createHelper(target, categoryId, contentId);
    var pageCommand = parseInt($(helper.inputSelector).val()) + 1;
    PostPaginationRenderer.requestPage(helper, pageCommand);

    return false;
}

function GoPage(target, categoryId, contentId) {

    var helper = PostPaginationRenderer.createHelper(target, categoryId, contentId);
    var pageCommand = $(helper.inputSelector).val();
    PostPaginationRenderer.requestPage(helper, pageCommand);

    return false;
}

