--[[///////////////////////////////////////////////////////////////////////////

  Mod Name : Blink Open Bag(or AllInOneInventory) v1.91
  Author   : Blink
  Date     : 2005/01/16
  LastUpdate : 2007/03/27

/////////////////////////////////////////////////////////////////////////////]]

old_BOB_MerchantFrame_OnShow = nil;
old_BOB_MerchantFrame_OnHide = nil;
old_BOB_BankFrame_OnEvent = nil;
old_BOB_MailFrame_OnEvent = nil;
old_BOB_AuctionFrame_Show = nil;
old_BOB_TradeFrame_OnEvent = nil;

function BlinkOpenBag_OnLoad()
	old_BOB_MerchantFrame_OnShow = MerchantFrame_OnShow;
	old_BOB_MerchantFrame_OnHide = MerchantFrame_OnHide;
	old_BOB_BankFrame_OnEvent = BankFrame_OnEvent;
	old_BOB_MailFrame_OnEvent = MailFrame_OnEvent;

	MerchantFrame_OnShow = BOB_MerchantFrame_OnShow;
	MerchantFrame_OnHide = BOB_MerchantFrame_OnHide;
	BankFrame_OnEvent = BOB_BankFrame_OnEvent;
	MailFrame_OnEvent = BOB_MailFrame_OnEvent;
	TradeFrame_OnEvent = BOB_TradeFrame_OnEvent;

	-- Events for auction UI handling
	this:RegisterEvent("AUCTION_HOUSE_SHOW");
	this:RegisterEvent("AUCTION_HOUSE_CLOSED");
end

function BlinkOpenBag_OnEvent(event)
	-- Events for auction UI handling
	if ( event == "AUCTION_HOUSE_SHOW" ) then
		AuctionFrame_LoadUI();
		if ( AuctionFrame_Show ) then
			if ( not old_BOB_AuctionFrame_Show ) then
				BlinkOpenBag_OpenBagAll();
				old_BOB_AuctionFrame_Show = AuctionFrame_Show;
				AuctionFrame_Show = function ()
					if ( AuctionFrame:IsVisible() ) then
						AuctionFrameBrowse_Update();
						AuctionFrameBid_Update();
						AuctionFrameAuctions_Update();
					else
						ShowUIPanel(AuctionFrame);
						BlinkOpenBag_OpenBagAll();
						if ( not AuctionFrame:IsVisible() ) then
							CloseAuctionHouse();
						end
					end
				end
			end
		end
		return;
	elseif ( event == "AUCTION_HOUSE_CLOSED" ) then
		BlinkOpenBag_CloseBagAll();
		return;
	end
end

function BlinkOpenBag_OpenBagAll()
	if( OpenAllInOneInventoryFrame )then
		OpenAllInOneInventoryFrame();
	else
		OpenBackpack();
		OpenBag(1);
		OpenBag(2);
		OpenBag(3);
		OpenBag(4);
		if ( BankFrame:IsVisible() ) then
			OpenBag(5);
			OpenBag(6);
			OpenBag(7);
			OpenBag(8);
			OpenBag(9);
			OpenBag(10);
			OpenBag(11);
		end
		if ( IsOptionFrameOpen() ) then
			return;
		end
-- 열쇠고리는 열리지 않게 한다.. 은근히 귀찮더라구;;ㅋ
--		local shownContainerID = IsBagOpen(KEYRING_CONTAINER);
--		if ( not shownContainerID ) then
--			ContainerFrame_GenerateFrame(ContainerFrame_GetOpenFrame(), GetKeyRingSize(), KEYRING_CONTAINER);
--
--			-- Stop keyring button pulse
--			SetButtonPulse(KeyRingButton, 0, 1);
--		end
	end
end

function BlinkOpenBag_CloseBagAll()
	if( CloseAllInOneInventoryFrame )then
		CloseAllInOneInventoryFrame();
	else
		CloseAllBags();
		if ( IsOptionFrameOpen() ) then
			return;
		end
		local shownContainerID = IsBagOpen(KEYRING_CONTAINER);
		if ( shownContainerID ) then
			getglobal("ContainerFrame"..shownContainerID):Hide();
		end
	end
end

-- 상점창 ---------------------------------------------------------------------
function BOB_MerchantFrame_OnShow()
	BlinkOpenBag_OpenBagAll();
	-- Update repair all button status
	if ( MerchantRepairAllIcon ) then
		local repairAllCost, canRepair = GetRepairAllCost();
		if ( canRepair ) then
			SetDesaturation(MerchantRepairAllIcon, nil);
			MerchantRepairAllButton:Enable();
		else
			SetDesaturation(MerchantRepairAllIcon, 1);
			MerchantRepairAllButton:Disable();
		end	
	end
	PanelTemplates_SetTab(MerchantFrame, 1);
	MerchantFrame_Update();
end

function BOB_MerchantFrame_OnHide()
	CloseMerchant();
	BlinkOpenBag_CloseBagAll();
	ResetCursor();
end

-- 은행창 ---------------------------------------------------------------------
function BOB_BankFrame_OnEvent(event)
	if ( event == "BANKFRAME_OPENED" ) then
		BankFrameTitleText:SetText(UnitName("npc"));
		SetPortraitTexture(BankPortraitTexture,"npc");
		ShowUIPanel(this);

		BlinkOpenBag_OpenBagAll();

		if ( not this:IsVisible() ) then
			CloseBankFrame();
		end
		UpdateBagSlotStatus();
	elseif ( event == "BANKFRAME_CLOSED" ) then
		HideUIPanel(this);
		BlinkOpenBag_CloseBagAll();
	elseif ( event == "PLAYER_MONEY" or event == "PLAYERBANKBAGSLOTS_CHANGED" ) then
		UpdateBagSlotStatus();
	elseif ( event == "PLAYERBANKSLOTS_CHANGED" ) then
		old_BOB_BankFrame_OnEvent(event);
		UpdateBagSlotStatus();
	end
end

-- 우편함 ---------------------------------------------------------------------
function BOB_MailFrame_OnEvent()
	if ( event == "MAIL_SHOW" ) then
		ShowUIPanel(MailFrame);
		if ( not MailFrame:IsVisible() ) then
			CloseMail();
			return;
		end

		-- Update the roster so auto-completion works
		if ( IsInGuild() and GetNumGuildMembers() == 0 ) then
			GuildRoster();
		end

		BlinkOpenBag_OpenBagAll();
		SendMailFrame_Update();
		MailFrameTab_OnClick(1);
		CheckInbox();
	elseif ( event == "MAIL_INBOX_UPDATE" ) then
		InboxFrame_Update();
		OpenMail_Update();
	elseif ( event == "MAIL_SEND_INFO_UPDATE" ) then
		SendMailFrame_Update();
	elseif ( event == "MAIL_SEND_SUCCESS" ) then
		SendMailFrame_Reset();
		PlaySound("igAbiliityPageTurn");
		-- If open mail frame is open then switch the mail frame back to the inbox
		if ( SendMailFrame.sendMode == "reply" ) then
			MailFrameTab_OnClick(1);
		end
	elseif ( event == "MAIL_FAILED" ) then
		SendMailMailButton:Enable();
	elseif ( event == "MAIL_CLOSED" ) then
		HideUIPanel(MailFrame);
		BlinkOpenBag_CloseBagAll();
	elseif ( (event == "CLOSE_INBOX_ITEM") and (InboxFrame.openMailID == arg1) ) then
		HideUIPanel(OpenMailFrame);
	end
end

-- 경매창 ---------------------------------------------------------------------
function BOB_AuctionFrame_OnEvent()
	if ( event == "AUCTION_HOUSE_SHOW" ) then
		if ( this:IsVisible() ) then
			AuctionFrameBrowse_Update();
			AuctionFrameBid_Update();
			AuctionFrameAuctions_Update();
		else
			ShowUIPanel(this);
			BlinkOpenBag_OpenBagAll();
			if ( not this:IsVisible() ) then
				CloseAuctionHouse();
--				BlinkOpenBag_CloseBagAll();
			end
		end
	elseif ( event == "AUCTION_HOUSE_CLOSED" ) then
		HideUIPanel(this);
		BlinkOpenBag_CloseBagAll();
	end
end

-- 거래창 ---------------------------------------------------------------------
function BOB_TradeFrame_OnEvent()
	if ( event == "TRADE_SHOW" or event == "TRADE_UPDATE" ) then
		ShowUIPanel(this, 1);
		BlinkOpenBag_OpenBagAll();
		if ( not this:IsVisible() ) then
			CloseTrade();
			return;
		end
		TradeFrameTradeButton:Enable();
		TradeFrame_Update();
	elseif ( event == "TRADE_CLOSED" ) then
		HideUIPanel(this);
		BlinkOpenBag_CloseBagAll();
	elseif ( event == "TRADE_TARGET_ITEM_CHANGED" ) then
		TradeFrame_UpdateTargetItem(arg1);
	elseif ( event == "TRADE_PLAYER_ITEM_CHANGED" ) then
		TradeFrame_UpdatePlayerItem(arg1);
	elseif ( event == "TRADE_ACCEPT_UPDATE" ) then
		TradeFrame_SetAcceptState(arg1, arg2);
	end
end
