﻿$(function () {
    pageid = parseInt($("#ctl00_ContentPlaceHolder2_hfPageID").val());
    prevpageid = parseInt($("#ctl00_ContentPlaceHolder2_hfPrevPageID").val());
    maxid = parseInt($("#ctl00_ContentPlaceHolder2_hfMaxID").val());
    if ($("#content .entry").length == 5 && pageid < maxid) {
        $("#ctl00_ContentPlaceHolder2_hfPageID").val(maxid);
        $("#ctl00_ContentPlaceHolder2_hfPrevPageID").val(0);
        LoadPage(maxid, 0)
    }
    $(window).scroll(function () {
        if (($(window).scrollTop() == $(document).height() - $(window).height() || $(window).scrollTop() + 1 == $(document).height() - $(window).height()) && $("#ctl00_ContentPlaceHolder2_pnlMore")[0]) {
            LoadPage(parseInt($("#ctl00_ContentPlaceHolder2_hfPageID").val()), parseInt($("#ctl00_ContentPlaceHolder2_hfPrevPageID").val()));
        }
    });
});
function LoadPage(pageid, prevpageid) {
    $("#down-arrow").css("display", "none");
    if (prevpageid == 0) {
        LoadNextPage(pageid);
        $("#ctl00_ContentPlaceHolder2_hfPrevPageID").val(pageid);
    }
    else if (prevpageid != pageid) {
        LoadNextPage(pageid);
        $("#ctl00_ContentPlaceHolder2_hfPrevPageID").val(pageid);
    }

}
function CheckForNewPage() {
    if ($("#ctl00_ContentPlaceHolder2_pnlMore")[0]) {
        $("#down-arrow").css("display", "none");
        pageid = parseInt($("#ctl00_ContentPlaceHolder2_hfPageID").val());
        prevpageid = parseInt($("#ctl00_ContentPlaceHolder2_hfPrevPageID").val());
        if (prevpageid == 0) {
            LoadNextPage(pageid);
            $("#ctl00_ContentPlaceHolder2_hfPrevPageID").val(pageid);
        }
        else if (prevpageid != pageid) {
            LoadNextPage(pageid);
            $("#ctl00_ContentPlaceHolder2_hfPrevPageID").val(pageid);
        }
    }

}
function LoadNextPage(pageid) {
    HideShowLoading(1);
    $.ajax({
        type: "POST",
        url: "/default.aspx/GetPostContent",
        data: "{'pageid':'" + pageid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (item) {
            if (item.d == null)
                $(".panel-more").html("<div class=\"timeout\">Oturum süresi doldu, lütfen sayfayı yenileyiniz!</div>");
            if (item.d.length > 0)
                BuildListView(item);
            else
                HideShowLoading(2);
        }
    });
}
function BuildListView(list) {
    var divcontent = "";
    var k;
    var id;
    for (var post in list) {
        k = list[post].length;
        id = list[post][k - 1].PostID;
        $("#ctl00_ContentPlaceHolder2_hfPageID").val(id);
        var items = "";
        for (var i = 0; i < k; i++) {
            divcontent = "";
            divcontent += " <div class=\"entry\"><div class=\"entrytitle\"><h2>";
            divcontent += "<a href=\"" + list[post][i].PostLink + "\" rel=\"\" title=\"" + list[post][i].Title + " içeriğini görüntüle\">";
            divcontent += list[post][i].Title + "</a></h2><div class=\"entrymeta\"><div class=\"postinfo\"><span class=\"postedby\">";
            divcontent += list[post][i].Author + " | " + list[post][i].DateCreatedText + " </span><span class=\"filedto\">Kategori ";
            divcontent += "<span class=\"span\" id=\"sppostcategory\">" + list[post][i].PostCategoryText + "</span></span>";
            divcontent += "<a href=\"" + list[post][i].PostLink + "#comments\" rel=\"\" title=\"yorumlar\">";
            divcontent += "<span class=\"commentslink\"><span id=\"spcommentcount\">" + list[post][i].CommentCountText + "</span>Yorum";
            divcontent += "</span></a></div><div class=\"tagslink\"><img src=\"/i/theme/tags.png\" alt=\"\" border=\"0\" /> Etiketler <span id=\"spposttag\">" + list[post][i].PostTagText + "</span>";
            divcontent += "</div></div><div class=\"thumb\"><a href=\"" + list[post][i].PostLink + "\" rel=\"\"><span id=\"spimage\">";
            divcontent += list[post][i].ImageText + "</span></a></div></div><div class=\"desclink\"><p>";
            divcontent += "<a href=\"Content.aspx?p=" + list[post][i].PostID + "\" rel=\"\" >" + list[post][i].Description + "</a></p>";
            divcontent += "<div class=\"deepbox\"><div class=\"goonreading\"><a href=" + list[post][i].PostLink + "><img src=\"/i/theme/goonreading.png\" border=\"0\" alt=\"Devamını Oku\" /></a></div></div><div style=\"height:5px;\"></div></div></div>";
            items += divcontent;
        }
    }
    $("#listcontainer").append(items);
    HideShowLoading(0);
    if (id < 2 || k < 5)
        HideShowLoading(2);
    $("#down-arrow").css("display", "block");
};
function HideShowLoading(act) {
    if (act == 1) $("#more").css("display", "block");
    else if (act == 0) $("#more").css("display", "none");
    else if (act == 2) $("#ctl00_ContentPlaceHolder2_pnlMore").remove();
};

