-------------------------------------------------------------------------------
--
--  Mod Name : Blink Chat Header
--  Author   : Blink
--  Date     : 2004/10/11
--  LastUpdate : 2006/09/05
--
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

BCH_VERSION_N = 2.2;

BCH_VERSION_INFO = 0;

ChatHeadType = {};
ChatHeadType[1] = {"SAY",TEXT(BLINK_CHAT_HEAD_SAY)};
ChatHeadType[2] = {"WHISPER",TEXT(BLINK_CHAT_HEAD_WHISPER)};
ChatHeadType[3] = {"GUILD",TEXT(BLINK_CHAT_HEAD_GUILD)};
ChatHeadType[4] = {"PARTY",TEXT(BLINK_CHAT_HEAD_PARTY)};
ChatHeadType[5] = {"YELL",TEXT(BLINK_CHAT_HEAD_YELL)};
ChatHeadType[6] = {"CHANNEL",TEXT(BLINK_CHAT_HEAD_CHANNEL)};
ChatHeadType[7] = {"RAID",TEXT(BLINK_CHAT_HEAD_RAID)};
ChatHeadType[8] = {"RAID_WARNING",TEXT(BLINK_CHAT_HEAD_RAID_WARNING)};
ChatHeadType[9] = {"RAID_LEADER",TEXT(BLINK_CHAT_HEAD_RAID_LEADER)};
ChatHeadType[10] = {"BATTLEGROUND",TEXT(BLINK_CHAT_HEAD_BATTLEGROUND)};
ChatHeadType[11] = {"BATTLEGROUND_LEADER",TEXT(BLINK_CHAT_HEAD_BATTLEGROUND_LEADER)};

-- PagerMessagePrefix is variable of Pager.
PagerMessagePrefix = getglobal("PagerMessagePrefix");

-- overriding function
BCH_OriginalSendChatMessage = nil;

BCH_BM_TYPE = "ALL";
BCH_BM_CURRENT_CHAT_HEAD = "";

-------------------------------------------------------------------------------
-- BlinkPrint
-------------------------------------------------------------------------------
if( not BlinkPrint ) then
	BlinkPrint = function(msg)
		if(ChatFrame1) then
			ChatFrame1:AddMessage(msg, 1.0, 1.0, 0.0);
		end
	end
end


-------------------------------------------------------------------------------
function BlinkChatHead_OnLoad()
	BCH_DATA = {};
	this:RegisterEvent("VARIABLES_LOADED");
	
	-- overriding..
	BCH_OriginalSendChatMessage = getglobal("SendChatMessage");
	setglobal("SendChatMessage",BCH_SendChatMessage);

	if( Cosmos_RegisterChatCommand ~= nil ) then
		-- using Cosmos
		local id = "BLINK_CH_CMD";
		local func = function(msg)
			local ctype = gsub(msg, "%s*([^%s]+).*", "%1");
			local headmsg = gsub(msg, "%s*([^%s]+)%s*(.*)", "%2");
			headmsg = headmsg.." ";

			if(strlen(ctype) <= 0) then
				BlinkPrint( TEXT(getglobal("BLINK_CHAT_HEAD_DESC")) );
			else
				ChatHead(ctype,headmsg);
			end
		end
		Cosmos_RegisterChatCommand ( "BLINK_CH_CMD", BLINK_CHATHEAD_COMMAND, func, BLINK_CHAT_HEAD_DESC );
		Cosmos_RegisterChatCommand ( "BLINK_CH_CMD_LIST", BLINK_CHATHEAD_LIST_COMMAND, function(msg) ChatHeadList() end, BLINK_CHATHEAD_LIST_COMMAND_DESC );
		Cosmos_RegisterChatCommand ( "BLINK_CH_CMD_CONFIGURE", BLINK_CHATHEAD_CONFIGURE, function ()
			if ( not BlinkChatHeadConfigure:IsShown()) then
				ShowUIPanel(BlinkChatHeadConfigure);
			else
				HideUIPanel(BlinkChatHeadConfigure);
			end
		end, BLINK_CHATHEAD_CONFIGURE_DESC );
	else
		-- standalone
		SlashCmdList["CHAT_HEAD"] = function(msg)
			local ctype = gsub(msg, "%s*([^%s]+).*", "%1");
			local headmsg = gsub(msg, "%s*([^%s]+)%s*(.*)", "%2");
			if(strlen(ctype) <= 0) then
				BlinkPrint( TEXT(getglobal("BLINK_CHAT_HEAD_DESC")) );
			else
				ChatHead(ctype,headmsg);
			end
		end
		for i=1,#BLINK_CHATHEAD_COMMAND,1 do
			setglobal("SLASH_CHAT_HEAD"..i,BLINK_CHATHEAD_COMMAND[i]);
		end

		SlashCmdList["CHAT_HEAD_LIST"] = function(msg)
			ChatHeadList();
		end
		for i=1,#BLINK_CHATHEAD_LIST_COMMAND,1 do
			setglobal("SLASH_CHAT_HEAD_LIST"..i,BLINK_CHATHEAD_LIST_COMMAND[i]);
		end

		SlashCmdList["CHAT_HEAD_CONFIGURE"] = function (msg)
			if ( not BlinkChatHeadConfigure:IsShown()) then
				ShowUIPanel(BlinkChatHeadConfigure);
			else
				HideUIPanel(BlinkChatHeadConfigure);
			end
		end;
		SLASH_CHAT_HEAD_CONFIGURE1 = "/말머리설정";
		SLASH_CHAT_HEAD_CONFIGURE2 = "/chatheadconfig";
	end
	BCH_BlinkModRegister();
	BCH_RegistGUI();
end
-------------------------------------------------------------------------------
function BlinkChatHead_OnEvent(event)
	if ( event == "VARIABLES_LOADED" ) then
		if ( not BCH_VERSION_INFO or BCH_VERSION_INFO < BCH_VERSION_N ) then
			BCH_DATA = {};
			BCH_VERSION_INFO = BCH_VERSION_N;
		end
	end
end

-- override SendChatMessage function //////////////////////////////////////////

function BCH_SendChatMessage( text, ctype, lang, ... )

	if( text == nil ) then
		return;
	end
	local h = "";
	if ( BCH_DATA ) then
		if( ctype == "WHISPER" )then
			if( PagerMessagePrefix == nil or strsub(text, 1, strlen(PagerMessagePrefix)) ~= PagerMessagePrefix ) then
				h = BCH_DATA["WHISPER"];
			end
		elseif ( ctype == "CHANNEL" ) then
			if( IsAllowedChannel( select(1,...) ) ) then
				h = BCH_DATA["CHANNEL"];
			else
				local channelNum, channelName = GetChannelName(select(1,...));
				if ( BCH_DATA[channelName] ) then
					h = BCH_DATA[channelName];
				end
			end
		else
			h = BCH_DATA[ctype];
		end

		if( h ~= nil and h ~= "" ) then
			text = h .. text;
		end
	end

	if( type(BCH_OriginalSendChatMessage) == "function" ) then
		BCH_OriginalSendChatMessage( text, ctype, lang , ...);
	elseif( SendChatMessage ~= BCH_SendChatMessage ) then
		SendChatMessage( text, ctype, lang, ... );
	end
end


-- ////////////////////////////////////////////////////////////////////////////

function ChatHead( ctype, headmsg , silence)
	if( ctype == nil ) then
		if ( not silence ) then
			BlinkPrint( BLINK_CHAT_HEAD_DESC );
		end
		return;
	end

	if( headmsg == nil or headmsg == "" or headmsg == " " ) then
		headmsg = nil;
		headmsg_desc = BLINK_CHAT_HEAD_EMPTY;
	else
		headmsg_desc = headmsg;
	end
	
	local function inTable(t,v)
		for idx,value in pairs(t) do
			if ( idx == v or value[1] == v or value[2] == v ) then
				return true;
			end
		end
		return false;
	end

	if( string.upper(ctype) == "ALL" or ctype == TEXT(BLINK_CHAT_HEAD_ALL) ) then
		for i=1,#ChatHeadType,1 do
			BCH_SetPlayerInfo(ChatHeadType[i][1],headmsg);
			if ( not silence ) then
				BlinkPrint( format(BLINK_CHAT_HEAD_OK, ChatHeadType[i][2], headmsg_desc) );
			end
		end
		for c, h in pairs(BCH_DATA) do
			if ( inTable(ChatHeadType,c) == false ) then
				if ( BCH_InChannel(c) ) then
					BCH_SetPlayerInfo(c,headmsg);
					if ( not silence ) then
						BlinkPrint( format(BLINK_CHAT_HEAD_OK, c, headmsg_desc) );
					end
				else
					BCH_SetPlayerInfo(c,nil);
				end
			end
		end
		return;
	end

	for index, value in pairs(ChatHeadType) do
		if( ctype == index ) then
			BCH_SetPlayerInfo(value[1],headmsg);
			if ( not silence ) then
				BlinkPrint( format(BLINK_CHAT_HEAD_OK, ChatHeadType[index][2], headmsg_desc) );
			end
			return;
		else
			for i=1,#value,1 do
				if( value[i] == string.upper(ctype) ) then
					BCH_SetPlayerInfo(value[1],headmsg);
					if ( not silence ) then
						BlinkPrint( format(BLINK_CHAT_HEAD_OK, ChatHeadType[index][2], headmsg_desc) );
					end
					return;
				end
			end
		end
	end
	if ( inTable(ChatHeadType,ctype) == false ) then
		if ( BCH_InChannel(ctype) ) then
			BCH_SetPlayerInfo(ctype,headmsg);
			if ( not silence ) then
				BlinkPrint( format(BLINK_CHAT_HEAD_OK, ctype, headmsg_desc) );
			end
			return;
		else
			BCH_SetPlayerInfo(ctype,nil);
		end
	end
	if ( not silence ) then
		BlinkPrint( BLINK_CHAT_HEAD_DESC );
	end
end

function ChatHeadList()
	local function inTable(t,v)
		for idx,value in pairs(t) do
			if ( idx == v or value[1] == v or value[2] == v ) then
				return true;
			end
		end
		return false;
	end

	for index,value in pairs(ChatHeadType) do
		local headmsg;
		if( BCH_DATA == nil or BCH_DATA[value[1]] == nil or BCH_DATA[value[1]] == "" ) then
			headmsg = BLINK_CHAT_HEAD_EMPTY;
		else
			headmsg = BCH_DATA[value[1]];
		end
		BlinkPrint( format(BLINK_CHATHEAD_LIST,value[2],headmsg) );
	end
	for key, head in pairs(BCH_DATA) do
		if ( not inTable(ChatHeadType,key) ) then
			BlinkPrint( format(BLINK_CHATHEAD_LIST,key,head) );
		end
	end
end

function IsAllowedChannel(cn)
	local channelNum, channelName = GetChannelName(cn);
	local ret = false;
	if (channelNum > 0 and  channelName ~= nil) then
		for i=1, #BLINK_CHAT_HEAD_ALLOWED_CHANNELS, 1 do
			if( string.find(channelName,BLINK_CHAT_HEAD_ALLOWED_CHANNELS[i]) ) then
				ret = true;
			end
		end
	end
	return ret;
end

function BCH_InChannel(cn)
	for i = 1, 10, 1 do
		local channelNum, channelName = GetChannelName(i);
		if ( channelName == cn ) then
			return true;
		end
	end
	return false;
end

function BCH_SetPlayerInfo(ctype,head)
	if ( not BCH_DATA ) then
		BCH_DATA = {};
	end
	BCH_DATA[ctype] = head;
end

function BCH_BlinkModRegister()
	if ( not BlinkMod_RegisterMenu ) then
		return;
	end

	local info;
	local color;
	local chattypes = getglobal("ChatTypeInfo");

	-- tier 1
	info = {};
	info.parentkey = "SELF";
	info.key = "BCHMAIN";
	info.text = "말머리 애드온";
	info.hasArrow = 1;
	info.tooltipTitle = "말머리 애드온";
	info.tooltipText = "채팅창에 지정한 말머리를 표시합니다.";
	info.notCheckable = 1;
	BlinkMod_RegisterMenu( info, 1 );

		-- tier 2 : parent = BCHMAIN
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHTITLE";
		info.text = "말머리 애드온";
		info.isTitle = 1;
		info.notCheckable = 1;
		BlinkMod_RegisterMenu( info, 2 );

		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHCONFIGURE";
		info.text = "설정창 열기";
		info.tooltipTitle = "말머리 설정창 열기";
		info.tooltipText = "말머리 설정창 열기";
		info.ClickFunction = function ()
			HideDropDownMenu(1);
			if ( not BlinkChatHeadConfigure:IsShown()) then
				ShowUIPanel(BlinkChatHeadConfigure);
			else
				HideUIPanel(BlinkChatHeadConfigure);
			end
		end;
		info.notCheckable = 1;
		BlinkMod_RegisterMenu( info, 2 );

		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHEDITALL";
		info.text = "모든 말머리";
		info.tooltipTitle = "모든 말머리";
		info.tooltipText = "대화/파티/귓말/길드/채널/공격대/사설채널명 말머리를 한꺼번에 지정합니다.";
		info.ClickFunction = BCH_BM_EditAllClick;
		info.notCheckable = 1;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["SAY"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHSAY";
		info.text = "대화";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "대화 말머리";
		info.tooltipText = "일반 대화시 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditSayClick;
		info.GetNameFunction = BCH_BM_EditSayName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["WHISPER"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHWHISPER";
		info.text = "귓말";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "귓말 말머리";
		info.tooltipText = "귓말 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditWhisperClick;
		info.GetNameFunction = BCH_BM_EditWhisperName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["WHISPER"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHGUILD";
		info.text = "길드";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "길드 말머리";
		info.tooltipText = "길드대화 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditGuildClick;
		info.GetNameFunction = BCH_BM_EditGuildName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["PARTY"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHPARTY";
		info.text = "파티";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "파티 말머리";
		info.tooltipText = "파티 대화 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditPartyClick;
		info.GetNameFunction = BCH_BM_EditPartyName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["YELL"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHYELL";
		info.text = "외침";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "외침 말머리";
		info.tooltipText = "외침 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditYellClick;
		info.GetNameFunction = BCH_BM_EditYellName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["CHANNEL"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHCHANNEL";
		info.text = "채널";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "채널";
		info.tooltipText = "공개채널 대화 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditChannelClick;
		info.GetNameFunction = BCH_BM_EditChannelName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["RAID"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHRAID";
		info.text = "공격대";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "공격대";
		info.tooltipText = "공격대 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditRaidClick;
		info.GetNameFunction = BCH_BM_EditRaidName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["RAID_WARNING"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHRAID_WARNING";
		info.text = "공격대 경보";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "공격대 경보";
		info.tooltipText = "공격대 경보 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditRaidWarningClick;
		info.GetNameFunction = BCH_BM_EditRaidWarningName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["RAID_LEADER"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHRAID_LEADER";
		info.text = "공격대장";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "공격대장";
		info.tooltipText = "공격대장 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditRaidLeaderClick;
		info.GetNameFunction = BCH_BM_EditRaidLeaderName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["BATTLEGROUND"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BATTLEGROUND";
		info.text = "전장";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "전장";
		info.tooltipText = "전장 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditBattlegroundClick;
		info.GetNameFunction = BCH_BM_EditBattlegroundName;
		BlinkMod_RegisterMenu( info, 2 );

		color = chattypes["BATTLEGROUND_LEADER"];
		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BATTLEGROUND_LEADER";
		info.text = "전장지휘관";
		info.textR = color.r;
		info.textG = color.g;
		info.textB = color.b;
		info.tooltipTitle = "전장 지휘관";
		info.tooltipText = "전장 지휘관 말머리를 설정합니다.";
		info.notCheckable = 1;
		info.ClickFunction = BCH_BM_EditBattlegroundLeaderClick;
		info.GetNameFunction = BCH_BM_EditBattlegroundLeaderName;
		BlinkMod_RegisterMenu( info, 2 );

		info = {};
		info.parentkey = "BCHMAIN";
		info.key = "BCHCUSTOMCHANNEL";
		info.hasArrow = 1;
		info.text = "사설채널";
		info.tooltipTitle = "사설채널";
		info.tooltipText = "사설채널의 설정된 말머리를 확인합니다.";
		info.notCheckable = 1;
		BlinkMod_RegisterMenu( info, 2 );

			-- tier 3 : parent = BCHCUSTOMCHANNEL
			for i=1,15,1 do
				info = {};
				info.parentkey = "BCHCUSTOMCHANNEL";
				info.key = "BCHCUSTOMCHANNEL"..i;
				info.text = "채널";
				info.tooltipTitle = "사설채널";
				info.tooltipText = "목록에서 제거하시려면 클릭하세요.";
				info.ClickFunction = getglobal("BCH_BM_Remove"..i.."Click");
				info.GetNameFunction = getglobal("BCH_BM_Remove"..i.."Name");
				info.ShowHideFunction = getglobal("BCH_BM_Remove"..i.."Show");
				info.notCheckable = 1;
				BlinkMod_RegisterMenu( info, 3 );
			end
end

-- all
function BCH_BM_EditAllClick()
	--BCH_BM_TYPE = TEXT(BLINK_CHAT_HEAD_ALL);
	setglobal("BCH_BM_TYPE",TEXT(BLINK_CHAT_HEAD_ALL));
	StaticPopupDialogs["EDIT_CHATHEAD"].text = BLINK_FORMAT_STATIC_MSG_ALL; --format("%s\n",TEXT(BLINK_CHAT_HEAD_ALL));
	BCH_BM_CURRENT_CHAT_HEAD = "";
	StaticPopup_Show("EDIT_CHATHEAD");
end

-- template
function BCH_BM_EditClick(h)
	setglobal("BCH_BM_TYPE",ChatHeadType[h][2]);
	StaticPopupDialogs["EDIT_CHATHEAD"].text = format(BLINK_FORMAT_STATIC_MSG,ChatHeadType[h][2]);
	if ( BCH_DATA and BCH_DATA[ChatHeadType[h][1]] and strlen(BCH_DATA[ChatHeadType[h][1]])>0 ) then
		BCH_BM_CURRENT_CHAT_HEAD = BCH_DATA[ChatHeadType[h][1]];
	else
		BCH_BM_CURRENT_CHAT_HEAD = "";
	end
	StaticPopup_Show("EDIT_CHATHEAD");
end
function BCH_BM_EditName(h)
	if ( BCH_DATA and BCH_DATA[ChatHeadType[h][1]] and strlen(BCH_DATA[ChatHeadType[h][1]])>0 ) then
		return ChatHeadType[h][2] .. " : " .. BCH_DATA[ChatHeadType[h][1]];
	else
		return ChatHeadType[h][2] .. " : " .. BLINK_CHAT_HEAD_EMPTY;
	end
end

-- say
function BCH_BM_EditSayClick() BCH_BM_EditClick(1); end
function BCH_BM_EditSayName() return BCH_BM_EditName(1); end

-- whisper
function BCH_BM_EditWhisperClick() BCH_BM_EditClick(2); end
function BCH_BM_EditWhisperName() return BCH_BM_EditName(2); end

-- guild
function BCH_BM_EditGuildClick() BCH_BM_EditClick(3); end
function BCH_BM_EditGuildName() return BCH_BM_EditName(3); end

-- party
function BCH_BM_EditPartyClick() BCH_BM_EditClick(4); end
function BCH_BM_EditPartyName() return BCH_BM_EditName(4); end

-- yell
function BCH_BM_EditYellClick() BCH_BM_EditClick(5); end
function BCH_BM_EditYellName() return BCH_BM_EditName(5); end

-- channel
function BCH_BM_EditChannelClick() BCH_BM_EditClick(6); end
function BCH_BM_EditChannelName() return BCH_BM_EditName(6); end

-- raid
function BCH_BM_EditRaidClick() BCH_BM_EditClick(7); end
function BCH_BM_EditRaidName() return BCH_BM_EditName(7); end

-- raid warning
function BCH_BM_EditRaidWarningClick() BCH_BM_EditClick(8); end
function BCH_BM_EditRaidWarningName() return BCH_BM_EditName(8); end

-- raid leader
function BCH_BM_EditRaidLeaderClick() BCH_BM_EditClick(9); end
function BCH_BM_EditRaidLeaderName() return BCH_BM_EditName(9); end

-- battleground
function BCH_BM_EditBattlegroundClick() BCH_BM_EditClick(10); end
function BCH_BM_EditBattlegroundName() return BCH_BM_EditName(10); end

-- battleground leader
function BCH_BM_EditBattlegroundLeaderClick() BCH_BM_EditClick(11); end
function BCH_BM_EditBattlegroundLeaderName() return BCH_BM_EditName(11); end

StaticPopupDialogs["EDIT_CHATHEAD"] = {
	text = format("%s\n",getglobal("BCH_BM_TYPE")),
	button1 = TEXT(ACCEPT),
	button2 = TEXT(CANCEL),
	hasEditBox = 1,
	maxLetters = 33,
	OnAccept = function()
		local editBox = getglobal(this:GetParent():GetName().."EditBox");
		ChatHead( getglobal("BCH_BM_TYPE"), editBox:GetText() );
	end,
	OnShow = function()
		StaticPopupDialogs["EDIT_CHATHEAD"].text = format("%s\n",getglobal("BCH_BM_TYPE"));
		getglobal(this:GetName().."EditBox"):SetText(BCH_BM_CURRENT_CHAT_HEAD);
		getglobal(this:GetName().."EditBox"):SetFocus();
	end,
	OnHide = function()
		if ( ChatFrameEditBox:IsVisible() ) then
			ChatFrameEditBox:SetFocus();
		end
		getglobal(this:GetName().."EditBox"):SetText("");
	end,
	EditBoxOnEnterPressed = function()
		local editBox = getglobal(this:GetParent():GetName().."EditBox");
		ChatHead( getglobal("BCH_BM_TYPE"), editBox:GetText() );
		this:GetParent():Hide();
	end,
	EditBoxOnEscapePressed = function()
		this:GetParent():Hide();
	end,
	timeout = 0,
	exclusive = 1,
	whileDead = 1
};

function BCH_BM_RemoveClick(number)
	local function inTable(t,v)
		for idx,value in pairs(t) do
			if ( idx == v or value[1] == v or value[2] == v ) then
				return true;
			end
		end
		return false;
	end
	if ( number ) then
		local i = 1;
		local cc,ch;
		for chat,head in pairs(BCH_DATA) do
			if ( inTable(ChatHeadType,chat) == false ) then
				if ( number == i ) then
					cc = chat;
					ch = head;
					break;
				end
				i = i + 1;
			end
		end
		BCH_DATA[cc] = nil;
	end
end

function BCH_BM_RemoveName(number)
	local function inTable(t,v)
		for idx,value in pairs(t) do
			if ( idx == v or value[1] == v or value[2] == v ) then
				return true;
			end
		end
		return false;
	end
	if ( number ) then
		local i = 1;
		local cc,ch;
		for chat,head in pairs(BCH_DATA) do
			if ( inTable(ChatHeadType,chat) == false ) then
				if ( number == i ) then
					return "["..chat.."] "..head;
				end
				i = i + 1;
			end
		end
	end
end

function BCH_BM_RemoveShow(number)
	local function inTable(t,v)
		for idx,value in pairs(t) do
			if ( idx == v or value[1] == v or value[2] == v ) then
				return true;
			end
		end
		return false;
	end
	if ( number ) then
		local i = 1;
		local cc,ch;
		for chat,head in pairs(BCH_DATA) do
			if ( inTable(ChatHeadType,chat) == false ) then
				if ( number == i ) then
					return true;
				end
				i = i + 1;
			end
		end
	end
	return false;
end

function BCH_BM_Remove1Click() BCH_BM_RemoveClick(1); end
function BCH_BM_Remove1Name() return BCH_BM_RemoveName(1); end
function BCH_BM_Remove1Show() return BCH_BM_RemoveShow(1); end

function BCH_BM_Remove2Click() BCH_BM_RemoveClick(2); end
function BCH_BM_Remove2Name() return BCH_BM_RemoveName(2); end
function BCH_BM_Remove2Show() return BCH_BM_RemoveShow(2); end

function BCH_BM_Remove3Click() BCH_BM_RemoveClick(3); end
function BCH_BM_Remove3Name() return BCH_BM_RemoveName(3); end
function BCH_BM_Remove3Show() return BCH_BM_RemoveShow(3); end

function BCH_BM_Remove4Click() BCH_BM_RemoveClick(4); end
function BCH_BM_Remove4Name() return BCH_BM_RemoveName(4); end
function BCH_BM_Remove4Show() return BCH_BM_RemoveShow(4); end

function BCH_BM_Remove5Click() BCH_BM_RemoveClick(5); end
function BCH_BM_Remove5Name() return BCH_BM_RemoveName(5); end
function BCH_BM_Remove5Show() return BCH_BM_RemoveShow(5); end

function BCH_BM_Remove6Click() BCH_BM_RemoveClick(6); end
function BCH_BM_Remove6Name() return BCH_BM_RemoveName(6); end
function BCH_BM_Remove6Show() return BCH_BM_RemoveShow(6); end

function BCH_BM_Remove7Click() BCH_BM_RemoveClick(7); end
function BCH_BM_Remove7Name() return BCH_BM_RemoveName(7); end
function BCH_BM_Remove7Show() return BCH_BM_RemoveShow(7); end

function BCH_BM_Remove8Click() BCH_BM_RemoveClick(8); end
function BCH_BM_Remove8Name() return BCH_BM_RemoveName(8); end
function BCH_BM_Remove8Show() return BCH_BM_RemoveShow(8); end

function BCH_BM_Remove9Click() BCH_BM_RemoveClick(9); end
function BCH_BM_Remove9Name() return BCH_BM_RemoveName(9); end
function BCH_BM_Remove9Show() return BCH_BM_RemoveShow(9); end

function BCH_BM_Remove10Click() BCH_BM_RemoveClick(10); end
function BCH_BM_Remove10Name() return BCH_BM_RemoveName(10); end
function BCH_BM_Remove10Show() return BCH_BM_RemoveShow(10); end

function BCH_BM_Remove11Click() BCH_BM_RemoveClick(11); end
function BCH_BM_Remove11Name() return BCH_BM_RemoveName(11); end
function BCH_BM_Remove11Show() return BCH_BM_RemoveShow(11); end

function BCH_BM_Remove12Click() BCH_BM_RemoveClick(12); end
function BCH_BM_Remove12Name() return BCH_BM_RemoveName(12); end
function BCH_BM_Remove12Show() return BCH_BM_RemoveShow(12); end

function BCH_BM_Remove13Click() BCH_BM_RemoveClick(13); end
function BCH_BM_Remove13Name() return BCH_BM_RemoveName(13); end
function BCH_BM_Remove13Show() return BCH_BM_RemoveShow(13); end

function BCH_BM_Remove14Click() BCH_BM_RemoveClick(14); end
function BCH_BM_Remove14Name() return BCH_BM_RemoveName(14); end
function BCH_BM_Remove14Show() return BCH_BM_RemoveShow(14); end

function BCH_BM_Remove15Click() BCH_BM_RemoveClick(15); end
function BCH_BM_Remove15Name() return BCH_BM_RemoveName(15); end
function BCH_BM_Remove15Show() return BCH_BM_RemoveShow(15); end

function BCH_RegistGUI()

	if ( not Blink_AddOption ) then
		return;
	end

	Blink_AddOption{
		category = BLINK_CATEGORY_CHAT,
		addonid = "BLINKCHATHEAD",
		addonname = "말머리 애드온",
		id = "BLINKCHATHEAD_HEADER",
		paneltype = "HEADER",
		text = "블링크의 말머리 애드온",
	};
	
	Blink_AddOption{
		addonid = "BLINKCHATHEAD",
		id = "BLINKCHATHEAD_DESC",
		paneltype = "TEXT",
		text = "채팅창의 각 채널별로 말머리를 추가합니다.",
		tooltip = "대화, 길드, 귓말, 파티, 공격대, 일반채널 및 사설채널에 말머리를 추가할수 있습니다.",
	};

	Blink_AddOption{
		addonid = "BLINKCHATHEAD",
		id = "BLINKCHATHEAD_CONFIGURE",
		paneltype = "BUTTON",
		text = "설정하기",
		click = function ()
			if ( not BlinkChatHeadConfigure:IsShown()) then
				ShowUIPanel(BlinkChatHeadConfigure);
			else
				HideUIPanel(BlinkChatHeadConfigure);
			end
		end,
	};
end