<Ui>
	<Button name="TellMeWhen_SpellSuggestTemplate" virtual="true" hidden="true">
		<Size y="19"/>
		<Anchors>
			<Anchor point="LEFT" x="5"/>
			<Anchor point="RIGHT" x="-5"/>
		</Anchors>
		<Scripts>
			<OnLoad>
				self.Background:SetVertexColor(0, 1, 0)
				self:RegisterForClicks("LeftButtonUp", "RightButtonUp")
			</OnLoad>
			<OnClick>
				TMW.SUG.CurrentModule:Entry_OnClick(self, button)
			</OnClick>
			<OnEnter>
				TMW:TT_Anchor(self)
				if self.tooltiptitle then
					GameTooltip:AddLine(self.tooltiptitle, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b, 1)
					GameTooltip:AddLine(self.tooltiptext, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 1)
				end
				if self.tooltipmethod and self.tooltiparg then
					GameTooltip[self.tooltipmethod](GameTooltip, self.tooltiparg)
				end

				local first, second

				if type(self.insert) == "number" then
					first = self.overrideInsertID or TMW.L["SUG_INSERTID"]
				else
					first = self.overrideInsertName or TMW.L["SUG_INSERTNAME"]
				end
				if type(self.insert2) == "number" then
					second = self.overrideInsertID or TMW.L["SUG_INSERTID"]
				else
					second = self.overrideInsertName or TMW.L["SUG_INSERTNAME"]
				end

				GameTooltip:AddLine(" ")
				if self.insert2 then
					local text = TMW.L["SUG_INSERT_LEFT"]
					if not TMW.SUG.CurrentModule.noTab and self:GetID() == 1 then
						text = text .. TMW.L["SUG_INSERT_TAB"]
					end
					GameTooltip:AddLine(first:format(text), 0, .5, 1, nil)
					GameTooltip:AddLine(second:format(TMW.L["SUG_INSERT_RIGHT"]), 0, .5, 1, nil)
				else
					local text = TMW.L["SUG_INSERT_ANY"]
					if not TMW.SUG.CurrentModule.noTab and self:GetID() == 1 then
						text = text .. TMW.L["SUG_INSERT_TAB"]
					end
					GameTooltip:AddLine(first:format(text), 0, .5, 1, nil)
				end

				GameTooltip:Show()

				TMW.SUG.mousedOver = self
			</OnEnter>
			<OnLeave>
				GameTooltip:Hide()

				if TMW.SUG.mousedOver == self then
					TMW.SUG.mousedOver = nil
				end
			</OnLeave>
			<OnMouseWheel>
				if (not TMW.SUG[TMW.SUG:GetNumFramesNeeded()]:IsShown() and delta > 0) or TMW.SUG[TMW.SUG:GetNumFramesNeeded()]:IsShown() then
					if IsShiftKeyDown() then
						delta = delta*5
					elseif IsControlKeyDown() then
						delta = delta*(#TMW.SUG-1)
					end
					
					TMW.SUG.offset = max(0, TMW.SUG.offset - delta)
				end
				TMW.SUG:SuggestingComplete()
			</OnMouseWheel>
		</Scripts>
		<NormalFont style="GameFontNormal"/>
		<HighlightFont style="GameFontHighlight"/>
		<HighlightTexture file="Interface\BUTTONS\UI-Listbox-Highlight2" alphaMode="ADD" alpha="0.5">
			<Anchors>
				<Anchor point="TOPLEFT" y="-0.5"/>
				<Anchor point="BOTTOMRIGHT" y="0.5"/>
			</Anchors>
		</HighlightTexture>
		<Layers>
			<Layer level="BACKGROUND">
				<Texture name="$parentBackground" parentKey="Background">
					<Color r="0.5" g="0.5" b="0.5" a="0.9"/>
					<Anchors>
						<Anchor point="TOPLEFT" y="-0.5"/>
						<Anchor point="BOTTOMRIGHT" y="0.5"/>
					</Anchors>
				</Texture>
			</Layer>
			<Layer level="OVERLAY">
				<Texture name="$parentIcon" parentKey="Icon" file="Interface\Icons\INV_Misc_QuestionMark">
					<Size x="18" y="18"/>
					<TexCoords left="0.07" right="0.93" top="0.07" bottom="0.93"/>
					<Anchors>
						<Anchor point="LEFT"/>
					</Anchors>
				</Texture>
				<FontString name="$parentID" parentKey="ID" inherits="GameFontHighlight" justifyH="RIGHT">
					<Anchors>
						<Anchor point="RIGHT" x="-1" y="0"/>
					</Anchors>
				</FontString>
				<FontString name="$parentName" parentKey="Name" inherits="GameFontHighlight" justifyH="LEFT">
					<Anchors>
						<Anchor point="LEFT" relativeTo="$parentIcon" relativePoint="RIGHT" x="2"/>
						<Anchor point="RIGHT" relativeTo="$parentID" relativePoint="LEFT" x="-1"/>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
	</Button>
	
	<Frame name="$parentSuggest" parent="TellMeWhen_IconEditor" parentKey="Suggest" hidden="true" enableMouse="true">
		<Size x="200"/>
		<Anchors>
			<Anchor point="TOP" y="-0"/> 
			<Anchor point="BOTTOM" y="0" />
			<Anchor point="LEFT" relativePoint="RIGHT" x="1"/>
		</Anchors>
		
		<Layers>
			<Layer level="BACKGROUND">
				<Texture name="$parentBackground" parentKey="Background">
					<Color r="0.05" g="0.05" b="0.05" a="0.970"/>
				</Texture>
			</Layer>
			<Layer level="OVERLAY">
				<FontString name="$parentHeader" inherits="GameFontNormalLarge" parentKey="Header" justifyH="LEFT">
					<Anchors>
						<Anchor point="TOP" y="-4"/>
					</Anchors>
				</FontString>
			</Layer>
			<Layer level="ARTWORK">
				<FontString name="$parentInfoText" parentKey="InfoText" hidden="true" inherits="GameFontHighlight">
					<Size x="180"/>
					<Anchors>
						<Anchor point="TOP" y="-45"/>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
		<Frames>
			<Slider name="$parentSpeed" inherits="TellMeWhen_SliderTemplate" parentKey="Speed" hidden="true">
				<Size x="165" y="18"/>
				<Anchors>
					<Anchor point="TOP" relativeTo="$parentInfoText" relativePoint="BOTTOM" y="-25"/>
				</Anchors>
				<Scripts>
					<OnLoad>
						self.exponent = 4
						self.max = 5000
						self.text:SetText(TMW.L["CACHINGSPEED"])
						self.Low:SetText(1)
						self.High:SetText(self.max)
						
						self.SpellCache = TMW:GetModule("SpellCache")
						
						function self:SetActualValue(val)
							if val ~= self:GetActualValue() then
								self:SetValue(val^(1/self.exponent))
							end
						end
						function self:GetActualValue()
							return min(floor(self:GetValue()^self.exponent+0.5), self.max)
						end
						
						TMW:RegisterCallback("TMW_SPELLCACHE_NUMCACHEPERFRAME_CHANGED", function(event, num)
							self:SetActualValue(num)
						end)
						
						self:SetMinMaxValues(1, self.max^(1/self.exponent))
						
						self.Mid:SetText(self.SpellCache:GetNumCachePerFrame())
						self:SetActualValue(self.SpellCache:GetNumCachePerFrame())
					</OnLoad>
					<OnValueChanged>
						value = self:GetActualValue()
						self.SpellCache:SetNumCachePerFrame(value)
						self.Mid:SetText(value)
					</OnValueChanged>
					<OnShow>
						self:GetParent().InfoText:Show()
					</OnShow>
					<OnHide>
						self:GetParent().InfoText:Hide()
					</OnHide>
				</Scripts>
			</Slider>
			<StatusBar name="$parentStatus" parentKey="Status" hidden="true">
				<Size x="175" y="25"/>
				<Anchors>
					<Anchor point="TOP" relativeTo="$parentSpeed" relativePoint="BOTTOM" y="-15"/>
				</Anchors>
				<Layers>
					<Layer level="ARTWORK">
						<Texture name="$parenttexture" parentKey="texture" file="Interface\TargetingFrame\UI-StatusBar"/>
					</Layer>

					<Layer level="OVERLAY">
						<FontString name="$parentText" parentKey="text" inherits="GameFontHighlight">
							<Anchors>
								<Anchor point="CENTER"/>
							</Anchors>
						</FontString>
					</Layer>
				</Layers>
				<Scripts>
					<OnLoad>
						self.SpellCache = TMW:GetModule("SpellCache")
						
						self:SetStatusBarTexture(self.texture)
						self.texture:SetTexture(LibStub("LibSharedMedia-3.0"):Fetch("statusbar", TMW.db.profile.TextureName))
						
						TMW:RegisterCallback("TMW_SPELLCACHE_EXPECTEDCACHELENGTH_UPDATED", function(event, max)
							self:SetMinMaxValues(1, max)
						end)
						
						TMW:RegisterCallback("TMW_SPELLCACHE_NUMCACHED_CHANGED", function(event, num)
							self:SetValue(num)
						end)
					</OnLoad>
					<OnValueChanged>
						if self:IsShown() then
							self.text:SetText(value .. " / ~" .. self.SpellCache:GetExpectedCacheLength())
						end
					</OnValueChanged>
				</Scripts>
			</StatusBar>
			<Button name="$parentHelp" inherits="MainMenuBarMicroButton" hidden="false">
				<Anchors>
					<Anchor point="TOPRIGHT" x="-5" y="16"/>
				</Anchors>
				<Scripts>
					<OnLoad>
						self:SetNormalTexture("Interface\\Buttons\\UI-MicroButton-Help-Up")
						self:SetPushedTexture("Interface\\Buttons\\UI-MicroButton-Help-Up")
						self:SetDisabledTexture("Interface\\Buttons\\UI-MicroButton-Help-Up")
						self:SetHighlightTexture("Interface\\Buttons\\UI-MicroButton-Hilight")
						self:SetScale(.58)
					</OnLoad>
					<OnEnter>
						TMW.SUG:ColorHelp(self)
					</OnEnter>
					<OnLeave>
						GameTooltip:Hide()
					</OnLeave>
					<OnClick>
						if TMW.SUG.CurrentModule.helpOnClick then
							TMW.SUG.CurrentModule.helpOnClick(self)
						end
					</OnClick>
				</Scripts>
			</Button>
			<Button name="$parentFinish" parentKey="Finish" inherits="TellMeWhen_ButtonTemplate" hidden="true">
				<Size x="190" y="15"/>
				<Anchors>
					<Anchor point="BOTTOM" y="20"/>
				</Anchors>
				<Scripts>
					<OnLoad>
						self:SetText(TMW.L["SUG_FINISHHIM"])
						TMW:TT(self, "SUG_FINISHHIM", "SUG_FINISHHIM_DESC")
						self:RegisterEvent("UNIT_FLAGS") -- accurately detects changes to InCombatLockdown
					</OnLoad>
					<OnEvent>
						-- Don't allow this when in combat because of Blizzard's new throttling systen in Mists of Pandaria
						if InCombatLockdown() then
							self:Disable()
						else
							self:Enable()
						end
					</OnEvent>
					<OnClick>
						TMW:GetModule("SpellCache"):SetNumCachePerFrame(math.huge)
					</OnClick>
				</Scripts>
			</Button>
		</Frames>
		<Scripts>
			<OnLoad>
				TMW.SUG.Suggest = self
				self.Header:SetText(TMW.L["SUGGESTIONS"])
				self.InfoText:SetText(TMW.L["CACHING"])

  				TMW.HELP:NewCode("SUG_FIRSTHELP", 100, true)
				
				local firstItem = TMW.SUG:GetFrame(1)
				firstItem:SetPoint("TOP", 0, -6 - TMW.SUG[1]:GetHeight())
				firstItem.ClearAllPoints = TMW.NULLFUNC
				firstItem.SetPoint = TMW.NULLFUNC
				firstItem.SetSize = TMW.NULLFUNC

				TMW.SUG[1]:HookScript("OnShow", function(self)
					TMW.HELP:Show{
						code = "SUG_FIRSTHELP",
						icon = nil,
						relativeTo = self,
						x = 3,
						y = 0,
						text = format(TMW.L["SUG_FIRSTHELP_DESC"])
					}
				end)
				
				-- Tukui_Skins tries to reposition this frame, but it totally ruins the resizability of it,
				-- so just disallow repositioning.
				--	self.SetPoint = TMW.NULLFUNC
				--self.ClearAllPoints = TMW.NULLFUNC
			</OnLoad>
			<OnSizeChanged>						
				if TMW.SUG:GetNumFramesNeeded() ~= #TMW.SUG and self:IsShown() then
					TMW.SUG:SuggestingComplete()
				end
			</OnSizeChanged>
		</Scripts>
	</Frame>

</Ui>