<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<!-- This script turns on verbose messages in the FrameXML log -->
	<Script>
		--FrameXML_Debug(1);
	</Script>

	<!-- This function is called when a script error occurs -->
	<Script>
		_ERROR_COUNT = 0; _ERROR_LIMIT = 1000;
		
		errors = {};
		errorsSeen = {};
		
		stacks = {}
		
		function removeFromDebugStack (table)
			for k, v in next, errors do
				if v == table then
					tremove(errors, k);
					break;
				end
			end
			
			errorsSeen[table.message..table.stackTrace] = nil;
			
			for k, v in next, table do
				table[k] = nil;
			end
			tinsert(stacks, table);
		end
		
		function addToDebugStack (message, stackTrace)
			if errorsSeen[message..stackTrace] then
				return;
			end
			
			local entry;
			if stacks[1] then
				entry = stacks[1];
				tremove(stacks, 1);
			else
				entry = {};
			end
				
			entry.message = message;
			entry.stackTrace = stackTrace;
			tinsert(errors, entry);
			errorsSeen[message..stackTrace] = true;
		end
		
		function getdebugstack ()
			return errors;
		end
		
		function cleardebugstack ()
			for k, table in next, errors do
				tremove(errors, k);
				
				errorsSeen[table.message..table.stackTrace] = nil;
				
				for k, v in next, table do
					table[k] = nil;
				end
				
				tinsert(stacks, table);
			end
		end
		
		function _ERRORMESSAGE(message)

			-- Show an error dialog for developers
			if GetCVarBool("scriptErrors") then
				if not ScriptErrors:IsShown() then
					ScriptErrors_Message:SetText(message);
					ScriptErrors:Show();
				end

				-- Show debugging information for internal use
				debuginfo();
			
				local errors = errors;
				
				if errors[(_ERROR_LIMIT/10)] then
					-- If we reach the limit, remove the oldest error and stack from the errors table.
					removeFromDebugStack(errors[1]);
				end
				
				addToDebugStack(message, debugstack(2));
			end
			
			-- Show a warning if there are too many errors
			_ERROR_COUNT = _ERROR_COUNT + 1;
			if _ERROR_COUNT == _ERROR_LIMIT then
				StaticPopup_Show("TOO_MANY_LUA_ERRORS");
			end

			return message;
		end

		seterrorhandler(_ERRORMESSAGE);
	</Script>

	<!-- This function can be used to show debugging messages -->
	<Script>
		function message(text)
			if not ScriptErrors:IsShown() then
				ScriptErrors_Message:SetText(text);
				ScriptErrors:Show();
			end
		end
	</Script>

	<!-- This function is used to translate text (DEPRECATED: actually it's already translated as-is) -->
	<Script>
		function TEXT(text)
			return text;
		end
	</Script>

	<!-- These are basic attributes for dialogs -->
	<Texture name="DialogButtonNormalTexture" file="Interface\Buttons\UI-Panel-Button-Up" virtual="true">
		<TexCoords left="0" right="0.625" top="0" bottom="0.6875"/>
	</Texture>
	<Texture name="DialogButtonPushedTexture" file="Interface\Buttons\UI-Panel-Button-Down" virtual="true">
		<TexCoords left="0" right="0.625" top="0" bottom="0.6875"/>
	</Texture>
	<Texture name="DialogButtonHighlightTexture" file="Interface\Buttons\UI-Panel-Button-Highlight" alphaMode="ADD" virtual="true">
		<TexCoords left="0" right="0.625" top="0" bottom="0.6875"/>
	</Texture>
	<Frame name="DialogBoxFrame" virtual="true" toplevel="true" frameStrata="DIALOG" hidden="true">
		<Anchors>
			<Anchor point="CENTER"/>
		</Anchors>
		<Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
			<EdgeSize>
				<AbsValue val="32"/>
			</EdgeSize>
			<TileSize>
				<AbsValue val="32"/>
			</TileSize>
			<BackgroundInsets>
				<AbsInset left="11" right="12" top="12" bottom="11"/>
			</BackgroundInsets>
		</Backdrop>
		<Frames>
			<Button name="$parentButton">
				<Size>
					<AbsDimension x="128" y="32"/>
				</Size>
				<Anchors>
					<Anchor point="BOTTOM">
						<Offset>
							<AbsDimension x="0" y="16"/>
						</Offset>
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>
						self:GetParent():Hide();
					</OnClick>
				</Scripts>
				<NormalTexture inherits="DialogButtonNormalTexture"/>
				<PushedTexture inherits="DialogButtonPushedTexture"/>
				<HighlightTexture inherits="DialogButtonHighlightTexture"/>
				<ButtonText text="OKAY"/>
				<NormalFont style="DialogButtonNormalText"/>
				<HighlightFont style="DialogButtonHighlightText"/>
			</Button>
		</Frames>
	</Frame>
	<!-- This is the general scripting error dialog -->
	<Frame name="ScriptErrors" inherits="DialogBoxFrame">
		<Size>
			<AbsDimension x="384" y="128"/>
		</Size>
		<Layers>
			<Layer level="ARTWORK">
				<FontString name="ScriptErrors_Message" inherits="GameFontRedLarge" justifyH="CENTER" justifyV="TOP" nonspacewrap="true">
					<Size>
						<AbsDimension x="360" y="0"/>
					</Size>
					<Anchors>
						<Anchor point="TOP">
							<Offset>
								<AbsDimension x="0" y="-16"/>
							</Offset>
						</Anchor>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
		<Scripts>
			<OnLoad>
				self.errors = {};
			</OnLoad>
		</Scripts>
	</Frame>
</Ui>
