//jQuery("#replyType option:selected").text() + jQuery("#replyType option:selected").val();
//var editNum = $(':hidden[name=_EditNum]').val();
//$('input[name=_DelFlag' + delarticleid + ']').attr('value', 1);
function _dlgcall(op) {
    //
}
function showitems(op) {

    op = $(':hidden[name=OpFrom]').val();
    $('input[name=OpFrom]').attr('value', op);
    var _htmTitle = "内容素材库";
    var _htm = "<div class='datapage'><div><div>";
    var TotalPage = 0;
    var PageIndex = $(':hidden[name=PageIndex]').val();
    var MaterialType = jQuery("#replyType option:selected").val();
    $.ajax({
        url: "/Member/GetMaterials?_r" + Math.random(),
        type: "POST", dataType: "json",
        timeout: "10000",
        data: { "TableFlag": op,
            "MaterialType": MaterialType,
            "PageIndex": PageIndex,
            "PageSize": 4
        },
        success: function (data) {
            $.each(data, function (i, o) {
                TotalPage = o.TotalPage;
                if (MaterialType == "SINGNEWS" ||
                        MaterialType == "MULITENEWS" ||
                        MaterialType == "PHOTHO" ||
                        MaterialType == "LBS") {
                    _htm = _htm + "<div class='gpc02 prb'>" + "<ul class='listContainer ky_cy kc'>" +
                        "<li><img src='" + o.imageurl + "' width='308' height='120' /></li>" +
                        "<li class='pr10'>" + o.title + "</li>" +
                        "<span class='clickchoose choose02' ArtId='" + o.JsonId + "'></span>" +
                    "</ul>" + "</div>"
                }
                else if (MaterialType == "TEXT") {
                    _htm = _htm + "<div class='gpc02 prb'>" + "<ul class='listContainer ky_cy kc'>" +
                        "<li class='add_text'><div>纯文本素材</div></li>" +
                        "<li class='pr10' >标题：" + o.title + "</li>" +
                        "<span class='clickchoose choose02' ArtId='" + o.JsonId + "'></span>" +
                    "</ul>" + "</div>"
                }
                else if (MaterialType == "MUSIC") {
                    _htm = _htm + "<div class='gpc02 prb'>" + "<ul class='listContainer ky_cy kc'>" +
                        "<li><img src='/Content/images/yy.gif' width='308' height='120'></li>" +
                        "<li class='pr10'>" + o.title + "</li>" +
                        "<span class='clickchoose choose02' ArtId='" + o.JsonId + "'></span>" +
                    "</ul>" + "</div>"
                }
                else {
                    _htm = _htm + "<div class='gpc02 prb'>" + "<ul class='listContainer ky_cy kc'>" +
                        "<li><img src='" + o.imageurl + "' width='308' height='120' /></li>" +
                        "<li class='pr10'>" + o.title + "</li>" +
                        "<span class='clickchoose choose02' ArtId='" + o.JsonId + "'></span>" +
                    "</ul>" + "</div>"
                }
            });
            _htm = _htm + "</div><div class='clear'></div><ul class='page02'>"
            $('input[name=PageCount]').attr('value', TotalPage);
            if (TotalPage > 1) {
                _htm = _htm + "<li class='p_up'><a href='javascript:void(0)' class='prevPage'>上一页</a></li>";
                //                for (var i = 1; i <= TotalPage; i++) {
                //                    if (i == PageIndex) {
                //                        _htm = _htm + "<li class='p_num02'><strong>" + i + "</strong></a></li>"
                //                    }
                //                    else {
                //                        _htm = _htm + "<li class='p_num'><a href='javascript:void(0)'>" + i + "</a></li>"
                //                    }
                //                }
                if (TotalPage <= 5) {
                    for (var i = 1; i <= TotalPage; i++) {
                        if (i == PageIndex) {
                            _htm = _htm + "<li class='p_num02'><strong>" + i + "</strong></a></li>"
                        }
                        else {
                            _htm = _htm + "<li class='p_num'><a href='javascript:void(0)'>" + i + "</a></li>"
                        }
                    }
                } else {
                    var startPage = parseInt(PageIndex) - 3;
                    var endPage = parseInt(PageIndex) + 3;
                    var startFlag = 1;
                    var endFlag = 1;
                    if (startPage < 1) {
                        var addNum = 1 - startPage;
                        startPage = 1;
                        endPage = endPage + addNum;
                        startFlag = 0;
                    }
                    if (endPage > TotalPage) {
                        var subNum = endPage - TotalPage;
                        startPage = startPage - subNum + 1;
                        endPage = TotalPage;
                        endFlag = 0;
                    }
                    for (var i = startPage; i <= endPage; i++) {
                        if (i > TotalPage) {
                            break;
                        }
                        if (startFlag == 1 && i == startPage) {
                            _htm = _htm + "<li class='p_num ___pum' attrPageIndex='" + i + "'><a href='javascript:void(0)'>...</a></li>"
                        }
                        else if (i == PageIndex) {
                            _htm = _htm + "<li class='p_num02'><strong>" + i + "</strong></a></li>"
                        }
                        else if (endFlag == 1 && i == endPage) {
                            _htm = _htm + "<li class='p_num ___pum' attrPageIndex='" + i + "'><a href='javascript:void(0)'>...</a></li>"
                        }
                        else {
                            _htm = _htm + "<li class='p_num'><a href='javascript:void(0)'>" + i + "</a></li>"
                        }
                    }
                }

                _htm = _htm + "<li class='p_up'><a href='javascript:void(0)' class='nextPage'>下一页</a></li>" +
                    "<li class='p_up'>" + PageIndex + "/" + TotalPage + "</li>";
            }
            _htm = _htm + "</ul><p class='confirm'><input type='button' value='确认' class='btnEnter bottong' /></p></div></div>";

            ymPrompt.win(_htm, 955, 520, _htmTitle, _dlgcall);

            //choose02 未选中 choose 已经选中
        }
    });
}

$(".prevPage").live("click", function () {
    var PageIndex = $(':hidden[name=PageIndex]').val();
    if (PageIndex == 1) {
        alert("没有上一页了");
        return;
    }
    PageIndex = PageIndex - 1;
    $('input[name=PageIndex]').attr('value', PageIndex);
    var _op = $(':hidden[name=OpFrom]').val();
    $(".datapage").remove();
    window.parent.ymPrompt.doHandler('ok', true);
    showitems(_op);
});

$(".nextPage").live("click", function () {
    var PageIndex = $(':hidden[name=PageIndex]').val();
    var TotalPage = $(':hidden[name=PageCount]').val();
    if (PageIndex == TotalPage) {
        alert("没有下一页了");
        return;
    }

    PageIndex = parseInt(PageIndex) + 1;
    $('input[name=PageIndex]').attr('value', PageIndex);
    var _op = $(':hidden[name=OpFrom]').val();
    $(".datapage").remove();
    window.parent.ymPrompt.doHandler('ok', true);
    showitems(_op);
});

$(".page02").find(".p_num").live("click", function () {
    $('input[name=PageIndex]').attr('value', $(this).find("a").html());
    var _op = $(':hidden[name=OpFrom]').val();
    $(".datapage").remove();
    window.parent.ymPrompt.doHandler('ok', true);
    showitems(_op);
});

$(".page02").find(".___pum").live("click", function () {
    $('input[name=PageIndex]').attr('value', $(this).attr("attrPageIndex"));
    var _op = $(':hidden[name=OpFrom]').val();
    $(".datapage").remove();
    window.parent.ymPrompt.doHandler('ok', true);
    showitems(_op);
});

$(".listContainer").live("click", function () {
    if ($(this).find(".clickchoose").hasClass("choose02")) {
        $(".listContainer").each(function (i) {
            var arrowspan = $(".listContainer").eq(i).find(".clickchoose");
            if (arrowspan.hasClass("choose")) {
                arrowspan.addClass("choose02");
                arrowspan.removeClass("choose");
            }
        });
        $(this).find(".clickchoose").removeClass("choose02");
        $(this).find(".clickchoose").addClass("choose");

        $('input[name=ChooseArtId]').attr('value', $(this).find(".clickchoose").attr("ArtId"));
        $(".CurrentCont").html("");
        $(".CurrentCont").append("<li>" + $(this).find("li").eq(0).html() + "</li>");
        $(".CurrentCont").append("<li>" + $(this).find("li").eq(1).html() + "</li>");
    }
    else {
        $(this).find(".clickchoose").removeClass("choose");
        $(this).find(".clickchoose").addClass("choose02");
        $('input[name=ChooseArtId]').attr('value', 0);
        $(".CurrentCont").html("");
    }
});

$(".btnEnter").live("click", function () {
    var ChooseArtId = $(':hidden[name=ChooseArtId]').val();
    if (ChooseArtId <= 0) {
        _alert("请选择一个内容");
    }
    else {
        window.parent.ymPrompt.doHandler('ok', true);
    }
});