CT_RAMeters_ColorTable = {
	[CT_RA_HUNTER] = "|c00AAD372",
	[CT_RA_WARLOCK] = "|c009382c9",
	[CT_RA_PRIEST] = "|c00FFFFFF",
	[CT_RA_PALADIN] = "|c00F48CBA",
	[CT_RA_MAGE] = "|c0068CCEF",
	[CT_RA_ROGUE] = "|c00FFF468",
	[CT_RA_DRUID] = "|c00FF7C0A",
	[CT_RA_SHAMAN] = "|c00F48CBA",
	[CT_RA_WARRIOR] = "|c00C69B6D"
};
CT_RAMeters_StatsTable = {
	["Generic"] = {
		["isDead"] = 0,
		["isAfk"] = 0,
		["isOffline"] = 0
	},
	[CT_RA_WARRIOR] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_DRUID] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_MAGE] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_WARLOCK] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_ROGUE] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_HUNTER] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_PRIEST] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_PALADIN] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
	[CT_RA_SHAMAN] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 }
}
	
function CT_RAMeters_InitDropDown()
	local info;
	if ( UIDROPDOWNMENU_MENU_LEVEL == 2 ) then
		info = {};
		info.text = UIDROPDOWNMENU_MENU_VALUE;
		info.justifyH = "CENTER";
		info.isTitle = 1;
		info.notCheckable = 1;
		UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL);
		
		local nonManaUsers = {
			[CT_RA_ROGUE] = 1,
			[CT_RA_WARRIOR] = 1
		};
		for k, v in pairs(CT_RA_ClassPositions) do
			if ( ( UIDROPDOWNMENU_MENU_VALUE == "생명력 표시" or UIDROPDOWNMENU_MENU_VALUE == "공격대 생명력" ) or not nonManaUsers[k] ) then
				if ( ( k ~= CT_RA_SHAMAN or ( UnitFactionGroup("player") and UnitFactionGroup("player") == "Horde" ) ) and ( k ~= CT_RA_PALADIN or ( UnitFactionGroup("player") and UnitFactionGroup("player") == "Alliance" ) ) ) then
					info = { };
					info.text = k;
					info.value = { UIDROPDOWNMENU_MENU_VALUE, k };
					info.checked = ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"][UIDROPDOWNMENU_MENU_VALUE] and CT_RAMenu_Options["temp"]["StatusMeters"][UIDROPDOWNMENU_MENU_VALUE][k] );
					info.keepShownOnClick = 1;
					info.func = CT_RAMeters_DropDown_OnClick;
					UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL);
				end
			end
		end
		return;
	end
	info = {};
	info.text = "공격대 상태";
	info.justifyH = "CENTER";
	info.isTitle = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "생명력 표시";
	info.hasArrow = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "마나 표시";
	info.hasArrow = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "공격대 생명력";
	info.hasArrow = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "공격대 마나";
	info.hasArrow = 1;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "자리비움 집계";
	info.value = "AFK Count";
	info.checked = ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"]["AFK Count"] );
	info.keepShownOnClick = 1;
	info.func = CT_RAMeters_DropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "사망자 집계";
	info.value = "Dead Count";
	info.checked = ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"]["Dead Count"] );
	info.keepShownOnClick = 1;
	info.func = CT_RAMeters_DropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "오프라인 집계";
	info.value = "Offline Count";
	info.checked = ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"]["Offline Count"] );
	info.keepShownOnClick = 1;
	info.func = CT_RAMeters_DropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.disabled = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	if ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"]["Lock"] ) then
		info.text = "창 위치 잠금해제";
	else
		info.text = "창 위치 잠금";
	end
	info.value = "LockMeter";
	info.notCheckable = 1;
	info.func = CT_RAMeters_DropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	info = { };
	info.text = "배경색 설정";
	info.hasColorSwatch = 1;
	info.hasOpacity = 1;
	if ( CT_RAMenu_Options["temp"]["StatusMeters"] and CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"] ) then
		info.r = ( CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].r );
		info.g = ( CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].g );
		info.b = ( CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].b );
		info.opacity = ( CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].a );
	else
		info.r = 0;
		info.g = 0;
		info.b = 1;
		info.opacity = 0.5;
	end
	info.notClickable = 1;
	info.swatchFunc = CT_RAMeters_DropDown_SwatchFunc;
	info.opacityFunc = CT_RAMeters_DropDown_OpacityFunc;
	info.cancelFunc = CT_RAMeters_DropDown_CancelFunc;
	info.notCheckable = 1;
	UIDropDownMenu_AddButton(info);
	
	info = {};
	info.text = "|c00FF8080창 숨김|r";
	info.value = "Hide";
	info.notCheckable = 1;
	info.func = CT_RAMeters_DropDown_OnClick;
	UIDropDownMenu_AddButton(info);
end

function CT_RAMeters_DropDown_SwatchFunc()
	if ( not CT_RAMenu_Options["temp"]["StatusMeters"] ) then
		CT_RAMenu_Options["temp"]["StatusMeters"] = {
			["생명력 표시"] = { },
			["마나 표사"] = { },
			["공격대 생명력"] = { },
			["공격대 마나"] = { },
			["배경색"] = {
				["r"] = 0,
				["g"] = 0,
				["b"] = 1,
				["a"] = 0.5
			}
		};
	end
	local r, g, b = ColorPickerFrame:GetColorRGB();
	CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"]["r"] = r;
	CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"]["g"] = g;
	CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"]["b"] = b;
	CT_RAMetersFrame:SetBackdropColor(r, g, b, CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"]["a"]);
end

function CT_RAMeters_DropDown_OpacityFunc()
	if ( not CT_RAMenu_Options["temp"]["StatusMeters"] ) then
		CT_RAMenu_Options["temp"]["StatusMeters"] = {
			["생명력 표시"] = { },
			["마나 표시"] = { },
			["공격대 생명력"] = { },
			["공격대 마나"] = { },
			["배경색"] = {
				["r"] = 0,
				["g"] = 0,
				["b"] = 1,
				["a"] = 0.5
			}
		};
	end
	local a = OpacitySliderFrame:GetValue();
	CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"]["a"] = a;
	CT_RAMetersFrame:SetBackdropColor(CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].r, CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].g, CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"].b, a);
	CT_RAMetersFrame:SetBackdropBorderColor(1, 1, 1, a);
end

function CT_RAMeters_DropDown_CancelFunc(val)
	CT_RAMenu_Options["temp"]["StatusMeters"]["배경색"] = { 
		["r"] = val.r,
		["g"] = val.g,
		["b"] = val.b,
		["a"] = val.opacity
	};
	CT_RAMetersFrame:SetBackdropColor(val.r, val.g, val.b, val.opacity);
	CT_RAMetersFrame:SetBackdropBorderColor(1, 1, 1, val.opacity);
end

function CT_RAMeters_OnLoad()
	this:SetBackdropColor(0, 0, 1, 0.5);
end

function CT_RAMeters_DropDown_OnLoad()
	UIDropDownMenu_Initialize(this, CT_RAMeters_InitDropDown, "MENU");
end

function CT_RAMeters_DropDown_OnClick()
	-- Create the table if we haven't already
	if ( not CT_RAMenu_Options["temp"]["StatusMeters"]  ) then
		CT_RAMenu_Options["temp"]["StatusMeters"] = {
			["생명력 표시"] = { },
			["마나 표시"] = { },
			["공격대 생명력"] = { },
			["공격대 마나"] = { },
			["배경색"] = {
				["r"] = 0,
				["g"] = 0,
				["b"] = 1,
				["a"] = 0.5
			}
		};
	end
	if ( this.value == "LockMeter" ) then
		CT_RAMenu_Options["temp"]["StatusMeters"]["Lock"] = not CT_RAMenu_Options["temp"]["StatusMeters"]["Lock"];
		return;
	elseif ( this.value == "Hide" ) then
		CT_RAMenuFrameGeneralMiscShowMetersCB:SetChecked(false);
		CT_RAMenu_Options["temp"]["StatusMeters"]["Show"] = nil;
		CT_RAMetersFrame:Hide();
		return;
	end
	
	if ( type(this.value) == "table" ) then
		-- We have either HP or Mana Display/Totals
		CT_RAMenu_Options["temp"]["StatusMeters"][this.value[1]][this.value[2]] = not CT_RAMenu_Options["temp"]["StatusMeters"][this.value[1]][this.value[2]];
	else
		-- Just AFK Count/Dead Count
		CT_RAMenu_Options["temp"]["StatusMeters"][this.value] = not CT_RAMenu_Options["temp"]["StatusMeters"][this.value];
	end
	CT_RAMeters_UpdateWindow();
end

function CT_RAMeters_UpdateWindow()
	if ( not CT_RAMenu_Options["temp"]["StatusMeters"] or GetNumRaidMembers() == 0 ) then
		CT_RAMetersFrameText:SetText("집계할 수치 없음");
		CT_RAMetersFrame:SetWidth(125);
		CT_RAMetersFrame:SetHeight(41);
		return;
	end
	CT_RAMeters_StatsTable = {
		["Generic"] = {
			["isDead"] = 0,
			["isAfk"] = 0,
			["isOffline"] = 0
		},
		[CT_RA_WARRIOR] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
		[CT_RA_DRUID] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_MAGE] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_WARLOCK] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_ROGUE] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0 },
		[CT_RA_HUNTER] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_PRIEST] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_PALADIN] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 },
		[CT_RA_SHAMAN] = { ["health"] = 0, ["mana"] = 0, ["num"] = 0, ["numMana"] = 0 }
	};
	local stats = {
		["hpDisplay"] = { "", 0 },
		["mpDisplay"] = { "", 0 },
		["raidHp"] = { "", 1 },
		["raidMp"] = { "", 1 },
		["deadCount"] = { "", 1 },
		["afkCount"] = { "", 1 },
		["offlineCount"] = { "", 1 }
	};
	
	-- Get all the stats
	for i = 1, GetNumRaidMembers(), 1 do
		local id = "raid" .. i;
		if ( UnitIsConnected(id) ) then
			local name = UnitName(id);
			local class, health, mana, isDead, isAfk = UnitClass(id), 0, 0, ( ( UnitIsDead(id) or UnitIsGhost(id) ) and ( not CT_RA_Stats[name] or not CT_RA_Stats[name]["FD"] ) ), ( CT_RA_Stats[name] and CT_RA_Stats[name]["AFK"] );
			if ( class and CT_RAMeters_StatsTable[class] ) then
				if ( 
					( CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 생명력"] and CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 생명력"][class] ) or
					( CT_RAMenu_Options["temp"]["StatusMeters"]["생명력 표시"] and CT_RAMenu_Options["temp"]["StatusMeters"]["생명력 표시"][class] )
				) then
					health = UnitHealth(id)/UnitHealthMax(id);
				end
				if ( 
					UnitPowerType(id) == 0 and (
						( CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 마나"] and CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 마나"][class] ) or
						( CT_RAMenu_Options["temp"]["StatusMeters"]["마나 표시"] and CT_RAMenu_Options["temp"]["StatusMeters"]["마나 표시"][class] )
					)
				) then
					CT_RAMeters_StatsTable[class]["numMana"] = CT_RAMeters_StatsTable[class]["numMana"] + 1;
					mana = UnitMana(id)/UnitManaMax(id);
				end
				CT_RAMeters_StatsTable[class]["health"] = CT_RAMeters_StatsTable[class]["health"] + health;
				CT_RAMeters_StatsTable[class]["mana"] = CT_RAMeters_StatsTable[class]["mana"] + mana;
				if ( isDead ) then
					CT_RAMeters_StatsTable["Generic"]["isDead"] = CT_RAMeters_StatsTable["Generic"]["isDead"] + 1;
				end
				if ( isAfk ) then
					CT_RAMeters_StatsTable["Generic"]["isAfk"] = CT_RAMeters_StatsTable["Generic"]["isAfk"] + 1;
				end
				CT_RAMeters_StatsTable[class]["num"] = CT_RAMeters_StatsTable[class]["num"] + 1;
			end
		else
			CT_RAMeters_StatsTable["Generic"]["isOffline"] = CT_RAMeters_StatsTable["Generic"]["isOffline"] + 1;
		end
	end
	
	-- Raid Health
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 생명력"] ) then
		local combinedHealth, numHealth = 0, 0;
		for k, v in pairs(CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 생명력"]) do
			if ( v and CT_RAMeters_StatsTable[k] and CT_RAMeters_StatsTable[k]["num"] > 0 ) then
				combinedHealth, numHealth = combinedHealth + CT_RAMeters_StatsTable[k]["health"], numHealth + CT_RAMeters_StatsTable[k]["num"];
			end
		end
		if ( numHealth > 0 ) then
			combinedHealth = floor(combinedHealth/numHealth*100+0.5);
			stats["raidHp"][1] = "공격대 생명력: " .. combinedHealth .. "%";
		end
	end
	
	-- Raid Mana
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 마나"] ) then
		local combinedMana, numMana = 0, 0;
		for k, v in pairs(CT_RAMenu_Options["temp"]["StatusMeters"]["공격대 마나"]) do
			if ( v and CT_RAMeters_StatsTable[k] and CT_RAMeters_StatsTable[k]["numMana"] > 0 ) then
				combinedMana = combinedMana + CT_RAMeters_StatsTable[k]["mana"];
				numMana = numMana + CT_RAMeters_StatsTable[k]["numMana"];
			end
		end
		if ( numMana > 0 ) then
			combinedMana = floor(combinedMana/numMana*100+0.5);
			stats["raidMp"][1] = "공격대 마나: " .. combinedMana .. "%";
		end
	end
	
	-- AFK Count
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["AFK Count"] ) then
		stats["afkCount"][1] = "자리비움 수: " .. CT_RAMeters_StatsTable["Generic"]["isAfk"];
	end
	
	-- Dead Count
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["Dead Count"] ) then
		stats["deadCount"][1] = "사망자 수: " .. CT_RAMeters_StatsTable["Generic"]["isDead"];
	end
	
	-- Offline Count
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["Offline Count"] ) then
		stats["offlineCount"][1] = "오프라인 수: " .. CT_RAMeters_StatsTable["Generic"]["isOffline"];
	end
	
	-- Health Display
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["생명력 표시"] ) then
		for k, v in pairs(CT_RAMenu_Options["temp"]["StatusMeters"]["생명력 표시"]) do
			if ( v and CT_RAMeters_StatsTable[k] and CT_RAMeters_StatsTable[k]["num"] > 0 ) then
				if ( strlen(stats["hpDisplay"][1]) > 0 ) then
					stats["hpDisplay"][1] = stats["hpDisplay"][1] .. "\n";
				end
				stats["hpDisplay"][1] = stats["hpDisplay"][1] .. CT_RAMeters_ColorTable[k] .. k .. " 생명력: " .. floor(CT_RAMeters_StatsTable[k]["health"]/CT_RAMeters_StatsTable[k]["num"]*100+0.5) .. "%|r";
				stats["hpDisplay"][2] = stats["hpDisplay"][2] + 1;
			end
		end
	end
	
	-- Mana Display
	if ( CT_RAMenu_Options["temp"]["StatusMeters"]["마나 표시"]) then
		for k, v in pairs(CT_RAMenu_Options["temp"]["StatusMeters"]["마나 표시"]) do
			if ( v and CT_RAMeters_StatsTable[k] and CT_RAMeters_StatsTable[k]["numMana"] > 0 ) then
				if ( strlen(stats["mpDisplay"][1]) > 0 ) then
					stats["mpDisplay"][1] = stats["mpDisplay"][1] .. "\n";
				end
				stats["mpDisplay"][1] = stats["mpDisplay"][1] .. CT_RAMeters_ColorTable[k] .. k .. " 마나: " .. floor(CT_RAMeters_StatsTable[k]["mana"]/CT_RAMeters_StatsTable[k]["numMana"]*100+0.5) .. "%|r";
				stats["mpDisplay"][2] = stats["mpDisplay"][2] + 1;
			end
		end
	end
	
	-- Add together all the stats
	local out, numLines = "", 0;
	local order = {
		{ "raidHp", "|c00FF2222", "|r" },
		{ "raidMp", "|c006666FF", "|r" },
		{ "hpDisplay", "", "" },
		{ "mpDisplay", "", "" },
		{ "deadCount", "|c00666666", "|r" },
		{ "afkCount", "|c00CCCCCC", "|r" },
		{ "offlineCount", "|c00999999", "|r" }
	};
	for i = 1, 7, 1 do
		local val = stats[order[i][1]];
		if ( strlen(val[1]) > 0 ) then
			if ( strlen(out) > 0 ) then
				out = out .. "\n";
			end
			out = out .. order[i][2] .. val[1] .. order[i][3];
			numLines = numLines + val[2];
		end
	end
	if ( out == "" ) then
		numLines = 1;
		CT_RAMetersFrameText:SetText("집계할 수치 없음");
	else
		CT_RAMetersFrameText:SetText(out);
	end
	local width = CT_RAMetersFrameText:GetStringWidth();
	if ( width < 109 ) then
		width = 109;
	end
	CT_RAMetersFrame:SetWidth(width+16);
	CT_RAMetersFrame:SetHeight(25+(numLines*16));
end

function CT_RAMeters_OnUpdate(elapsed)
	this.update = this.update - elapsed;
	if ( this.update <= 0 ) then
		this.update = 2;
		CT_RAMeters_UpdateWindow();
	end
end