--
--  AddOn: Atlas
--  Date: 3/13/2005
--  Author: Razark
--  Contact: loglow@gmail.com
--

--------------------------------------------------
--
--  Globals
--
--------------------------------------------------

ATLAS_VERSION = "v0.8.2";
ATLAS_TITLE = "Atlas";
ATLAS_SUBTITLE = "Interface Maps";
ATLAS_DESC = "Browse a collection of instance maps.";
ATLAS_VERS_TITLE = ATLAS_TITLE.." "..ATLAS_VERSION;

BINDING_HEADER_ATLAS_TITLE = ATLAS_TITLE;
BINDING_NAME_ATLAS_TOGGLE = "Toggle "..ATLAS_TITLE;
BINDING_NAME_ATLAS_CHOOSE_MAP = "Smart Map Toggle";

ATLAS_DROPDOWN_LIST = {
	{name = "Blackfathom Deeps",	file = "BlackfathomDeeps"},
	{name = "Blackrock Depths",	file = "BlackrockDepths"},
	{name = "Gnomeregan",		file = "Gnomeregan"},
	{name = "Maraudon",		file = "Maraudon"},
	{name = "Molten Core",		file = "MoltenCore"},
	{name = "Ragefire Chasm",	file = "RagefireChasm"},
	{name = "Razorfen Downs",	file = "RazorfenDowns"},
	{name = "Razorfen Kraul",	file = "RazorfenKraul"},
	{name = "Scarlet Monastery",	file = "ScarletMonastery"},
	{name = "The Deadmines",	file = "TheDeadmines"},
	{name = "Uldaman",		file = "Uldaman"},
	{name = "Wailing Caverns",	file = "WailingCaverns"},
	{name = "Zul'farrak",		file = "Zul'farrak"}
};

AtlasLocked = false;

--------------------------------------------------
--
--  Functions
--
--------------------------------------------------

function Atlas_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
	tinsert(UISpecialFrames, "AtlasFrame");
	UIPanelWindows["AtlasFrame"] = nil;
	AtlasFrame:RegisterForDrag("LeftButton");
	SLASH_ATLAS1 = "/atlas";
	SlashCmdList["ATLAS"] = function(msg)
		Atlas_SlashCommand(msg);
	end

	Atlas_Refresh();

	---------------------
	-- support for Cosmos
	---------------------
	if(Cosmos_RegisterButton) then
		Cosmos_RegisterButton(
			ATLAS_VERS_TITLE,
			ATLAS_SUBTITLE,
			ATLAS_DESC,
			"Interface\\Icons\\INV_Misc_Map_01",
			Atlas_Toggle);
	end
end

function Atlas_ToggleLock()
	if(AtlasLocked) then
		Atlas_Unlock();
	else
		Atlas_Lock();
	end
end

function Atlas_Lock()
	AtlasLockNorm:SetTexture("Interface\\AddOns\\Atlas\\Images\\UI-Lock-Locked-Up");
	AtlasLockPush:SetTexture("Interface\\AddOns\\Atlas\\Images\\UI-Lock-Locked-Down");
	AtlasLocked = true;
end

function Atlas_Unlock()
	AtlasLockNorm:SetTexture("Interface\\AddOns\\Atlas\\Images\\UI-Lock-Unlocked-Up");
	AtlasLockPush:SetTexture("Interface\\AddOns\\Atlas\\Images\\UI-Lock-Unlocked-Down");
	AtlasLocked = false;
end

function Atlas_StartMoving()
	if(not AtlasLocked) then
		AtlasFrame:StartMoving();
	end
end

function Atlas_OnEvent()
	if(event == "VARIABLES_LOADED") then

		if(AtlasLocked) then
			Atlas_Lock();
		else
			Atlas_Unlock();
		end

		-----------------------
		-- support for myAddOns
		-----------------------
 		if(myAddOnsList) then
			myAddOnsList.Atlas = {
				name = ATLAS_TITLE,
				description = ATLAS_DESC,
				version = ATLAS_VERSION,
				category = MYADDONS_CATEGORY_MAP,
				frame = "AtlasFrame"};
		end
	end
end

function Atlas_OnShow()
	local currentZone = GetRealZoneText();
	local currentMapID = UIDropDownMenu_GetSelectedID(AtlasFrameDropDown);
	if(currentZone ~= ATLAS_DROPDOWN_LIST[currentMapID].name) then
		for i = 1, getn(ATLAS_DROPDOWN_LIST), 1 do
			if(currentZone == ATLAS_DROPDOWN_LIST[i].name) then
				UIDropDownMenu_Initialize(AtlasFrameDropDown, AtlasFrameDropDown_Initialize);
				UIDropDownMenu_SetSelectedID(AtlasFrameDropDown, i);
				Atlas_Refresh();
			end
		end
	end
end

--[[ Why won't this close the World Map when bound to a key!?
function Atlas_ChooseMap()
	local useAtlas = false;
	local currentZone = GetRealZoneText();
	for i = 1, getn(ATLAS_DROPDOWN_LIST), 1 do
		if(currentZone == ATLAS_DROPDOWN_LIST[i].name) then
			useAtlas = true;
		end
	end
	if(useAtlas) then
		Atlas_Toggle();
	else
		ToggleWorldMap();
	end
end
]]--

function Atlas_SlashCommand()
	Atlas_Toggle();
end

function Atlas_Toggle()
	if(AtlasFrame:IsVisible()) then
		HideUIPanel(AtlasFrame);
	else
		ShowUIPanel(AtlasFrame);
	end
end

function AtlasFrameDropDown_Initialize()
	local info;
	for i = 1, getn(ATLAS_DROPDOWN_LIST), 1 do
		info = { };
		info.text = ATLAS_DROPDOWN_LIST[i].name;
		info.func = AtlasFrameDropDownButton_OnClick;
		UIDropDownMenu_AddButton(info);
	end
end

function AtlasFrameDropDown_OnLoad()
	UIDropDownMenu_Initialize(AtlasFrameDropDown, AtlasFrameDropDown_Initialize);
	UIDropDownMenu_SetSelectedID(AtlasFrameDropDown, 1);
	UIDropDownMenu_SetWidth(150);
end

function AtlasFrameDropDownButton_OnClick()
	local oldID = UIDropDownMenu_GetSelectedID(AtlasFrameDropDown);
	UIDropDownMenu_SetSelectedID(AtlasFrameDropDown, this:GetID());
	if(oldID ~= this:GetID()) then
		Atlas_Refresh();
	end
end

function Atlas_Refresh()
	local currentID = UIDropDownMenu_GetSelectedID(AtlasFrameDropDown);
	AtlasMap:ClearAllPoints();
	AtlasMap:SetWidth(512);
	AtlasMap:SetHeight(512);
	AtlasMap:SetPoint("TOPLEFT", "AtlasFrame", "TOPLEFT", 18, -72);
	AtlasMap:SetTexture("Interface\\AddOns\\Atlas\\Images\\"..ATLAS_DROPDOWN_LIST[currentID].file);
end
