var m_SystemTagCache = [];                  //系统所有标签缓存
var m_NowSelectFriendTags = "";             //当前选中好友标签集合
var m_SystemGgTagCache = [];               //系统所有广告标签缓存
var m_NowSelectFriendggTags = "";             //当前选中好友广告系统标签集合
function ResetFriendTagList() {
	talkObj.gettaglistjson(m_NowSelectFriendTags + "," + m_NowSelectFriendggTags);
}
//获取标签数据回执
function AsyncGetTagListResult(jsonResult) {
    if (jsonResult.success) {
        var strContent = "";
        if (jsonResult.list.length > 0) {
            for (var i = 0; i < jsonResult.list.length; i++) {
                var item = jsonResult.list[i];
                //strContent += "<li><span style=\"background:" + item.color + ";\"></span><a>" + item.tname + "</a></li>";
                strContent += "<span style=\"background-color:" + item.color + "\">" + item.tname + "</span>";
            }
            //$("#ulDocHeadFriendSelectTags").html(strContent);
            $("#ulDocFriendSelectTags").html(strContent);
        } else {
            $("#ulDocFriendSelectTags").html("暂无标签");
        }

		//广告标签，暂时注释
        //strContent = "";
        //if (jsonResult.gglist.length > 0) {
        //	m_NowSelectFriendggTags = "";
        //	for (var i = 0; i < jsonResult.gglist.length; i++) {
        //		var item = jsonResult.gglist[i];
        //		m_NowSelectFriendggTags += "|" + item.tid + "|";
        //		strContent += "<li><span style=\"background:\"\";\"></span><a>" + item.tname + "</a></li>";
        //	}
        //	m_NowSelectFriendggTags.replace("||", "|");
        //	$("#ulDocFriendSelectggTags").html(strContent);
        //} else {
        //	$("#ulDocFriendSelectggTags").html("暂无标签");
        //}

       
    } else {
        ShowError("myPageAlert", "获取标签数据异常：" + jsonResult.error);
    }
}

function AsyncGetTagTreeResult(jsonResult) {
    if (jsonResult.success) {
        m_SystemTagCache = jsonResult.list;
        m_SystemGgTagCache = jsonResult.gglist
        var vhtmltaggroup = "";
        vhtmltaggroup += "<option value=\"0\">";
        vhtmltaggroup += "不限";
        vhtmltaggroup += "</option>";
        if (m_SystemTagCache != null && m_SystemTagCache.length > 0) {
            for (var i = 0; i < m_SystemTagCache.length; i++) {
                var jsonItem = m_SystemTagCache[i];
                vhtmltaggroup += "<option value=\"" + jsonItem.tgid + "\">";
                vhtmltaggroup += jsonItem.tgname;
                vhtmltaggroup += "</option>";
            }
            $("#seltaggroup").html(vhtmltaggroup);
        }
    }
}

//显示标签选择模态对话框
function ShowEditTagModel() {
    BindFansTagList();
    $("#selectFansTagModel").modal({ backdrop: 'static', keyboard: false });
}

//判断标签是否存在
function TagIsExists(tid) {
    var strContent = "|" + tid + "|";
    return m_NowSelectFriendTags.indexOf(strContent) >= 0;
}
//判断广告标签是否存在
function GgTagIsExists(ggtid) {
	var strContent = "|" + ggtid + "|";
	return m_NowSelectFriendggTags.indexOf(strContent) >= 0;
}

//从缓存数据中绑定、刷新标签集合
function BindFansTagList() {
    $("#TagButton").show();
    if (m_SystemTagCache != null && m_SystemTagCache.length > 0) {
    	var strTreeContent = "<ul class=\"list-unstyled\">";//树型菜单内容
        for (var i = 0; i < m_SystemTagCache.length; i++) {
            var jsonItem = m_SystemTagCache[i];
            var strGroupId = jsonItem.tgid;
            var nGroupType = Number(jsonItem.type);
            var strTagType = nGroupType == 1 ? "【互斥】" : "";
            strTreeContent += "<li class=\"check-tags-header\">";
            //父级标签组
            strTreeContent += "<a id=\"liTagGroup_" + strGroupId + "\" class=\"qt-tittle\">" + jsonItem.tgname + strTagType + "</a>";
            strTreeContent += "<ul class=\"list-inline list-unstyled\">";
            strTreeContent += "<li>";
            for (var j = 0; j < jsonItem.tag.length; j++) {
                var tagItem = jsonItem.tag[j];
                if (m_NowSelectFriendTags.length > 0 && TagIsExists(tagItem.tid)) {

                	strTreeContent += "<span id=\"liTag_" + tagItem.tid + "\"  style=\"background-color:" + tagItem.color + ";border-color:" + tagItem.color + "\" onclick=\"ChangeFansTags('" + tagItem.tid + "','" + nGroupType + "','" + strGroupId + "','"+tagItem.color+"');\">" + tagItem.tname + "</span>";

                	//strTreeContent += "<li id=\"liTag_" + tagItem.tid + "\" class=\"taggroup" + strGroupId + " active\" onclick=\"ChangeFansTags('" + tagItem.tid + "','" + nGroupType + "','" + strGroupId + "');\"><span style=\"background:" + tagItem.color + ";\"></span><a href=\"#\">" + tagItem.tname + "</a></li>";
                } else {

                	strTreeContent += "<span id=\"liTag_" + tagItem.tid + "\" style=\"background-color:;border-color:" + tagItem.color + "\" onclick=\"ChangeFansTags('" + tagItem.tid + "','" + nGroupType + "','" + strGroupId + "','" + tagItem.color + "');\">" + tagItem.tname + "</span>";

                    //strTreeContent += "<li id=\"liTag_" + tagItem.tid + "\" class=\"taggroup"+strGroupId+"\" onclick=\"ChangeFansTags('" + tagItem.tid + "','"+nGroupType+"','"+strGroupId+"');\"><span style=\"background:" + tagItem.color + ";\"></span><a href=\"#\">" + tagItem.tname + "</a></li>";
                }
            }
            strTreeContent += "</li>";
            strTreeContent += "</ul>";
            strTreeContent += "</li>";
        }
        strTreeContent += "</ul>";
        $("#divFriendSelectTag").html(strTreeContent);
    }
    else {
        $("#divFriendSelectTag").html("暂无标签,请添加!");
        $("#TagButton").hide();
    }
}

//选中、释放标签
function ChangeFansTags(tid, ishc, groupid,color) {
	var objLiClickTag = $("#liTag_" + tid);
	
	var num = parseInt(objLiClickTag.css("background-color").replace(/[^0-9]/ig, ""));
	
	if (num != "0000") {
    	//删除标签操作
    	objLiClickTag.css("background-color","");
    	//$("#liTag_" + tid).removeAttr("background-color");
        m_NowSelectFriendTags = SuperReplace(m_NowSelectFriendTags, "|" + tid + "|", "|");
        m_NowSelectFriendTags = SuperReplace(m_NowSelectFriendTags, "||", "|");
    } else {
        //选择标签操作
        if (ishc == "1") {
        	//$(".taggroup" + groupid).next("ul li").css("background-color", "");
            //移除此互斥标签组内所有标签
            for (var i = 0; i < m_SystemTagCache.length; i++) {
                if (m_SystemTagCache[i].tgid == groupid) {
                    //移除组内所有标签
                    for (var j = 0; j < m_SystemTagCache[i].tag.length; j++) {
                    	m_NowSelectFriendTags = SuperReplace(m_NowSelectFriendTags, "|" + m_SystemTagCache[i].tag[j].tid + "|", "|");
                    	$("#liTag_" + m_SystemTagCache[i].tag[j].tid).css("background-color", "");
                    }
                    break;
                }
            }
        }
        objLiClickTag.css("background-color", color)
        //$("#liTag_" + tid).addClass("active");
        if (!TagIsExists(tid)) {
            m_NowSelectFriendTags += "|" + tid + "|";
            m_NowSelectFriendTags = SuperReplace(m_NowSelectFriendTags, "||", "|");
        }
    }
    if (m_NowSelectFriendTags == "|")
        m_NowSelectFriendTags = "";
}
function ChangeFansggTags(ggtid, ggtaggroupid, type) {
	var objLiClickTag = $("#liggTag_" + ggtid);
	if (objLiClickTag.hasClass("active")) {
		//删除标签操作
		$("#liggTag_" + ggtid).removeClass("active");
		m_NowSelectFriendggTags = SuperReplace(m_NowSelectFriendggTags, "|" + ggtid + "|", "|");
		m_NowSelectFriendggTags = SuperReplace(m_NowSelectFriendggTags, "||", "|");

	} else {
		//选择标签操作
		if (type == "1") {
			$(".ggtaggroup" + ggtaggroupid).removeClass("active");
			//移除此互斥标签组内所有标签
			for (var i = 0; i < m_SystemGgTagCache.length; i++) {
				if (m_SystemGgTagCache[i].ggtgid == ggtaggroupid) {
					//移除组内所有标签
					for (var j = 0; j < m_SystemGgTagCache[i].ggtag.length; j++) {
						m_NowSelectFriendggTags = SuperReplace(m_NowSelectFriendggTags, "|" + m_SystemGgTagCache[i].ggtag[j].ggtagid + "|", "|");
					}
					break;
				}
			}
		}
		$("#liggTag_" + ggtid).addClass("active");
		if (!TagIsExists(ggtid)) {
			m_NowSelectFriendggTags += "|" + ggtid + "|";
			m_NowSelectFriendggTags = SuperReplace(m_NowSelectFriendggTags, "||", "|");
		}
	}
	if (m_NowSelectFriendggTags == "|")
		m_NowSelectFriendggTags = "";
}