﻿$(function() {
    /*********全局变量***********/
    $("#loading").ajaxStart(function() {
        $(this).show();
    });

    $("#loading").ajaxStop(function() {
        $(this).hide();
    });



    var newtype = "law";
    var strtype = $("#hdTpye").val();
    var strsort = $("#hdSort").val();
    var hdSortName = $("#hdSortName").val();
    getCount(strtype, strsort);
    var url;
    (function() {
        var url;
        InitUrl = function(i) {
            if (i) {
                url = i;
            }
            return {
                Get: get1,
                Set: set1
            };
        };
        function get1() {
            return url;
        }
        function set1(i) {
            if (i) {
                url = i;
            }
        }
    })();

    var pageInfo;

    (function() {
        var pageInfo;
        InitPageInfo = function(i) {
            if (i) {
                pageInfo = eval('(' + i + ')');
            }
            return {
                Get: get1,
                Set: set1
            };
        };
        function get1() {
            return pageInfo;
        }
        function set1(i) {
            if (i) {
                pageInfo = eval('(' + i + ')');
            }
        }
    })();

    $("#aaLaw").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("law", "");
        paging.redirectTo(1, true);
    });
    $("#aDr").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("Dr", "");
        paging.redirectTo(1, true);
    });
    $("#aQua").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("Qua", "");
        paging.redirectTo(1, true);
    });
    $("#aInsPic").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("InsPic", "");
        paging.redirectTo(1, true);
    });
    $("#aInsOper").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("InsOper", "");
        paging.redirectTo(1, true);
    });
    $("#aMan").click(function() {
        $("#ddlSort").val("all");
        hdSortName = "";
        $("#tbName").val("");
        strsort = "";
        getCount("Man", "");
        paging.redirectTo(1, true);
    });

    $("#btnSearch").click(function() {
        hdSortName = "";
        if ($("#ddlSort").val() == "all") {
            alert("请选择类别！");
            return;
        }
        strsort = "";
        $("#lbSortName").html("知识库");
        $("#lbNew").html("知识库");
        getCount($("#ddlSort").val());
        paging.redirectTo(1, true);
    });

    var totalCount;
    (function() {
        var totalCount;
        InitTotalCount = function(i) {
            if (i) {
                totalCount = i;
            }
            return {
                Get: get1,
                Set: set1
            };
        };
        function get1() {
            return totalCount;
        }
        function set1(i) {
            if (i) {
                totalCount = i;
            }
        }
    })();

    /******** 初始化全局变量******/

    (function() {
        url = InitUrl($("#hfSiteUrl").val());
        try {
            pageInfo = InitPageInfo($("#hfPageInfo").val());
        } catch (e) {
            alert(e);
        }
        totalCount = InitPageInfo($("#hfTotalCount").val());


        //  $("#hfSiteUrl,#hfPageInfo,#hfJson,#hfTotalCount").remove();
        newtype = "law";
    })();

    function changeDiv() {
        //alert($(this).attr("title"));
        if ($(this).attr("title") == "展开全文") {
            $(this).attr("title", "收起全文");
            $(this).attr("src", "/Content/Acknowledge/Img/arrow1.gif");
            $(this).parent().next().children().next().css("display", "none");
            $(this).parent().next().children().next().next().css("display", "");
        } else if ($(this).attr("title") == "收起全文") {
            $(this).attr("title", "展开全文");
            $(this).attr("src", "/Content/Acknowledge/Img/arrow0.gif");
            $(this).parent().next().children().next().css("display", "");
            $(this).parent().next().children().next().next().css("display", "none");
        }
    }


    (function() {
        $("#sortList").find("a").click(showSort);
    })();

    function showSort() {
        hdSortName = "";
        $("#ddlSort").val("all");
        $("#tbName").val("");
        $("#lbNew").html($(this).parent().parent().children().children().html());
        if ($(this).attr("id") != "aaLaw" && $(this).parent().parent().children().children().attr("id") == "aaLaw") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("law", strsort);
        }
        if ($(this).attr("id") != "aDr" && $(this).parent().parent().children().children().attr("id") == "aDr") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("Dr", strsort);
        }
        if ($(this).attr("id") != "aQua" && $(this).parent().parent().children().children().attr("id") == "aQua") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("Qua", strsort);
        }
        if ($(this).attr("id") != "aInsPic" && $(this).parent().parent().children().children().attr("id") == "aInsPic") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("InsPic", strsort);
        }
        if ($(this).attr("id") != "aInsOper" && $(this).parent().parent().children().children().attr("id") == "aInsOper") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("InsOper", strsort);
        }
        if ($(this).attr("id") != "aMan" && $(this).parent().parent().children().children().attr("id") == "aMan") {
            $("#lbSortName").html($(this).html());
            strsort = $(this).attr("id");
            getCount("Man", strsort);
        }
        paging.redirectTo(1, true);
    }


    var loading = false;

    /***********分页*************/
    function accountPagingChanging(curPage, newPage) {
        if (loading) {
            return false;
        }
        loading = true;
        return true;
    }

    var paging = cy.csts.paging({
        id: 'Ack-paging-bottom',
        totalRecords: totalCount.Get(),
        pageSize: pageInfo.Get().PageSize,
        curPage: pageInfo.Get().CurrentPage,
        pageChanging: accountPagingChanging,
        pageChanged: function(newPageNum) {
            getAckPaged(newPageNum, strtype, strsort);
        }
    });



    function stripHtml(html) {
        if (typeof html !== 'string') {
            return '';
        }

        var reg = /<\/?[^<>]+>/g;
        return html.replace(reg, "");
    }

    function getAckPaged(newPageNum, Type, sort) {
        $("#lbCurPage").html(newPageNum);
        var pSize = pageInfo.Get().PageSize;
        var TotlereCount = parseInt($("#lbTotleRecord").html());
        var totlePage;
        if (TotlereCount % pSize == 0) {
            totlePage = parseInt(TotlereCount / pSize);
        }
        else {
            totlePage = parseInt(TotlereCount / pSize)+1;
         }
        
        $("#lbToTlePage").html(totlePage);
        loading = true;
        var html = '<h3 class="clearfix"><a target="_blank" href="AckDetail.aspx?id=_Id_&type=_Type_">_Title_</a><img title="展开全文" alt="" src="/Content/Acknowledge/Img/arrow0.gif" /></h3>' +
        '<div class="clearfix"><div class="from"><span style="color:#1D7591">_User_</span>  <span style="color:Gray">_Time_</span></div><div id="divSim" style="text-indent: 24px;">_ContentSim_</div><div id="divMore" style="display:none; text-indent: 24px;">_ContentMore_</div></div><div style="height:20px"></div>';
        var info = pageInfo.Get();
        info.CurrentPage = newPageNum;
        $("#lbAckList").empty();
        try {
            $.ajax({
                type: 'POST',
                url: url.Get() + '/Ajax/Acknowledge/GetAllAck.ashx',
                data: 'pageinfo=' + $.toJSON(info) + '&type=' + Type + '&Sort=' + sort + '&name=' + $("#tbName").val(),
                dataType: 'json',
                success: function(data, textStatus) {
                    if (data && data.success) {
                        for (var i = 0; data.list[i]; i++) {
                            (function(item) {
                                var s = html.replace('_Id_', unescape(item.id));
                                var c = item.Content;
                                //alert(c.length);
                                s = s.replace("_Type_", unescape(item.type));
                                s = s.replace("_ContentSim_", stripHtml(c).toString().substring(0, 400));
                                //;
                                s = s.replace("_ContentMore_", c);
                                s = s.replace("_Title_", unescape(item.Title));
                                s = s.replace("_User_", unescape(item.User));
                                s = s.replace("_Time_", unescape(item.Time));
                                $(s).appendTo("#lbAckList");
                            })(data.list[i]);
                            $("#AckList").find("img").click(changeDiv);
                        }
                    }
                    loading = false;
                }
            });
        } catch (e) {
            alert(e);
        }
    }

    function getCount(type, sort) {
        $("#law").attr("style", "display:none");
        $("#Dr").attr("style", "display:none");
        $("#Qua").attr("style", "display:none");
        $("#InsPic").attr("style", "display:none");
        $("#InsOper").attr("style", "display:none");
        $("#Man").attr("style", "display:none");
        $("#hfTotalCount").val("");
        if (type == "law") {
            if (sort == "") {
                $("#lbSortName").html("法律法规");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("法律法规");
        } else if (type == "Dr") {
            if (sort == "") {
                $("#lbSortName").html("检测标准");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("检测标准");
        } else if (type == "Qua") {
            if (sort == "") {
                $("#lbSortName").html("质量控制");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("质量控制");
        } else if (type == "InsPic") {
            if (sort == "") {
                $("#lbSortName").html("仪器图谱");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("仪器图谱");
        } else if (type == "InsOper") {
            if (sort == "") {
                $("#lbSortName").html("仪器操作");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("仪器操作");
        } else if (type == "Man") {
            if (sort == "") {
                $("#lbSortName").html("厂商信息");
            }
            if (hdSortName != "") {
                $("#lbSortName").html(hdSortName);
            }
            $("#lbNew").html("厂商信息");
        }
        document.getElementById(type).style.display = "";
        try {
            $.ajax({
                type: 'POST',
                url: '/Ajax/Acknowledge/GetCount.ashx',
                data: '&type=' + type + '&Sort=' + sort + '&name=' + $("#tbName").val(),
                dataType: 'json',
                success: function(data, textStatus) {
                    if (data.success) {
                        $("#hfTotalCount").val(data.count);
                        strtype = type;
                        //alert(document.getElementById(type));
                        $("#lbTotleRecord").html(data.count);

                        newtype = type;
                        paging.setTotalRecords(data.count, false);
                    }
                }
            });
        } catch (e) {
            alert(e);
        }
    }

    function showLawSort(type, sort) {
        getCount(type, sort);
    }


    //    (function() {
    //        getCount(strtype, showSort);
    //    })();
});
