function PlugInit(type) {
    switch (type) {
        case "gathertest":
            var State = '<div id="load"><div id="loading"></div></div><div id="tong_ji" class="green_font">'
                + '当前 <span id="Span1" style="color: Red;">0</span> 域名匹配，计 <span style="color: Red;" '
                + ' id="Span2">0</span> 列表页面, 执行共获取 <span style="color: Red;" id="Span3">0</span> 待采集地址'
                + '</div><div id="jieguo">正在等待采集...</div>';
            $("#State").html(State);
            $("#BtnDef").click(function () { BtnPlugClickTest(); });
            break;
        case "gatherexe":
            var State = '<div id="load"><div id="loading"></div></div><div id="tong_ji" class="green_font">'
            + '当前任务 <span id="Span3" style="color: Red;">----</span> , 已执行 <span id="Span1" style="color: Red;">0</span> 采集，'
            + ' 共 <span style="color: Red;" id="Span2">0</span> 采集地址'
            + '</div><div id="jieguo">正在等待采集...</div>';
            $("#State").html(State);
            $("#BtnDef").click(function () { BtnPlugClickExe(); });
            break;
        default:
            break;
    }
}

function BtnPlugClickTest() {
    CreateOninit();
    var id = Number($("#GatherID").val());
    if (id > 0) {
        var weburl = $("#Host").val();
        if (weburl == "") {
            $.ajax({
                type: "post",
                dataType: "html",
                url: "dataajax.aspx?type=gather&ctype=0&r=" + Math.random(),
                data: { id: id },
                beforeSend: function (XMLHttpRequest) {
                    CmsData.Common.BtnDisabled(true);
                    $("#jieguo").html("正在统计采集网址列表...");
                },
                success: function (data) {
                    if (data != "") {
                        var splist = data.split(',');
                        var sum = splist.length;
                        $("#Span1").html(sum);
                        DataTest(id, splist, 0, sum);
                    } else {
                        $("#Result").prepend(data);
                    }

                    CmsData.Common.BtnDisabled(false);
                }
            });
        } else {
            $.ajax({
                type: "post",
                url: "dataajax.aspx?type=gather&ctype=2&r=" + Math.random(),
                data: { id: id, url: weburl },
                beforeSend: function (XMLHttpRequest) {
                    $("#Result").append("<strong>" + weburl + "</strong>" + "<br />");
                    $("#Span1").html("1");
                    $("#Span2").html("1");
                    $("#Span3").html("1");
                },
                success: function (data) {
                    document.getElementById("loading").style.width = "100%";
                    $("#Result").append(data);
                },
                error: function () { }
            });
        }
    } else {
        CmsData.Common.MsgFailed("未选中采集任务...");
    }
}

function DataTest(id, arr, start, len) {
    if (start < len) {
        var ht = "";
        $.ajax({
            type: "post",
            url: "dataajax.aspx?type=gather&ctype=1&r=" + Math.random(),
            data: { id: id, url: arr[start] },
            beforeSend: function (XMLHttpRequest) {
                ht += "<strong>" + arr[start] + "</strong>" + "<br />";
                $("#Span2").html(Number($("#Span2").html()) + (1));
                document.getElementById("loading").style.width = ((start + 1) / len * 100) + "%";
            },
            success: function (data) {
                start = start + 1;

                var sp = data.split(',');
                var j = 0;
                for (var i = 0; i < sp.length; i++) {
                    if (sp[i] != "") {
                        ht += "├─&nbsp;&nbsp;" + sp[i] + "<br />";
                        j++;
                    }
                }
                $("#Span3").html((Number($("#Span3").html()) + j));
                $("#Result").append(ht);

                if (start < len)
                    DataTest(id, arr, start, len);
            },
            error: function () {
                $("#Result").prepend("Error:" + "<span style='color: Red;' >" + arr[start] + "</span>");
                start = start + 1;
                if (start < len)
                    GatherUrlExe(id, arr, start, len);
            }
        });
    }
}


function BtnPlugClickExe() {
    CreateOninit();
    $("#Span3").html($("#GatherID").find("option:selected").text());
    var id = Number($("#GatherID").val());
    if (id > 0) {
        $.ajax({
            type: "post",
            url: "dataajax.aspx?type=gather&ctype=0&r=" + Math.random(),
            data: { id: id },
            beforeSend: function (XMLHttpRequest) {
                CmsData.Common.BtnDisabled(true);
                $("#jieguo").html("正在统计采集网址列表...");
            },
            success: function (data) {
                if (data != "") {
                    var splist = data.split(',');
                    var sum = splist.length;
                    var urls = new Array();
                    DataExe(urls, id, splist, 0, sum);

                    var pageSum = urls.length;

                    if (pageSum > 0) {
                        var dl = Number($("#Thread").val());

                        $("#Span2").html(pageSum);
                        if (pageSum > dl) {
                            var p = pageSum % dl;
                            var p1 = (pageSum - p) / dl;

                            for (var i = 0; i < dl; i++) {
                                CreateStart(urls, id, i * p1, (i + 1) * p1, pageSum);
                            }
                            if (p > 0)
                                CreateStart(urls, id, pageSum - p, pageSum, pageSum);
                        } else {
                            CreateStart(urls, id, 0, pageSum, pageSum);
                        }
                    } else {
                        $("#Result").prepend("无可采集的地址，请检测任务正则规则" + "<br />");
                    }
                } else {
                    $("#Result").prepend(data);
                }
            }
        });
    } else {
        CmsData.Common.MsgFailed("未选中采集任务...");
    }
}

function DataExe(urls, id, arr, start, len) {
    if (start < len) {
        $.ajax({
            type: "post",
            url: "dataajax.aspx?type=gather&ctype=1&r=" + Math.random(),
            data: { id: id, url: arr[start] },
            async: false,
            beforeSend: function (XMLHttpRequest) { },
            success: function (data) {
                start = start + 1;
                var sp = data.split(',');
                if (sp.length > 0) {
                    for (var i = 0; i < sp.length; i++) {
                        if (sp[i] != undefined && sp[i] != "") {
                            urls.push(sp[i]);
                        }
                    }
                }
                if (start < len)
                    DataExe(urls, id, arr, start, len);
            },
            error: function () {
                $("#Result").prepend("Error: " + "<span style='color: Red;' >" + arr[start] + "</span><br />");
                start = start + 1;
                if (start < len)
                    DataExe(urls, id, arr, start, len);
            }
        });
    }
}

function CreateStart(sp, id, start, pageSum, al) {
    var url = sp[start];
    $.post("dataajax.aspx?type=gather&ctype=3&r=" + Math.random(), { id: id, url: url }, function (data) {
        start = start + 1;
        $("#Span1").html(Number($("#Span1").html()) + 1);
        document.getElementById("loading").style.width = parseFloat(Number($("#Span1").html()) / al * 100).toFixed(2) + "%";

        if (start < pageSum) {
            CreateStart(sp, id, start, pageSum, al);
        } else {
            if (Number($("#Span1").html()) == al) {
                $("#jieguo").html("所有页面生成结束...");
                CmsData.Common.BtnDisabled(false);
            }
        }

        if (CmsData.Common.IsSuccess(data) == false) {
            $("#Result").prepend(url + "<br />");
            $("#Result").prepend(data + "<br />");
        }
    }, "html");
}

//初始化状态
function CreateOninit() {
    $("#Span1").html("0");
    $("#Span2").html("0");
    $("#Span3").html("0");
    $("#jieguo").html("正在等待采集...");
    $("#Result").html("");
    document.getElementById("loading").style.width = "0%";
}
