OpenAllBags_InitLoaded = false;

function OpenAllBags_OnLoad()
	this:RegisterEvent("BANKFRAME_OPENED");
	this:RegisterEvent("BANKFRAME_CLOSED");
	this:RegisterEvent("MERCHANT_SHOW");
	this:RegisterEvent("MERCHANT_CLOSED");
	this:RegisterEvent("MAIL_SHOW");
	this:RegisterEvent("MAIL_CLOSED");

	OpenAllBags_InitLoaded = true;
end

function OpenAllBags_OnEvent()
	if (event == "BANKFRAME_OPENED" or event == "MAIL_SHOW") then
		if ( Cosmos_TurnOnOpenAllBags and Cosmos_TurnOnOpenAllBags == 1 ) then
			OpenAllBags_Open(OpenAllBags_GetAmmoSlot());
		end
	elseif (event == "MERCHANT_SHOW") then
		if ( Cosmos_TurnOnOpenAllBags and Cosmos_TurnOnOpenAllBags == 1 ) then
			OpenAllBags_Open(0);
		end
	end
	if (event == "BANKFRAME_CLOSED" or event == "MERCHANT_CLOSED" or event == "MAIL_CLOSED" ) then
		CloseAllBags();
	end
end

function OpenAllBags_Init()
	for i = 0, 4, 1 do
		bagName = GetBagName(i);
		
		if ( bagName ~= nil) then
			if ( strfind(bagName, "탄약주머니") or strfind(bagName, "화살통") or strfind(bagName, "탄띠") or strfind(bagName, "잎주머니") ) then
				return i;
			end
		end
	end
	
	return -1;
end

function OpenAllBags_Open(arg)
	for i = 0, 11, 1 do
		if ( i ~= arg ) then
			OpenBag(i);
		end
	end	
end

function OpenAllBags_GetAmmoSlot()
	if (OpenAllBags_AmmoSlot ~= nil) then
		return OpenAllBags_AmmoSlot;
	else
		OpenAllBags_AmmoSlot = OpenAllBags_Init();
		return OpenAllBags_AmmoSlot;
	end
end