
local lUpdateTimer = 0;
local ACTIONBAR_UPDATE_TIME = 0.5;
local lBagUpdateTime = 0;

function Nurfed_ActionButton_OnLoad()
	this.showgrid = 1;
	this.flashing = 0;
	this.flashtime = 0;
	Nurfed_ActionButton_Update();
	this:RegisterForDrag("LeftButton", "RightButton");
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	this:RegisterEvent("ACTIONBAR_SHOWGRID");
	this:RegisterEvent("ACTIONBAR_HIDEGRID");
	this:RegisterEvent("ACTIONBAR_PAGE_CHANGED");
	this:RegisterEvent("ACTIONBAR_SLOT_CHANGED");
	this:RegisterEvent("ACTIONBAR_UPDATE_STATE");
	this:RegisterEvent("ACTIONBAR_UPDATE_USABLE");
	this:RegisterEvent("ACTIONBAR_UPDATE_COOLDOWN");
	this:RegisterEvent("UPDATE_INVENTORY_ALERTS");
	this:RegisterEvent("PLAYER_AURAS_CHANGED");
	this:RegisterEvent("PLAYER_TARGET_CHANGED");
	this:RegisterEvent("UNIT_AURASTATE");
	this:RegisterEvent("UNIT_INVENTORY_CHANGED");
	this:RegisterEvent("CRAFT_SHOW");
	this:RegisterEvent("CRAFT_CLOSE");
	this:RegisterEvent("TRADE_SKILL_SHOW");
	this:RegisterEvent("TRADE_SKILL_CLOSE");
	this:RegisterEvent("UNIT_HEALTH");
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("UNIT_RAGE");
	this:RegisterEvent("UNIT_FOCUS");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("PLAYER_ENTER_COMBAT");
	this:RegisterEvent("PLAYER_LEAVE_COMBAT");
	this:RegisterEvent("PLAYER_COMBO_POINTS");
	this:RegisterEvent("UPDATE_BINDINGS");
	this:RegisterEvent("START_AUTOREPEAT_SPELL");
	this:RegisterEvent("STOP_AUTOREPEAT_SPELL");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	Nurfed_ActionButton_UpdateHotkeys();
end

function Nurfed_ActionButton_OnEvent(event)
	if( event == "PLAYER_ENTERING_WORLD" ) then
		local id = Nurfed_ActionButton_GetID(this);
		if(UnitClass("player") == "Warrior" or UnitClass("player") == "Krieger" or UnitClass("player") == "Guerrier") then
			if (id >= 73 and id <= 108) then
				this:SetID(id - 36);
			end
		elseif(UnitClass("player") == "Druid" or UnitClass("player") == "Druide") then
			if(id >= 73 and id <= 96) then
				this:SetID(id - 24);
			elseif (id >= 97 and id <= 108) then
				this:SetID(id - 12);
			end
		elseif(UnitClass("player") == "Rogue" or UnitClass("player") == "Schurke" or UnitClass("player") == "Voleur") then
			if (id >= 73 and id <= 84) then
				this:SetID(id - 12);
			end
		end
		return;
	end
	if ( event == "ACTIONBAR_SLOT_CHANGED" ) then
		if ( arg1 == -1 or arg1 == Nurfed_ActionButton_GetID(this) ) then
			Nurfed_ActionButton_Update();
		end
		return;
	end
	if ( event == "PLAYER_AURAS_CHANGED") then
		Nurfed_ActionButton_Update();
		Nurfed_ActionButton_UpdateState();
		return;
	end
	if ( event == "ACTIONBAR_SHOWGRID" ) then
		Nurfed_ActionButton_ShowGrid();
		return;
	end
	if ( event == "ACTIONBAR_HIDEGRID" ) then
		Nurfed_ActionButton_HideGrid();
		return;
	end
	if ( event == "UPDATE_BINDINGS" ) then
		Nurfed_ActionButton_UpdateHotkeys();
	end

	-- All event handlers below this line MUST only be valid when the button is visible
	if ( not this:IsVisible() ) then
		return;
	end

	if ( event == "PLAYER_TARGET_CHANGED" or event == "PLAYER_AURAS_CHANGED" ) then
		Nurfed_ActionButton_UpdateUsable();
		return;
	end
	if ( event == "UNIT_AURASTATE" ) then
		if ( arg1 == "player" or arg1 == "target" ) then
			Nurfed_ActionButton_UpdateUsable();
		end
		return;
	end
	if ( event == "UNIT_INVENTORY_CHANGED" ) then
		if ( arg1 == "player" ) then
			Nurfed_ActionButton_Update();
		end
		return;
	end
	if ( event == "ACTIONBAR_UPDATE_STATE" ) then
		Nurfed_ActionButton_UpdateState();
		return;
	end
	if ( event == "ACTIONBAR_UPDATE_USABLE" or event == "UPDATE_INVENTORY_ALERTS" or event == "ACTIONBAR_UPDATE_COOLDOWN" ) then
		Nurfed_ActionButton_UpdateUsable();
		Nurfed_ActionButton_UpdateCooldown();
		return;
	end
	if ( event == "CRAFT_SHOW" or event == "CRAFT_CLOSE" or event == "TRADE_SKILL_SHOW" or event == "TRADE_SKILL_CLOSE" ) then
		Nurfed_ActionButton_UpdateState();
		return;
	end
	if ( arg1 == "player" and (event == "UNIT_HEALTH" or event == "UNIT_MANA" or event == "UNIT_RAGE" or event == "UNIT_FOCUS" or event == "UNIT_ENERGY") ) then
		Nurfed_ActionButton_UpdateUsable();
		return;
	end
	if ( event == "PLAYER_ENTER_COMBAT" ) then
		--IN_ATTACK_MODE = 1;
		if ( IsAttackAction(Nurfed_ActionButton_GetID(this)) ) then
			Nurfed_ActionButton_StartFlash();
		end
		return;
	end
	if ( event == "PLAYER_LEAVE_COMBAT" ) then
		--IN_ATTACK_MODE = 0;
		if ( IsAttackAction(Nurfed_ActionButton_GetID(this)) ) then
			Nurfed_ActionButton_StopFlash();
		end
		return;
	end
	if ( event == "PLAYER_COMBO_POINTS" ) then
		Nurfed_ActionButton_UpdateUsable();
		return;
	end
	if ( event == "START_AUTOREPEAT_SPELL" ) then
		--IN_AUTOREPEAT_MODE = 1;
		if ( IsAutoRepeatAction(Nurfed_ActionButton_GetID(this)) ) then
			Nurfed_ActionButton_StartFlash();
		end
		return;
	end
	if ( event == "STOP_AUTOREPEAT_SPELL" ) then
		--IN_AUTOREPEAT_MODE = nil;
		if ( Nurfed_ActionButton_IsFlashing() and not IsAttackAction(Nurfed_ActionButton_GetID(this)) ) then
			Nurfed_ActionButton_StopFlash();
		end
		return;
	end
end

function Nurfed_ActionButton_UpdateHotkeys()
	local name = this:GetName();
	local hotkey = getglobal(name.."HotKey");
	local s, e, id = string.find(name, "^Nurfed_ActionButton(%d+)$");
	local action = "NURFEDACTIONBUTTON"..id;
	local text = GetBindingText(GetBindingKey(action), "KEY_");
	
	text = string.gsub(text, "CTRL%-", "C-");
	text = string.gsub(text, "ALT%-", "A-");
	text = string.gsub(text, "SHIFT%-", "S-");
	text = string.gsub(text, "Num Pad", "NP");
	text = string.gsub(text, "Backspace", "Bksp");
	text = string.gsub(text, "Spacebar", "Space");
	text = string.gsub(text, "Page", "Pg");
	text = string.gsub(text, "Down", "Dn");
	text = string.gsub(text, "Arrow", "");
	text = string.gsub(text, "Insert", "Ins");
	text = string.gsub(text, "Delete", "Del");
	
	hotkey:SetText(text);
end

function Nurfed_ActionButton_Update()
	-- Determine whether or not the button should be flashing or not since the button may have missed the enter combat event
	--local buttonID = Nurfed_ActionButton_GetID(this);
	--[[
	if ( IsAttackAction(buttonID) and IsCurrentAction(buttonID) ) then
		IN_ATTACK_MODE = 1;
	else
		IN_ATTACK_MODE = nil;
	end
	IN_AUTOREPEAT_MODE = IsAutoRepeatAction(buttonID);
	]]
	
	local icon = getglobal(this:GetName().."Icon");
	local buttonCooldown = getglobal(this:GetName().."Cooldown");
	local texture = GetActionTexture(Nurfed_ActionButton_GetID(this));
	if ( texture ) then
		icon:SetTexture(texture);
		icon:Show();
		this.rangeTimer = -1;
		this:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
	else
		icon:Hide();
		buttonCooldown:Hide();
		this.rangeTimer = nil;
		this:SetNormalTexture("Interface\\Buttons\\UI-Quickslot");
		getglobal(this:GetName().."HotKey"):SetVertexColor(0.6, 0.6, 0.6);
	end
	Nurfed_ActionButton_UpdateCount();
	if ( HasAction(Nurfed_ActionButton_GetID(this)) ) then
		if( not this.hidden ) then
			this:Show();
		else
			this:Hide();
		end
		Nurfed_ActionButton_UpdateUsable();
		Nurfed_ActionButton_UpdateCooldown();
	else
		local showgrid = this.showgrid;
		if( this.hidden ) then
			showgrid = showgrid - 1;
		end
		if ( showgrid == 0 ) then
			this:Hide();
		else
			buttonCooldown:Hide();
		end
	end
	--[[
	if ( IN_ATTACK_MODE or IN_AUTOREPEAT_MODE ) then
		Nurfed_ActionButton_StartFlash();
	else
		Nurfed_ActionButton_StopFlash();
	end
	]]
	if ( GameTooltip:IsOwned(this) ) then
		Nurfed_ActionButton_SetTooltip();
	else
		this.updateTooltip = nil;
	end

	-- Update Macro Text
	local macroName = getglobal(this:GetName().."Name");
	macroName:SetText(GetActionText(Nurfed_ActionButton_GetID(this)));
end

function Nurfed_ActionButton_GetID(button)
	if ( button == nil ) then
		message("nil button passed into Nurfed_ActionButton_GetID(), contact Jeff");
		return 0;
	end
	return (button:GetID())
end

function Nurfed_ActionButton_UpdateState()
	if ( IsCurrentAction(Nurfed_ActionButton_GetID(this)) or IsAutoRepeatAction(Nurfed_ActionButton_GetID(this)) ) then
		this:SetChecked(1);
	else
		this:SetChecked(0);
	end
end

function Nurfed_ActionButton_OnDragStart()
	PickupAction(Nurfed_ActionButton_GetID(this));
	Nurfed_ActionButton_UpdateState();
end

function Nurfed_ActionButton_SetTooltip()
	if( this:GetCenter() < GetScreenWidth() / 2 ) then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
	else
		GameTooltip:SetOwner(this, "ANCHOR_LEFT");
	end

	if ( GameTooltip:SetAction(Nurfed_ActionButton_GetID(this)) ) then
		this.updateTooltip = TOOLTIP_UPDATE_TIME;
	else
		this.updateTooltip = nil;
	end
end

function Nurfed_ActionButton_OnUpdate(elapsed)
	if ( Nurfed_ActionButton_IsFlashing() ) then
		this.flashtime = this.flashtime - elapsed;
		if ( this.flashtime <= 0 ) then
			local overtime = -this.flashtime;
			if ( overtime >= ATTACK_BUTTON_FLASH_TIME ) then
				overtime = 0;
			end
			this.flashtime = ATTACK_BUTTON_FLASH_TIME - overtime;

			local flashTexture = getglobal(this:GetName().."Flash");
			if ( flashTexture:IsVisible() ) then
				flashTexture:Hide();
			else
				flashTexture:Show();
			end
		end
	end
--[[
	-- Handle range indicator
	if ( this.rangeTimer ) then
		this.rangeTimer = this.rangeTimer - elapsed;

		if ( this.rangeTimer <= 0 ) then
			Nurfed_ActionButton_UpdateUsable();
			this.rangeTimer = TOOLTIP_UPDATE_TIME;
		else
			this.rangeTimer = this.rangeTimer - elapsed;
		end
	end
]]
	-- Handle range indicator
	if ( this.rangeTimer ) then
		this.rangeTimer = this.rangeTimer - elapsed;

		if ( this.rangeTimer <= 0 ) then
			local count = getglobal(this:GetName().."HotKey");
			if ( IsActionInRange( Nurfed_ActionButton_GetID(this)) == 0 ) then
				count:SetVertexColor(1.0, 0.1, 0.1);
			else
				count:SetVertexColor(0.6, 0.6, 0.6);
			end
			this.rangeTimer = TOOLTIP_UPDATE_TIME;
		end
	end

	if ( not this.updateTooltip ) then
		return;
	end

	this.updateTooltip = this.updateTooltip - elapsed;
	if ( this.updateTooltip > 0 ) then
		return;
	end

	if ( GameTooltip:IsOwned(this) ) then
		Nurfed_ActionButton_SetTooltip();
	else
		this.updateTooltip = nil;
	end
end

function Nurfed_ActionButton_ShowGrid()
	this.showgrid = this.showgrid+1;
	getglobal(this:GetName().."NormalTexture"):SetVertexColor(1.0, 1.0, 1.0);
	if( not this.hidden ) then
		this:Show();
	end
end

function Nurfed_ActionButton_HideGrid()
	this.showgrid = this.showgrid-1;
	local showgrid = this.showgrid;
	if( Nurfed_ActionBars and Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShowGrid ~= 1 ) then
		showgrid = showgrid - 1;
	end
	if ( showgrid == 0 and not HasAction(Nurfed_ActionButton_GetID(this)) ) then
		this:Hide();
	end
end

function Nurfed_ActionButton_UpdateUsable()
	local icon = getglobal(this:GetName().."Icon");
	local normalTexture = getglobal(this:GetName().."NormalTexture");
	local isUsable, notEnoughMana = IsUsableAction(Nurfed_ActionButton_GetID(this));
	if ( isUsable ) then
		icon:SetVertexColor(1.0, 1.0, 1.0);
		normalTexture:SetVertexColor(1.0, 1.0, 1.0);
	elseif ( notEnoughMana ) then
		icon:SetVertexColor(0.5, 0.5, 1.0);
		normalTexture:SetVertexColor(0.5, 0.5, 1.0);
	else
		icon:SetVertexColor(0.4, 0.4, 0.4);
		normalTexture:SetVertexColor(1.0, 1.0, 1.0);
	end

	--[[
	local count = getglobal(this:GetName().."HotKey");
	local text = count:GetText();
	local inRange = IsActionInRange(Nurfed_ActionButton_GetID(this));
	if( inRange == 0 ) then
		count:SetVertexColor(1.0, 0.1, 0.1);
	else
		count:SetVertexColor(0.6, 0.6, 0.6);
	end
	if ( isUsable ) then
		if ( (not text or text == "" or not count:IsVisible()) and inRange == 0 ) then
			icon:SetVertexColor(1.0, 0.1, 0.1);
			normalTexture:SetVertexColor(1.0, 0.1, 0.1);
		else
			icon:SetVertexColor(1.0, 1.0, 1.0);
			normalTexture:SetVertexColor(1.0, 1.0, 1.0);
		end
	elseif ( notEnoughMana ) then
		icon:SetVertexColor(0.5, 0.5, 1.0);
		normalTexture:SetVertexColor(0.5, 0.5, 1.0);
	else
		icon:SetVertexColor(0.4, 0.4, 0.4);
		normalTexture:SetVertexColor(1.0, 1.0, 1.0);
	end
	]]
end

function Nurfed_ActionButton_UpdateCooldown()
	local cooldown = getglobal(this:GetName().."Cooldown");
	local start, duration, enable = GetActionCooldown(Nurfed_ActionButton_GetID(this));
	CooldownFrame_SetTimer(cooldown, start, duration, enable);
end

function Nurfed_ActionButton_StartFlash()
	this.flashing = 1;
	this.flashtime = 0;
	Nurfed_ActionButton_UpdateState();
end

function Nurfed_ActionButton_StopFlash()
	this.flashing = 0;
	getglobal(this:GetName().."Flash"):Hide();
	Nurfed_ActionButton_UpdateState();
end

function Nurfed_ActionButton_IsFlashing()
	if ( this.flashing == 1 ) then
		return 1;
	else
		return nil;
	end
end

function Nurfed_ActionButton_UpdateCount()
	local text = getglobal(this:GetName().."Count");
	if ( IsConsumableAction(Nurfed_ActionButton_GetID(this)) ) then
		text:SetText(GetActionCount(Nurfed_ActionButton_GetID(this)));
	else
		text:SetText("");
	end
	--[[
	local text = getglobal(this:GetName().."Count");
	local count = GetActionCount(Nurfed_ActionButton_GetID(this));
	if ( count > 1 ) then
		text:SetText(count);
	else
		text:SetText("");
	end
	]]
end

function Nurfed_ActionButtonDown(id)
	local button = getglobal("Nurfed_ActionButton"..id);
	if ( button:GetButtonState() == "NORMAL" ) then
		button:SetButtonState("PUSHED");
	end
end

function Nurfed_ActionButtonUp(id)
	local button = getglobal("Nurfed_ActionButton"..id);
	if ( button:GetButtonState() == "PUSHED" ) then
		button:SetButtonState("NORMAL");
		-- Used to save a macro
		if ( MacroFrame_SaveMacro ) then
			MacroFrame_SaveMacro();
		end
		UseAction(Nurfed_ActionButton_GetID(button), 0);
		if ( IsCurrentAction(Nurfed_ActionButton_GetID(button)) ) then
			button:SetChecked(1);
		else
			button:SetChecked(0);
		end
	end
end

local originalShowBonusActionBar = ShowBonusActionBar;
function ShowBonusActionBar()
	originalShowBonusActionBar();
	Nurfed_ActionBar.Hide = 1;
	BonusActionButton1:ClearAllPoints();
	BonusActionButton1:SetPoint("TOPLEFT", "ActionButton1", "TOPLEFT", 0, 0);
	for i=1, NUM_ACTIONBAR_BUTTONS, 1 do
		getglobal("ActionButton"..i):Hide();
	end
end

local originalHideBonusActionBar = HideBonusActionBar;
function HideBonusActionBar()
	originalHideBonusActionBar();
	Nurfed_ActionBar.Hide = 0;
	for i=1, NUM_ACTIONBAR_BUTTONS, 1 do
		getglobal("ActionButton"..i):Show();
	end
end

local originalShapeshiftBar_UpdatePosition = ShapeshiftBar_UpdatePosition;
function ShapeshiftBar_UpdatePosition()
	if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ShapeshiftBar == 1) then
		ShapeshiftBarFrame:Show();
	else
		ShapeshiftBarFrame:Hide();
	end
end

local originalShowPetActionBar = ShowPetActionBar;
function ShowPetActionBar()
	if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].PetActionBar == 1) then
		originalShowPetActionBar();
	else
		HidePetActionBar();
	end
end

local function Nurfed_ActionBar_MouseIsOver(frame)
--[[
	local x, y = GetCursorPosition();
	
	if( not frame ) then
		return nil;
	end
	
	x = x / frame:GetScale();
	y = y / frame:GetScale();

	local left = frame:GetLeft();
	local right = frame:GetRight();
	local top = frame:GetTop();
	local bottom = frame:GetBottom();
	
	if( not left or not right or not top or not bottom ) then
		return nil;
	end
	
	if( (x > left and x < right) and (y > bottom and y < top) ) then
		return 1;
	else
		return nil;
	end
]]
	if (MouseIsOver(frame)) then
		return true;
	else
		return nil;
	end
end

local function Nurfed_ActionBar_ShowWidgets(framename)
	local frame = getglobal(framename);
	local framebg = getglobal(framename.."Backdrop");
	frame:EnableMouse(1);
	framebg:Show();
end

local function Nurfed_ActionBar_HideWidgets()
	Nurfed_ActionBar:EnableMouse(nil);
	Nurfed_ActionBarBackdrop:Hide();
	Nurfed_ActionBar1:EnableMouse(nil);
	Nurfed_ActionBar1Backdrop:Hide();
	Nurfed_ActionBar2:EnableMouse(nil);
	Nurfed_ActionBar2Backdrop:Hide();
	Nurfed_ActionBar3:EnableMouse(nil);
	Nurfed_ActionBar3Backdrop:Hide();
	Nurfed_ActionBar4:EnableMouse(nil);
	Nurfed_ActionBar4Backdrop:Hide();
	Nurfed_Bags:EnableMouse(nil);
	Nurfed_BagsBackdrop:Hide();
	Nurfed_MicroButtons:EnableMouse(nil);
	Nurfed_MicroButtonsBackdrop:Hide();
	Nurfed_ShapeshiftBar:EnableMouse(nil);
	Nurfed_ShapeshiftBarBackdrop:Hide();
	Nurfed_PetActionBar:EnableMouse(nil);
	Nurfed_PetActionBarBackdrop:Hide();
	Nurfed_CastingBar:EnableMouse(nil);
	Nurfed_CastingBarBackdrop:Hide();
	Nurfed_BGScore:EnableMouse(nil);
	Nurfed_BGScore:Hide();
end

local function ActionBar_HandleWidgets()
--[[
	local actionbarslocked;
	if (NURFED_GENERAL == 1) then
		actionbarslocked = NURFED_LOCKALL;
	else
		actionbarslocked = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].Locked;
	end
]]
	if( NURFED_LOCKALL ~= 1) then
		if( (Nurfed_ActionBar_MouseIsOver(Nurfed_ActionBar) or Nurfed_ActionBar.BeingDragged) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ActionBar");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_ActionBar1) or Nurfed_ActionBar1.BeingDragged) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar1 == 1) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached < 1) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ActionBar1");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_ActionBar2) or Nurfed_ActionBar2.BeingDragged) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar2 == 1) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached < 2) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ActionBar2");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_ActionBar3) or Nurfed_ActionBar3.BeingDragged) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar3 == 1) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached < 3) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ActionBar3");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_ActionBar4) or Nurfed_ActionBar4.BeingDragged) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].ActionBar4 == 1) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].MainAttached < 4) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ActionBar4");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_Bags) or Nurfed_Bags.BeingDragged) and (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags ~= 1 or ContainerFrame1.bagsShown > 0) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_Bags");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_MicroButtons) or Nurfed_MicroButtons.BeingDragged) and Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideMicro ~= 1 ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_MicroButtons");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_ShapeshiftBar) and ShapeshiftButton1:IsVisible() ) or Nurfed_ShapeshiftBar.BeingDragged ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_ShapeshiftBar");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_PetActionBar) and PetActionButton1:IsVisible() ) or Nurfed_PetActionBar.BeingDragged ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_PetActionBar");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_CastingBar) or Nurfed_CastingBar.BeingDragged) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_CastingBar");
		elseif( (Nurfed_ActionBar_MouseIsOver(Nurfed_BGScore) or Nurfed_BGScore.BeingDragged) ) then
			Nurfed_ActionBar_ShowWidgets("Nurfed_BGScore");
		else
			Nurfed_ActionBar_HideWidgets();
		end
	else
		Nurfed_ActionBar_HideWidgets();
	end
end

local function Nurfed_UpdateBags()
	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");
		if (Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].HideBags == 1 and ContainerFrame1.bagsShown == 0) then
			button:Hide();
		else
			button:Show();
		end
	end
end

local function Nurfed_CheckBonusAction()
	if (Nurfed_ActionBar.Hide == 1 and CURRENT_ACTIONBAR_PAGE == 1) then
		for i=1, NUM_ACTIONBAR_BUTTONS, 1 do
			getglobal("ActionButton"..i):Hide();
		end
	end
end

function Nurfed_ActionBars_OnUpdate(elapsed)
	lUpdateTimer = lUpdateTimer + elapsed;
	lBagUpdateTime = lBagUpdateTime + elapsed;
	if( lUpdateTimer >= ACTIONBAR_UPDATE_TIME ) then
		lUpdateTimer = 0;
		ActionBar_HandleWidgets();
		Nurfed_CheckBonusAction();
	end

	if (lBagUpdateTime > 2) then
		lBagUpdateTime = 0;
		Nurfed_UpdateBags();
	end
end

function Nurfed_ActionBar_OnDragStart(frame)
--[[
	local actionbarslocked;
	if (NURFED_GENERAL == 1) then
		actionbarslocked = NURFED_LOCKALL;
	else
		actionbarslocked = Nurfed_ActionBarsConfig[Nurfed_ActionBarsPlayer].Locked;
	end
]]
	if( NURFED_LOCKALL ~= 1) then
		frame.BeingDragged = 1;
		ActionBar_HandleWidgets();
		frame:StartMoving();
	end
end

function Nurfed_ActionBar_OnDragStop(frame)
	frame:StopMovingOrSizing();
	frame.BeingDragged = nil;
	ActionBar_HandleWidgets();
end

function ActionButton_SetTooltip()
	if ( GetCVar("UberTooltips") == "1" ) then
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
	else
		if ( this:GetParent() == MultiBarBottomRight or this:GetParent() == MultiBarRight or this:GetParent() == MultiBarLeft ) then
			GameTooltip:SetOwner(this, "ANCHOR_LEFT");
		else
			GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
		end
		
	end
	
	if ( GameTooltip:SetAction(ActionButton_GetPagedID(this)) ) then
		this.updateTooltip = TOOLTIP_UPDATE_TIME;
	else
		this.updateTooltip = nil;
	end
end
