function WindChatConfig_OnShow()
	for i=1,7 do WindChatConfig_init(i); end
	for i=1,7 do WindChatConfig_initColor(i); end
	if (WindChatConfigAll[8]) then WindChatConfigTop:SetChecked(1);
	else WindChatConfigTop:SetChecked(0); end
	if (WindChatConfigAll[9]) then WindChatConfigSec:SetChecked(1);
	else WindChatConfigSec:SetChecked(0); end
	if (WindChatConfigAll[10]) then WindChatConfig12Hour:SetChecked(1);
	else WindChatConfig12Hour:SetChecked(0); end
	if (WindChatConfigAll[11]) then WindChatConfigTimeColor:SetChecked(1);
	else WindChatConfigTimeColor:SetChecked(0); end
	if (WindChatConfigAll[12]) then WindChatConfig_initTimeColor(); end
	if (WindChatConfigAll[13]) then WindChatConfigSticky:SetChecked(1);
	else WindChatConfigSticky:SetChecked(0); end
	if (WindChatConfigAll[14]) then WindChatConfigStrings:SetChecked(1);
	else WindChatConfigStrings:SetChecked(0); end
	if (WindChatConfigAll[15]) then WindChatConfigRaid:SetChecked(1);
	else WindChatConfigRaid:SetChecked(0); end
	if (WindChatConfigAll[16]) then WindChatConfigNameColor:SetChecked(1);
	else WindChatConfigNameColor:SetChecked(0); end
end

function WindChatConfig_init(index)
	getglobal("WindChatConfig"..index):SetText(WindChatConfigAll[index][1]);
	if (WindChatConfigAll[index][2]) then getglobal("WindChatConfig"..index.."Border"):SetChecked(1);
	else getglobal("WindChatConfig"..index.."Border"):SetChecked(0); end
	if (WindChatConfigAll[index][3]) then getglobal("WindChatConfig"..index.."Time"):SetChecked(1);
	else getglobal("WindChatConfig"..index.."Time"):SetChecked(0); end
end

function WindChatConfig_initColor(index)
	if (WindChatConfigAll[index][4]) then getglobal("WindChatConfig"..index.."COLOR1NormalTexture"):SetVertexColor(WindChatConfigAll[index][4][1].r, WindChatConfigAll[index][4][1].g, WindChatConfigAll[index][4][1].b, WindChatConfigAll[index][4][1].a);
	getglobal("WindChatConfig"..index.."COLOR2NormalTexture"):SetVertexColor(WindChatConfigAll[index][4][2].r, WindChatConfigAll[index][4][2].g, WindChatConfigAll[index][4][2].b, WindChatConfigAll[index][4][2].a);
	end
end

function WindChatConfig_initTimeColor()
	local color = WindChatConfigAll[12];
	local r, g, b;
	if (color and string.find(color, "%x%x%x%x%x%x")) then
		r = tonumber(string.sub(color, 1, 2), 16) / 255.0;
		g = tonumber(string.sub(color, 3, 4), 16) / 255.0;
		b = tonumber(string.sub(color, 5, 6), 16) / 255.0;
	else
		r = 1.0;
		g = 1.0;
		b = 1.0;
	end
	WindChatConfigTIMECOLORNormalTexture:SetVertexColor(r, g, b);
end

function WindChatConfig_changeScale(index)
	WindChatConfigAll[index][1] = WindConfig_validScale(this:GetNumber());
	WindChat_setScale(index);
	if (index ~= 7) then getglobal("WindChatConfig"..index+1):SetFocus();
	else this:ClearFocus(); end
end

function WindChatConfig_changeBorder(index)
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[index][2] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[index][2] = false;
	end
	WindChat_setBorder(index);
end

function WindChatConfig_changeTime(index)
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[index][3] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[index][3] = false;
	end
	WindChat_setTime(index);
end

function WindChatConfig_changeColor(color)
	if (color) then WindChat_setColor(); end
end

function WindChatConfig_resetFrameColor(frameIndex)
	WindChat_resetColor(frameIndex);

	getglobal("WindChatConfig"..frameIndex.."COLOR1NormalTexture"):SetVertexColor(WindChatConfigAll[frameIndex][4][1].r, WindChatConfigAll[frameIndex][4][1].g, WindChatConfigAll[frameIndex][4][1].b, WindChatConfigAll[frameIndex][4][1].a);
	getglobal("WindChatConfig"..frameIndex.."COLOR2NormalTexture"):SetVertexColor(WindChatConfigAll[frameIndex][4][2].r, WindChatConfigAll[frameIndex][4][2].g, WindChatConfigAll[frameIndex][4][2].b, WindChatConfigAll[frameIndex][4][2].a);
end

function WindChatConfig_changeTop()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[8] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[8] = false;
	end
	WindChatEditBox_setLocation();
end

function WindChatConfig_changeSec()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[9] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[9] = false;
	end
end

function WindChatConfig_change12Hour()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[10] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[10] = false;
	end
end

function WindChatConfig_changeTimeColor()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[11] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[11] = false;
	end
end

function WindChatConfig_OpenColorPicker()
	local color = WindChatConfigAll[12];
	local r, g, b;

	if (color and string.find(color, "%x%x%x%x%x%x")) then
		r = tonumber(string.sub(color, 1, 2), 16) / 255.0;
		g = tonumber(string.sub(color, 3, 4), 16) / 255.0;
		b = tonumber(string.sub(color, 5, 6), 16) / 255.0;
	else
		r = 1.0;
		g = 1.0;
		b = 1.0;
	end
	ColorPickerFrame.previousValue  =  color;
	ColorPickerFrame.previousValues = {r, g, b};
	ColorPickerFrame.func           =  WindChatConfig_SaveColorPicker;
	ColorPickerFrame.cancelFunc     =  WindChatConfig_CancelColorPicker;
	ColorPickerFrame.hasOpacity     =  false;
	ColorPickerFrame:SetColorRGB(r, g, b);
	ShowUIPanel(ColorPickerFrame);
end

function WindChatConfig_SaveColorPicker()
	local r, g, b = ColorPickerFrame:GetColorRGB();
	WindChatConfigTIMECOLORNormalTexture:SetVertexColor(r, g, b);
	WindChatConfigAll[12] = WindChatConfig_FloatColorsToHex(r, g, b);
end

function WindChatConfig_CancelColorPicker()
	WindChatConfigAll[12] = ColorPickerFrame.previousValue;
	WindChatConfig_initTimeColor();
end

function WindChatConfig_FloatColorsToHex(r, g, b)
	return string.format("%02X%02X%02X", r * 255.0, g * 255.0, b * 255.0);
end

function WindChatConfig_changeSticky()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[13] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[13] = false;
	end
	WindChat_setSticky();
end

function WindChatConfig_changeStrings()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[14] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[14] = false;
	end
	WindChat_setStrings();
end

function WindChatConfig_changeRaid()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[15] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[15] = false;
	end
end

function WindChatConfig_changeNameColor()
	if (this:GetChecked()) then
		this:SetChecked(1);
		WindChatConfigAll[16] = true;
	else
		this:SetChecked(0);
		WindChatConfigAll[16] = false;
	end
end