LockBar_Cosmos_Registered = 0;
LockBar_IsLocked = 0;
LockBar_IsDisplayed = 1;

LockBar_Saved_PickupAction = nil;

-- hooked function
function LockBar_PickupAction(id)
	if ( LockBar_ShouldAllowPickupAction() ) then
		LockBar_Saved_PickupAction(id);
	end
end

function LockBar_ShouldAllowPickupAction()
	if ( ( LockBar_IsLocked ~= 1 ) or ( LockBar_IsDisplayed ~= 1 ) or ( IsShiftKeyDown() ) ) then
		return true;
	else
		return false;
	end
end

function LockBar_Setup_Hooks(toggle)
	if ( toggle ) then
		if ( ( PickupAction ~= LockBar_PickupAction ) and (LockBar_Saved_PickupAction == nil) ) then
			LockBar_Saved_PickupAction = PickupAction;
			PickupAction = LockBar_PickupAction;
		end
	else
		if ( PickupAction == LockBar_PickupAction) then
			PickupAction = LockBar_Saved_PickupAction;
			LockBar_Saved_PickupAction = nil;
		end
	end
end

function LockBar_OnClick()
	LockBar_SetLockedState(this:GetChecked());
end

function LockBar_OnEvent(event)
	if ( event == "VARIABLES_LOADED" ) then
		if ( LockBar_Cosmos_Registered ~= 1 ) then
			LockBar_SetDisplayState(LockBar_IsDisplayed);
			LockBar_SetLockedState(LockBar_IsLocked);
		end
	end
end

function LockBar_Cosmos_CheckBoxUpdate(name, value)
	if ( Cosmos_UpdateValue ) then
		Cosmos_UpdateValue(name, CSM_CHECKONOFF, value);
		if ( CosmosMaster_DrawData ) then
			CosmosMaster_DrawData();
		end
	end
end

function LockBar_SetLockedState(toggle)
	if ( toggle == 1 ) or ( toggle == true ) then
		LockBar_IsLocked = 1;
		ActionLockButton:SetChecked(1);
	else
		LockBar_IsLocked = 0;
		ActionLockButton:SetChecked(0);
	end
	LockBar_Cosmos_CheckBoxUpdate("COS_LOCKBAR_LOCK", LockBar_IsLocked);
end


function LockBar_SetDisplayState(toggle)
	if ( toggle == 1 ) or ( toggle == true ) then
		LockBar_Setup_Hooks(true);
		getglobal("ActionLockButton"):Show();
		LockBar_IsDisplayed = 1;
	else
		LockBar_Setup_Hooks(false);
		getglobal("ActionLockButton"):Hide();
		LockBar_IsDisplayed = 0;
	end
	LockBar_Cosmos_CheckBoxUpdate("COS_LOCKBAR_DISPLAY", LockBar_IsDisplayed);
end

function LockBar_OnLoad_Cosmos()
-- Start Added by Gryphen
	if (Cosmos_RegisterConfiguration) and ( Cosmos_UpdateValue ) then
		Cosmos_RegisterConfiguration(
			"COS_LOCKBAR",
			"SECTION",
			TEXT(LOCKBAR_OPTION_SEP),
			TEXT(LOCKBAR_OPTION_SEP_INFO)
		);
		Cosmos_RegisterConfiguration(
			"COS_LOCKBAR_SEP",
			"SEPARATOR",
			TEXT(LOCKBAR_OPTION_SEP),
			TEXT(LOCKBAR_OPTION_SEP_INFO)
		);
		Cosmos_RegisterConfiguration(
			"COS_LOCKBAR_DISPLAY",
			"CHECKBOX",
			TEXT(LOCKBAR_OPTION_DISPLAY),
			TEXT(LOCKBAR_OPTION_DISPLAY_INFO),
			LockBar_SetDisplayState,
			LockBar_IsDisplayed
		);
		Cosmos_RegisterConfiguration(
			"COS_LOCKBAR_LOCK",
			"CHECKBOX",
			TEXT(LOCKBAR_OPTION_LOCK),
			TEXT(LOCKBAR_OPTION_LOCK_INFO),
			LockBar_SetLockedState,
			LockBar_IsLocked
		);
		LockBar_Cosmos_Registered = 1;
	end
-- End Added by Gryphen
end

function LockBar_OnLoad()
	LockBar_OnLoad_Cosmos();
	this:RegisterEvent("VARIABLES_LOADED");
end

function LockBar_OnEnter()
	GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
	GameTooltip:SetText(TEXT(LOCKBAR_BUTTON_TOOLTIP), 1.0, 1.0, 1.0);
end

function LockBar_OnLeave()
	if ( GameTooltip:IsOwned(this) ) then
		GameTooltip:Hide();
	end
end

