
var m_LgId = "";//标签组ID
var m_LId = "";//标签ID
var m_JsonResult = [];//粉丝标签缓存
var m_groupname = "";//储存选中标签组的名称;
var m_tname = "";//储存选中标签的名称;
var m_frist = 1;//初始化界面时，第一个标签组展开（1为初次加载页面）
var m_isGroupSubmit = true;//判断标签组顺序是否提交过,提交过true，没提交过false
var isSelectIndex = -1;//移动标签时，当前所在标签的下标,用于设置改组展开状态
$(function () {
    $("#myPageAlert").hide();
    BindData();
    PageLoadTrees();
});

function BindData() {
    $(".loading").show();
    $(".loadingcover").show();
    manageObj.gettagtreelist();
}

function getcolor(objId) {
    $("#" + objId).colorpicker({
        fillcolor: true
    });

}

//获取标签树型结构内容回调方法
function AsyncGetTagTreeListResult(jsonTree) {
    $("#selectGroup").empty();//清空
    if (jsonTree.success) {
        m_JsonResult = jsonTree;
        CommonErgodic();//调用公共遍历方法
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonTree.error);
    }
    PageLoadTrees();
    if (m_TgId != "" && m_TgId != undefined) {
        $("#selectGroup").val(m_TgId);
    }
    $(".loadingcover").hide();
    $(".loading").hide();
}

//树形菜单高度设置
function PageLoadTrees() {
    $(".left-list-l").css({
        height: $(window).height() - 40
    });
    $(".talklist-m").css({
        height: $(window).height() - 100
    });
    $("#right").css({
        height: $(window).height() - 40
    });
    $("#left").css({
        height: $(window).height() - 40
    });
    initTree();
}

//选中标签组时
function SetEditGroupBind(value, name, type, source) {
    m_groupname = name;
    m_LgId = value;//标签组ID
    $("#txtGroupName").val(name);
    $("#spDeleteGroupName").html(name);
    $("#txtGroupName").focus();
    if (type == "1") {
        $("#cbxIsHC").prop("checked", true);
    } else {
        $("#cbxIsHC").prop("checked", false);
    }
    if (value.length > 0) {
        $("#DeleteTagGroup").show();

    } else {
        $("#DeleteTagGroup").hide();
    }
    //if (source == "0") {
    //    $("#cbxIsAll").prop("checked", true);
    //} else {
    //    $("#cbxIsAll").prop("checked", false);
    //}
    $("#divEditGroup").removeClass("tab-pane fade").addClass("tab-pane fade active in");//显示
    $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
    $("#liCreatTagsGroup").removeClass("active");
    $("#liCreateTag").removeClass("active");
}

//添加,修改标签组
function EditGroup() {
    var objtxtGroupName = $("#txtGroupName");//标签组名称
    var bIsHC = $("#cbxIsHC").is(":checked") ? 1 : 0;//1互斥，0正常
    //var bIsAll = $("#cbxIsAll").is(":checked") ? "0" : "";//0全局，空个人
    objtxtGroupName.val($.trim(Canceller(objtxtGroupName.val())));
    if (objtxtGroupName.val().length == 0) {
        ShowError("myPageAlert", "标签组名称不能为空！");
        objtxtGroupName.focus();
        return;
    }

    if (m_LgId.length == 0) {
        var number = 10001;
        if (m_JsonResult.result.list.length > 0) {
            //添加查重
            number = m_isGroupSubmit ? Number(m_JsonResult.result.list[m_JsonResult.result.list.length - 1].sort) + 1 : "";
            for (var i = 0; i < m_JsonResult.result.list.length; i++) {
                if ($.trim($(txtGroupName).val()) == m_JsonResult.result.list[i].tgname) {
                    ShowError("myPageAlert", "标签组名称不能重复！");
                    return;
                }
            }
        }
        var strParaJson = $.toJSON({
            tgname: objtxtGroupName.val(), type: bIsHC, sort: number
        });
        manageObj.addtaggroup(strParaJson);
    }
    else {
        //修改
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            if ($.trim($(txtGroupName).val()) == m_JsonResult.result.list[i].tgname && $.trim($(txtGroupName).val()) != m_groupname) {
                ShowError("myPageAlert", "标签组名称不能重复！");
                return;
            }
        }
        var strParaJson = $.toJSON({
            tgid: m_LgId, tgname: objtxtGroupName.val(), type: bIsHC,
        });
        manageObj.updatetaggroup(strParaJson);
    }
}

//显示删除标签组对话框
function ShowDeleteGroup() {
    if (m_LgId.length == 0) {
        return;
    }
    for (var i = 0; i < m_JsonResult.result.list.length; i++) {
        if (m_LgId == m_JsonResult.result.list[i].tgid) {
            if (m_JsonResult.result.list[i].tag.length > 0) {
                ShowError("myPageAlert", "此标签组下面存在标签，请先删除标签！");
                return;
            }
        }
    }
    $("#DeleteGroupModal").modal("show");
}

//提交删除部门
function CommitGroupDelete() {
    var strParaJson = $.toJSON({
        tgid: m_LgId,
    });
    manageObj.deletetaggroup(strParaJson);
}
//删除标签组
function AsyncDeleteTagGroupResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsHC").attr("checked", false);
        //$("#cbxIsAll").attr("checked", false);
        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}


//变更标签组结果回调方法
function AsyncUpdateTagGroupResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsHC").attr("checked", false);
        //$("#cbxIsAll").attr("checked", false);
        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
function AsyncAddTagGroupResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtGroupName").val("");
        $("#cbxIsHC").attr("checked", false);
        //$("#cbxIsAll").attr("checked", false);
        $("#DeleteGroupModal").modal("hide");
        $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

//选中标签时
function SetEditTagBind(value, name, color, lgid, zname) {
    m_TgId = lgid;
    m_tname = name;
    m_groupname = zname;//标签组
    var remark = decodeURIComponent(remark);
    closecolorpanel();
    m_LId = value;//标签组ID
    if (m_LId.length > 0) {
        $("#divTagOperBtn").show();
        $("#DeleteTag").show();
        $("#spDeleteTagName").html(name);
        $("#txtTagName").val(name);
        $("#txtTagColor").val(color);
        $("#txtTagColor").css("color", color);
        //$("#txtTagRemark").val(remark);
        $("#selectGroup").val(lgid);
    } else {
        $("#btnPersonSubmit").show();
        $("#DeleteTag").hide();
        $("#txtTagName").val("");
        $("#txtTagColor").val("");
        //$("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');
    }
    $("#txtTagName").focus();
    $("#divEditTag").removeClass("tab-pane fade").addClass("tab-pane fade active in");//显示
    $("#divEditGroup").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
    $("#liCreatTagsGroup").removeClass("active");
    $("#liCreateTag").removeClass("active");
}

//添加标签，修改标签
function EditTag() {
    closecolorpanel();
    var objtxtTagName = $("#txtTagName");
    var objtxtTagColor = $("#txtTagColor");
    var objselectGroup = $("#selectGroup");
    objtxtTagName.val($.trim(objtxtTagName.val().replace(/["]/g, '').replace(/[']/g, '')));
    objtxtTagColor.val($.trim(objtxtTagColor.val()));
    //objtxtTagRemark.val($.trim(objtxtTagRemark.val()));
    objselectGroup.val($.trim(objselectGroup.val()));
    if (objtxtTagName.val().length == 0) {
        ShowError("myPageAlert", "标签名称不能为空！");
        objtxtTagName.focus();
        return;
    }
    if (objtxtTagColor.val().length == 0) {
        ShowError("myPageAlert", "标签颜色不能为空！");
        objtxtTagColor.focus();
        return;
    }
    if (m_JsonResult.result.list.length == 0) {
        ShowError("myPageAlert", "没有标签组,请先设值标签组!");
        return;
    }
    if (objselectGroup.val().length < 1) {
        ShowError("myPageAlert", "请选择所属分组，若没有请先添加标签组！");
        return;
    }

    var number = "";//当前组内标签的sort值
    var TagGroup = $("#selectGroup").val();
    for (var i = 0; i < m_JsonResult.result.list.length; i++) {
        if (m_JsonResult.result.list[i].tgid == TagGroup) {
            for (var j = 0; j < m_JsonResult.result.list[i].tag.length; j++) {
                if (m_JsonResult.result.list[i].tag[j].sort != "" && m_JsonResult.result.list[i].tag[j].sort != undefined && m_JsonResult.result.list[i].tag[j].sort != null) {
                    number = Number(m_JsonResult.result.list[i].tag[m_JsonResult.result.list[i].tag.length - 1].sort) + 1;
                    break;
                }
            }
            break;
        }
    }

    var jsonResult;
    if (m_LId.length == 0) {
        //添加
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var taglist = m_JsonResult.result.list[i];
            if (taglist.tgid == $.trim($("#selectGroup").val())) {
                for (var j = 0; j < taglist.tag.length; j++) {
                    if ($.trim($("#txtTagName").val()) == taglist.tag[j].tname) {
                        ShowError("myPageAlert", "标签名称不可以重复！");
                        return;
                    }
                }
            }
        }

        var strParaJson = $.toJSON({
            tgid: objselectGroup.val(),
            tname: objtxtTagName.val(),
            color: objtxtTagColor.val(),
            sort: number
        });
        manageObj.addtag(strParaJson);
    }
    else {
        //修改
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var taglist = m_JsonResult.result.list[i];
            if ($("#selectGroup option:selected").text() == m_groupname && m_groupname == taglist.tgname) {
                //相同标签组时修改标签名称
                for (var j = 0; j < taglist.tag.length; j++) {
                    if ($.trim($("#txtTagName").val()) == taglist.tag[j].tname && $.trim($("#txtTagName").val()) != m_tname) {
                        ShowError("myPageAlert", "该标签名称已存在！");
                        return;
                    }
                }
            }
            else if ($("#selectGroup option:selected").text() != m_groupname && $("#selectGroup option:selected").text() == taglist.tgname) { //不同标签组时修改标签名称
                for (var j = 0; j < taglist.tag.length; j++) {
                    if ($.trim($("#txtTagName").val()) == taglist.tag[j].tname) {
                        ShowError("myPageAlert", "该标签名称已存在！");
                        return;
                    }
                }
            }
        }
        var strParaJson = $.toJSON({
            tid: m_LId,
            tgid: objselectGroup.val(),
            tname: objtxtTagName.val(),
            color: objtxtTagColor.val(),
            sort: number
        });
        manageObj.updatetag(strParaJson);
    }
}

//显示删除标签对话框
function ShowDeleteTag() {
    closecolorpanel();
    if (m_LId.length == 0) {
        return;
    }
    $("#DeleteTagModal").modal("show");
}

//提交删除部门
function CommitTagDelete() {
    var strParaJson = $.toJSON({
        tid: m_LId,
    });
    manageObj.deletetag(strParaJson);
}
function AsyncDeleteTagResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#txtTagColor").val("");
        //$("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');
        $("#DeleteTagModal").modal("hide");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}


function AsyncUpdateTagResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#txtTagColor").val("");
        //$("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');
        $("#DeleteTagModal").modal("hide");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}

function AsyncAddTagResult(jsonResult) {
    if (jsonResult.success) {
        $("#txtTagName").val("");
        $("#txtTagColor").val("");
        //$("#txtTagRemark").val("");
        $('#selectGroup option:eq(0)').attr('selected', 'selected');
        $("#DeleteTagModal").modal("hide");
        $("#divEditTag").removeClass("tab-pane fade active in").addClass("tab-pane fade");//隐藏
        ShowMsg("myPageAlert", "操作成功！");
        BindData();
    }
    else {
        //错误信息
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
    }
}
//查询按钮

function checkLabelOrRemarks() {
    var objselectGroup = $("#selectGroup");
    objselectGroup.empty();//清空
    var replaycondition = $.trim($("#txttag").val().replace(/["]/g, '').replace(/[']/g, ''));
    if (replaycondition.length < 1) {
        BindData();
    } else {
        var strTreeContent = "";//树型菜单内容
        strTreeContent += "<div class=\"input-group\"><input id=\"txttag\"  name=\"txttag\" type=\"text\"  value=\"" + replaycondition + "\" class=\"form-control input-txt-l\" placeholder=\"标签名称\"><span class=\"input-group-btn\"><button onclick=\"checkLabelOrRemarks()\" class=\"btn btn-default\" type=\"button\" style=\"border-radius:0\"><i class=\"glyphicon glyphicon-search\"></i></button></span><span class=\"input-group-btn\"><button class=\"btn btn-default\"  onclick=\"resetTag()\" type=\"button\"><i class=\"glyphicon glyphicon-trash\"></i></button></span></div>";
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            var groupname = "";
            var jsonItem = m_JsonResult.result.list[i];
            if (jsonItem != null) {
                for (var k = 0; k < jsonItem.tag.length; k++) {
                    var lablename = jsonItem.tag[k].tname;
                    //var lableremark = jsonItem.tag[k].remark;
                    if (lablename.indexOf(replaycondition) != -1) {
                        if (groupname != jsonItem.tgname) {
                            strTreeContent += "<ul class=\"yiji-m\">";
                            strTreeContent += "<li>";
                            strTreeContent += "<a href=\"#divEditDept\" class=\"inactive-m\" data-toggle=\"tab\" onclick=\"SetEditGroupBind('" + jsonItem.tgid + "','" + jsonItem.tgname + "','" + jsonItem.type + "','" + jsonItem.tick + "');\">" + jsonItem.tgname + "<span class=\"label label-info pull-right badge-color\"></span></a>";
                            strTreeContent += "<ul style=\"display: none;\">";
                            groupname = jsonItem.tgname;
                        }
                        var tagItem = jsonItem.tag[k];
                        strTreeContent += "<li><a href=\"#divEditPerson\" data-toggle=\"tab\" onclick=\"SetEditTagBind('" + tagItem.tid + "','" + tagItem.tname + "','" + tagItem.color + "','" + jsonItem.tgid + "','" + jsonItem.tgname + "');\"><span class=\"pull-left\"></span><span class=\"icon-circle-blank circle-tags-color\" style=\"color:" + tagItem.color + "\"> </span>" + tagItem.tname + "</a></li>";
                    }
                }
                strTreeContent += "</ul>";
                strTreeContent += "</li>";
                strTreeContent += "</ul>";
                //DDL标签组
                objselectGroup.append("<option value='" + jsonItem.tgid + "'>" + jsonItem.tgname + "</option>");
            }
        }
    }
    $("#divTree").html(strTreeContent);
    PageLoadTrees();
}

//清空按钮
function resetTag() {
    BindData();
    $(".loading").show();
    $(".historyzhezhao").show();
}
//关闭颜色框
function closecolorpanel() {
    $("#colorpanel").hide();
}
$(window).resize(function () {
    PageLoadTrees();
});

//遍历公共方法
function CommonErgodic() {

    var strTreeContent = "";//树型菜单内容
    strTreeContent += "<div class=\"input-group\"><input id=\"txttag\"  name=\"txttag\" type=\"text\" class=\"form-control input-txt-l\" placeholder=\"标签名称\"><span class=\"input-group-btn\"><button onclick=\"checkLabelOrRemarks()\" class=\"btn btn-default\" type=\"button\" style=\"border-radius:0\"><i class=\"glyphicon glyphicon-search\"></i></button></span><span class=\"input-group-btn\"><button class=\"btn btn-default\"  onclick=\"resetTag()\" type=\"button\"><i class=\"glyphicon glyphicon-trash\"></i></button></span></div>";
    var strDeptCss = "";
    for (var i = 0; i < m_JsonResult.result.list.length; i++) {

        //当前没有字段sort的时候，给默认值，解决老版本兼容问题
        if (m_JsonResult.result.list[i].sort == "" || m_JsonResult.result.list[i].sort == undefined || m_JsonResult.result.list[i].sort == null) {
            m_isGroupSubmit = false;//没提交过是false
        } else {
            m_isGroupSubmit = true;//提交过是true
        }
        //初次加载时，第一个标签组展开状态
        if (m_frist == 1) {
            strDeptCss = i == 0 ? "inactive-m inactives-m" : "inactive-m";
        } else {
            strDeptCss = "inactive-m";
        }

        var jsonItem = m_JsonResult.result.list[i];
        var strTagType = jsonItem.type == "1" ? "" : "";
        strTreeContent += "<ul class=\"yiji-m\">";
        strTreeContent += "<li>";
        strTreeContent += "<a id=\"group" + i + "\" href=\"#divEditDept\" class=\"" + strDeptCss + "\" data-toggle=\"tab\" onclick=\"SetEditGroupBind('" + jsonItem.tgid + "','" + jsonItem.tgname + "','" + jsonItem.type + "','" + jsonItem.tick + "');\"><span class=\"pull-left\" style=\"width:109px;\">" + jsonItem.tgname + strTagType + "</span><span  ></span><span  ></span> ";

        //判断组能否向上向下移动
        if (i != 0 && i != m_JsonResult.result.list.length - 1) {
            //所选分组内既不是第一个，也不是最后一个（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\" style=\"margin-right:5px;\" onclick=\"TagGroupTop(" + i + ")\"></i>";
            strTreeContent += "<i class=\"fa fa-angle-down\" onclick=\"TagGroupBottom(" + i + ")\"></i></div>";
        } else if (i == 0 && i != m_JsonResult.result.list.length - 1) {
            //所选分组内标签是当前分组的第一个标签是第一个（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-down\" style=\"margin-top: 10px;\" onclick=\"TagGroupBottom(" + i + ")\"></i></div>";
        } else if (i != 0 && i == m_JsonResult.result.list.length - 1) {
            //所选分组内标签是当前分组内的最后一个标签（分组数大于1个）
            strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\" style=\"margin-top: 10px;\" onclick=\"TagGroupTop(" + i + ")\"></i></div>";
        } else if (i == 0 && i == m_JsonResult.result.list.length - 1) {
            //所选分组内仅有一个标签
        }

        strTreeContent += "<span class=\"label pull-left badge-color\">" + jsonItem.tag.length + "</span></a>"

        if (m_frist == 1) {
            //初次加载页面时，第一个标签组展开状态
            if (i == 0) {
                strTreeContent += "<ul id=\"Tag" + i + "\" style=\"display: block;\">";
            } else {
                strTreeContent += "<ul id=\"Tag" + i + "\" style=\"display: none;\">";
            }
            m_frist = 2;//初次加载完成，将m_frist的值改变
        } else {
            strTreeContent += "<ul id=\"Tag" + i + "\" style=\"display: none;\">";
        }

        for (var j = 0; j < jsonItem.tag.length; j++) {

            var tagItem = jsonItem.tag[j];
            strTreeContent += "<li><a href=\"#divEditPerson\" data-toggle=\"tab\" onclick=\"SetEditTagBind('" + tagItem.tid + "','" + tagItem.tname + "','" + tagItem.color + "','" + jsonItem.tgid + "','" + jsonItem.tgname + "');\"><span class=\"icon-circle-blank circle-tags-color\" style=\"color:" + tagItem.color + "\"> </span>" + tagItem.tname;

            //判断组内标签能否向上向下移动
            if (j != 0 && j != jsonItem.tag.length - 1) {
                //所选分组内既不是第一个，也不是最后一个（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\" style=\"margin-right:5px;\" onclick=\"TagTop(" + i + "," + j + ")\"></i>";
                strTreeContent += "<i class=\"fa fa-angle-down\" onclick=\"TagBottom(" + i + "," + j + ")\"></i></div>";
            } else if (j == 0 && j != jsonItem.tag.length - 1) {
                //所选分组内标签是当前分组的第一个标签是第一个（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-down\" style=\"margin-top: 10px;\" onclick=\"TagBottom(" + i + "," + j + ")\"></i></div>";
            } else if (j != 0 && j == jsonItem.tag.length - 1) {
                //所选分组内标签是当前分组内的最后一个标签（分组数大于1个）
                strTreeContent += "<div class=\"pull-right\"><i class=\"fa fa-angle-up\"  style=\"margin-top: 10px;\" onclick=\"TagTop(" + i + "," + j + ")\"></i></div>";
            } else if (j == 0 && j == jsonItem.tag.length - 1) {
                //所选分组内仅有一个标签
            }
            strTreeContent += "</a></li>";
        }
        strTreeContent += "</ul>";
        strTreeContent += "</li>";
        strTreeContent += "</ul>";
        //DDL标签组
        $("#selectGroup").append("<option value='" + jsonItem.tgid + "'>" + jsonItem.tgname + "</option>");
    }
    $("#divTree").html(strTreeContent);
    $(".loading").hide();
    $(".historyzhezhao").hide();

    //展开当前标签所在的组
    if (isSelectIndex != -1) {
        $("#Tag" + isSelectIndex).attr("style", "display:block");
        $("#group" + isSelectIndex).removeClass().addClass("inactive-m inactives-m");
    }
}

///----------------------分组向上向下移动-------------------start
//向上移动
function TagGroupTop(index) {
    event.cancelBubble = true;//禁止触发父级事件
    $(".loading").show();
    $(".loadingcover").show();
    isSelectIndex = -1;
    //没提交过赋值
    if (!m_isGroupSubmit) {
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            m_JsonResult.result.list[i].sort = 10000 + i + 1;
        }
    }
    var top = m_JsonResult.result.list[index].sort;
    m_JsonResult.result.list[index].sort = m_JsonResult.result.list[index - 1].sort;
    m_JsonResult.result.list[index - 1].sort = top;

    var list = [];
    var jObject = {};
    if (m_isGroupSubmit) {
        jObject.id = m_JsonResult.result.list[index - 1].tgid;
        jObject.sort = m_JsonResult.result.list[index - 1].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[index].tgid;
        jObject.sort = m_JsonResult.result.list[index].sort;
        list.push(jObject);
    } else {
        //没提交过
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[i].tgid;
            jObject.sort = m_JsonResult.result.list[i].sort;
            list.push(jObject);
        }
    }
    var json = $.toJSON({
        grouplist: list
    });
    manageObj.changetaggroupposition(json);
}
function TagGroupBottom(index) {
    event.cancelBubble = true;//禁止触发父级事件
    $(".loading").show();
    $(".loadingcover").show();
    isSelectIndex = -1;
    //没提交过赋值
    if (!m_isGroupSubmit) {
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            m_JsonResult.result.list[i].sort = 10000 + Number(i) + 1;
        }
    }
    var bottom = m_JsonResult.result.list[index].sort;
    m_JsonResult.result.list[index].sort = m_JsonResult.result.list[index + 1].sort;
    m_JsonResult.result.list[index + 1].sort = bottom;

    var list = [];
    var jObject = {};
    if (m_isGroupSubmit) {
        jObject.id = m_JsonResult.result.list[index].tgid;
        jObject.sort = m_JsonResult.result.list[index].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[index + 1].tgid;
        jObject.sort = m_JsonResult.result.list[index + 1].sort;
        list.push(jObject);
    } else {
        //没提交过
        for (var i = 0; i < m_JsonResult.result.list.length; i++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[i].tgid;
            jObject.sort = m_JsonResult.result.list[i].sort;
            list.push(jObject);
        }
    }
    var json = $.toJSON({
        grouplist: list

    });
    manageObj.changetaggroupposition(json);
}

//标签组上下移动回调
function AsynChangeTagGroupPositionResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
        $(".loadingcover").hide();
        $(".loading").hide();
    }
    BindData();
}
///----------------------分组向上向下移动-------------------end

///---------------------分组内标签向上向下移动--------------start
var m_TgId = "";//当前标签所属的组的ID,移动后绑定组所用
//分组内标签向上移动单击
function TagTop(GroupIndex, TagIndex) {
    //参数：GroupIndex是所选标签组的下标，TagIndex是所选标签的下标
    event.cancelBubble = true;//禁止触发父级事件
    $(".loading").show();
    $(".loadingcover").show();
    isSelectIndex = GroupIndex;
    var isTag = true;//true提交过当前组，false没提交过
    if (m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == "" || m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == undefined || m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == null) {
        for (var i = 0; i < m_JsonResult.result.list[GroupIndex].tag.length; i++) {
            m_JsonResult.result.list[GroupIndex].tag[i].sort = 10000 + i + 1;
        }
        isTag = false;
    }
    var top = m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort;
    m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex - 1].sort;
    m_JsonResult.result.list[GroupIndex].tag[TagIndex - 1].sort = top;

    var list = [];
    var jObject = {};
    if (!isTag) {
        for (var j = 0; j < m_JsonResult.result.list[GroupIndex].tag.length; j++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[GroupIndex].tag[j].tid;
            jObject.sort = m_JsonResult.result.list[GroupIndex].tag[j].sort;
            list.push(jObject);
        }
    } else {
        jObject.id = m_JsonResult.result.list[GroupIndex].tag[TagIndex - 1].tid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex - 1].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[GroupIndex].tag[TagIndex].tid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort;
        list.push(jObject);

    }
    var json = $.toJSON({
        taglist: list
    });
    manageObj.changetagposition(json);
}

//分组内标签向下移动单击事件
function TagBottom(GroupIndex, TagIndex) {
    //参数：GroupIndex是所选标签组的下标，TagIndex是所选标签的下标
    event.cancelBubble = true;//禁止触发父级事件
    $(".loading").show();
    $(".loadingcover").show();
    isSelectIndex = GroupIndex;
    var isTag = true;//true提交过当前组，false没提交过
    if (m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == "" || m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == undefined || m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort == null) {
        for (var i = 0; i < m_JsonResult.result.list[GroupIndex].tag.length; i++) {
            m_JsonResult.result.list[GroupIndex].tag[i].sort = 10000 + Number(i) + 1;
        }
        isTag = false;
    }
    var bottom = m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort
    m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex + 1].sort;
    m_JsonResult.result.list[GroupIndex].tag[TagIndex + 1].sort = bottom;

    var list = [];
    var jObject = {};
    if (!isTag) {
        for (var i = 0; i < m_JsonResult.result.list[GroupIndex].tag.length; i++) {
            jObject = {};
            jObject.id = m_JsonResult.result.list[GroupIndex].tag[i].tid;
            jObject.sort = m_JsonResult.result.list[GroupIndex].tag[i].sort;
            list.push(jObject);
        }
    } else {
        jObject.id = m_JsonResult.result.list[GroupIndex].tag[TagIndex].tid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex].sort;
        list.push(jObject);
        jObject = {};
        jObject.id = m_JsonResult.result.list[GroupIndex].tag[TagIndex + 1].tid;
        jObject.sort = m_JsonResult.result.list[GroupIndex].tag[TagIndex + 1].sort;
        list.push(jObject);

    }
    var json = $.toJSON({
        taglist: list
    });
    manageObj.changetagposition(json);
}

//标签上下移动回调
function AsynChangeTagPositionResult(jsonResult) {
    if (jsonResult.success) {
        ShowMsg("myPageAlert", "操作成功！");
    } else {
        ShowError("myPageAlert", "操作失败：" + jsonResult.error);
        $(".loadingcover").hide();
        $(".loading").hide();
    }
    BindData();
}
///---------------------分组内标签向上向下移动--------------end