--[[
	QuickLoot: easier, faster looting
		copyright 2004 by Telo
		modified 2013-2014

	- Automatically positions the most relevant part of the loot window under your cursor
]]

-- Configuration variable
QuickLoot_AutoHide = 1;	-- set to 1 if you want the window to auto-hide when there's no loot or
						-- set to nil if you don't want this behavior


local function QuickLoot_ItemUnderCursor()
	local index;
	local x, y = GetCursorPosition();

	--print("Cursor:", x, y)

	local lim = UIParent:GetWidth() - 120;
	if x < 30 then x = 30 end
	if x > lim then x = lim end

	lim = UIParent:GetHeight() - 70
	if y > lim then y = lim end
	if y < 200 then y = 200 end

	local scale = LootFrame:GetEffectiveScale();
	x = x / scale;
	y = y / scale;

	LootFrame:ClearAllPoints();

	for index = 1, LOOTFRAME_NUMBUTTONS, 1 do
		local button = getglobal("LootButton"..index);
		if( button:IsVisible() ) then
			x = x - 42;
			y = y + 56 + (40 * index);
			LootFrame:SetPoint("TOPLEFT", "UIParent", "BOTTOMLEFT", x, y);
			LootFrame:Raise()
			return;
		end
	end

	if( LootFrameDownButton:IsVisible() ) then
		-- If down arrow, position on it
		x = x - 158;
		y = y + 223;
	else
		if( QuickLoot_AutoHide and GetNumLootItems() == 0 ) then
			HideUIPanel(LootFrame);
			return
		end
		-- Otherwise, position on close box
		x = x - 173;
		y = y + 25;
	end
	
	LootFrame:SetPoint("TOPLEFT", "UIParent", "BOTTOMLEFT", x, y);
	LootFrame:Raise()
end


-- ONLOAD CODE

local f = CreateFrame("Frame") -- nil, UIParent)
f:RegisterEvent("LOOT_OPENED")
f:RegisterEvent("LOOT_SLOT_CLEARED")
f:SetScript("OnEvent", QuickLoot_ItemUnderCursor)

-- Don't treat the LootFrame as a UI panel -- не закрывать окно Loot по ESC (в 3.3.5a Loot мобов всё равно закрывается)
--UIPanelWindows["LootFrame"] = nil;

if DEFAULT_CHAT_FRAME then
	DEFAULT_CHAT_FRAME:AddMessage("QuickLoot AddOn v2014 loaded");
end
