--[[
Non-BEB templates and their file
GameFontNormal					Fonts.xml
GameFontNormalSmall				Fonts.xml
InputBoxTemplate				UIPanelTemplates.xml
UIPanelButtonTemplate			UIPanelTemplates.xml
UICheckButtonTemplate			UIPanelTemplates.xml

Elements for each selector
	BEBMain ( Dragable, Position, Attachment, Size, Level, Strata)
	BEBBackground ( Flash, Shown, Colors, Texture, Level, Strata)
	BEBXpBar ( Flash, Colors, Texture, Level, Strata)
	BEBRestedXpBar ( Flash, Shown, Colors (rested,maxrested), Texture, Level, Strata)
	BEBMarkers ( Flash, Shown, Colors, Texture, Level, Strata)
	BEBXpTick ( Flash, Shown, Colors, Texture, position, Size, Level, Strata)
	BEBRestedXpTick ( Flash, Shown, Colors (rested, maxrested), Position, Size, Level, Strata)
	BEBRestedXpTickGlow ( Flash, Colors (rested, maxrested), Texture, Level, Strata)
	BEBBarText ( Flash, Shown, Colors, Text, Mouseover, Size (y), clicktext, Position, Attachment (point, relpoint), Level, Strata)

			<OnEnter>
				if ( this.tooltipText ) then
					GameTooltip:SetOwner(this, "ANCHOR_RIGHT")
					GameTooltip:SetText(this.tooltipText, nil, nil, nil, nil, 1)
				end
				if ( this.tooltipRequirement ) then
					GameTooltip:AddLine(this.tooltipRequirement, "", 1.0, 1.0, 1.0)
					GameTooltip:Show()
				end
			</OnEnter>
			<OnLeave>
				GameTooltip:Hide()
			</OnLeave>
]]
BEBOPTIONS.ARROWLEFT =				"<"
BEBOPTIONS.ARROWRIGHT =				">"

BEBOPTIONS.SIZELIMITS = {
	BEBMain={x={min=50,max=2000,step=1},y={min=2,max=32,step=1}},
	BEBXpTick={x={min=2,max=128,step=1},y={min=2,max=128,step=1}},
	BEBRestedXpTick={x={min=2,max=128,step=1},y={min=2,max=128,step=1}},
	BEBBarText={y={min=6,max=27,step=0.25}},
	level={min=1,max=25,step=1}
}
BEBOPTIONS.CURRENTFRAME = "BEBMain"
BEBOPTIONS.ELEMENTS = {"BEBColorsHeader","BEBAttachHeader",
	"BEBDragableCheckButton","BEBFlashingCheckButton","BEBShownCheckButton","BEBShowOnMouseoverCheckButton","BEBUnrestedTextureColorButton","BEBRestedTextureColorButton","BEBMaxrestedTextureColorButton",
	"BEBUnrestedTextColorButton","BEBRestedTextColorButton","BEBMaxrestedTextColorButton","BEB_MainAttachToFrame","BEB_BarTextFrame","BEB_TextureFrame","BEB_MainAttachPointButton","BEB_MainAttachToPointButton","BEBOptionsPositionControls",
	"BEBOptionsHeightSlider","BEBOptionsWidthSlider","BEBClickTextCheckButton","BEBProfileFrame","BEBOptionsLevelSlider","BEB_StrataDDMButton"}
BEBOPTIONS.FRAMEELEMENTS = {
	BEBMain = {BEBDragableCheckButton=1,BEBOptionsPositionControls=1,BEB_MainAttachToFrame=1,BEB_MainAttachPointButton=1,BEB_MainAttachToPointButton=1,BEBAttachHeader=1,BEBOptionsHeightSlider=1,BEBOptionsWidthSlider=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBBackground = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBUnrestedTextureColorButton=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBXpBar = {BEBFlashingCheckButton=1,BEBColorsHeader=1,BEBUnrestedTextureColorButton=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBRestedXpBar = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBMarkers = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBUnrestedTextureColorButton=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBXpTick = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBUnrestedTextureColorButton=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsPositionControls=1,BEBOptionsHeightSlider=1,BEBOptionsWidthSlider=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBRestedXpTick = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsPositionControls=1,BEBOptionsHeightSlider=1,BEBOptionsWidthSlider=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBRestedXpTickGlow = {BEBFlashingCheckButton=1,BEBColorsHeader=1,BEBRestedTextureColorButton=1,BEBMaxrestedTextureColorButton=1,BEB_TextureFrame=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBBarText = {BEBFlashingCheckButton=1,BEBShownCheckButton=1,BEBColorsHeader=1,BEBUnrestedTextColorButton=1,BEBRestedTextColorButton=1,BEBMaxrestedTextColorButton=1,BEB_BarTextFrame=1,BEBShowOnMouseoverCheckButton=1,BEBOptionsHeightSlider=1,BEBClickTextCheckButton=1,BEBOptionsPositionControls=1,BEB_MainAttachPointButton=1,BEB_MainAttachToPointButton=1,BEBAttachHeader=1,BEBOptionsLevelSlider=1,BEB_StrataDDMButton=1},
	BEBProfile = {BEBProfileFrame=1}
}

function BEBOPTIONS.OnLoad(this)
	tinsert(UISpecialFrames,"BEBConfigFrame")
	this:RegisterEvent("ADDON_LOADED")
end

function BEBOPTIONS.OnEvent(this, event, arg1)
	if (event == "ADDON_LOADED" and arg1 == "BEBOptions") then
		BEBOPTIONS.Initialize()
		this:UnregisterEvent("ADDON_LOADED")
	end
end

function BEBOPTIONS.Initialize()
		BEBOPTIONS.SetTexts()
end

function BEBOPTIONS.SetTexts()
		for element,text in pairs(BEBOPTIONS.TEXTELEMENTS) do
			local Element = _G[element]
			Element:SetText(text)
		end
end

function BEBOPTIONS.OnShow(self, frame)
	if ( not BEBINITIALIZED ) then
		BEBConfigFrame:Hide()
		return
	end
	local this
	if (frame) then
		this = _G[frame.."Selector"]
	else
		this = _G[BEBOPTIONS.CURRENTFRAME.."Selector"]
	end
	BEBOPTIONS.SelectFrame(this)
end

function BEBOPTIONS.SelectFrame(this)
	ColorPickerFrame:Hide()
	BEBOPTIONS.CURRENTFRAME = this.index
	BEBOptionsHeightSlider.enabled = nil
	BEBOptionsWidthSlider.enabled = nil
	BEBOptionsLevelSlider.enabled = nil
	for _,v in pairs(BEBOPTIONS.ELEMENTS) do
		if ( BEBOPTIONS.FRAMEELEMENTS[BEBOPTIONS.CURRENTFRAME][v] ) then
			BEBOPTIONS.SetElementValue(v)
			_G[v]:Show()
		else
			_G[v]:Hide()
		end
	end
	BEBOptionsHeightSlider.enabled = true
	BEBOptionsWidthSlider.enabled = true
	BEBOptionsLevelSlider.enabled = true
	for k,_ in pairs(BEBOPTIONS.FRAMEELEMENTS) do
		_G[k.."Selector"]:UnlockHighlight()
	end
	_G[BEBOPTIONS.CURRENTFRAME.."Selector"]:LockHighlight()
	BEBCurrentSelector:SetText(BEBOPTIONS.TEXTELEMENTS[BEBOPTIONS.CURRENTFRAME.."Selector"])
end

function BEBOPTIONS.SetElementValue(element)
	if ((element == "BEBColorsHeader")or(element == "BEBAttachHeader")) then
	elseif ((element == "BEBUnrestedTextureColorButton")or(element == "BEBRestedTextureColorButton")or(element == "BEBMaxrestedTextureColorButton")) then
		_G[element.."Texture"]:SetTexture(BEB.TexturePath(BEBCharSettings[BEBOPTIONS.CURRENTFRAME].texture))
		local button = _G[element]
		_G[element.."Texture"]:SetVertexColor(unpack(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.whenindex]))
		if ((BEBOPTIONS.CURRENTFRAME == "BEBBackground")or(BEBOPTIONS.CURRENTFRAME == "BEBXpBar")or(BEBOPTIONS.CURRENTFRAME == "BEBRestedXpBar")or(BEBOPTIONS.CURRENTFRAME == "BEBMarkers")) then
			_G[element.."Texture"]:SetTexCoord(0.046875,0.0625,0,1)
		else
			_G[element.."Texture"]:SetTexCoord(0.5,1,0,0.5)
		end
	elseif ((element == "BEBUnrestedTextColorButton")or(element == "BEBRestedTextColorButton")or(element == "BEBMaxrestedTextColorButton")) then
		local button = _G[element]
		_G[element.."Texture"]:SetVertexColor(unpack(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.whenindex]))
	elseif ((element == "BEBDragableCheckButton")or(element == "BEBFlashingCheckButton")or(element == "BEBShownCheckButton") or(element == "BEBClickTextCheckButton") or(element == "BEBShowOnMouseoverCheckButton")) then
		local button = _G[element]
		button:SetChecked(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.index])
	elseif ((element == "BEB_MainAttachToFrame")or(element == "BEB_BarTextFrame")or(element == "BEB_TextureFrame")) then
		local button = _G[element.."Button"]
                local text
		if (button.subindex) then
                   text = BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.typeindex][button.subindex]
                   
		else
                   text = BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.typeindex]
                end
                if (button.xlate) then
                   text = button.xlate[text]
                end
                _G[element.."EditBox"]:SetText(string.gsub(text, "^%d%d", ""))
	elseif ((element == "BEB_MainAttachPointButton")or(element == "BEB_MainAttachToPointButton")) then
		local button = _G[element]
		local attachpoint = BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.typeindex][button.subindex]
		button:SetText(string.gsub(BEBOPTIONS.ATTACHPOINTS[attachpoint], "^%d%d", ""))
	elseif ((element == "BEBOptionsPositionControls")) then
		_G[element.."xEditBox"]:SetText(BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x)
		_G[element.."yEditBox"]:SetText(BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y)
	elseif ((element == "BEBOptionsHeightSlider")or(element == "BEBOptionsWidthSlider")) then
		local slider = _G[element]
		slider:SetMinMaxValues(BEBOPTIONS.SIZELIMITS[BEBOPTIONS.CURRENTFRAME][slider.axisindex].min,BEBOPTIONS.SIZELIMITS[BEBOPTIONS.CURRENTFRAME][slider.axisindex].max)
		_G[element.."Low"]:SetText(BEBOPTIONS.SIZELIMITS[BEBOPTIONS.CURRENTFRAME][slider.axisindex].min)
		_G[element.."High"]:SetText(BEBOPTIONS.SIZELIMITS[BEBOPTIONS.CURRENTFRAME][slider.axisindex].max)
		slider:SetValueStep(BEBOPTIONS.SIZELIMITS[BEBOPTIONS.CURRENTFRAME][slider.axisindex].step)
		slider:SetValue(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][slider.whatindex][slider.axisindex])
	elseif (element == "BEBProfileFrame") then
		BEBOPTIONS.PROFILETABLE = {[BEBOPTIONS.TEXT.none] = BEBOPTIONS.TEXT.none}
		local Profiles = 0
		for _,_ in pairs(BEBGlobal.profiles) do
			Profiles = Profiles + 1
		end
		if (BEBGlobal.profiles and Profiles>0) then
			for k,_ in pairs(BEBGlobal.profiles) do
				BEBOPTIONS.PROFILETABLE[k] = k
			end
			if (BEBCharSettings.BEBProfile) then
				BEBProfileFrameUseButton:LockHighlight()
				BEBProfileFrameUseButtonCheckButton:SetChecked(1)
				BEBProfileFrameUseButtonDDMButton:SetText(BEBCharSettings.BEBProfile)
			else
				BEBProfileFrameUseButton:UnlockHighlight()
				BEBProfileFrameUseButtonCheckButton:SetChecked(0)
				BEBProfileFrameUseButtonDDMButton:SetText(BEBOPTIONS.TEXT.none)
			end
		else
			BEBProfileFrameUseButton:UnlockHighlight()
			BEBProfileFrameUseButtonCheckButton:SetChecked(0)
			BEBProfileFrameUseButtonDDMButton:SetText(BEBOPTIONS.TEXT.none)
		end
		BEBProfileFrameLoadButtonDDMButton:SetText(BEBOPTIONS.TEXT.none)
		BEBProfileFrameSaveButtonDDMButton:SetText(BEBOPTIONS.TEXT.none)
		BEBProfileFrameDeleteButtonDDMButton:SetText(BEBOPTIONS.TEXT.none)
	elseif (element == "BEBOptionsLevelSlider") then
		local slider = _G[element]
		slider:SetMinMaxValues(BEBOPTIONS.SIZELIMITS[slider.whatindex].min,BEBOPTIONS.SIZELIMITS[slider.whatindex].max)
		_G[element.."Low"]:SetText(BEBOPTIONS.SIZELIMITS[slider.whatindex].min)
		_G[element.."High"]:SetText(BEBOPTIONS.SIZELIMITS[slider.whatindex].max)
		slider:SetValueStep(BEBOPTIONS.SIZELIMITS[slider.whatindex].step)
		slider:SetValue(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][slider.whatindex])
	elseif (element == "BEB_StrataDDMButton") then
		local button = _G[element]
		button:SetText(string.gsub(BEBOPTIONS.STRATAS[BEBCharSettings[BEBOPTIONS.CURRENTFRAME][button.typeindex]], "^%d%d", ""))
	end
end

function BEBOPTIONS.OnMouseDown()
	BEBConfigFrame:StartMoving()
end

function BEBOPTIONS.OnMouseUp()
	BEBConfigFrame:StopMovingOrSizing()
end

function BEBOPTIONS.CheckButton_OnClick(this)
	BEBCharSettings[BEBOPTIONS.CURRENTFRAME][this.index] = not(not this:GetChecked())
	if (this.index == "flashing") then
		if (_G[BEBOPTIONS.CURRENTFRAME].textframe) then
			_G[BEBOPTIONS.CURRENTFRAME].textframe:SetAlpha(1)
		else
			_G[BEBOPTIONS.CURRENTFRAME].texture:SetAlpha(1)
		end
	elseif ((this.index == "shown") and (BEBOPTIONS.CURRENTFRAME == "BEBBarText")) then
		if (BEBCharSettings.BEBBarText.mouseover) then
			BEB.Feedback(BEBOPTIONS.TEXT.optionstextwasmouseover)
			BEBCharSettings.BEBBarText.mouseover = false
			BEBShowOnMouseoverCheckButton:SetChecked(false)
		end
	elseif ((this.index == "mouseover") and (BEBOPTIONS.CURRENTFRAME == "BEBBarText")) then
		if (not this:GetChecked()) then
			BEB.TextTimeToHide = nil
		end
		if (not BEBCharSettings.BEBBarText.shown) then
			BEB.Feedback(BEBOPTIONS.TEXT.optionstextwashidden)
			BEBCharSettings.BEBBarText.shown = true
			BEBShownCheckButton:SetChecked(true)
		end
	end
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
end

function BEBOPTIONS.ColorPicker_OnClick(this)
	if (ColorPickerFrame:IsShown()) then
		BEBOPTIONS.ColorPicker_Cancelled(ColorPickerFrame.previousValues)
		ColorPickerFrame:Hide()
	else
		local Red,Green,Blue,Alpha = unpack(BEBCharSettings[BEBOPTIONS.CURRENTFRAME][this.whenindex])
		ColorPickerFrame.previousValues = {Red, Green, Blue, Alpha}
		ColorPickerFrame.cancelFunc = BEBOPTIONS.ColorPicker_Cancelled
		ColorPickerFrame.opacityFunc = BEBOPTIONS.ColorPicker_OpacityChanged
		ColorPickerFrame.func = BEBOPTIONS.ColorPicker_ColorChanged
		ColorPickerFrame.index = this:GetName().."Texture"
		ColorPickerFrame.objindex = BEBOPTIONS.CURRENTFRAME
		ColorPickerFrame.whenindex = this.whenindex
		ColorPickerFrame.opacity = Alpha
		OpacitySliderFrame:SetValue(Alpha)
		ColorPickerFrame:SetColorRGB(Red, Green, Blue)
		ColorPickerFrame.hasOpacity = true
		ColorPickerFrame:ClearAllPoints()
		local x = (BEBConfigFrame:GetCenter() - (BEBDefaultsButton:GetWidth()/2))
		if (x < UIParent:GetWidth() / 2) then
			ColorPickerFrame:SetPoint("LEFT", "BEBConfigFrame", "RIGHT", 0, 0)
		else
			ColorPickerFrame:SetPoint("RIGHT", "BEBConfigFrame", "LEFT",- BEBDefaultsButton:GetWidth(), 0)
		end
		ColorPickerFrame:Show()
	end
end
function BEBOPTIONS.ColorPicker_Cancelled(this, color)
	BEBCharSettings[ColorPickerFrame.objindex][ColorPickerFrame.whenindex] = color
	BEBOPTIONS.OnShow(this, BEBOPTIONS.CURRENTFRAME)
	ColorPickerFrame.hasOpacity = false
end
function BEBOPTIONS.ColorPicker_ColorChanged()
	local r, g, b = ColorPickerFrame:GetColorRGB()
	local a = OpacitySliderFrame:GetValue()
	_G[ColorPickerFrame.index]:SetVertexColor(r,g,b,a)
	if (not ColorPickerFrame:IsShown()) then
		BEBCharSettings[ColorPickerFrame.objindex][ColorPickerFrame.whenindex] = {r,g,b,a}
		BEB.SetColors(nil, "force")
		ColorPickerFrame.hasOpacity = false
	end
end
function BEBOPTIONS.ColorPicker_OpacityChanged()
	if (ColorPickerFrame:IsShown()) then
		local r, g, b = ColorPickerFrame:GetColorRGB()
		local a = OpacitySliderFrame:GetValue()
		_G[ColorPickerFrame.index]:SetVertexColor(r, g, b, a)
	end
end

function BEBOPTIONS.Nudge(this,arg1,button)
	if (button) then
		this = _G[button]
	end
	local change
	if (arg1 == "RightButton") then
		change = 5
	else
		change = 1
	end
	if (this.index == "0") then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x = 0
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y = 0
	elseif (this.index == "up") then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y = BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y + change
	elseif (this.index == "down") then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y = BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y - change
	elseif (this.index == "left") then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x = BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x - change
	elseif (this.index == "right") then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x = BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x + change
	end
	_G[this:GetParent():GetName().."xEditBox"]:SetText(BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.x)
	_G[this:GetParent():GetName().."yEditBox"]:SetText(BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location.y)
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
end
function BEBOPTIONS.Placement_OnUpdate(self, elapsed)
	if (BEBNudging) then
		if (BEBNudgeTime <= 0) then
			BEBOPTIONS.Nudge(self, "MiddleButton",BEBNudging)
			BEBNudgeTime = 0.015
		else
			BEBNudgeTime = BEBNudgeTime - elapsed
		end
	end
end
function BEBOPTIONS.PositionEditBox_OnEnterPressed(this)
	local axis = this.index
	BEBCharSettings[BEBOPTIONS.CURRENTFRAME].location[axis] = this:GetNumber()
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
end

function BEBOPTIONS.SliderOnChange(this)
	_G[this:GetName().."_EditBox"]:SetText(this:GetValue())
	if (this.enabled) then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME][this.whatindex][this.axisindex] = this:GetValue()
	end
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
	if (BEBOPTIONS.CURRENTFRAME == "BEBMain") then
		BEB.SetupElement("BEBXpBar")
		BEB.SetupElement("BEBRestedXpBar")
		BEB.SetupElement("BEBRestedXpTick")
	end
	if ((this.label == "height") and (BEBOPTIONS.CURRENTFRAME == "BEBBarText") and(BEBCharSettings.BEBBarText.mouseover)) then
		BEBBarText:Show()
		BEB.TextTimeToHide = 2
	end
end

function BEBOPTIONS.SliderEditBox_OnEnterPressed(this)
	local value = this:GetNumber()
	local slidervalue = this:GetParent():GetValue(value)
	if (value ~= slidervalue) then
		this:GetParent():SetValue(value)
	end
end


function BEBOPTIONS.ShowMenu(this)
	if (BEB_DropDownMenu:IsVisible()) then
		BEB_DropDownMenu:Hide()
		return
	end
	BEB_DropDownMenu.whatindex = BEBOPTIONS.CURRENTFRAME
	if (this.typeindex) then
		BEB_DropDownMenu.typeindex = this.typeindex
	else
		BEB_DropDownMenu.typeindex = nil
	end
	if (this.subindex) then
		BEB_DropDownMenu.subindex = this.subindex
	else
		BEB_DropDownMenu.subindex = nil
	end
	if (not this.controlbox) then
		this.controlbox = this:GetName()
	end
	BEB_DropDownMenu.controlbox = this.controlbox
	if (this.func) then
		BEB_DropDownMenu.func = this.func
	else
		BEB_DropDownMenu.func = nil
	end
	BEB_DropDownMenu.controlboxtype = this.controlboxtype
	BEB_DropDownMenu.table = this.table
	local count = 0
	local widest = 0
	for value, text in pairs(BEBOPTIONS[this.table]) do
		count = count + 1
		_G["BEB_DropDownMenu_Button"..count]:SetText(text)
		_G["BEB_DropDownMenu_Button"..count]:SetAttribute("value", value)
		_G["BEB_DropDownMenu_Button"..count]:Show()
		local width = _G["BEB_DropDownMenu_Button"..count]:GetTextWidth()
		widest = math.max(widest, width)
	end
	for i=1, BEB_DropDownMenu.count do
		if (i <= count) then
			_G["BEB_DropDownMenu_Button"..i]:SetWidth(widest)
		else
			_G["BEB_DropDownMenu_Button"..i]:Hide()			
		end
	end
	BEB_DropDownMenu:SetWidth(widest + 20)
	BEB_DropDownMenu:SetHeight(count * 15 + 20)
	BEB_DropDownMenu:ClearAllPoints()
	BEB_DropDownMenu:SetPoint("TOPLEFT", _G[this.controlbox], "BOTTOMLEFT", 0, 0)
	if (BEB_DropDownMenu:GetBottom() < UIParent:GetBottom()) then
		local yoffset = UIParent:GetBottom() - BEB_DropDownMenu:GetBottom()
		BEB_DropDownMenu:ClearAllPoints()
		BEB_DropDownMenu:SetPoint("TOPLEFT", _G[this.controlbox], "BOTTOMLEFT", 0, yoffset)
	end
	BEB_DropDownMenu:Show()
	BEB_DropDownMenu.timer = 2
end
function BEBOPTIONS.MenuOptionOnClick(this)
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
	BEB_DropDownMenu:Hide()
end


function BEBOPTIONS.DDM_OnUpdate(this,elapsed)
	if (this.timer) then
		if (this.timer <= 0) then
			this:Hide()
		else
			this.timer = this.timer - elapsed
		end
	end
end
function BEBOPTIONS.DDM_ReplaceText(this)
	local table = BEBOPTIONS[this.table]
	_G[this.controlbox]:SetText(table[this.value])
	if (this.subindex) then
		BEBCharSettings[this.whatindex][this.typeindex][this.subindex] = this.value
	else
		BEBCharSettings[this.whatindex][this.typeindex] = this.value
	end
	BEBOPTIONS.OnShow(this, BEBOPTIONS.CURRENTFRAME)
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
end
function BEBOPTIONS.DDM_InsertText(this)
	_G[this.controlbox]:Insert(this.value)
	if (this.subindex) then
		BEBCharSettings[this.whatindex][this.typeindex][this.subindex] = _G[this.controlbox]:GetText()
	else
		BEBCharSettings[this.whatindex][this.typeindex] = _G[this.controlbox]:GetText()
	end
	if (this.subindex and this.subindex == "string") then
		BEBOPTIONS.ChangeText(this.whatindex)
	end
end

function BEBOPTIONS.DDM_SetText(this)
	_G[this.controlbox]:SetText(this.value)
end

function BEBOPTIONS.MenuEditBoxChangeText(this)
	if (_G[this:GetParent():GetName().."Button"].subindex and _G[this:GetParent():GetName().."Button"].subindex == "relto") then
		BEBOPTIONS.ChangeAttachTo(BEBOPTIONS.CURRENTFRAME)
	elseif (_G[this:GetParent():GetName().."Button"].subindex and _G[this:GetParent():GetName().."Button"].subindex == "string") then
		BEBOPTIONS.ChangeText(BEBOPTIONS.CURRENTFRAME)
	elseif (_G[this:GetParent():GetName().."Button"].typeindex == "texture") then
		BEBOPTIONS.ChangeTexture(BEBOPTIONS.CURRENTFRAME)
	end
end

function BEBOPTIONS.ChangeText(frame)
	if (frame == "BEBBarText") then
		BEB.CompileString(BEB_BarTextFrameEditBox:GetText(), _G[frame])
		BEB.StringEvent("WRITE_ALL", frame)
		BEBCharSettings.BEBBarText.text.string = BEB_BarTextFrameEditBox:GetText()
	end
end

function BEBOPTIONS.ChangeAttachTo(this,frame)
	if (BEB.IsFrame(this:GetText())) then
		BEBCharSettings[frame].location.relto = this:GetText()
		BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
	else
		BEB.Feedback(BEBOPTIONS.TEXT.frameisinvalid)
		this:SetText(this.prevvals)
	end
end

function BEBOPTIONS.ChangeTexture(this,frame)
	BEBCharSettings[frame].texture = this:GetText()
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
end

function BEBOPTIONS.CreateProfile(this)
	local bebprofile = _G[this:GetName().."EditBox"]:GetText()
	if (bebprofile == BEBOPTIONS.TEXT.none) then
		return
	end
	if (BEBGlobal.profiles[bebprofile]) then
		BEB.Feedback(BEBOPTIONS.TEXT.profilealreadyexists)
	else
		BEBGlobal.profiles[bebprofile] = {}
		BEB.SaveProfile(bebprofile)
--		table.setn(BEBGlobal.profiles,(table.getn(BEBGlobal.profiles)+1))
	end
	BEBOPTIONS.SetElementValue("BEBProfileFrame")
end
function BEBOPTIONS.UseProfile(this)
	if (BEBCharSettings.BEBProfile) then
		BEBCharSettings.BEBProfile = false
	else
		local bebprofile = _G[this:GetName().."DDMButton"]:GetText()
		if (bebprofile == BEBOPTIONS.TEXT.none) then
			return
		end
		BEB.LoadProfile(bebprofile)
		BEBCharSettings.BEBProfile = bebprofile
		BEB.SetupBars()
	end
	BEBOPTIONS.SetElementValue("BEBProfileFrame")
end
function BEBOPTIONS.LoadProfile(this)
	local bebprofile = _G[this:GetName().."DDMButton"]:GetText()
	if (bebprofile == BEBOPTIONS.TEXT.none) then
		return
	end
	BEB.LoadProfile(bebprofile)
	BEBCharSettings.BEBProfile = false
	BEB.SetupBars()
	BEBOPTIONS.SetElementValue("BEBProfileFrame")
end
function BEBOPTIONS.SaveProfile(this)
	local bebprofile = _G[this:GetName().."DDMButton"]:GetText()
	if (bebprofile == BEBOPTIONS.TEXT.none) then
		return
	end
	BEB.SaveProfile(bebprofile)
	BEBOPTIONS.SetElementValue("BEBProfileFrame")
end
function BEBOPTIONS.DeleteProfile(this)
	local bebprofile = _G[this:GetName().."DDMButton"]:GetText()
	if (bebprofile == BEBOPTIONS.TEXT.none) then
		return
	end
	if (bebprofile == BEBCharSettings.BEBProfile) then
		BEBCharSettings.BEBProfile = false
		BEBGlobal.profiles[bebprofile] = nil
--		table.setn(BEBGlobal.profiles,(table.getn(BEBGlobal.profiles)-1))
	else
		BEBGlobal.profiles[bebprofile] = nil
--		table.setn(BEBGlobal.profiles,(table.getn(BEBGlobal.profiles)-1))
	end
	BEBOPTIONS.SetElementValue("BEBProfileFrame")
end

function BEBOPTIONS.LevelSliderOnChange(this)
	_G[this:GetName().."_EditBox"]:SetText(this:GetValue())
	if (this.enabled) then
		BEBCharSettings[BEBOPTIONS.CURRENTFRAME][this.whatindex] = this:GetValue()
	end
	BEB.SetupElement(BEBOPTIONS.CURRENTFRAME)
	if ((BEBOPTIONS.CURRENTFRAME == "BEBBarText") and(BEBCharSettings.BEBBarText.mouseover)) then
		BEBBarText:Show()
		BEB.TextTimeToHide = 2
	end
end
function BEBOPTIONS.LevelSliderEditBox_OnEnterPressed(this)
	local value = this:GetNumber()
	local slidervalue = this:GetParent():GetValue(value)
	if (value ~= slidervalue) then
		this:GetParent():SetValue(value)
	end
end

function BEBOPTIONS.DDM_Button_OnClick(this)
   	this.whatindex = BEBOPTIONS.CURRENTFRAME
	BEB_DropDownMenu.owner = this
	ToggleDropDownMenu(1, nil, BEB_DropDownMenu, this:GetName(), 0, 0);
end

function BEBOPTIONS.DDM_MenuButton_OnClick(this)
	this.owner.value = this.value
	BEBOPTIONS[this.owner.func](this.owner)
end

function BEBOPTIONS.DDM_Initialize(this)
	if this.owner then
		owner = this.owner
		local info = {};
		info.func = BEBOPTIONS.DDM_MenuButton_OnClick
		info.owner = owner
		local menutable = BEBOPTIONS[owner.table]
		local sorted = {}
		for key, _ in pairs(menutable) do
			table.insert(sorted, key)
		end
		table.sort(sorted, function(a, b) if menutable[a] < menutable[b] then return true end end)
		for i=1, #sorted, 1 do
			info.value = sorted[i]
			local value = menutable[info.value]
			info.text = string.gsub(value, "^%d%d", "")
				UIDropDownMenu_AddButton(info)
		end
	end
end

local function argsToString(arg, ...)
	if select("#", ...) > 0 then
		return tostring(arg), argsToString(...)
	else
		return tostring(arg)
	end
end

function BEBOprint(...)
	local output = strjoin(" ", argsToString(...))
	ChatFrame1:AddMessage("BEBOptions: "..output)
end
