-- For information, visit the GemHelper development page at:
--	http://www.wowace.com/addons/gem-helper/

-- Local variables
local GEMHELPER_DISP_HEIGHT	= 16
local GEMHELPER_ITEMS_SHOWN	= 8

-- For setting up use
local GemHelper_CheckButtonIndexes = {	1, 2, 3, 4, 5,
					11, 12, 13, 14, 15, 16,
					21, 22, 23, 24, 25, 26, 27,
					31, 32, 33, 34, 35, 36, 37,
					41, 42,
					51, 52, 53, 54, 55, 56,
					121, 122, 123, 124, 125, 126, 127,
					131, 132, 133, 134, 135, 136, 137,
					141, 142, 143,
					151, 152, 153, 154, 155, 156,
					161, 162, 163, 164, 165, 166, 168,
					171, 172, 173, 174, 175, 176,
					177,
					181, 182, 183, 184, 185, 186,
					190, 191, 192, 193, 194, 195,
					196, 197,
					200, 201, 202, 203, 204, 205,
					210,
					61, 62, 63, 64, 65, 
					70,     72, 74, 76, 66,		-- 71
					81, 82, 84, 85, 86,
					90, 91, 
					101, 102, 103, 104, 105,
					1011, 1012, 1013,
					111 }
-- For filtering use
local GemHelper_GCheckButtonIndexes = { {11, 12, 13, 14, 15, 16},
					{
					210, 
					200, 201, 202, 203, 204, 205,
					196, 197,
					190, 191, 192, 193, 194, 195,
					181, 182, 183, 184, 185, 186,
					171, 172, 173, 174, 175, 176,
					177, 168,
					161, 162, 163, 164, 165, 166,
					151, 152, 153, 154, 155, 156,
					142, 141, 137, 127, 143,
					131, 132, 133, 134, 135, 136,
					121, 122, 123, 124, 125, 126,
					51, 52, 53, 54, 55, 56,
					42, 41, 37, 27,
					31, 32, 33, 34, 35, 36,
					21, 22, 23, 24, 25, 26
					},
					{61, 62, 63, 64, 65, 
					70,     72, 74, 76, 66,		-- 71
					81, 82, 84, 85, 86,
					90, 91},
					{101, 102, 103, 104, 105},
					[101] = {1011, 1012, 1013} }
-- For collapsing use
local GemHelper_CollapseHeight	= { {66, 30},  -- 50
				{190, 30},
				{134, 30} }

local GemHelper_DispResults = {}
local selectedGemID = nil
local info = {}
local bagScan = {}
local otherPlayer = {}

-- Localize some globals
local _G = getfenv(0)
local tsort, tinsert = table.sort, tinsert
local pairs, ipairs = pairs, ipairs
local tonumber = tonumber
local strfind, strsplit, gsub = strfind, strsplit, gsub
local GetItemInfo, GetContainerNumSlots, GetContainerItemLink = GetItemInfo, GetContainerNumSlots, GetContainerItemLink
local GetTradeSkillItemLink, GetTradeSkillInfo = GetTradeSkillItemLink, GetTradeSkillInfo
local GameTooltip = GameTooltip
local wipe = wipe

-- Localize some tables
local GemHelper_GemData = GemHelper_GemData
local GemHelper_GemLocaleData = GemHelper_GemLocaleData
local GemHelper_MaterialGemID = GemHelper_MaterialGemID
local GemHelper_ReverseMaterialGemID = GemHelper_ReverseMaterialGemID
local ITEM_QUALITY_COLORS = ITEM_QUALITY_COLORS


---------------------------------------------------------
-- Gem Helper functions

local function GemHelper_Init()
	local checkButton, checkButtonText
	local CreateFrame = CreateFrame

	if not GemHelper_Save then
		-- SavedVariable
		GemHelper_Save = {}
	end

	if not GemHelper_SaveCraft then
		-- SavedVariable
		GemHelper_SaveCraft = {}
	end

	-- Background color defaults
	if not GemHelper_Save.r then GemHelper_Save.r = 0 end
	if not GemHelper_Save.g then GemHelper_Save.g = 0 end
	if not GemHelper_Save.b then GemHelper_Save.b = 0 end
	if not GemHelper_Save.a then GemHelper_Save.a = 0.5 end
	GemHelper_Frame:SetBackdropColor(GemHelper_Save.r, GemHelper_Save.g, GemHelper_Save.b, GemHelper_Save.a)
	GemHelper_ColorSwatchNormalTexture:SetVertexColor(GemHelper_Save.r, GemHelper_Save.g, GemHelper_Save.b)

	-- Default positioning
	if not GemHelper_Save.pospoint then
		GemHelper_Save.pospoint = "TOPLEFT"
		GemHelper_Save.posrelpoint = "TOPLEFT"
		GemHelper_Save.posoffsetx = 300
		GemHelper_Save.posoffsety = -150
	end
	GemHelper_Frame:ClearAllPoints()
	GemHelper_Frame:SetPoint(GemHelper_Save.pospoint, nil, GemHelper_Save.posrelpoint, GemHelper_Save.posoffsetx, GemHelper_Save.posoffsety)
	GemHelper_Frame:SetUserPlaced(nil)

	GemHelper_ScrollFrame2 = CreateFrame("ScrollFrame", "GemHelper_ScrollFrame2", GemHelper_Frame, "GemHelper_ScrollFrameBarTemplate")
--	GemHelper_Frame.ScrollFrame = GemHelper_ScrollFrame2
	GemHelper_ContentFrame2 = CreateFrame("Frame", "GemHelper_ContentFrame2", GemHelper_ScrollFrame2)

	for k, v in ipairs(GemHelper_CheckButtonIndexes) do
		if GemHelper_Save[v] == nil then
			-- SavedVariable Defaults
			if v == 101 or v == 105 then
				GemHelper_Save[v] = true
			else
				GemHelper_Save[v] = false
			end
		end

		if (v > 20 and v < 60) or (v > 120 and v < 220) then
			checkButton = CreateFrame("CheckButton", "GemHelper_CheckButton"..v, GemHelper_ContentFrame2, "GemHelperCheckButtonTemplate")
		else
			checkButton = CreateFrame("CheckButton", "GemHelper_CheckButton"..v, GemHelper_Frame, "GemHelperCheckButtonTemplate")
		end --if
		checkButton:SetID(v)
		checkButton:SetFrameLevel(checkButton:GetFrameLevel() + 1)
		checkButtonText = _G["GemHelper_CheckButton"..v.."Text"]
		checkButtonText:SetText(GEMHELPER_CHECKBUTTON_TEXT[v])
		checkButton:SetHitRectInsets(5, -(checkButtonText:GetWidth() + 5), 5, 5)
		checkButton:SetChecked(GemHelper_Save[v])
	end
	
	for v = 1, 4 do
		checkButton = CreateFrame("CheckButton", "GemHelper_Background"..v, GemHelper_Frame, "GemHelperOptionBackground")
	end

	for v = 1, 3 do
		checkButton = CreateFrame("Button", "GemHelper_CollapseButton"..v, GemHelper_Frame, "GemHelper_CollapseButtonTemplate")
		RaiseFrameLevel(checkButton)
		checkButton:SetID(v)
		checkButton:SetPoint("TOPRIGHT", _G["GemHelper_Background"..v], "TOPRIGHT", -3, -3)
		if GemHelper_Save[-v] == nil then
			-- SavedVariable Defaults
			GemHelper_Save[-v] = false
		end

		checkButton = CreateFrame("Button", "GemHelper_ResetButton"..v, GemHelper_Frame, "GemHelper_ResetButtonTemplate")
		RaiseFrameLevel(checkButton)
		checkButton:SetID(v)
		checkButton:SetPoint("TOPRIGHT", _G["GemHelper_CollapseButton"..v], "TOPLEFT", 5, 0)
	end

	GemHelper_CheckButton1:SetPoint("TOPLEFT", GemHelper_Frame, "TOPLEFT", 25, -20)
	GemHelper_CheckButton5:SetPoint("TOPLEFT", GemHelper_CheckButton1, "TOPLEFT", 150, 0)
	GemHelper_CheckButton11:SetPoint("TOPLEFT", GemHelper_CheckButton1, "BOTTOMLEFT", 25, 6)
	GemHelper_CheckButton12:SetPoint("TOPLEFT", GemHelper_CheckButton1, "BOTTOMLEFT", 175, 6)
	GemHelper_CheckButton13:SetPoint("TOPLEFT", GemHelper_CheckButton1, "BOTTOMLEFT", 325, 6)
	GemHelper_CheckButton14:SetPoint("TOPLEFT", GemHelper_CheckButton11, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton15:SetPoint("TOPLEFT", GemHelper_CheckButton12, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton16:SetPoint("TOPLEFT", GemHelper_CheckButton13, "BOTTOMLEFT", 0, 6)

	GemHelper_CheckButton2:SetPoint("TOPLEFT", GemHelper_CheckButton1, "TOPLEFT", 0, -GemHelper_CollapseHeight[1][1])
	GemHelper_CheckButton210:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 25, 2)  -- column1

	GemHelper_CheckButton200:SetPoint("TOPLEFT", GemHelper_CheckButton210, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton201:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 130, -14)  -- column2
	GemHelper_CheckButton202:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 235, -14)  -- column3
	GemHelper_CheckButton203:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 340, -14)  -- column4
	GemHelper_CheckButton204:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 445, -14)  -- column5
	GemHelper_CheckButton205:SetPoint("TOPLEFT", GemHelper_ContentFrame2, "TOPLEFT", 550, -14)  -- column6

	GemHelper_CheckButton196:SetPoint("TOPLEFT", GemHelper_CheckButton200, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton197:SetPoint("TOPLEFT", GemHelper_CheckButton201, "BOTTOMLEFT", 0, 6)  -- column2

	GemHelper_CheckButton190:SetPoint("TOPLEFT", GemHelper_CheckButton196, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton191:SetPoint("TOPLEFT", GemHelper_CheckButton197, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton192:SetPoint("TOPLEFT", GemHelper_CheckButton202, "BOTTOMLEFT", 0, -14)  -- column3
	GemHelper_CheckButton193:SetPoint("TOPLEFT", GemHelper_CheckButton203, "BOTTOMLEFT", 0, -14)  -- column4
	GemHelper_CheckButton194:SetPoint("TOPLEFT", GemHelper_CheckButton204, "BOTTOMLEFT", 0, -14)  -- column5
	GemHelper_CheckButton195:SetPoint("TOPLEFT", GemHelper_CheckButton205, "BOTTOMLEFT", 0, -14)  -- column6

	GemHelper_CheckButton181:SetPoint("TOPLEFT", GemHelper_CheckButton190, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton182:SetPoint("TOPLEFT", GemHelper_CheckButton191, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton183:SetPoint("TOPLEFT", GemHelper_CheckButton192, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton184:SetPoint("TOPLEFT", GemHelper_CheckButton193, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton185:SetPoint("TOPLEFT", GemHelper_CheckButton194, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton186:SetPoint("TOPLEFT", GemHelper_CheckButton195, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton171:SetPoint("TOPLEFT", GemHelper_CheckButton181, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton172:SetPoint("TOPLEFT", GemHelper_CheckButton182, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton173:SetPoint("TOPLEFT", GemHelper_CheckButton183, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton174:SetPoint("TOPLEFT", GemHelper_CheckButton184, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton175:SetPoint("TOPLEFT", GemHelper_CheckButton185, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton176:SetPoint("TOPLEFT", GemHelper_CheckButton186, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton177:SetPoint("TOPLEFT", GemHelper_CheckButton171, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton168:SetPoint("TOPLEFT", GemHelper_CheckButton172, "BOTTOMLEFT", 0, 6)  -- column2

	GemHelper_CheckButton161:SetPoint("TOPLEFT", GemHelper_CheckButton177, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton162:SetPoint("TOPLEFT", GemHelper_CheckButton168, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton163:SetPoint("TOPLEFT", GemHelper_CheckButton173, "BOTTOMLEFT", 0, -14)  -- column3
	GemHelper_CheckButton164:SetPoint("TOPLEFT", GemHelper_CheckButton174, "BOTTOMLEFT", 0, -14)  -- column4
	GemHelper_CheckButton165:SetPoint("TOPLEFT", GemHelper_CheckButton175, "BOTTOMLEFT", 0, -14)  -- column5
	GemHelper_CheckButton166:SetPoint("TOPLEFT", GemHelper_CheckButton176, "BOTTOMLEFT", 0, -14)  -- column6

	GemHelper_CheckButton151:SetPoint("TOPLEFT", GemHelper_CheckButton161, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton152:SetPoint("TOPLEFT", GemHelper_CheckButton162, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton153:SetPoint("TOPLEFT", GemHelper_CheckButton163, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton154:SetPoint("TOPLEFT", GemHelper_CheckButton164, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton155:SetPoint("TOPLEFT", GemHelper_CheckButton165, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton156:SetPoint("TOPLEFT", GemHelper_CheckButton166, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton142:SetPoint("TOPLEFT", GemHelper_CheckButton151, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton141:SetPoint("TOPLEFT", GemHelper_CheckButton152, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton137:SetPoint("TOPLEFT", GemHelper_CheckButton153, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton127:SetPoint("TOPLEFT", GemHelper_CheckButton154, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton143:SetPoint("TOPLEFT", GemHelper_CheckButton155, "BOTTOMLEFT", 0, 6)  -- column5

	GemHelper_CheckButton131:SetPoint("TOPLEFT", GemHelper_CheckButton142, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton132:SetPoint("TOPLEFT", GemHelper_CheckButton141, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton133:SetPoint("TOPLEFT", GemHelper_CheckButton137, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton134:SetPoint("TOPLEFT", GemHelper_CheckButton127, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton135:SetPoint("TOPLEFT", GemHelper_CheckButton143, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton136:SetPoint("TOPLEFT", GemHelper_CheckButton156, "BOTTOMLEFT", 0, -14)  -- column6

	GemHelper_CheckButton121:SetPoint("TOPLEFT", GemHelper_CheckButton131, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton122:SetPoint("TOPLEFT", GemHelper_CheckButton132, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton123:SetPoint("TOPLEFT", GemHelper_CheckButton133, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton124:SetPoint("TOPLEFT", GemHelper_CheckButton134, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton125:SetPoint("TOPLEFT", GemHelper_CheckButton135, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton126:SetPoint("TOPLEFT", GemHelper_CheckButton136, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton51:SetPoint("TOPLEFT", GemHelper_CheckButton121, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton52:SetPoint("TOPLEFT", GemHelper_CheckButton122, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton53:SetPoint("TOPLEFT", GemHelper_CheckButton123, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton54:SetPoint("TOPLEFT", GemHelper_CheckButton124, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton55:SetPoint("TOPLEFT", GemHelper_CheckButton125, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton56:SetPoint("TOPLEFT", GemHelper_CheckButton126, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton42:SetPoint("TOPLEFT", GemHelper_CheckButton51, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton41:SetPoint("TOPLEFT", GemHelper_CheckButton52, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton37:SetPoint("TOPLEFT", GemHelper_CheckButton53, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton27:SetPoint("TOPLEFT", GemHelper_CheckButton54, "BOTTOMLEFT", 0, 6)  -- column4

	GemHelper_CheckButton31:SetPoint("TOPLEFT", GemHelper_CheckButton42, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton32:SetPoint("TOPLEFT", GemHelper_CheckButton41, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton33:SetPoint("TOPLEFT", GemHelper_CheckButton37, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton34:SetPoint("TOPLEFT", GemHelper_CheckButton27, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton35:SetPoint("TOPLEFT", GemHelper_CheckButton55, "BOTTOMLEFT", 0, -14)  -- column5
	GemHelper_CheckButton36:SetPoint("TOPLEFT", GemHelper_CheckButton56, "BOTTOMLEFT", 0, -14)  -- column6
	
	GemHelper_CheckButton21:SetPoint("TOPLEFT", GemHelper_CheckButton31, "BOTTOMLEFT", 0, 6)  -- column1
	GemHelper_CheckButton22:SetPoint("TOPLEFT", GemHelper_CheckButton32, "BOTTOMLEFT", 0, 6)  -- column2
	GemHelper_CheckButton23:SetPoint("TOPLEFT", GemHelper_CheckButton33, "BOTTOMLEFT", 0, 6)  -- column3
	GemHelper_CheckButton24:SetPoint("TOPLEFT", GemHelper_CheckButton34, "BOTTOMLEFT", 0, 6)  -- column4
	GemHelper_CheckButton25:SetPoint("TOPLEFT", GemHelper_CheckButton35, "BOTTOMLEFT", 0, 6)  -- column5
	GemHelper_CheckButton26:SetPoint("TOPLEFT", GemHelper_CheckButton36, "BOTTOMLEFT", 0, 6)  -- column6

	GemHelper_CheckButton3:SetPoint("TOPLEFT", GemHelper_CheckButton2, "TOPLEFT", 0, -GemHelper_CollapseHeight[2][1])
	GemHelper_CheckButton4:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 150, 0)
	GemHelper_CheckButton61:SetPoint("TOPLEFT", GemHelper_CheckButton3, "BOTTOMLEFT", 25, 6)
	GemHelper_CheckButton62:SetPoint("TOPLEFT", GemHelper_CheckButton61, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton63:SetPoint("TOPLEFT", GemHelper_CheckButton62, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton64:SetPoint("TOPLEFT", GemHelper_CheckButton63, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton65:SetPoint("TOPLEFT", GemHelper_CheckButton64, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton70:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 325, 0)
--	GemHelper_CheckButton71:SetPoint("TOPLEFT", GemHelper_CheckButton70, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton72:SetPoint("TOPLEFT", GemHelper_CheckButton70, "BOTTOMLEFT", 0, -14)
--	GemHelper_CheckButton81:SetPoint("TOPLEFT", GemHelper_CheckButton72, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton82:SetPoint("TOPLEFT", GemHelper_CheckButton72, "BOTTOMLEFT", 0, -14)
	GemHelper_CheckButton66:SetPoint("TOPLEFT", GemHelper_CheckButton82, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton74:SetPoint("TOPLEFT", GemHelper_CheckButton3, "BOTTOMLEFT", 175, 6)
	GemHelper_CheckButton76:SetPoint("TOPLEFT", GemHelper_CheckButton74, "BOTTOMLEFT", 0, 6)
--	GemHelper_CheckButton84:SetPoint("TOPLEFT", GemHelper_CheckButton76, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton85:SetPoint("TOPLEFT", GemHelper_CheckButton76, "BOTTOMLEFT", 0, -14)
	GemHelper_CheckButton86:SetPoint("TOPLEFT", GemHelper_CheckButton85, "BOTTOMLEFT", 0, 6)
	GemHelper_CheckButton90:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 475, 0)
	GemHelper_CheckButton91:SetPoint("TOPLEFT", GemHelper_CheckButton90, "BOTTOMLEFT", 0, 6)

	GemHelper_CheckButton101:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 275, -GemHelper_CollapseHeight[3][1])
	GemHelper_CheckButton1011:SetPoint("TOPLEFT", GemHelper_CheckButton101, "BOTTOMLEFT", 25, 11)
	GemHelper_CheckButton1012:SetPoint("TOPLEFT", GemHelper_CheckButton1011, "BOTTOMLEFT", 0, 9)
	GemHelper_CheckButton1013:SetPoint("TOPLEFT", GemHelper_CheckButton1012, "BOTTOMLEFT", 0, 9)
	GemHelper_CheckButton102:SetPoint("TOPLEFT", GemHelper_CheckButton1013, "BOTTOMLEFT", -25, 9)
	GemHelper_CheckButton103:SetPoint("TOPLEFT", GemHelper_CheckButton102, "BOTTOMLEFT", 0, 9)
	GemHelper_CheckButton104:SetPoint("TOPLEFT", GemHelper_CheckButton103, "BOTTOMLEFT", 0, 9)
	GemHelper_CheckButton105:SetPoint("TOPLEFT", GemHelper_CheckButton104, "BOTTOMLEFT", 0, 9)

	GemHelper_CheckButton111:SetPoint("LEFT", GemHelper_CreateButton, "RIGHT", 0, 0)

	for v = 1, GEMHELPER_ITEMS_SHOWN do
		checkButton = CreateFrame("Button", "GemHelper_ItemButton"..v, GemHelper_Frame, "GemHelper_ItemButtonTemplate")
		RaiseFrameLevel(checkButton)
		if v == 1 then
			checkButton:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[3][1] - 5)
		else
			checkButton:SetPoint("TOPLEFT", _G["GemHelper_ItemButton"..v-1], "BOTTOMLEFT", 0, 0)
		end
	end

	checkButton = CreateFrame("ScrollFrame", "GemHelper_ListScrollFrame", GemHelper_Frame, "FauxScrollFrameTemplate")
	GemHelper_ListScrollFrame:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[3][1] - 5)
	GemHelper_ListScrollFrame:SetWidth(GemHelper_ItemButton1:GetWidth() - 3)
	GemHelper_ListScrollFrame:SetHeight(GemHelper_ItemButton1:GetHeight() * GEMHELPER_ITEMS_SHOWN)
	GemHelper_ListScrollFrame:SetScript("OnVerticalScroll", function(self, offset)
		FauxScrollFrame_OnVerticalScroll(self, offset, GEMHELPER_DISP_HEIGHT, function()
			GemHelper_UpdateFrame:SetScript("OnUpdate", GemHelper_UpdateFrame_OnUpdate)
		end)
	end)

	GemHelper_Background1:SetPoint("TOPLEFT", GemHelper_CheckButton1)
	GemHelper_Background1:SetWidth(665)
	GemHelper_Background1:SetHeight(GemHelper_CollapseHeight[1][1])
	GemHelper_Background2:SetPoint("TOPLEFT", GemHelper_CheckButton2)
	GemHelper_Background2:SetWidth(665)
	GemHelper_Background2:SetHeight(GemHelper_CollapseHeight[2][1])
	GemHelper_ScrollFrame2:SetPoint("TOPLEFT", GemHelper_CheckButton2, 0, -24)
	GemHelper_ScrollFrame2:SetWidth(665)
	GemHelper_ScrollFrame2:SetHeight(GemHelper_CollapseHeight[2][1] - (24 + 4))
	GemHelper_ContentFrame2:SetWidth(665)
	GemHelper_ContentFrame2:SetHeight(236) --224
	GemHelper_Background3:SetPoint("TOPLEFT", GemHelper_CheckButton3)
	GemHelper_Background3:SetWidth(665)
	GemHelper_Background3:SetHeight(GemHelper_CollapseHeight[3][1])
	GemHelper_Background4:SetPoint("TOPLEFT", GemHelper_ItemButton1, "TOPLEFT", -5, 5)
	GemHelper_Background4:SetWidth(GemHelper_ItemButton1:GetWidth() + 10)
	GemHelper_Background4:SetHeight(GemHelper_ItemButton1:GetHeight() * GEMHELPER_ITEMS_SHOWN + 10)

	GemHelper_ScrollFrame2:SetScrollChild(GemHelper_ContentFrame2)

	GemHelper_CreateAllButton:SetPoint("TOPLEFT", GemHelper_Background4, "BOTTOMLEFT", 0, 0)
	GemHelper_CreateAllButton:Disable()
	GemHelper_CreateButton:Disable()

	-- Filter by Material
	for _, v in ipairs(GemHelper_GCheckButtonIndexes[2]) do
		checkButton = _G["GemHelper_CheckButton"..v]
		checkButton:SetScript("OnEnter", GemHelper_CheckboxMaterial_OnEnter)
		checkButtonText = _G["GemHelper_CheckButton"..v.."Text"]
		checkButtonText:SetWidth(80)
		checkButtonText:SetHeight(16)
		checkButtonText:SetJustifyH("LEFT")
		checkButtonText:SetText(GEMHELPER_CHECKBUTTON_TEXT[v]..(GetItemInfo(GemHelper_MaterialGemID[v]) or ""))
		checkButton:SetHitRectInsets(5, -(checkButtonText:GetWidth() + 5), 5, 5)
	end
--	GemHelper_CheckButton141Text:SetWidth(40)
--	GemHelper_CheckButton141:SetHitRectInsets(5, -40, 5, 5)

	for buttonID = 1, 3 do
		if GemHelper_Save[-buttonID] then
			-- Collapse
			for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
				_G["GemHelper_CheckButton"..v]:Hide()
			end
			checkButton = _G["GemHelper_CollapseButton"..buttonID]
			checkButton:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up")
			checkButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round")
			checkButton:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled")
			checkButton:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down")
			_G["GemHelper_Background"..buttonID]:SetHeight(GemHelper_CollapseHeight[buttonID][2])
			if buttonID < 3 then
				_G["GemHelper_CheckButton"..(buttonID+1)]:SetPoint("TOPLEFT", _G["GemHelper_CheckButton"..buttonID], "TOPLEFT", 0, -GemHelper_CollapseHeight[buttonID][2])
			else
				GemHelper_ListScrollFrame:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][2] - 5)
				GemHelper_CheckButton101:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 275, -GemHelper_CollapseHeight[buttonID][2])
				GemHelper_ItemButton1:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][2] - 5)
			end
			GemHelper_Frame:SetHeight(GemHelper_Frame:GetHeight() - GemHelper_CollapseHeight[buttonID][1] + GemHelper_CollapseHeight[buttonID][2])
		else
			-- Uncollapse
			for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
				_G["GemHelper_CheckButton"..v]:Show()
			end
			checkButton = _G["GemHelper_CollapseButton"..buttonID]
			checkButton:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Up")
			checkButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round")
			checkButton:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Disabled")
			checkButton:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Down")
			_G["GemHelper_Background"..buttonID]:SetHeight(GemHelper_CollapseHeight[buttonID][1])
			if buttonID < 3 then
				_G["GemHelper_CheckButton"..(buttonID+1)]:SetPoint("TOPLEFT", _G["GemHelper_CheckButton"..buttonID], "TOPLEFT", 0, -GemHelper_CollapseHeight[buttonID][1])
			else
				GemHelper_ListScrollFrame:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][1] - 5)
				GemHelper_CheckButton101:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 275, -GemHelper_CollapseHeight[buttonID][1])
				GemHelper_ItemButton1:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][1] - 5)
			end
		end
	end
	-- Resize frame based on whether material filtering is collapsed
	if GemHelper_Save[-2] then
		GemHelper_Frame:SetWidth(655)
		GemHelper_Background1:SetWidth(605)
		GemHelper_Background2:SetWidth(605)
		GemHelper_ScrollFrame2:Hide()
		GemHelper_Background3:SetWidth(605)
	else
		GemHelper_Frame:SetWidth(715)
		GemHelper_Background1:SetWidth(665)
		GemHelper_Background2:SetWidth(665)
		GemHelper_ScrollFrame2:Show()
		GemHelper_Background3:SetWidth(665)
	end

	checkButton = CreateFrame("Frame", "GemHelper_UserDropdown", GemHelper_CheckButton1012, "UIDropDownMenuTemplate")

	GemHelper_UserDropdownLeft:Hide()
	GemHelper_UserDropdownMiddle:Hide()
	GemHelper_UserDropdownRight:Hide()
	GemHelper_UserDropdownText:Hide()
	GemHelper_UserDropdownButtonHighlightTexture:SetTexture("Interface\\Buttons\\ButtonHilight-Round")
	GemHelper_UserDropdown:SetPoint("LEFT", GemHelper_CheckButton1012, "LEFT", 3, -3)
	GemHelper_CheckButton1012Text:SetPoint("LEFT", GemHelper_CheckButton1012, "RIGHT", 17, 0)
	-- Workaround for positional bug
	GemHelper_CheckButton1012:SetScale(1.01)
	GemHelper_CheckButton1012:SetScale(1)

	GemHelper_Frame:CreateFontString("GemHelper_WhoText", "ARTWORK", "GameFontNormal")
	GemHelper_WhoText:SetPoint("TOPLEFT", GemHelper_CheckButton101, "TOPLEFT", 4, -6)

	-- Add the slash command
	SlashCmdList["GEMHELPER"] = function(msg)
		if GemHelper_Frame:IsVisible() then
			GemHelper_Frame:Hide()
		else
			GemHelper_Frame:Show()
		end
	end
	SLASH_GEMHELPER1 = "/gemhelper"
	SLASH_GEMHELPER2 = "/gh"
	-- Workaround for AddOnLoader bug
	if IsAddOnLoaded("AddOnLoader") then
		SLASH_GH1 = nil
	end

	-- Makes ESC key close GemHelper
	tinsert(UISpecialFrames, "GemHelper_Frame")

	GemHelper_UpdateCheckboxes(1)
	GemHelper_UpdateCheckboxes(2)
	GemHelper_UpdateCheckboxes(3)
	GemHelper_UpdateCheckboxes(101)

	hooksecurefunc("CloseTradeSkill", GemHelper_Update)

	if IsLoggedIn() then
		if GemHelper_isJewelcraftingPanel() then
			GemHelper_ScanJCPanel()
			if GemHelper_Save[111] == true then
				GemHelper_Frame:Show()
				HideUIPanel(TradeSkillFrame)
				GemHelper_HookTradeSkillFrame_Show()
				if ATSWFrame then
					HideUIPanel(ATSWFrame)
					GemHelper_ATSW_ShowWindow()
				end
				if SkilletFrame then
					Skillet:HideTradeSkillWindow()
					GemHelper_Skillet_ShowWindow()
				end
			end
		end
	end

	GemHelper_Frame:UnregisterEvent("ADDON_LOADED")

	-- Create a frame for doing OnUpdates, this isn't used for anything else or shown
	-- This is to reduce the number of times GemHelper updates inventory changes
	GemHelper_UpdateFrame = CreateFrame("Frame")
end

function GemHelper_Filter()
	local GemHelper_Save = GemHelper_Save
	wipe(GemHelper_DispResults)
	-- Scan bags for filter for "Have Materials"
	if GemHelper_Save[101] and GemHelper_Save[1011] then
		local _, bagNum_Slots, itemLink, itemID
		wipe(bagScan)
		for bagNum = 0, 4 do
			bagNum_Slots = GetContainerNumSlots(bagNum)
			for bagItem = 1, bagNum_Slots do
				itemLink = GetContainerItemLink(bagNum, bagItem)
				itemID = nil
				if itemLink then
					_, _, itemID = strfind(itemLink, "item:(%d+):")
					itemID = tonumber(itemID)
				end
				if itemID and GemHelper_ReverseMaterialGemID[itemID] then
					bagScan[GemHelper_ReverseMaterialGemID[itemID]] = true
				end
			end
		end
	end

	local isLinked, playerName = IsTradeSkillLinked()
	local isGuildSkillLink = IsTradeSkillGuild()  -- link from "view all" on guild roster linking all cuts known by guild members
	local t2
	if isLinked or isGuildSkillLink then
		t2 = otherPlayer
	else
		playerName = GemHelper_UserDropdown_GetValue()
		t2 = GemHelper_SaveCraft[playerName]
	end
	local flag, flag2
	for gemID, gemTable in pairs(GemHelper_GemData) do
		-- Filter by Gem Colour
		flag1 = true
		if GemHelper_Save[1] then
			flag1 = false
			local t = GemHelper_GCheckButtonIndexes[1]
			for i = 1, #t do
				local j = t[i]
				if GemHelper_Save[5] then  -- Use AND logic
					if not GemHelper_Save[j] and gemTable[j] then
						flag1 = false
						break
					else
						flag1 = true
					end
				else    -- Use OR logic
					if GemHelper_Save[j] and gemTable[j] then
						flag1 = true
						break
					end
				end
			end
		end

		-- Filter by "Exclude non-WotLK gems"
--		flag2 = true
--		if flag1 and GemHelper_Save[105] then
--			flag2 = true
--			if gemTable.r < 10 then
--				flag2 = false
--			end
--		end
--		flag1 = flag1 and flag2

		-- Filter by Material
		flag2 = true
		if flag1 and GemHelper_Save[2] then
			flag2 = false
			local t = GemHelper_GCheckButtonIndexes[2]
			for i = 1, #t do
				local j = t[i]
				if GemHelper_Save[j] and gemTable[j] then
					flag2 = true
					break
				end
			end
		end
		flag1 = flag1 and flag2

		-- Filter by Statistic
		flag2 = true
		if flag1 and GemHelper_Save[3] then
			flag2 = false
			local t = GemHelper_GCheckButtonIndexes[3]
			for i = 1, #t do
				local j = t[i]
				if GemHelper_Save[4] then  -- Use AND logic
					if not GemHelper_Save[j] and gemTable[j] or gemTable[0] then
						flag2 = false
						break
					else
						flag2 = true
					end
				else    -- Use OR logic
					if GemHelper_Save[j] and gemTable[j] then
						flag2 = true
						break
					end
				end
			end
		end
		flag1 = flag1 and flag2

		-- Filter by "Exclude JC-Only" --- Luc's mod
		flag2 = true
		if flag1 and GemHelper_Save[101] and GemHelper_Save[1013] then
			flag2 = true
			if gemTable[1013] then
				flag2 = false
			end
		end
		flag1 = flag1 and flag2

		-- Filter by Creation
		flag2 = true
		if flag1 and not isLinked and not isGuildSkillLink then
			flag2 = false
			local t = GemHelper_GCheckButtonIndexes[4]
			for i = 1, #t do
				local j = t[i]
				if GemHelper_Save[j] and gemTable[j] then
					flag2 = true
					break
				end
			end
		end
		flag1 = flag1 and flag2

		-- Filter for "Have Materials"
		flag2 = true
		if flag1 and (GemHelper_Save[101] or isLinked or isGuildSkillLink) and GemHelper_Save[1011] then
			flag2 = false
			local t = GemHelper_GCheckButtonIndexes[2]
			for i = 1, #t do
				local j = t[i]
				if bagScan[j] and gemTable[j] then
					flag2 = true
					break
				end
			end
		end
		flag1 = flag1 and flag2

		-- Filter for "That I can cut" / linked JC can cut
		flag2 = true
		if isLinked or isGuildSkillLink then
		-- Auto filter for what [Link] can cut
			if flag1 then
				flag2 = false
				if t2[gemID] then flag2 = true end
			end
		else
		-- Filter for "That I can cut"
			if flag1 and GemHelper_Save[101] and GemHelper_Save[1012] then
				flag2 = false
				if t2[gemID] then flag2 = true end
			end
		end --if
		
		if flag1 and flag2 then
			-- Gem matches filters
			tinsert(GemHelper_DispResults, gemID)
		end
	end

	tsort(GemHelper_DispResults, GemHelper_SortResults)
end

function GemHelper_SortResults(a, b)
	local ra, rb = GemHelper_GemData[a].r, GemHelper_GemData[b].r
	if ra ~= rb then	-- Rarity sort
		return ra > rb
	else	-- If same rarity then go by name
		local la, lb = GemHelper_GemLocaleData[a], GemHelper_GemLocaleData[b]
		if la and lb then
			local na, nb = la.n, lb.n
			if na and nb then
				return na < nb
			end
		end
	end
	return a < b	-- If name data isn't available, then go by gem ID
end

function GemHelper_UpdateFrame_OnUpdate(self, elapsed)
	self:SetScript("OnUpdate", nil)
	GemHelper_Update()
end

function GemHelper_OnEvent(self, event, ...)
	local arg1, arg2, arg3, arg4 = ...
	if ((event == "UNIT_INVENTORY_CHANGED" and arg1 == "player") or event == "BAG_UPDATE") and GemHelper_Save[101] and GemHelper_Save[1011] then
		-- Delay updating to the next frame as multiple UNIT_INVENTORY_CHANGED events can occur in 1 frame
		GemHelper_UpdateFrame:SetScript("OnUpdate", GemHelper_UpdateFrame_OnUpdate)
	elseif event == "TRADE_SKILL_UPDATE" then
		GemHelper_UpdateFrame:SetScript("OnUpdate", GemHelper_UpdateFrame_OnUpdate)
	elseif event == "TRADE_SKILL_SHOW" then
		if GemHelper_isJewelcraftingPanel() then
			if GemHelper_Save[111] == true then
				GemHelper_Frame:Show()
			else
				GemHelper_ScanJCPanel()
			end
		end
	elseif event == "TRADE_SKILL_CLOSE" then
		if GemHelper_Save[111] == true then
			GemHelper_Frame:Hide()
		elseif GemHelper_Frame:IsVisible() then
			GemHelper_Update()
		end
	elseif event == "UPDATE_TRADESKILL_RECAST" then
		GemHelper_InputBox:SetNumber(GetTradeskillRepeatCount())
	elseif event == "ADDON_LOADED" and arg1 == "GemHelper" then
		GemHelper_Init()
		GemHelper_Init = nil
	end
end

function GemHelper_isJewelcraftingPanel()
	if strfind(GetTradeSkillLine(), GemHelper_Localize.String.Jewelcrafting) then
		return true
	else
		return false
	end
end

--[[function GemHelper_isJewelcrafter()
	local skillname, isHeader
	for i = 1, GetNumSkillLines() do
		skillname, isHeader = GetSkillLineInfo(i)
		if not isHeader then
			if strfind(skillname, GemHelper_Localize.String.Jewelcrafting) then
				return true
			end
		end
	end
	return false
end]]

function GemHelper_ScanJCPanel()
	playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	if not GemHelper_SaveCraft[playerName] then
		GemHelper_SaveCraft[playerName] = {}
	end
	local t = GemHelper_SaveCraft[playerName]
	for k in pairs(t) do
		t[k] = 0 -- Set all to 0 index
	end
	-- Now check if its a linked tradeskill of another player
	local isLinked, otherPlayerName = IsTradeSkillLinked()
	local isGuildSkillLink = IsTradeSkillGuild()  -- link from "view all" on guild roster linking all cuts known by guild members
	if isLinked or isGuildSkillLink then
		wipe(otherPlayer)
		t = otherPlayer
	end

	if GemHelper_isJewelcraftingPanel() then
		local skillName, skillType, numAvailable, isExpanded
		local _, itemID

		for i = 1, GetNumTradeSkills() do
			skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(i)
			if skillType ~= "header" and GetTradeSkillItemLink(i) then
				_, _, itemID = strfind(GetTradeSkillItemLink(i), "item:(%d+):")
				itemID = tonumber(itemID)
				if GemHelper_GemData[itemID] then
					t[itemID] = i -- Set to tradeskill index
				end
			end
		end
	end
end

function GemHelper_Trim(s)
	return gsub(s, "^%s*(.-)%s*$", "%1")
end

function GemHelper_CheckButton_OnClick(this, button, down)
	local buttonID = this:GetID()
	-- If user shift-clicked a material to link it in chat, then do so
	if (buttonID > 20 and buttonID < 60) or (buttonID > 120 and buttonID < 220) then
		local itemName, itemLink, itemRarity = GetItemInfo(GemHelper_MaterialGemID[buttonID])
		local temp = BrowseName and BrowseName:IsVisible() and BrowseName:GetText()
		print(itemLink)
		if HandleModifiedItemClick(itemLink) then
			this:SetChecked(GemHelper_Save[buttonID])
			if IsAltKeyDown() and BrowseName and BrowseName:IsVisible() then
				BrowseName:SetText(temp..BrowseName:GetText())
			end
			return
		end
	end

	-- Else toggle the checkbox itself
	if GemHelper_Save[buttonID] then
		GemHelper_Save[buttonID] = false
	else
		GemHelper_Save[buttonID] = true
	end
	this:SetChecked(GemHelper_Save[buttonID])
	if (buttonID >= 1 and buttonID <= 3) or (buttonID == 101) then
		GemHelper_UpdateCheckboxes(buttonID)
	end
	PlaySound("igMainMenuOptionCheckBoxOn")
	GemHelper_Update()
end

function GemHelper_CheckboxMaterial_OnEnter(this)
	local buttonID = this:GetID()
	local itemName, itemLink, itemRarity = GetItemInfo(GemHelper_MaterialGemID[buttonID])
	GameTooltip:SetOwner(this, "ANCHOR_LEFT")
	if itemLink then
		checkButtonText = _G["GemHelper_CheckButton"..buttonID.."Text"]
		checkButtonText:SetText(GEMHELPER_CHECKBUTTON_TEXT[buttonID]..itemName)
	end
	-- Query server for it, it should always be available
	GameTooltip:SetHyperlink("item:"..GemHelper_MaterialGemID[buttonID])
end

function GemHelper_UpdateCheckboxes(buttonID)
	for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
		if GemHelper_Save[buttonID] then
			GemHelper_EnableCheckBox(_G["GemHelper_CheckButton"..v])
		else
			GemHelper_DisableCheckBox(_G["GemHelper_CheckButton"..v])
		end
	end
	if buttonID == 101 then
		if GemHelper_Save[101] then
			UIDropDownMenu_EnableDropDown(GemHelper_UserDropdown)
		else
			UIDropDownMenu_DisableDropDown(GemHelper_UserDropdown)
		end
	elseif buttonID == 3 then
		if GemHelper_Save[3] then
			GemHelper_EnableCheckBox(GemHelper_CheckButton4)
		else
			GemHelper_DisableCheckBox(GemHelper_CheckButton4)
		end
	elseif buttonID == 1 then
		if GemHelper_Save[1] then
			GemHelper_EnableCheckBox(GemHelper_CheckButton5)
		else
			GemHelper_DisableCheckBox(GemHelper_CheckButton5)
		end
	end
end

function GemHelper_DisableCheckBox(checkBox)
	--checkBox:Disable()
	checkBox:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check-Disabled")
	_G[checkBox:GetName().."Text"]:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b)
end

function GemHelper_EnableCheckBox(checkBox, setChecked, checked, isWhite)
	if setChecked then
		checkBox:SetChecked(checked)
	end
	--checkBox:Enable()
	checkBox:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check")
	if isWhite then
		_G[checkBox:GetName().."Text"]:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
	else
		_G[checkBox:GetName().."Text"]:SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b)
	end
end

function GemHelper_CollapseButton_OnClick(this, button, down)
	local buttonID = this:GetID()
	if GemHelper_Save[-buttonID] then
		-- Uncollapse
		GemHelper_Save[-buttonID] = false
		for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
			_G["GemHelper_CheckButton"..v]:Show()
		end
		this:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Up")
		this:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round")
		this:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Disabled")
		this:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollUp-Down")
		_G["GemHelper_Background"..buttonID]:SetHeight(GemHelper_CollapseHeight[buttonID][1])
		if buttonID < 3 then
			_G["GemHelper_CheckButton"..(buttonID+1)]:SetPoint("TOPLEFT", _G["GemHelper_CheckButton"..buttonID], "TOPLEFT", 0, -GemHelper_CollapseHeight[buttonID][1])
		else
			GemHelper_ListScrollFrame:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][1] - 5)
			GemHelper_CheckButton101:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 275, -GemHelper_CollapseHeight[buttonID][1])
			GemHelper_ItemButton1:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][1] - 5)
		end
		GemHelper_Frame:SetHeight(GemHelper_Frame:GetHeight() + GemHelper_CollapseHeight[buttonID][1] - GemHelper_CollapseHeight[buttonID][2])
	else
		-- Collapse
		GemHelper_Save[-buttonID] = true
		for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
			_G["GemHelper_CheckButton"..v]:Hide()
		end
		this:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up")
		this:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round")
		this:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled")
		this:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down")
		_G["GemHelper_Background"..buttonID]:SetHeight(GemHelper_CollapseHeight[buttonID][2])
		if buttonID < 3 then
			_G["GemHelper_CheckButton"..(buttonID+1)]:SetPoint("TOPLEFT", _G["GemHelper_CheckButton"..buttonID], "TOPLEFT", 0, -GemHelper_CollapseHeight[buttonID][2])
		else
			GemHelper_ListScrollFrame:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][2] - 5)
			GemHelper_CheckButton101:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 275, -GemHelper_CollapseHeight[buttonID][2])
			GemHelper_ItemButton1:SetPoint("TOPLEFT", GemHelper_CheckButton3, "TOPLEFT", 5, -GemHelper_CollapseHeight[buttonID][2] - 5)
		end
		GemHelper_Frame:SetHeight(GemHelper_Frame:GetHeight() - GemHelper_CollapseHeight[buttonID][1] + GemHelper_CollapseHeight[buttonID][2])
	end
	-- Resize frame based on whether material filtering is collapsed
	if GemHelper_Save[-2] then
		GemHelper_Frame:SetWidth(655)
		GemHelper_Background1:SetWidth(605)
		GemHelper_Background2:SetWidth(605)
		GemHelper_ScrollFrame2:Hide()
		GemHelper_Background3:SetWidth(605)
	else
		GemHelper_Frame:SetWidth(715)  -- 715
		GemHelper_Background1:SetWidth(665)
		GemHelper_Background2:SetWidth(665)
		GemHelper_ScrollFrame2:Show()
		GemHelper_Background3:SetWidth(665)
	end
end

function GemHelper_ResetButton_OnClick(this, button, down)
	local buttonID = this:GetID()
	for k, v in ipairs(GemHelper_GCheckButtonIndexes[buttonID]) do
		if GemHelper_Save[v] then
			GemHelper_Save[v] = false
		end
		_G["GemHelper_CheckButton"..v]:SetChecked(GemHelper_Save[v])
	end
	PlaySound("igMainMenuOptionCheckBoxOff")
	GemHelper_Update()
end

function GemHelper_Update()
	if not GemHelper_Frame:IsVisible() then
		return
	end
	-- Scan JC panel and generate results
	GemHelper_ScanJCPanel()
	GemHelper_Filter()

	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local isLinked, otherPlayerName
	local isGuildSkillLink
	local t
	if GemHelper_isJewelcraftingPanel() then
		GemHelper_PlusButton:Show()
		isLinked, otherPlayerName = IsTradeSkillLinked()
		isGuildSkillLink = IsTradeSkillGuild()  -- link from "view all" on guild roster linking all cuts known by guild members
		if isLinked or isGuildSkillLink then
			GemHelper_TradeSkillLinkButton:Hide()
			GemHelper_WhoText:Show()
			if not otherPlayerName and isLinked then
				otherPlayerName = "UNKNOWN"     -- doesn't seem to be set properly if guild member is offline and clicking on jc link in roster 
			elseif isGuildSkillLink then
				otherPlayerName = "GUILD"
			end --if
			GemHelper_WhoText:SetFormattedText(GemHelper_Localize.String.ShowOtherJC, otherPlayerName)
			GemHelper_CheckButton101:Hide()
			GemHelper_CheckButton1011:Show()
			GemHelper_CheckButton1012:Hide()
			GemHelper_CheckButton102:Hide()
			GemHelper_CheckButton103:Hide()
			GemHelper_CheckButton104:Hide()
			GemHelper_CheckButton105:Hide()
			GemHelper_EnableCheckBox(GemHelper_CheckButton1011)
			t = otherPlayer
		else
			GemHelper_TradeSkillLinkButton:Show()
			GemHelper_WhoText:Hide()
			GemHelper_CheckButton101:Show()
			GemHelper_CheckButton1011:Show()
			GemHelper_CheckButton1012:Show()
			GemHelper_CheckButton102:Show()
			GemHelper_CheckButton103:Show()
			GemHelper_CheckButton104:Show()
			GemHelper_CheckButton105:Show()
			GemHelper_UpdateCheckboxes(101)
			t = GemHelper_SaveCraft[playerName]
		end
	else
		GemHelper_TradeSkillLinkButton:Hide()
		GemHelper_PlusButton:Hide()
		GemHelper_WhoText:Hide()
		GemHelper_CheckButton101:Show()
		GemHelper_CheckButton1011:Show()
		GemHelper_CheckButton1012:Show()
		GemHelper_CheckButton102:Show()
		GemHelper_CheckButton103:Show()
		GemHelper_CheckButton104:Show()
		GemHelper_CheckButton105:Show()
		GemHelper_UpdateCheckboxes(101)
		t = GemHelper_SaveCraft[playerName]
	end

	GemHelper_CheckButton1012Text:SetFormattedText(GEMHELPER_CHECKBUTTON_TEXT[1012], strsplit("|", GemHelper_UserDropdown_GetValue()))
	GemHelper_CheckButton1012:SetHitRectInsets(5, -(GemHelper_CheckButton1012Text:GetWidth() + 24), 5, 5)

	local itemIndex, button
	local itemID, itemName, itemLink, itemRarity
	local skillName, skillType, numAvailable, isExpanded

	FauxScrollFrame_Update(GemHelper_ListScrollFrame, #GemHelper_DispResults, GEMHELPER_ITEMS_SHOWN, GEMHELPER_DISP_HEIGHT)
	for i = 1, GEMHELPER_ITEMS_SHOWN do
		itemIndex = i + FauxScrollFrame_GetOffset(GemHelper_ListScrollFrame)
		button = _G["GemHelper_ItemButton"..i]
		
		if itemIndex <= #GemHelper_DispResults then
			itemID = GemHelper_DispResults[itemIndex]
			itemName, itemLink, itemRarity = GetItemInfo(itemID)
			numAvailable = 0
			if t[itemID] and t[itemID] > 0 then
				skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(t[itemID])
			end

			if itemName then
				if numAvailable == 0 then
					button:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName)
				else
					button:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName.." ["..numAvailable.."]")
				end
			else
				if GemHelper_GemLocaleData[itemID] and GemHelper_GemLocaleData[itemID].n then
					button:SetText("|cFF3FFFBF"..GemHelper_GemLocaleData[itemID].n)
				else
					button:SetText("|cFF3FFFBFGem itemID "..itemID) -- Teal for unknown
				end
				--GameTooltip:SetHyperlink("item:"..itemID)	-- Request gem from server
			end
			button:SetID(itemID)

			if itemID == selectedGemID then
				button:LockHighlight()
				_G["GemHelper_ItemButton"..i.."Highlight"]:Show()
			else
				button:UnlockHighlight()
				_G["GemHelper_ItemButton"..i.."Highlight"]:Hide()
			end
			button:Show()
		else
			button:Hide()
		end
	end

	-- Enable the Create buttons if the selectedGem is in the results, and the player can craft it, with mats available
	numAvailable = nil
	if not isLinked and not isGuildSkillLink and t[selectedGemID] and t[selectedGemID] > 0 then
		skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(t[selectedGemID])
	end
	if numAvailable and numAvailable > 0 and tContains(GemHelper_DispResults, selectedGemID) then
		GemHelper_CreateAllButton:Enable()
		GemHelper_CreateButton:Enable()
	else
		GemHelper_CreateAllButton:Disable()
		GemHelper_CreateButton:Disable()
	end
end

function GemHelper_ItemButton_OnClick(this, button, down)
	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local itemID = this:GetID()
	local t = GemHelper_isJewelcraftingPanel() and IsTradeSkillLinked() and IsTradeSkillGuild() and otherPlayer or GemHelper_SaveCraft[playerName]
	local itemName, itemLink, itemRarity = GetItemInfo(itemID)
	local skillName, skillType, numAvailable, isExpanded

	numAvailable = 0
	if t[itemID] and t[itemID] > 0 then
		skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(t[itemID])
	end

	if itemLink then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:SetHyperlink(itemLink)
		if GemHelper_GemLocaleData[itemID] and GemHelper_GemLocaleData[itemID].d then
			GameTooltip:AddLine(GemHelper_GemLocaleData[itemID].d, ITEM_QUALITY_COLORS[2].r, ITEM_QUALITY_COLORS[2].g, ITEM_QUALITY_COLORS[2].b, 1)
		end

		local id = t[itemID]
		if id and GetTradeSkillCooldown(id) then
			GameTooltip:AddLine(COOLDOWN_REMAINING.." "..SecondsToTime(GetTradeSkillCooldown(id)), 1, nil, nil, 1)
		end

		GameTooltip:Show()
		if numAvailable == 0 then
			this:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName)
		else
			this:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName.." ["..numAvailable.."]")
		end
	end

	local temp = BrowseName and BrowseName:IsVisible() and BrowseName:GetText()
	if HandleModifiedItemClick(itemLink) then
		if IsAltKeyDown() and BrowseName and BrowseName:IsVisible() then
			BrowseName:SetText(temp..BrowseName:GetText())
		end
		return
	end


	if button == "LeftButton" then
		-- Set current button/gem as selected
		selectedGemID = itemID
		GemHelper_Update()
		return
	end

	if not itemLink and button == "RightButton" then
		-- Query server for gem
        	ChatFrame1:AddMessage(GemHelper_Localize.String.QueryMessage.."|cff40ffc0".."["..this:GetText().."]".."|r.")
		GameTooltip:SetHyperlink("item:"..itemID)
		return
	end
end

function GemHelper_ItemButton_OnEnter(this)
-- runs when moving mouse over filtered gems to cut
	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local itemID = this:GetID()
	local t = GemHelper_isJewelcraftingPanel() and (IsTradeSkillLinked() or IsTradeSkillGuild()) and otherPlayer or GemHelper_SaveCraft[playerName]
	local itemName, itemLink, itemRarity = GetItemInfo(itemID)
	local skillName, skillType, numAvailable, isExpanded
	numAvailable = 0
	if t[itemID] and t[itemID] > 0 then
		skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(t[itemID])
	end

	GameTooltip:SetOwner(this, "ANCHOR_RIGHT")

	if itemLink then
		GameTooltip:SetHyperlink(itemLink)
		if numAvailable == 0 then
			this:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName)
		else
			this:SetText(ITEM_QUALITY_COLORS[itemRarity].hex..itemName.." ["..numAvailable.."]")
		end
	else
		GameTooltip:SetHyperlink("item:"..itemID) -- query server
		GameTooltip:ClearLines()
		GameTooltip:AddLine(GemHelper_Localize.String.ErrorTooltip_L1)
		GameTooltip:AddLine(GemHelper_Localize.String.ErrorTooltip_L2..this:GetID())
		GameTooltip:AddLine(GemHelper_Localize.String.ErrorTooltip_L3, 1, 1, 1, 1)
		GameTooltip:AddLine(GemHelper_Localize.String.ErrorTooltip_L4, nil, nil, nil, 1)
	end
	if GemHelper_GemLocaleData[itemID] and GemHelper_GemLocaleData[itemID].d then
		GameTooltip:AddLine(GemHelper_GemLocaleData[itemID].d, ITEM_QUALITY_COLORS[2].r, ITEM_QUALITY_COLORS[2].g, ITEM_QUALITY_COLORS[2].b, 1)
	end

	local id = t[itemID]
	if id and GetTradeSkillCooldown(id) then
		GameTooltip:AddLine(COOLDOWN_REMAINING.." "..SecondsToTime(GetTradeSkillCooldown(id)), 1, nil, nil, 1)
	end

	GameTooltip:Show()
end

function GemHelper_ItemButton_OnLeave(this)
	GameTooltip:Hide()
end

function GemHelper_Increment_OnClick()
	if GemHelper_InputBox:GetNumber() < 100 then
		GemHelper_InputBox:SetNumber(GemHelper_InputBox:GetNumber() + 1)
	end
end

function GemHelper_Decrement_OnClick()
	if GemHelper_InputBox:GetNumber() > 1 then
		GemHelper_InputBox:SetNumber(GemHelper_InputBox:GetNumber() - 1)
	end
end

function GemHelper_CreateAll_OnClick()
	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local skillName, skillType, numAvailable, isExpanded
	local id

	GemHelper_Update()

	id = GemHelper_SaveCraft[playerName][selectedGemID]
	numAvailable = nil
	if id and id > 0 then
		skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(id)
	end
	if numAvailable and numAvailable > 0 and tContains(GemHelper_DispResults, selectedGemID) then
		SelectTradeSkill(id)
		if GetTradeSkillSelectionIndex() > GetNumTradeSkills() then
			return
		end
		GemHelper_InputBox:SetNumber(numAvailable)
		DoTradeSkill(id, numAvailable)
	end
end

function GemHelper_Create_OnClick()
	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local skillName, skillType, numAvailable, isExpanded
	local id

	GemHelper_Update()

	id = GemHelper_SaveCraft[playerName][selectedGemID]
	numAvailable = nil
	if id and id > 0 then
		skillName, skillType, numAvailable, isExpanded = GetTradeSkillInfo(id)
	end
	if numAvailable and numAvailable > 0 and tContains(GemHelper_DispResults, selectedGemID) then
		SelectTradeSkill(id)
		if GetTradeSkillSelectionIndex() > GetNumTradeSkills() then
			return
		end
		DoTradeSkill(id, GemHelper_InputBox:GetNumber())
	end
end

function GemHelper_CheckCooldown(this)
	if not selectedGemID then
		return
	end

	local playerName = UnitName("player").."|"..GemHelper_Trim(GetRealmName())
	local id = GemHelper_SaveCraft[playerName][selectedGemID]

	if id and GetTradeSkillCooldown(id) then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
		GameTooltip:ClearLines()
		GameTooltip:AddLine(COOLDOWN_REMAINING.." "..SecondsToTime(GetTradeSkillCooldown(id)), nil, nil, nil, 1)
		GameTooltip:Show()
	end
end

function GemHelper_UserDropdown_GetValue()
	if not GemHelper_Save.selectedPlayer then
		GemHelper_Save.selectedPlayer = (UnitName("player").."|"..GemHelper_Trim(GetRealmName()))
	end
	return GemHelper_Save.selectedPlayer
end

function GemHelper_UserDropdown_OnShow()
	UIDropDownMenu_Initialize(GemHelper_UserDropdown, GemHelper_UserDropdown_Initialize)
	UIDropDownMenu_SetSelectedValue(GemHelper_UserDropdown, GemHelper_UserDropdown_GetValue())
	UIDropDownMenu_SetWidth(GemHelper_UserDropdown, 9)
	if GemHelper_Save[101] then
		UIDropDownMenu_EnableDropDown(GemHelper_UserDropdown)
	else
		UIDropDownMenu_DisableDropDown(GemHelper_UserDropdown)
	end
end

function GemHelper_UserDropdown_OnClick(self)
	UIDropDownMenu_SetSelectedValue(GemHelper_UserDropdown, self.value)
	GemHelper_Save.selectedPlayer = self.value
	GemHelper_Update()
end

function GemHelper_UserDropdown_Initialize()
	--local selectedValue = UIDropDownMenu_GetSelectedValue(GemHelper_UserDropdown)
	local selectedValue = GemHelper_UserDropdown_GetValue()
	local currRealm = GemHelper_Trim(GetRealmName())
	local allRealms = 1	-- Always show all realms for now

	for k, v in pairs(GemHelper_SaveCraft) do
		if type(v) == "table" then
			local theName, thisRealmPlayers = strsplit("|", k)
			if allRealms == 1 or thisRealmPlayers == currRealm then
				info.text = theName..GemHelper_Localize.String.Of..thisRealmPlayers
				info.value = k
				info.func = GemHelper_UserDropdown_OnClick
				if selectedValue == info.value then
					info.checked = 1
				else
					info.checked = nil
				end
				UIDropDownMenu_AddButton(info)
			end
		end
	end
end

function GemHelper_SetNewBackgroundColor()
	local r, g, b = ColorPickerFrame:GetColorRGB()
	local a = 1.0 - OpacitySliderFrame:GetValue()
	GemHelper_Frame:SetBackdropColor(r, g, b, a)
	GemHelper_ColorSwatchNormalTexture:SetVertexColor(r, g, b)
	GemHelper_Save.r = r
	GemHelper_Save.g = g
	GemHelper_Save.b = b
	GemHelper_Save.a = a
end

function GemHelper_SetOldBackgroundColor(colortable)
	GemHelper_Frame:SetBackdropColor(colortable.r, colortable.g, colortable.b, colortable.a)
	GemHelper_ColorSwatchNormalTexture:SetVertexColor(colortable.r, colortable.g, colortable.b)
	GemHelper_Save.r = colortable.r
	GemHelper_Save.g = colortable.g
	GemHelper_Save.b = colortable.b
	GemHelper_Save.a = colortable.a
end

function GemHelper_SelectBackgroundColor()
	-- These 2 lines fix a bug for an uninitialised OpacitySliderFrame returning and saving a
	-- value of 0 on SetColorRGB and SetValue
	ColorPickerFrame.opacityFunc = nil
	OpacitySliderFrame:SetValue(1.0 - GemHelper_Save.a)

	ColorPickerFrame.func = GemHelper_SetNewBackgroundColor
	ColorPickerFrame.hasOpacity = 1
	ColorPickerFrame.opacityFunc = GemHelper_SetNewBackgroundColor
	ColorPickerFrame.opacity = 1.0 - GemHelper_Save.a
	ColorPickerFrame.previousValues = {["r"] = GemHelper_Save.r, ["g"] = GemHelper_Save.g, ["b"] = GemHelper_Save.b, ["a"] = GemHelper_Save.a}
	ColorPickerFrame.cancelFunc = GemHelper_SetOldBackgroundColor
	ColorPickerFrame:SetColorRGB(GemHelper_Save.r, GemHelper_Save.g, GemHelper_Save.b)
	ShowUIPanel(ColorPickerFrame)
end

--[[
Hook TradeSkillFrame_Show() and check if the GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting
to prevent it from opening. If it isn't shown, make sure to call CloseTradeSkill() on closing GH window.
]]

--TradeSkillFrame_LoadUI()  -- We "## RequiredDeps: Blizzard_TradeSkillUI" instead in the TOC
GemHelper_OrigTradeSkillFrame_Show = TradeSkillFrame_Show
function GemHelper_HookTradeSkillFrame_Show()
	if GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting then
		if GemHelper_Save[111] == true then
			CloseDropDownMenus()
		else
			GemHelper_OrigTradeSkillFrame_Show()
			GemHelper_PlusButton:SetText("-")
		end
	else
		GemHelper_OrigTradeSkillFrame_Show()
		GemHelper_PlusButton:SetText("-")
	end
end
TradeSkillFrame_Show = GemHelper_HookTradeSkillFrame_Show

--[[
Hooking the OnHide script handler directly instead of TradeSkillFrame_Hide().
]]

GemHelper_OrigTradeSkillFrame_OnHide = TradeSkillFrame:GetScript("OnHide")
function GemHelper_TradeSkillFrame_OnHide(self)
	if GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting then
		if GemHelper_Save[111] == true then
			PlaySound("igCharacterInfoClose")
			if not GemHelper_Frame:IsShown() then
				GemHelper_OrigTradeSkillFrame_OnHide(self)
			end
		else
			GemHelper_OrigTradeSkillFrame_OnHide(self)
		end
	else
		GemHelper_OrigTradeSkillFrame_OnHide(self)
	end
	GemHelper_PlusButton:SetText("+")
end
TradeSkillFrame:SetScript("OnHide", GemHelper_TradeSkillFrame_OnHide)

--[[
Repeat and hook the same thing for AdvancedTradeSkillWindow
]]
if ATSW_ShowWindow and ATSW_HideWindow then
	GemHelper_Orig_ATSW_ShowWindow = ATSW_ShowWindow
	function GemHelper_ATSW_ShowWindow()
		if GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting then
			if GemHelper_Save[111] == true then
				CloseDropDownMenus()
			else
				GemHelper_Orig_ATSW_ShowWindow()
				GemHelper_PlusButton:SetText("-")
			end
		else
			GemHelper_Orig_ATSW_ShowWindow()
			GemHelper_PlusButton:SetText("-")
		end
	end
	ATSW_ShowWindow = GemHelper_ATSW_ShowWindow

	function GemHelper_ATSW_HideWindow()
		GemHelper_PlusButton:SetText("+")
	end
	hooksecurefunc("ATSW_HideWindow", GemHelper_ATSW_HideWindow)
end


--[[
Repeat and hook the same thing for Skillet
]]
if Skillet and SkilletFrame then
	function GemHelper_Skillet_ShowWindow()
		if GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting then
			if GemHelper_Save[111] == true then
				CloseDropDownMenus()
			else
				GemHelper_Orig_Skillet_ShowWindow(Skillet)
				GemHelper_PlusButton:SetText("-")
			end
		else
			GemHelper_Orig_Skillet_ShowWindow(Skillet)
			GemHelper_PlusButton:SetText("-")
		end
	end
	GemHelper_Orig_Skillet_ShowWindow = Skillet.ShowTradeSkillWindow
	Skillet.ShowTradeSkillWindow = GemHelper_Skillet_ShowWindow

	GemHelper_Orig_Skillet_OnHide = SkilletFrame:GetScript("OnHide")
	function GemHelper_SkilletFrame_OnHide(self)
		if GetTradeSkillLine() == GemHelper_Localize.String.Jewelcrafting then
			if GemHelper_Save[111] == true then
				PlaySound("igCharacterInfoClose")
				if not GemHelper_Frame:IsShown() then
					GemHelper_Orig_Skillet_OnHide(self)
				end
			else
				GemHelper_Orig_Skillet_OnHide(self)
			end
		else
			GemHelper_Orig_Skillet_OnHide(self)
		end
		GemHelper_PlusButton:SetText("+")
	end
	SkilletFrame:SetScript("OnHide", GemHelper_SkilletFrame_OnHide)
end

