function WindBag_OnLoad()
	local index = WindBag_getIndex(this:GetName());
	ContainerFrame = getglobal("ContainerFrame"..index);
	this:SetAllPoints(ContainerFrame);
	this:SetPoint("TOPLEFT", ContainerFrame, "TOPLEFT", 2, 1);
	this:SetPoint("BOTTOMRIGHT", ContainerFrame, "BOTTOMRIGHT", 1, 0);
	this:SetBackdrop(WindCommon_ColorBorder);
	WindBag_setName(getglobal("ContainerFrame"..index.."Name"), ContainerFrame);
	getglobal("ContainerFrame"..index.."Name"):SetWidth(132);
	getglobal("ContainerFrame"..index.."Portrait"):Hide();
	WindCommon_setPoint(getglobal("ContainerFrame"..index.."BackgroundTop"), "BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", 1000, 0);
	this:SetFrameLevel(ContainerFrame:GetFrameLevel()-1);
end

function WindBag_getIndex(frame)
	return string.sub(frame, 8);
end

function WindBag_setName(frame, container)
	WindCommon_setPoint(frame, "TOPLEFT", container, "TOPLEFT", 16, -10);
	frame:SetJustifyH("LEFT");
	frame:SetTextColor(1.0, 0.8, 0.0);
end

function WindBank_OnLoad()
	BankCloseButton:SetParent(WindBank);
	BankFramePurchaseButton:SetParent(WindBank);
	BankFrameDetailMoneyFrame:SetParent(WindBank);
	BankFrameMoneyFrame:SetParent(WindBank);
	for i=1,28 do getglobal("BankFrameItem"..i):SetParent(WindBank); end
	for i=1,7 do getglobal("BankFrameBag"..i):SetParent(WindBank); end
	BankFramePurchaseButton:SetWidth(38);
	WindCommon_setPoint(BankCloseButton, "TOPRIGHT", WindBank, "TOPRIGHT", -4, -4);
	WindCommon_setPoint(BankFrameItem1, "TOPLEFT", WindBank, "TOPLEFT", 16, -33);
	WindCommon_setPoint(BankFrameBag1, "TOPLEFT", WindBank, "TOPLEFT", 16, -230);
	WindCommon_setPoint(BankFramePurchaseButton, "TOPRIGHT", WindBank, "TOPRIGHT", -13, -272);
	WindCommon_setPoint(BankFrameDetailMoneyFrame, "RIGHT", BankFramePurchaseButton, "LEFT", 0, 0);
	WindCommon_setPoint(BankFrameMoneyFrame, "BOTTOMLEFT", WindBank, "BOTTOMLEFT", 16, 16);
	BankFrame:SetAlpha(0.0);
	WindCommon_setSize(BankFrame, 1, 1);
	WindBank:RegisterEvent("BANKFRAME_OPENED");
	WindBank:RegisterEvent("BANKFRAME_CLOSED");
	WindBank:RegisterEvent("PLAYERBANKBAGSLOTS_CHANGED");
	WindBank:RegisterEvent("PLAYER_MONEY");
	WindBank:RegisterEvent("VARIABLES_LOADED");
	updateContainerFrameAnchors = WindBB_updateContainerFrameAnchors;
	WindBank:SetBackdrop(WindCommon_ColorBorder);
end

function WindBank_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		WindBB_initConfig();
		WindBB_setScale();
		WindBB_setColor();
	elseif ( event == "BANKFRAME_OPENED" ) then
		ShowUIPanel(this);
		WindBB_OpenAllBags();
		if (not this:IsVisible()) then CloseBankFrame(); end
		WindBank_UpdateBagSlotStatus();
	elseif ( event == "BANKFRAME_CLOSED" ) then
		HideUIPanel(this);
		CloseAllBags();
	elseif ( event == "PLAYER_MONEY" or event == "PLAYERBANKBAGSLOTS_CHANGED" ) then
		WindBank_UpdateBagSlotStatus();
	end
end

function WindBank_UpdateBagSlotStatus()
	local numSlots,full = GetNumBankSlots();
	if( full ) then
		BankFramePurchaseButton:Hide();
		BankFrameDetailMoneyFrame:Hide();
	else
		BankFramePurchaseButton:Show();
		BankFrameDetailMoneyFrame:Show();
	end
	UpdateBagSlotStatus();
end

function WindBB_initConfig()
	if (not WindBBConfigAll) then WindBBConfigAll = WindBBDefault; end

	if (IsAddOnLoaded("WindConfig")) then 
		WindBBConfig_OnShow();
		WindBBConfigBody_setConfigColor(color);
	end
end

function WindBB_setScale()
	WindBank:SetScale(WindBBConfigAll[3]);
	for i=1, 13 do getglobal("ContainerFrame"..i):SetScale(WindBBConfigAll[3]); end
end

function WindBB_setColor()
	for i=1, 13 do
		getglobal("WindBag"..i):SetBackdrop(WindCommon_ColorBorder);
		local texture = getglobal("ContainerFrame"..i.."BackgroundTop"):GetTexture();
		if (string.find(texture,"-Keyring")) then
			getglobal("WindBag"..i):SetBackdropBorderColor(WindBBConfigAll[4][2][1].r, WindBBConfigAll[4][2][1].g, WindBBConfigAll[4][2][1].b, WindBBConfigAll[4][2][1].a);
			getglobal("WindBag"..i):SetBackdropColor(WindBBConfigAll[4][2][2].r, WindBBConfigAll[4][2][2].g, WindBBConfigAll[4][2][2].b, WindBBConfigAll[4][2][2].a);
		elseif (string.find(texture,"-Bank")) then
			getglobal("WindBag"..i):SetBackdropBorderColor(WindBBConfigAll[4][3][1].r, WindBBConfigAll[4][3][1].g, WindBBConfigAll[4][3][1].b, WindBBConfigAll[4][3][1].a);
			getglobal("WindBag"..i):SetBackdropColor(WindBBConfigAll[4][3][2].r, WindBBConfigAll[4][3][2].g, WindBBConfigAll[4][3][2].b, WindBBConfigAll[4][3][2].a);
		else
			getglobal("WindBag"..i):SetBackdropBorderColor(WindBBConfigAll[4][1][1].r, WindBBConfigAll[4][1][1].g, WindBBConfigAll[4][1][1].b, WindBBConfigAll[4][1][1].a);
			getglobal("WindBag"..i):SetBackdropColor(WindBBConfigAll[4][1][2].r, WindBBConfigAll[4][1][2].g, WindBBConfigAll[4][1][2].b, WindBBConfigAll[4][1][2].a);
		end
	end
	WindBank:SetBackdropBorderColor(WindBBConfigAll[4][3][1].r, WindBBConfigAll[4][3][1].g, WindBBConfigAll[4][3][1].b, WindBBConfigAll[4][3][1].a);
	WindBank:SetBackdropColor(WindBBConfigAll[4][3][2].r, WindBBConfigAll[4][3][2].g, WindBBConfigAll[4][3][2].b, WindBBConfigAll[4][3][2].a);
end

function WindBB_resetColor(index)
	WindBBConfigAll[4][index] = WindBBDefault[4][index];
	WindBB_setColor();
end

function WindBB_updateContainerFrameAnchors()
	local frame, xOffset, yOffset, screenHeight, freeScreenHeight, leftMostPoint, column;
	local screenWidth = GetScreenWidth();
	local containerScale = WindBBConfigAll[3];
	local leftLimit = 0;
	if ( BankFrame:IsShown() ) then leftLimit = BankFrame:GetRight() - 25; end
	while ( containerScale > WindBBConfigAll[3]*0.75 ) do
		screenHeight = GetScreenHeight() / containerScale;
		xOffset = WindBBConfigAll[1] / containerScale; 
		yOffset = WindBBConfigAll[2] / containerScale; 
		freeScreenHeight = screenHeight - yOffset;
		leftMostPoint = screenWidth - xOffset;
		column = 1;
		local frameHeight;
		for index, frameName in ipairs(ContainerFrame1.bags) do
			frameHeight = getglobal(frameName):GetHeight();
			if ( freeScreenHeight < frameHeight ) then
				column = column + 1;
				leftMostPoint = screenWidth - ( column * CONTAINER_WIDTH * containerScale ) - xOffset;
				freeScreenHeight = screenHeight - yOffset;
			end
			freeScreenHeight = freeScreenHeight - frameHeight - VISIBLE_CONTAINER_SPACING;
		end
		if ( leftMostPoint < leftLimit ) then
			containerScale = containerScale - 0.01;
		else
			break;
		end
	end
	
	if ( containerScale < WindBBConfigAll[3]*0.75 ) then
		containerScale = WindBBConfigAll[3]*0.75;
	end
	
	screenHeight = GetScreenHeight() / containerScale;
	xOffset = WindBBConfigAll[1] / containerScale;
	yOffset = WindBBConfigAll[2] / containerScale;
	freeScreenHeight = screenHeight - yOffset;
	column = 0;
	for index, frameName in ipairs(ContainerFrame1.bags) do
		frame = getglobal(frameName);
		frame:SetScale(containerScale);
		if ( index == 1 ) then
			frame:SetPoint("BOTTOMRIGHT", frame:GetParent(), "BOTTOMRIGHT", -xOffset, yOffset );
		elseif ( freeScreenHeight < frame:GetHeight() ) then
			column = column + 1;
			freeScreenHeight = screenHeight - yOffset;
			frame:SetPoint("BOTTOMRIGHT", frame:GetParent(), "BOTTOMRIGHT", -(column * CONTAINER_WIDTH) - xOffset, yOffset );
		else
			frame:SetPoint("BOTTOMRIGHT", ContainerFrame1.bags[index - 1], "TOPRIGHT", 0, CONTAINER_SPACING);	
		end
		freeScreenHeight = freeScreenHeight - frame:GetHeight() - VISIBLE_CONTAINER_SPACING;
	end
end

function ToggleBag(id)
	if ( IsOptionFrameOpen() ) then	return; end
	local size = GetContainerNumSlots(id);
	if ( size > 0 or id == KEYRING_CONTAINER ) then
		local containerShowing;
		for i=1, NUM_CONTAINER_FRAMES, 1 do
			local frame = getglobal("ContainerFrame"..i);
			if ( frame:IsShown() and frame:GetID() == id ) then containerShowing = i;
			frame:Hide();
			end
		end
		if ( not containerShowing ) then ContainerFrame_GenerateFrame(ContainerFrame_GetOpenFrame(), size, id);
		WindBB_setColor();
		end
	end
end

function OpenBag(id)
	if ( not CanOpenPanels() ) then
		if ( UnitIsDead("player") ) then NotWhileDeadError(); end
		return;
	end
	local size = GetContainerNumSlots(id);
	if ( size > 0 ) then
		local containerShowing;
		for i=1, NUM_CONTAINER_FRAMES, 1 do
			local frame = getglobal("ContainerFrame"..i);
			if ( frame:IsShown() and frame:GetID() == id ) then containerShowing = i; end
		end
		if ( not containerShowing ) then ContainerFrame_GenerateFrame(ContainerFrame_GetOpenFrame(), size, id);
		WindBB_setColor();
		end
	end
end

function ToggleKeyRing()
	if ( IsOptionFrameOpen() ) then return;	end
	local shownContainerID = IsBagOpen(KEYRING_CONTAINER);
	if ( shownContainerID ) then getglobal("ContainerFrame"..shownContainerID):Hide();
	else ContainerFrame_GenerateFrame(ContainerFrame_GetOpenFrame(), GetKeyRingSize(), KEYRING_CONTAINER);
	WindBB_setColor();
	SetButtonPulse(KeyRingButton, 0, 1);
	end
end

function WindBB_OpenAllBags()
	CloseAllBags();
	OpenAllBags(1);
	for i=5, 11 do OpenBag(i); end;
	local shownContainerID = IsBagOpen(KEYRING_CONTAINER);
	if ( not shownContainerID ) then ContainerFrame_GenerateFrame(ContainerFrame_GetOpenFrame(), GetKeyRingSize(), KEYRING_CONTAINER);
	SetButtonPulse(KeyRingButton, 0, 1); end
end