
UIPanelWindows["Nurfed_ActionBarsOptionsFrame"] = {area = "center", pushable = 1};

local Nurfed_ActionBarsOptionTabs = {
	[1] = NURFEDACTIONBARS_OPTIONS_TABS_GENERAL,
	[2] = NURFEDACTIONBARS_OPTIONS_TABS_ACTIONBAR1,
	[3] = NURFEDACTIONBARS_OPTIONS_TABS_ACTIONBAR2,
	[4] = NURFEDACTIONBARS_OPTIONS_TABS_ACTIONBAR3,
	[5] = NURFEDACTIONBARS_OPTIONS_TABS_ACTIONBAR4,
};

local Nurfed_ActionBarSettings = {
	["ActionBar"] = { name = "ActionButton", buttons = 12, buttonsize = 37, texturesize = 64, start = 1, finish = 12, point = "BOTTOMLEFT", relativeTo = "UIParent", relativePoint = "BOTTOMLEFT", x = 8, y = 8 },
	["ShapeshiftBar"] = { name = "ShapeshiftButton", buttons = 10, buttonsize = 30, texturesize = 58, start = 1, finish = 10, point = "BOTTOMLEFT", relativeTo = "Nurfed_ActionBar", relativePoint = "TOPLEFT", x = 0, y = 5 },
	["PetActionBar"] = { name = "PetActionButton", buttons = 10, buttonsize = 30, texturesize = 58, start = 1, finish = 10, point = "BOTTOMLEFT", relativeTo = "Nurfed_ActionBar", relativePoint = "TOPLEFT", x = 0, y = 5 },
	["ActionBar1"] = { name = "Nurfed_ActionButton", buttons = 12, buttonsize = 37, texturesize = 64, start = 1, finish = 12, point = "BOTTOM", relativeTo = "UIParent", relativePoint = "BOTTOM", x = 0, y = 8 },
	["ActionBar2"] = { name = "Nurfed_ActionButton", buttons = 12, buttonsize = 37, texturesize = 64, start = 13, finish = 24, point = "BOTTOM", relativeTo = "UIParent", relativePoint = "BOTTOM", x = 0, y = 50 },
	["ActionBar3"] = { name = "Nurfed_ActionButton", buttons = 12, buttonsize = 37, texturesize = 64, start = 25, finish = 36, point = "BOTTOM", relativeTo = "UIParent", relativePoint = "BOTTOM", x = 0, y = 100 },
	["ActionBar4"] = { name = "Nurfed_ActionButton", buttons = 12, buttonsize = 37, texturesize = 64, start = 37, finish = 48, point = "BOTTOM", relativeTo = "UIParent", relativePoint = "BOTTOM", x = 0, y = 150 },
	["Bags"] = { point = "BOTTOMRIGHT", relativeTo = "UIParent", relativePoint = "BOTTOMRIGHT", x = -8, y = 8 },
};

local function Nurfed_UpdateMicro()
	if ( Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideMicro == 1) then
		CharacterMicroButton:ClearAllPoints();
		CharacterMicroButton:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -50);
		CharacterMicroButton:Hide();
		SpellbookMicroButton:Hide();
		TalentMicroButton:Hide();
		QuestLogMicroButton:Hide();
		SocialsMicroButton:Hide();
		WorldMapMicroButton:Hide();
		MainMenuMicroButton:Hide();
		HelpMicroButton:Hide();
	else
		CharacterMicroButton:ClearAllPoints();
		CharacterMicroButton:SetPoint("BOTTOMLEFT", "Nurfed_MicroButtons", "BOTTOMLEFT", 10, 10);
		CharacterMicroButton:Show();
		SpellbookMicroButton:Show();
		TalentMicroButton:Show();
		QuestLogMicroButton:Show();
		SocialsMicroButton:Show();
		WorldMapMicroButton:Show();
		MainMenuMicroButton:Show();
		HelpMicroButton:Show();
	end
end

local function Nurfed_UpdateBags()
	local scale = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].BagScale;
	MainMenuBarBackpackButton:ClearAllPoints();
	getglobal("MainMenuBarBackpackButton"):SetWidth(37*scale);
	getglobal("MainMenuBarBackpackButton"):SetHeight(37*scale);
	getglobal("MainMenuBarBackpackButtonNormalTexture"):SetWidth(64*scale);
	getglobal("MainMenuBarBackpackButtonNormalTexture"):SetHeight(64*scale);
	MainMenuBarBackpackButton:SetPoint("BOTTOMRIGHT", "Nurfed_Bags", "BOTTOMRIGHT", -10*scale, 10*scale);
	if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags == 1 and ContainerFrame1.bagsShown == 0) then
		MainMenuBarBackpackButton:Hide();
	else
		MainMenuBarBackpackButton:Show();
	end
	for i=0, 3 do
		local button = getglobal("CharacterBag"..i.."Slot");
		local buttonx = getglobal("CharacterBag"..i.."SlotNormalTexture");
		local lastID = i - 1;
		local lastBag = "CharacterBag"..lastID.."Slot";
		button:ClearAllPoints();
		button:SetWidth(37*scale);
		button:SetHeight(37*scale);
		buttonx:SetWidth(64*scale);
		buttonx:SetHeight(64*scale);
		if (i == 0) then
			if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].VerticalBags == 1) then
				Nurfed_Bags:SetHeight((193*scale) + 18);
				Nurfed_Bags:SetWidth((37*scale) + 18);
				button:SetPoint("BOTTOM", "MainMenuBarBackpackButton", "TOP",0, 2);
			else
				Nurfed_Bags:SetHeight((37*scale) + 18);
				Nurfed_Bags:SetWidth((193*scale) + 18);
				button:SetPoint("RIGHT", "MainMenuBarBackpackButton", "LEFT",-2, 0);
			end
		else
			if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].VerticalBags == 1) then
				button:SetPoint("BOTTOM", lastBag, "TOP", 0, 2);
			else
				button:SetPoint("RIGHT", lastBag, "LEFT", -2, 0);
			end
		end
		if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags == 1 and ContainerFrame1.bagsShown == 0) then
			button:Hide();
		else
			button:Show();
		end
	end
end

local function Nurfed_UpdateBar(bar)
	local mainattached = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached;
	local id, found = gsub(bar, "ActionBar([1-4])", "%1");
	id = tonumber(id);
	if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][bar] == 1) then
		if (bar == "ShapeshiftBar") then
			ShapeshiftBarFrame:Show();
		elseif (bar == "PetActionBar") then
			PetActionBarFrame:Show();
		else
			getglobal("Nurfed_"..bar):Show();
		end
	elseif (found == 1 and id <= mainattached) then
		getglobal("Nurfed_"..bar):Show();
	else
		if (bar == "ShapeshiftBar") then
			ShapeshiftBarFrame:Hide();
		elseif (bar == "PetActionBar") then
			PetActionBarFrame:Hide();
		else
			getglobal("Nurfed_"..bar):Hide();
		end
	end
end

local function Nurfed_SetActionBarsGap(bar)
	local name = Nurfed_ActionBarSettings[bar].name;
	local buttonsize = Nurfed_ActionBarSettings[bar].buttonsize;
	local texturesize = Nurfed_ActionBarSettings[bar].texturesize;
	local start = Nurfed_ActionBarSettings[bar].start;
	local mainattached = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached;
	local id, found = gsub(bar, "ActionBar([1-4])", "%1");
	id = tonumber(id);
	local vertical, scale, buttons;
	if (found == 1 and id <= mainattached) then
		scale = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer]["ActionBarScale"];
		vertical = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer]["ActionBarVertical"];
	else
		scale = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][bar.."Scale"];
		vertical = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][bar.."Vertical"];
	end
	local finish = Nurfed_ActionBarSettings[bar].finish;
	local first = getglobal(name..start);
	local frame = "Nurfed_"..bar;
	if (id) then
		if (id <= mainattached) then
			buttons = 12;
		else
			buttons = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer]["ActionBar"..id.."Buttons"];
		end
	else
		buttons = Nurfed_ActionBarSettings[bar].buttons;
	end

	if (vertical == 1) then
		if (bar == "ActionBar") then
			getglobal(frame):SetWidth((buttonsize * scale) + 20 + ((buttonsize * scale) * mainattached));
		else
			getglobal(frame):SetWidth((buttonsize * scale) + 20);
		end
		getglobal(frame):SetHeight((((buttonsize+2) * scale) * buttons)+ 18);
		first:ClearAllPoints();
		if (found == 1 and id <= mainattached) then
			local attachframe;
			local attachto = id - 1;
			if (attachto == 0) then
				attachframe = "ActionButton1";
			else
				attachframe = "Nurfed_ActionButton"..Nurfed_ActionBarSettings["ActionBar"..attachto].start;
			end
			first:SetPoint("TOPLEFT", attachframe, "TOPRIGHT", 2, 0);
		else
			first:SetPoint("TOPLEFT", frame, "TOPLEFT", 10*scale, -10*scale);
		end
	else
		getglobal(frame):SetWidth((((buttonsize+2) * scale) * buttons)+ 18);
		if (bar == "ActionBar") then
			getglobal(frame):SetHeight((buttonsize * scale) + 20 + ((buttonsize * scale) * mainattached));
		else
			getglobal(frame):SetHeight((buttonsize * scale) + 20);
		end
		first:ClearAllPoints();
		if (found == 1 and id <= mainattached) then
			local attachframe;
			local attachto = id - 1;
			if (attachto == 0) then
				attachframe = "ActionButton1";
			else
				attachframe = "Nurfed_ActionButton"..Nurfed_ActionBarSettings["ActionBar"..attachto].start;
			end
			first:SetPoint("BOTTOMLEFT", attachframe, "TOPLEFT", 0, 2);
		else
			first:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 10*scale, 10*scale);
		end
	end

	for i=start, finish do
		-- Get current buttons
		local button = getglobal(name..i);

		-- Change horizontal bars
		button:SetWidth(buttonsize*scale);
		button:SetHeight(buttonsize*scale);
		if (getglobal(name..i.."NormalTexture")) then
			getglobal(name..i.."NormalTexture"):SetWidth(texturesize*scale);
			getglobal(name..i.."NormalTexture"):SetHeight(texturesize*scale);
		else
			getglobal(name..i.."NormalTexture2"):SetWidth(texturesize*scale);
			getglobal(name..i.."NormalTexture2"):SetHeight(texturesize*scale);
			getglobal(name..i.."AutoCastable"):SetWidth(texturesize*scale);
			getglobal(name..i.."AutoCastable"):SetHeight(texturesize*scale);
		end
		getglobal(name..i.."HotKey"):SetWidth(32*scale);
		getglobal(name..i.."HotKey"):SetHeight(10*scale);
		getglobal(name..i.."HotKey"):SetTextHeight(12*scale);
		getglobal(name..i.."HotKey"):SetPoint("TOPLEFT", name..i, "TOPLEFT", 2*scale, -2*scale);
		getglobal(name..i.."Count"):SetTextHeight(14*scale);
		getglobal(name..i.."Count"):SetPoint("BOTTOMRIGHT", name..i, "BOTTOMRIGHT", -2*scale, 2*scale);
		getglobal(name..i.."Name"):SetWidth(36*scale);
		getglobal(name..i.."Name"):SetHeight(10*scale);
		getglobal(name..i.."Name"):SetTextHeight(10*scale);
		getglobal(name..i.."Name"):SetPoint("BOTTOMLEFT", name..i, "BOTTOMLEFT", 0, 2*scale);
		if (getglobal(name..i.."Cooldown")) then
			getglobal(name..i.."Cooldown"):SetScale(0.85 * scale);
		end
		if (getglobal("ABI"..name..i)) then
			getglobal("ABI"..name..i):SetWidth(36*scale);
			getglobal("ABI"..name..i):SetHeight(36*scale);
			getglobal("ABI"..name..i.."Cooldown"):SetTextHeight(28*scale);
			getglobal("ABI"..name..i.."Count"):SetWidth(36*scale);
			getglobal("ABI"..name..i.."Count"):SetHeight(11*scale);
			getglobal("ABI"..name..i.."Count"):SetTextHeight(11*scale);
		end

		-- Decrement the ID
		local lastID = i - 1;
		if(lastID >= start) then
			local lastButton = name..lastID;
			button:ClearAllPoints();
			if (vertical == 1) then
				button:SetPoint("TOP", lastButton, "BOTTOM", 0, -2);
			else
				button:SetPoint("LEFT", lastButton, "RIGHT", 2, 0);
			end
		end
		if (id) then
			local count;
			if (id <= mainattached) then
				count = 12;
			else
				count = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer]["ActionBar"..id.."Buttons"];
			end
			if (i < (count + start)) then
				button:Show();
				button.hidden = nil;
			else
				button:Hide();
				button.hidden = true;
			end
		end
	end

	if (bar == "ActionBar") then
		name = "BonusActionButton";
		for i=start, finish do
			-- Get current buttons
			local button = getglobal(name..i);

			-- Change horizontal bars
			button:SetWidth(buttonsize*scale);
			button:SetHeight(buttonsize*scale);
			if (getglobal(name..i.."NormalTexture")) then
				getglobal(name..i.."NormalTexture"):SetWidth(texturesize*scale);
				getglobal(name..i.."NormalTexture"):SetHeight(texturesize*scale);
			else
				getglobal(name..i.."NormalTexture2"):SetWidth(texturesize*scale);
				getglobal(name..i.."NormalTexture2"):SetHeight(texturesize*scale);
				getglobal(name..i.."AutoCastable"):SetWidth(texturesize*scale);
				getglobal(name..i.."AutoCastable"):SetHeight(texturesize*scale);
			end
			getglobal(name..i.."HotKey"):SetWidth(32*scale);
			getglobal(name..i.."HotKey"):SetHeight(10*scale);
			getglobal(name..i.."HotKey"):SetTextHeight(12*scale);
			getglobal(name..i.."HotKey"):SetPoint("TOPLEFT", name..i, "TOPLEFT", 2*scale, -2*scale);
			getglobal(name..i.."Count"):SetTextHeight(14*scale);
			getglobal(name..i.."Count"):SetPoint("BOTTOMRIGHT", name..i, "BOTTOMRIGHT", -2*scale, 2*scale);
			getglobal(name..i.."Name"):SetWidth(36*scale);
			getglobal(name..i.."Name"):SetHeight(10*scale);
			getglobal(name..i.."Name"):SetTextHeight(10*scale);
			getglobal(name..i.."Name"):SetPoint("BOTTOMLEFT", name..i, "BOTTOMLEFT", 0, 2*scale);
			if (getglobal(name..i.."Cooldown")) then
				getglobal(name..i.."Cooldown"):SetScale(0.85 * scale);
			end
			if (getglobal("ABI"..name..i)) then
				getglobal("ABI"..name..i):SetWidth(36*scale);
				getglobal("ABI"..name..i):SetHeight(36*scale);
				getglobal("ABI"..name..i.."Cooldown"):SetTextHeight(28*scale);
				getglobal("ABI"..name..i.."Count"):SetWidth(36*scale);
				getglobal("ABI"..name..i.."Count"):SetHeight(11*scale);
				getglobal("ABI"..name..i.."Count"):SetTextHeight(11*scale);
			end

			-- Decrement the ID
			local lastID = i - 1;
			if(lastID >= start) then
				local lastButton = name..lastID;
				button:ClearAllPoints();
				if (vertical == 1) then
					button:SetPoint("TOP", lastButton, "BOTTOM", 0, -2);
				else
					button:SetPoint("LEFT", lastButton, "RIGHT", 2, 0);
				end
			end
		end
		for i=1, 4 do
			Nurfed_SetActionBarsGap("ActionBar"..i);
			Nurfed_UpdateBar("ActionBar"..i);
		end
	else
		Nurfed_UpdateBar(bar);
	end
end

local function Nurfed_ActionBarGraphics()
	BonusActionBarFrame:SetWidth(1);
	ActionBarUpButton:Hide();
	ActionBarDownButton:Hide();
	MainMenuBarTexture0:Hide();
	MainMenuBarTexture1:Hide();
	MainMenuBarTexture2:Hide();
	MainMenuBarTexture3:Hide();
	MainMenuBarLeftEndCap:Hide();
	MainMenuBarRightEndCap:Hide();
	MainMenuBarOverlayFrame:Hide();
	MainMenuBarPerformanceBarFrame:Hide();
	MainMenuExpBar:ClearAllPoints();
	MainMenuExpBar:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -50);
	ExhaustionTick:Hide();
	BonusActionBarTexture0:ClearAllPoints();
	BonusActionBarTexture0:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -50);
	ShapeshiftBarLeft:Hide();
	ShapeshiftBarMiddle:Hide();
	ShapeshiftBarRight:Hide();
	SlidingActionBarTexture0:ClearAllPoints();
	SlidingActionBarTexture0:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -50);
	if (PetHasActionBar()) then
		PetActionBar_Update();
		UnlockPetActionBar();
		HidePetActionBar();
		ShowPetActionBar();
		LockPetActionBar();
	end
	MainMenuBarPageNumber:Hide();
	ShapeshiftBarFrame:ClearAllPoints();
	ShapeshiftBarFrame:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -50);
	MainMenuBar:ClearAllPoints();
	MainMenuBar:SetPoint("BOTTOM", "UIParent", "BOTTOM", 0, -150);
end

local function Nurfed_UpdateButtons()
	local label;
	local button;
	for i = 1, 48 do
		label = getglobal("Nurfed_ActionButton"..i.."HotKey");
		if(Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowHotKeys ~= 1) then
			label:Hide();
		else
			label:Show();
		end
		button = getglobal("Nurfed_ActionButton"..i);
		if( Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowGrid ~= 1 and button.showgrid - 1 == 0 and not HasAction(Nurfed_ActionButton_GetID(button)) ) then
			button:Hide();
		else
			button:Show();
		end
	end
end

local function Nurfed_ActionBars_Init()
	Nurfed_SetActionBarsGap("ActionBar");
	Nurfed_SetActionBarsGap("ShapeshiftBar");
	Nurfed_SetActionBarsGap("PetActionBar");

	Nurfed_UpdateButtons();
	Nurfed_ActionBarGraphics();
	Nurfed_UpdateBags();
	Nurfed_UpdateMicro();

	Nurfed_ActionBarsOptionsFrameTitle:SetText(NURFEDACTIONBARS.."\nVers. "..Nurfed_ActionBarsConfig.Version);
end

local Nurfed_ActionBarCheckBoxes = {};
local Nurfed_ActionBarSliders = {};
local Nurfed_ActionBarDropDowns = {};

local function Nurfed_ActionBarsOptionsInit()
--Checks
	--general
	Nurfed_ActionBarCheckBoxes = {
		[1] = { text = "Lock Action Bars", option = "Locked" },
		[2] = { text = "Show Empty Buttons", option = "ShowGrid", func = Nurfed_UpdateButtons },
		[3] = { text = "Show HotKeys", option = "ShowHotKeys", func = Nurfed_UpdateButtons },
		[4] = { text = "Show Stance Bar", option = "ShapeshiftBar", func = Nurfed_UpdateBar, arg = "ShapeshiftBar" },
		[5] = { text = "Vertical Stance Bar", option = "ShapeshiftBarVertical", func = Nurfed_SetActionBarsGap, arg = "ShapeshiftBar" },
		[6] = { text = "Show Pet Bar", option = "PetActionBar", func = Nurfed_UpdateBar, arg = "PetActionBar" },
		[7] = { text = "Vertical Pet Bar", option = "PetActionBarVertical", func = Nurfed_SetActionBarsGap, arg = "PetActionBar" },
		[8] = { text = "Hide Bags When Closed", option = "HideBags", func = Nurfed_UpdateBags },
		[9] = { text = "Hide Micro Menu", option = "HideMicro", func = Nurfed_UpdateMicro },
		[10] = { text = "Vertical Action Bar", option = "ActionBarVertical", func = Nurfed_SetActionBarsGap, arg = "ActionBar", align = "right" },
		[11] = { text = "Vertical Bags", option = "VerticalBags", func = Nurfed_UpdateBags, align = "right" },
		[12] = { text = "Enable Nurfed Bar 1", option = "ActionBar1", func = Nurfed_UpdateBar, arg = "ActionBar1"},
		[13] = { text = "Vertical Nurfed Bar 1", option = "ActionBar1Vertical", func = Nurfed_SetActionBarsGap, arg = "ActionBar1" },
		[14] = { text = "Enable Nurfed Bar 2", option = "ActionBar2", func = Nurfed_UpdateBar, arg = "ActionBar2"},
		[15] = { text = "Vertical Nurfed Bar 2", option = "ActionBar2Vertical", func = Nurfed_SetActionBarsGap, arg = "ActionBar2" },
		[16] = { text = "Enable Nurfed Bar 3", option = "ActionBar3", func = Nurfed_UpdateBar, arg = "ActionBar3"},
		[17] = { text = "Vertical Nurfed Bar 3", option = "ActionBar3Vertical", func = Nurfed_SetActionBarsGap, arg = "ActionBar3" },
		[18] = { text = "Enable Nurfed Bar 4", option = "ActionBar4", func = Nurfed_UpdateBar, arg = "ActionBar4"},
		[19] = { text = "Vertical Nurfed Bar 4", option = "ActionBar4Vertical", func = Nurfed_SetActionBarsGap, arg = "ActionBar4" },
	};

--Sliders
	Nurfed_ActionBarSliders = {
		[1] = { text = "Scale Action Bar", option = "ActionBarScale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ActionBar" },
		[2] = { text = "Scale Stance Bar", option = "ShapeshiftBarScale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ShapeshiftBar" },
		[3] = { text = "Scale Pet Bar", option = "PetActionBarScale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "PetActionBar" },
		[4] = { text = "Scale Bag Bar", option = "BagScale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_UpdateBags },
		[5] = { text = "Attach To Main", option = "MainAttached", min = 0, max = 4, step = 1, func = Nurfed_SetActionBarsGap, arg = "ActionBar" },
		[6] = { text = "Scale Nurfed Bar 1", option = "ActionBar1Scale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ActionBar1" },
		[7] = { text = "Nurfed Bar 1 Buttons", option = "ActionBar1Buttons", min = 1, max = 12, step = 1, func = Nurfed_SetActionBarsGap, arg = "ActionBar1" },
		[8] = { text = "Scale Nurfed Bar 2", option = "ActionBar2Scale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ActionBar2" },
		[9] = { text = "Nurfed Bar 2 Buttons", option = "ActionBar2Buttons", min = 1, max = 12, step = 1, func = Nurfed_SetActionBarsGap, arg = "ActionBar2" },
		[10] = { text = "Scale Nurfed Bar 3", option = "ActionBar3Scale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ActionBar3" },
		[11] = { text = "Nurfed Bar 3 Buttons", option = "ActionBar3Buttons", min = 1, max = 12, step = 1, func = Nurfed_SetActionBarsGap, arg = "ActionBar3" },
		[12] = { text = "Scale Nurfed Bar 4", option = "ActionBar4Scale", min = 0.5, max = 1.5, step = 0.01, func = Nurfed_SetActionBarsGap, arg = "ActionBar4" },
		[13] = { text = "Nurfed Bar 4 Buttons", option = "ActionBar4Buttons", min = 1, max = 12, step = 1, func = Nurfed_SetActionBarsGap, arg = "ActionBar4" },
	};
end

Nurfed_ActionBarsPlayer = nil;

function Nurfed_ActionBars_OnLoad()
	Nurfed_ActionBar:RegisterForDrag("LeftButton");
	Nurfed_ActionBar1:RegisterForDrag("LeftButton");
	Nurfed_ActionBar2:RegisterForDrag("LeftButton");
	Nurfed_ActionBar3:RegisterForDrag("LeftButton");
	Nurfed_ActionBar4:RegisterForDrag("LeftButton");
	Nurfed_Bags:RegisterForDrag("LeftButton");
	Nurfed_MicroButtons:RegisterForDrag("LeftButton");
	Nurfed_ShapeshiftBar:RegisterForDrag("LeftButton");
	Nurfed_PetActionBar:RegisterForDrag("LeftButton");
	Nurfed_CastingBar:RegisterForDrag("LeftButton");
	this:RegisterEvent("VARIABLES_LOADED");
	
	-- Register our slash command
	SLASH_NURFEDACTIONBARS1 = "/nurfedab";
	SLASH_NURFEDACTIONBARS2 = "/nab";
	SlashCmdList["NURFEDACTIONBARS"] = function(msg)
		NurfedActionBars_SlashCommandHandler(msg);
	end
end

function Nurfed_ActionBars_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		if( not Nurfed_ActionBarsConfig or not Nurfed_ActionBarsConfig.Version) then
			Nurfed_ActionBarsConfig = {};
		end

		Nurfed_ActionBarsConfig.Version = "10.12.2005";

		local realm = GetCVar("realmName");
		local player = UnitName("player");
		Nurfed_ActionBarsPlayer = player.." - "..realm;

		if (not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer]) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer] = {};
		end

		--main action bar
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].Locked) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].Locked = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowGrid) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowGrid = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowHotKeys) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowHotKeys = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBar) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBar = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBarVertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBarVertical = 0;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBarScale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBarScale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBar) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBar = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBarVertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBarVertical = 0;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBarScale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBarScale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideMicro) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideMicro = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBarScale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBarScale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBarVertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBarVertical = 0;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].BagScale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].BagScale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].VerticalBags) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].VerticalBags = 0;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached = 2;
		end

		--nurfed action bar 1
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1 = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Scale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Scale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Buttons) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Buttons = 12;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Vertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1Vertical = 1;
		end

		--nurfed action bar 2
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Enabled) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Enabled = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Scale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Scale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Buttons) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Buttons = 12;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Vertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2Vertical = 1;
		end

		--nurfed action bar 3
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Enabled) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Enabled = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Scale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Scale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Buttons) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Buttons = 12;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Vertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3Vertical = 0;
		end

		--nurfed action bar 4
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Enabled) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Enabled = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Scale) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Scale = 1;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Buttons) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Buttons = 12;
		end
		if ( not Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Vertical) then
			Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4Vertical = 0;
		end

		if(NURFED_GENERAL == 1) then
			Nurfed_AddMenuItem(NURFEDACTIONBARS, Nurfed_ActionBarsMenu, 1);
		end

		Nurfed_ActionBars_Init();
		Nurfed_ActionBarsOptionsInit();
		return;
	end
end

function Nurfed_ActionBarsMenu()
	if (Nurfed_ActionBarsOptionsFrame:IsVisible()) then
		HideUIPanel(Nurfed_ActionBarsOptionsFrame);
	else
		ShowUIPanel(Nurfed_ActionBarsOptionsFrame);
	end
end

---------------------------------------------------------------------------
--			Nurfed ActionBars Slider Functions
---------------------------------------------------------------------------

function Nurfed_ActionBarsSliderOnShow()
	local id = this:GetID();
	getglobal("Nurfed_ActionBarsSlider"..id.."Text"):SetText(Nurfed_ActionBarSliders[id].text);
	getglobal("Nurfed_ActionBarsSlider"..id.."Low"):SetText(Nurfed_ActionBarSliders[id].min);
	getglobal("Nurfed_ActionBarsSlider"..id.."High"):SetText(Nurfed_ActionBarSliders[id].max);
	this:SetMinMaxValues(Nurfed_ActionBarSliders[id].min, Nurfed_ActionBarSliders[id].max);
	this:SetValueStep(Nurfed_ActionBarSliders[id].step);
	this:SetValue(Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][Nurfed_ActionBarSliders[id].option]);
	getglobal("Nurfed_ActionBarsSlider"..id.."SliderValue"):SetText(format("%.2f", this:GetValue()));
end

function Nurfed_ActionBarsSliderOnValueChanged()
	if (this.tip ~=1) then return; end
	local id = this:GetID();
	getglobal(this:GetName().."SliderValue"):SetText(format("%.2f", this:GetValue()));
	Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][Nurfed_ActionBarSliders[id].option] = this:GetValue();
	local func = Nurfed_ActionBarSliders[id].func;
	local arg = Nurfed_ActionBarSliders[id].arg;
	if (func) then
		if (arg) then
			func(arg);
		else
			func();
		end
	end
	PlaySound("igMainMenuOptionCheckBoxOn");
end

---------------------------------------------------------------------------
--			Nurfed ActionBars CheckBox Functions
---------------------------------------------------------------------------

function Nurfed_ActionBarsCheckBoxOnShow()
	local id = this:GetID();
	local option = Nurfed_ActionBarCheckBoxes[id].option;
	local value = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][option];
	local align = Nurfed_ActionBarCheckBoxes[id].align;
	if (align == "right") then
		getglobal("Nurfed_ActionBarsOptionCheck"..id.."Text"):ClearAllPoints();
		getglobal("Nurfed_ActionBarsOptionCheck"..id.."Text"):SetPoint("RIGHT", "Nurfed_ActionBarsOptionCheck"..id, "LEFT", 0, 0);
	end
	getglobal("Nurfed_ActionBarsOptionCheck"..id.."Text"):SetText(Nurfed_ActionBarCheckBoxes[id].text);
	this:SetChecked(value);
end

function Nurfed_ActionBarsCheckBoxOnClick()
	local id = this:GetID();
	local func = Nurfed_ActionBarCheckBoxes[id].func;
	local arg = Nurfed_ActionBarCheckBoxes[id].arg;
	local value = Nurfed_ActionBarCheckBoxes[id].value;
	local option = Nurfed_ActionBarCheckBoxes[id].option;
	if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][option] == 1) then
		Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][option] = 0;
		PlaySound("igMainMenuOptionCheckBoxOff");
	else
		Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer][option] = 1;
		PlaySound("igMainMenuOptionCheckBoxOn");
	end
	if (func) then
		if (arg) then
			func(arg);
		else
			func();
		end
	end
end

---------------------------------------------------------------------------
--			Nurfed Unit Tab Functions
---------------------------------------------------------------------------

function Nurfed_ActionBarsOptionTabsInit()
	for k,v in Nurfed_ActionBarsOptionTabs do
		getglobal("Nurfed_ActionBarsOptionTab"..k):SetText(v);
		getglobal("Nurfed_ActionBarsOptionPage"..k.."SubFrame1Title"):SetText(v);
	end
	if ( not this.selectedTab ) then
		this.selectedTab = 1;
	end
	Nurfed_ActionBarsOptionTabSelect(this.selectedTab);
end

function Nurfed_ActionBarsOptionTabSelect(page)
	for k,v in Nurfed_ActionBarsOptionTabs do
		if (page == k) then
			Nurfed_ActionBarsOptionsFrame.selectedTab = k;
			getglobal("Nurfed_ActionBarsOptionPage"..k):Show();
			getglobal("Nurfed_ActionBarsOptionTab"..k):Disable();
		else
			getglobal("Nurfed_ActionBarsOptionPage"..k):Hide();
			getglobal("Nurfed_ActionBarsOptionTab"..k):Enable();
		end
	end
end

function Nurfed_ActionBarsResetPos(bar)
	local frame = getglobal("Nurfed_"..bar);
	local info = {};
	info = Nurfed_ActionBarSettings[bar];
	frame:ClearAllPoints();
	frame:SetPoint(info.point, info.relativeTo, info.relativePoint, info.x, info.y);
end

function NurfedActionBars_SlashCommandHandler(msg)
	if( msg ) then
		Nurfed_ActionBarsMenu();
	end
end
