local _, core = ...;
core.Tab = {};
core.Config = {};
local Config = core.Config;
local UIConfig;
local UIListMissions;
local FirstShow = true;
local FirstShowAnnouce = true;
local currentTabDiffOpen = nil;
local currentChallOpen = nil;
local challengesDB;

print(core.L["StartText1"]);
print(core.L["StartText2"]);
print(core.L["StartText3"]);
print(core.L["StartText4"]);
print(core.L["StartText5"]);

local LoadChallengesDB = CreateFrame("Frame");
LoadChallengesDB:RegisterEvent("PLAYER_ENTERING_WORLD");
LoadChallengesDB:SetScript("OnEvent", function()
	core.DBchallenge.load();
	challengesDB = core.DBchallenge.data;
end)

local function checkIfHaveName(name, tableCheck)
	for i = 1, #tableCheck do
		if (name == tableCheck[i]) then
			return true;
		end
	end
	return false;
end

local function setStringChallengeDoneByNoDone(difficulty)
	local challengeDone = 0;
	local challengeMax = 0;
	if (challengesDB[difficulty] ~= nil) then
		for i = 1, (core.libs.tablelength(challengesDB[difficulty]) - 1) do
			local challengeTabDone = 0;
			local tabtoAvoid = {};
			local nbTab = 0;
			for z = 1, core.libs.tablelength(challengesDB[difficulty][i].tab) do
				if (challengesDB[difficulty][i].tab[z].name == core.L["Alliance"] or challengesDB[difficulty][i].tab[z].name == core.L["Horde"]) then
					local factionPlayer = UnitFactionGroup("player")
					if (factionPlayer ~= challengesDB[difficulty][i].tab[z].faction) then
						table.insert(tabtoAvoid, challengesDB[difficulty][i].tab[z].name);
					else
						nbTab = nbTab + 1;
					end
				else
					nbTab = nbTab + 1;
				end
			end
			for i2 = 1, core.libs.tablelength(challengesDB[difficulty][i].tab) do
				if (checkIfHaveName(challengesDB[difficulty][i].tab[i2].name, tabtoAvoid) == false) then
					local challengePageDone = 0;
					for i3 = 1, core.libs.tablelength(challengesDB[difficulty][i].tab[i2].page) do
						if (core.libs.ifDonePage(challengesDB[difficulty][i].tab[i2].page[i3]) == true) then
							challengePageDone = challengePageDone + 1;
						end
						if (challengePageDone == core.libs.tablelength(challengesDB[difficulty][i].tab[i2].page)) then
							challengeTabDone = challengeTabDone + 1;
						end
						if (challengeTabDone == nbTab) then
							challengeDone = challengeDone + 1;
						end
					end
				end
			end
			challengeMax = challengeMax + 1;
		end
	end
	if (challengeDone == 0) then
		return ("|cFFFF0000"..challengeDone.."/"..challengeMax.." "..core.L["accomplis"].."|r");
	elseif (challengeDone == challengeMax) then
		return ("|cFF00FF00"..challengeDone.."/"..challengeMax.." "..core.L["accomplis"].."|r");
	end
	return (challengeDone.."/"..challengeMax.." "..core.L["accomplis"]);
end

local function setStringProgressionCampaign()
	local nbDone = 0;
	if (Campaign1[#Campaign1].done == true) then
		nbDone = nbDone + 1;
	end
	if (Campaign2[#Campaign2].done == true) then
		nbDone = nbDone + 1;
	end
	if (Campaign3[#Campaign3].done == true) then
		nbDone = nbDone + 1;
	end
	if (nbDone == 0) then
		return (core.L["Campaigns"]..core.L["2points"].."|cFFFF0000"..nbDone.."/"..#core.DBcampaign.."|r");
	elseif (nbDone == #core.DBcampaign) then
		return (core.L["Campaigns"]..core.L["2points"].."|cFF00FF00"..nbDone.."/"..#core.DBcampaign.."|r");
	end
	return (core.L["Campaigns"]..core.L["2points"].."|cFFFFFFFF"..nbDone.."/"..#core.DBcampaign.."|r");
end

local function setStringProgressionHF()
	local challengeDone = 0;
	local challengeMax = 0;
	for idif = 1, core.libs.tablelength(challengesDB) do
		for i = 1, (core.libs.tablelength(challengesDB[idif]) - 1) do
			local challengeTabDone = 0;
			local tabtoAvoid = {};
			local nbTab = 0;
			for z = 1, core.libs.tablelength(challengesDB[idif][i].tab) do
				if (challengesDB[idif][i].tab[z].name == core.L["Alliance"] or challengesDB[idif][i].tab[z].name == core.L["Horde"]) then
					local factionPlayer = UnitFactionGroup("player")
					if (factionPlayer ~= challengesDB[idif][i].tab[z].faction) then
						table.insert(tabtoAvoid, challengesDB[idif][i].tab[z].name);
					else
						nbTab = nbTab + 1;
					end
				else
					nbTab = nbTab + 1;
				end
			end
			for i2 = 1, core.libs.tablelength(challengesDB[idif][i].tab) do
				if (checkIfHaveName(challengesDB[idif][i].tab[i2].name, tabtoAvoid) == false) then
					local challengePageDone = 0;
					for i3 = 1, core.libs.tablelength(challengesDB[idif][i].tab[i2].page) do
						if (core.libs.ifDonePage(challengesDB[idif][i].tab[i2].page[i3]) == true) then
							challengePageDone = challengePageDone + 1;
						end
						if (challengePageDone == core.libs.tablelength(challengesDB[idif][i].tab[i2].page)) then
							challengeTabDone = challengeTabDone + 1;
						end
						if (challengeTabDone == nbTab) then
							challengeDone = challengeDone + 1;
						end
					end
				end
			end
			challengeMax = challengeMax + 1;
		end
	end
	if (challengeDone == 0) then
		return (core.L["Challenges"]..core.L["2points"].."|cFFFF0000"..challengeDone.."/"..challengeMax.."|r");
	elseif (challengeDone == challengeMax) then
		return (core.L["Challenges"]..core.L["2points"].."|cFF00FF00"..challengeDone.."/"..challengeMax.."|r");
	end
	return (core.L["Challenges"]..core.L["2points"].."|cFFFFFFFF"..challengeDone.."/"..challengeMax.."|r");
end

local function setStringProgressionMounts()
	if (core.libs.getNbMountDone() == 0) then
		return (core.L["Mounts"]..core.L["2points"].."|cFFFF0000"..core.libs.getNbMountDone().."/"..core.libs.getNbMaxMount().."|r");
	elseif (core.libs.getNbMountDone() == core.libs.getNbMaxMount()) then
		return (core.L["Mounts"]..core.L["2points"].."|cFF00FF00"..core.libs.getNbMountDone().."/"..core.libs.getNbMaxMount().."|r");
	end
	return (core.L["Mounts"]..core.L["2points"].."|cFFFFFFFF"..core.libs.getNbMountDone().."/"..core.libs.getNbMaxMount().."|r");
end

local function setStringProgressionPets()
	if (core.libs.getNbPetDone() == 0) then
		return (core.L["Pets"]..core.L["2points"].."|cFFFF0000"..core.libs.getNbPetDone().."/"..core.libs.getNbMaxPet().."|r");
	elseif (core.libs.getNbPetDone() == core.libs.getNbMaxPet()) then
		return (core.L["Pets"]..core.L["2points"].."|cFF00FF00"..core.libs.getNbPetDone().."/"..core.libs.getNbMaxPet().."|r");
	end
	return (core.L["Pets"]..core.L["2points"].."|cFFFFFFFF"..core.libs.getNbPetDone().."/"..core.libs.getNbMaxPet().."|r");
end

local lootOnEnter = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT", -10, -10);
	GameTooltip:AddLine(core.L["Rewards"], 1, 1, 1);
	words = {}
    for w in string.gmatch(self.challForTooltip, "%d+") do
      table.insert(words, w)
    end
	challenge = challengesDB[tonumber(words[1])][tonumber(words[2])];
	GameTooltip:AddLine(core.libs.getTotalLootsChallenge(challenge));
	GameTooltip:Show();
end

local OnLeave = function(self, motion)
	GameTooltip:Hide()
end

local lootOnEnterIcon = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	GameTooltip:AddLine(core.L["Rewards"], 1, 1, 1);
	words = {}
    for w in string.gmatch(self.stepForTooltip, "%d+") do
      table.insert(words, w)
    end
	local challengeStep = challengesDB[tonumber(words[1])][tonumber(words[2])].tab[tonumber(words[3])].page[tonumber(words[4])].step[tonumber(words[5])];
	if (core.libs.ifDoneStep(challengeStep) == true) then
		GameTooltip:AddLine(core.libs.getPointsHFStep(challengeStep)..core.L["HFPoints"], 0, 1, 0);
	else
		GameTooltip:AddLine(core.libs.getPointsHFStep(challengeStep)..core.L["HFPoints"]);
	end
	for i = 1, core.libs.tablelength(challengeStep.task) do
		if (challengeStep.task[i]["type"] == "hf") then
			if (core.libs.ifDoneTask(challengeStep.task[i]) == true) then
				GameTooltip:AddLine(core.libs.getLootTask(challengeStep.task[i]), 0, 1, 0);
			else
				GameTooltip:AddLine(core.libs.getLootTask(challengeStep.task[i]));
			end
		end
	end
	GameTooltip:Show();
end
			
local lootOnEnterIconLock = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	GameTooltip:AddLine(core.L["Lock"], 0.5, 0.5, 0.5);
	GameTooltip:Show();
end

local lootOnEnterTask = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 5);
	words = {}
    for w in string.gmatch(self.taskForTooltip, "%d+") do
		table.insert(words, w)
    end
	local challengeTask = challengesDB[tonumber(words[1])][tonumber(words[2])].tab[tonumber(words[3])].page[tonumber(words[4])].step[tonumber(words[5])].task[tonumber(words[6])];
	if (challengeTask["type"] == "hf") then
		if (challengeTask["hf"][4] == true) then
			if (challengeTask["hf"][14] ~= "") then
				GameTooltip:AddLine(core.L["AcheveBy"].." "..challengeTask["hf"][14], 0, 1, 0);
			else
				GameTooltip:AddLine(core.L["Acheve"], 0, 1, 0);
			end
		else
			GameTooltip:AddLine(core.L["NoAcheve"], 0.5, 0.5, 0.5);
		end
		GameTooltip:AddLine(challengeTask["hf"][8], 1, 1, 1);
	elseif (challengeTask["type"] == "quest") then
		if (challengeTask["quest"].completed == true) then
			GameTooltip:AddLine(core.L["Acheve"], 0, 1, 0);
		else
			GameTooltip:AddLine(core.L["NoAcheve"], 0.5, 0.5, 0.5);
		end
		if (challengeTask["MA"] ~= nil) then
			GameTooltip:AddLine(challengeTask["quest"].desc);
		elseif (challengeTask["quest"].completed == true) then
			GameTooltip:AddLine(core.L["DescQuestComplete"], 1, 1, 1);
		else
			GameTooltip:AddLine(core.L["DescQuest"], 1, 1, 1);
		end
	end
	if (challengeTask["faction"] ~= nil and challengeTask["MA"] == nil) then
		GameTooltip:AddLine(core.libs.changeReputToText(challengeTask["faction"]), 1, 1, 1);
	end
	if (challengeTask["type"] == "hf" and GetAchievementNumCriteria(challengeTask["hf"][1]) ~= 0) then
		GameTooltip:AddLine(core.libs.printCriteriaHF(challengeTask["hf"][1]));
	end
	GameTooltip:Show();
end

local lootOnEnterIconHelp = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	GameTooltip:AddLine(core.L["Aide"]);
	words = {}
    for w in string.gmatch(self.stepForTooltip, "%d") do
      table.insert(words, w)
    end
	local challengeStep = challengesDB[tonumber(words[1])][tonumber(words[2])].tab[tonumber(words[3])].page[tonumber(words[4])].step[tonumber(words[5])];
	GameTooltip:AddLine(challengeStep.help, 1, 1, 1, true);
	GameTooltip:Show();
end

local function UpdateAll()
	loaded = UIParentLoadAddOn("WoWChallenger");
	if (loaded == nil) then
		print("Error to refresh addon");
	else
		core.DBchallenge.load();
		challengesDB = core.DBchallenge.data;
		for i = 1, 3 do
			_G["Difficult"..i.."ChallSub"]:SetText(setStringChallengeDoneByNoDone(i));
		end
		for i = 1, core.libs.tablelength(challengesDB) do
			for i2 = 1, (core.libs.tablelength(challengesDB[i]) - 1) do
				_G[challengesDB[i].id.."challenge"..challengesDB[i][i2].id.."ProgressBarLootFrame"]:SetSize(core.libs.progressPourcent(challengesDB[i][i2])*(328/100), 15);
				_G[challengesDB[i].id.."challenge"..challengesDB[i][i2].id.."ProgressBarText"]:SetText(math.floor(core.libs.progressPourcent(challengesDB[i][i2])).."%");
				if (math.floor(core.libs.progressPourcent(challengesDB[i][i2])) == 100) then
					_G[challengesDB[i].id.."challenge"..challengesDB[i][i2].id.."IconLootFrame"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/icon_loot_done"});
				else
					_G[challengesDB[i].id.."challenge"..challengesDB[i][i2].id.."IconLootFrame"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/icon_loot"});
				end
				for i3 = 1, core.libs.tablelength(challengesDB[i][i2].tab) do
					if (challengesDB[i][i2].tab[i3].name == core.L["Alliance"] or challengesDB[i][i2].tab[i3].name == core.L["Horde"]) then
						local factionPlayer = UnitFactionGroup("player")
						if (factionPlayer == challengesDB[i][i2].tab[i3].faction) then
							for i4 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page) do
							if (core.libs.ifDonePage(challengesDB[i][i2].tab[i3].page[i4]) == true) then
								if (_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].selected == true) then
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done_selected");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done_selected");								
								else
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done");
								end
								_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].saveIcon = challengesDB[i][i2].tab[i3].page[i4].icon.."_done";
							else
								if (_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].selected == true) then
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_selected");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_selected");								
								else
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon);
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon);
								end
							end
								for i5 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step) do
									if (i5 == 1 or core.libs.ifDoneAllPrevStep(challengesDB[i][i2].tab[i3].page[i4], challengesDB[i][i2].tab[i3].page[i4].step[i5].id) == true) then
										if (core.libs.ifDoneStep(challengesDB[i][i2].tab[i3].page[i4].step[i5]) == true) then
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."TitleText"]:SetText(core.L["Etape"].." "..challengesDB[i][i2].tab[i3].page[i4].step[i5].id.."/"..core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step)..core.L["2points"]..core.L["Acheve"]);
										else
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."TitleText"]:SetText(core.L["Etape"].." "..challengesDB[i][i2].tab[i3].page[i4].step[i5].id.."/"..core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step)..core.L["2points"]);
										end
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:SetSize(252, core.libs.heightBgTask(challengesDB[i][i2].tab[i3].page[i4].step[i5].task)+10);
										if (challengesDB[i][i2].tab[i3].page[i4].step[i5].help ~= nil) then
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetSize(20, 20);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconHelp);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnLeave", OnLeave);
										end
										if (challengesDB[i][i2].tab[i3].page[i4].step[i5].AccessLoot == true) then
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lootIconStep"});
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", lootOnEnterIcon);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
										else
											if (challengesDB[i][i2].tab[i3].page[i4].step[i5].help == nil) then
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = ""});
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", OnLeave);
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
											end
										end
										for i6 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step[i5].task) do
											if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["type"] == "hf") then
												if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["hf"][4] == true) then
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontGreenLarge");
												else
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontHighlightLarge");
												end
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText(challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["hf"][2]);
											elseif (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["type"] == "quest") then
												if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].completed == true) then
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontGreenLarge");
												else
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontHighlightLarge");
												end
												if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].title == nil) then
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText("|c007D7D7d"..core.L["QuestNotDis"].."|r");
												else
													_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText(challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].title);
												end
											end
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetSize(_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:GetWidth(), 40);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetSize(_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:GetWidth()-15, 0);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetPoint("LEFT", challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6, "LEFT", 25, 0);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetScript("OnEnter", lootOnEnterTask);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetScript("OnLeave", OnLeave);	
										end
									else
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask1Text"]:SetFontObject("GameFontHighlight");
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask1Text"]:SetText(core.L["EtapeLock"]);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:SetSize(252, 50);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep"});
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", lootOnEnterIconLock);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
									end
								end
							end
						end
					else
						for i4 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page) do
							if (core.libs.ifDonePage(challengesDB[i][i2].tab[i3].page[i4]) == true) then
								if (_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].selected == true) then
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done_selected");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done_selected");								
								else
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_done");
								end
								_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].saveIcon = challengesDB[i][i2].tab[i3].page[i4].icon.."_done";
							else
								if (_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4].selected == true) then
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_selected");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon.."_selected");								
								else
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetNormalTexture(challengesDB[i][i2].tab[i3].page[i4].icon);
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4]:SetHighlightTexture(challengesDB[i][i2].tab[i3].page[i4].icon);
								end
							end
							for i5 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step) do
								if (i5 == 1 or core.libs.ifDoneAllPrevStep(challengesDB[i][i2].tab[i3].page[i4], challengesDB[i][i2].tab[i3].page[i4].step[i5].id) == true) then
									if (core.libs.ifDoneStep(challengesDB[i][i2].tab[i3].page[i4].step[i5]) == true) then
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."TitleText"]:SetText(core.L["Etape"].." "..challengesDB[i][i2].tab[i3].page[i4].step[i5].id.."/"..core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step)..core.L["2points"]..core.L["Acheve"]);
									else
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."TitleText"]:SetText(core.L["Etape"].." "..challengesDB[i][i2].tab[i3].page[i4].step[i5].id.."/"..core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step)..core.L["2points"]);
									end
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:SetSize(252, core.libs.heightBgTask(challengesDB[i][i2].tab[i3].page[i4].step[i5].task)+10);
									if (challengesDB[i][i2].tab[i3].page[i4].step[i5].AccessLoot == true) then
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lootIconStep"});
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", lootOnEnterIcon);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
										if (challengesDB[i][i2].tab[i3].page[i4].step[i5].help ~= nil) then
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetSize(20, 20);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconHelp);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnLeave", OnLeave);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetPoint("RIGHT", _G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5], "RIGHT", -25, 0);
										end
									else
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = ""});
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", OnLeave);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetPoint("RIGHT", _G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5], "RIGHT", -10000, 0);
										if (challengesDB[i][i2].tab[i3].page[i4].step[i5].help ~= nil) then
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetSize(20, 20);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconHelp);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetScript("OnLeave", OnLeave);
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconHelp"]:SetPoint("RIGHT", _G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5], "RIGHT", -5, 0);
										end
									end
									for i6 = 1, core.libs.tablelength(challengesDB[i][i2].tab[i3].page[i4].step[i5].task) do
										if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["type"] == "hf") then
											if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["hf"][4] == true) then
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontGreenLarge");
											else
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontHighlightLarge");
											end
											_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText(challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["hf"][2]);
										elseif (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["type"] == "quest") then
											if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].completed == true) then
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontGreenLarge");
											else
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetFontObject("GameFontHighlightLarge");
											end
											if (challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].title == nil) then
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText("|c007D7D7d"..core.L["QuestNotDis"].."|r");
											else
												_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetText(challengesDB[i][i2].tab[i3].page[i4].step[i5].task[i6]["quest"].title);
											end
										end
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetSize(_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:GetWidth(), 40);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetSize(_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:GetWidth()-15, 0);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6.."Text"]:SetPoint("LEFT", challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6, "LEFT", 25, 0);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetScript("OnEnter", lootOnEnterTask);
										_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask"..i6]:SetScript("OnLeave", OnLeave);	
									end
								else
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask1Text"]:SetFontObject("GameFontHighlight");
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."ContentTask1Text"]:SetText(core.L["EtapeLock"]);
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."Content"]:SetSize(252, 50);
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep"});
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnEnter", lootOnEnterIconLock);
									_G[challengesDB[i].id.."ChallengeFrame"..i2.."Tab"..i3.."Page"..i4.."ContentStepScrollChild"..i5.."iconLoot"]:SetScript("OnLeave", OnLeave);
								end
							end
						end
					end
				end
			end
		end
	end
end

local function Tab1_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab2Content:Hide();
	Tab3Content:Hide();
	Tab4Content:Hide();
	Tab5Content:Hide();
	core.cpgn.SetShowHideToModel("show");
	self.content:Show();
end

local function Tab2_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab3Content:Hide();
	Tab4Content:Hide();
	Tab5Content:Hide();
	core.cpgn.SetShowHideToModel("hide");
	self.content:Show();
end

local function Tab3_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab2Content:Hide();
	Tab4Content:Hide();
	Tab5Content:Hide();
	core.cpgn.SetShowHideToModel("hide");
	self.content:Show();
end

local function Tab4_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab2Content:Hide();
	Tab3Content:Hide();
	Tab5Content:Hide();
	core.cpgn.SetShowHideToModel("hide");
	self.content:Show();
end

local function Tab5_OnClick(self)
	PanelTemplates_SetTab(self:GetParent(), self:GetID());
	Tab1Content:Hide();
	Tab2Content:Hide();
	Tab3Content:Hide();
	Tab4Content:Hide();
	core.cpgn.SetShowHideToModel("hide");
	self.content:Show();
end

function Config:Toggle()
	if (AnnoucementShow4 == true) then
		if (FirstShowAnnouce == true) then
			FirstShowAnnouce = false;
			PlaySound("77003", "sfx");
			core.AnnoucementFunction();
		end
	else
		local menu = UIConfig or Config:CreateMenu();
		if (menu:IsShown()) then
			core.cpgn.SetShowHideToModel("hide");
			PlaySound("680", "sfx");
			menu:SetShown(false);
		elseif (not menu:IsShown()) then
			core.cpgn.SetShowHideToModel("show");
			PlaySound("679", "sfx");
			menu:SetShown(true);
			if (FirstShow == false) then
				UpdateAll();
				core.UpdateMounts();
				core.UpdatePets();
			end
			FirstShow = false;
		end
	end
end

function Config:CreateMenu()
	UIConfig = CreateFrame("Frame", "WoWChallengesFrame", UIParent, "PortraitFrameTemplate");
	UIConfig:EnableMouse(true)
	UIConfig:SetSize(800,475);
	UIConfig:SetPoint("CENTER", UIParent, "CENTER");
	UIConfig:SetScript("OnKeyDown", function(self, key)
		if (key == "ESCAPE") then
			PlaySound("680", "sfx");
			core.cpgn.SetShowHideToModel("hide");
			self:Hide();
		end
	end);
	UIConfig:SetScript("OnHide", function()
		if (FirstShow == false) then
			core.cpgn.SetShowHideToModel("hide");
		end
	end);
	UIConfig:SetPropagateKeyboardInput(true)

	UIConfig.title = UIConfig:CreateFontString(nil, "OVERLAY", "GameFontNormal");
	UIConfig.title:SetText("WoW Challenger");
	UIConfig.title:SetPoint("CENTER", UIConfig.TitleBg, "CENTER");

	local frameName = UIConfig:GetName();
	UIConfig.numTabs = 5;

	local tab1 = CreateFrame("Button", frameName.."Tab1", UIConfig, "CharacterFrameTabButtonTemplate");
	tab1:SetID(1);
	tab1:SetText(core.L["Campaigns"].." BETA");
	tab1:SetScript("OnClick", Tab1_OnClick);
	tab1:SetPoint("TOPLEFT", UIConfig, "BOTTOMLEFT", 0, 1);
	tab1.content = CreateFrame("Frame", "Tab1Content", UIConfig);
	tab1.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab1.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab1.content:Hide();
	core.Tab.t1 = tab1;

	local tab2 = CreateFrame("Button", frameName.."Tab2", UIConfig, "CharacterFrameTabButtonTemplate");
	tab2:SetID(2);
	tab2:SetText(core.L["Challenges"]);
	tab2:SetScript("OnClick", Tab2_OnClick);
	tab2:SetPoint("TOPLEFT", _G[frameName.."Tab1"], "TOPRIGHT", -15, 0);
	tab2.content = CreateFrame("Frame", "Tab2Content", UIConfig);
	tab2.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 8, -28);
	tab2.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT", -9, 7);
	tab2.content:Hide();
	core.Tab.t2 = tab2;
	
	local tab3 = CreateFrame("Button", frameName.."Tab3", UIConfig, "CharacterFrameTabButtonTemplate");
	tab3:SetID(3);
	tab3:SetText(core.L["Mounts"]);
	tab3:SetScript("OnClick", Tab3_OnClick);
	tab3:SetPoint("TOPLEFT", _G[frameName.."Tab2"], "TOPRIGHT", -15, 0);
	tab3.content = CreateFrame("Frame", "Tab3Content", UIConfig);
	tab3.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab3.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab3.content:Hide();
	core.Tab.t3 = tab3;
	
	local tab4 = CreateFrame("Button", frameName.."Tab4", UIConfig, "CharacterFrameTabButtonTemplate");
	tab4:SetID(4);
	tab4:SetText(core.L["Pets"]);
	tab4:SetScript("OnClick", Tab4_OnClick);
	tab4:SetPoint("TOPLEFT", _G[frameName.."Tab3"], "TOPRIGHT", -15, 0);
	tab4.content = CreateFrame("Frame", "Tab4Content", UIConfig);
	tab4.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab4.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab4.content:Hide();
	core.Tab.t4 = tab4;
	
	local tab5 = CreateFrame("Button", frameName.."Tab5", UIConfig, "CharacterFrameTabButtonTemplate");
	tab5:SetID(5);
	tab5:SetText(core.L["Thanks"]);
	tab5:SetScript("OnClick", Tab5_OnClick);
	tab5:SetPoint("TOPLEFT", _G[frameName.."Tab4"], "TOPRIGHT", -15, 0);
	tab5.content = CreateFrame("Frame", "Tab5Content", UIConfig);
	tab5.content:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", 0, -20);
	tab5.content:SetPoint("BOTTOMRIGHT", UIConfig, "BOTTOMRIGHT");
	tab5.content:Hide();
	core.Tab.t5 = tab5;
	
	Tab1_OnClick(_G[frameName.."Tab1"]);

	local UIDifficult = CreateFrame("Frame", "DifficultiesFrame", Tab2Content, nil);
	UIDifficult:SetSize(220,450);
	UIDifficult:SetPoint("LEFT", Tab2Content, "LEFT", -7, 1);
	UIDifficult:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-blue-bg",
												edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
												tile = false, edgeSize = 16, 
												insets = { left = 0, right = 3, top = 0, bottom = -105 }});
				
	UIDifficult.pct_top = CreateFrame("Frame", "UIDiff_pct_topFrame", UIDifficult, nil);
	UIDifficult.pct_top:SetSize(170,50);
	UIDifficult.pct_top:SetFrameLevel(5);
	UIDifficult.pct_top:SetPoint("TOP", UIDifficult, "TOP", 0, -10);
	UIDifficult.pct_top:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/difficulties/pct_uidifficulty_top",
												tile = false,
												insets = { left = 0, right = 0, top = 0, bottom = 0 }
												});
												
	UIDifficult.pct_bottom = CreateFrame("Frame", "UIDiff_pct_bottomFrame", UIDifficult, nil);
	UIDifficult.pct_bottom:SetSize(170,50);
	UIDifficult.pct_bottom:SetFrameLevel(5);
	UIDifficult.pct_bottom:SetPoint("BOTTOM", UIDifficult, "BOTTOM", 0, 10);
	UIDifficult.pct_bottom:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/difficulties/pct_uidifficulty_bottom",
												tile = false,
												insets = { left = 0, right = 0, top = 0, bottom = 0 }
												});

	UIListMissions = CreateFrame("Frame", "ListMissionsFrame", Tab2Content, nil);
	UIListMissions:SetSize(580,449);
	UIListMissions:SetPoint("RIGHT", Tab2Content, "RIGHT", 7, 0);
	UIListMissions:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-paper-bg",
												tile = false,
												insets = { left = 0, right = -177, top = 0, bottom = -75 }
												});
	local function ScrollFrame_OnMouseWheel(self, delta)
		local newValue = self:GetVerticalScroll() - (delta * 100);

		if (newValue < 0) then
			newValue = 0;
		elseif (newValue > self:GetVerticalScrollRange()) then
			newValue = self:GetVerticalScrollRange();
		end
		self:SetVerticalScroll(newValue);
	end

	UIListMissions.ScrollFrame = CreateFrame("ScrollFrame", nil, UIListMissions, "UIPanelScrollFrameTemplate");
	UIListMissions.ScrollFrame:SetPoint("TOPLEFT", UIListMissions, "TOPLEFT", 0, 0);
	UIListMissions.ScrollFrame:SetPoint("BOTTOMRIGHT", UIListMissions, "BOTTOMRIGHT", 0, 0);
	UIListMissions.ScrollFrame:SetClipsChildren(true);
	UIListMissions.ScrollFrame.ScrollBar:ClearAllPoints();
	UIListMissions.ScrollFrame.ScrollBar:SetPoint("TOPRIGHT", UIListMissions.ScrollFrame, "TOPRIGHT", -6, -18);
	UIListMissions.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", UIListMissions.ScrollFrame, "BOTTOMRIGHT", -6, 18);
	local child = CreateFrame("Frame", nil, UIListMissions.ScrollFrame);
	child:SetSize(550, 500);

	UIListMissions.ScrollFrame:SetScrollChild(child);
	UIListMissions.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);

	local UIPortrait = CreateFrame("Frame", "PortraitFrame", UIConfig, nil);
	UIPortrait:SetSize(80,80);
	UIPortrait:SetFrameLevel(15);
	UIPortrait:SetPoint("TOPLEFT", UIConfig, "TOPLEFT", -15, 17);
	UIPortrait:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/difficulties/icon_wc",
												tile = false,
												insets = { left = 0, right = 0, top = 0, bottom = 0 }
												});

	local tabDiff = {};

	local function TabDiff_Selected(id)
		for i=1, 3 do
			if (i == id) then
				tabDiff[i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
				tabDiff[i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
			end
			tabDiff[i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty");
			tabDiff[i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty");
		end
	end


	local function TabDiff_OnClick(self)
		PlaySound("681", "sfx");
		TabDiff_Selected(self:GetID());
		currentTabDiffOpen = self:GetID();
		self:SetNormalTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
		self:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
		self.content:Show();
		local scrollChild = UIListMissions.ScrollFrame:GetScrollChild();
		if (scrollChild) then
			scrollChild:Hide();
		end
		UIListMissions.ScrollFrame:SetScrollChild(self.content);
		self.content:Show();
	end
	
	local function TabDiff_OnClick_Second(self)
		if (currentTabDiffOpen ~= self:GetID()) then
			currentTabDiffOpen = self:GetID();
			PlaySound("681", "sfx");
			TabDiff_Selected(self:GetID());
			self:SetNormalTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
			self:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty_clicked");
			if (currentChallOpen ~= nil) then
				self.content:Show();
				_G[currentChallOpen]:Hide();
				currentChallOpen = nil;
				local scrollChild = UIListMissions.ScrollFrame:GetScrollChild():Hide();
				if (scrollChild) then
					scrollChild:Hide();
				end
				UIListMissions.ScrollFrame:SetScrollChild(self.content);
				UIListMissions.ScrollFrame:Show();
			else
				self.content:Show();
				local scrollChild = UIListMissions.ScrollFrame:GetScrollChild();
				if (scrollChild) then
					scrollChild:Hide();
				end
				UIListMissions.ScrollFrame:SetScrollChild(self.content);
				self.content:Show();
			end
		end
	end

	local function CreateTabDiff(id, numTabs, frameParent, title, subtitle, icon)
		frameParent.numTabs = numTabs;

		local frameParentName = frameParent:GetName();

		local tabDiff = CreateFrame("Button", frameParentName.."Tab"..id, frameParent);
		tabDiff:SetID(id);
		tabDiff:SetNormalTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty");
		tabDiff:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/difficulties/button_difficulty");
		tabDiff:SetSize(211,65);
		tabDiff:SetScript("OnClick", TabDiff_OnClick_Second);
		
		tabDiff.content = CreateFrame("Frame", nil, UIListMissions.ScrollFrame);
		tabDiff.content:SetSize(574,449);
		
		tabDiff.title = tabDiff:CreateFontString(nil, "OVERLAY");
		tabDiff.title:SetFontObject("GameFontNormalLarge");
		tabDiff.title:SetPoint("CENTER", tabDiff, "CENTER", 20, 10);
		tabDiff.title:SetText(title);

		tabDiff.subtitle = tabDiff:CreateFontString("Difficult"..id.."ChallSub", "OVERLAY");
		tabDiff.subtitle:SetFontObject("GameFontNormalSmall");
		tabDiff.subtitle:SetPoint("CENTER", tabDiff, "CENTER", 20, -11);
		tabDiff.subtitle:SetText(subtitle);
		
		tabDiff.icon = CreateFrame("Frame", "TabIconChallenge"..id, tabDiff);
		tabDiff.icon:SetSize(80,80);
		tabDiff.icon:SetFrameLevel(5)
		tabDiff.icon:SetPoint("LEFT", tabDiff, "LEFT", 0, 0);
		tabDiff.icon:SetBackdrop({bgFile = icon,
								tile = false,
								insets = { left = 0, right = 0, top = 0, bottom = 0 }
								});
		if (id == 1) then
			tabDiff:SetPoint("TOP", frameParent, "TOP", 0, -105);
		else
			tabDiff:SetPoint("TOP", _G[frameParentName.."Tab"..(id - 1)], "TOP", 0, -85);
		end
		return tabDiff;
	end	

	tabDiff[1] = CreateTabDiff(1, 3, UIDifficult, core.L["Expert"], setStringChallengeDoneByNoDone(1), "Interface/Challenges/ChallengeMode_Medal_Gold");
	tabDiff[2] = CreateTabDiff(2, 3, UIDifficult, core.L["Medium"], setStringChallengeDoneByNoDone(2), "Interface/Challenges/ChallengeMode_Medal_Silver");
	tabDiff[3] = CreateTabDiff(3, 3, UIDifficult, core.L["Novice"], setStringChallengeDoneByNoDone(3), "Interface/Challenges/ChallengeMode_Medal_Bronze");

	TabDiff_OnClick(_G["DifficultiesFrameTab1"]);

	local function ListChallenge_OnClick(self)
		UIListMissions.ScrollFrame:Hide();
		PlaySound("21966", "sfx");
		self.content:Show();
		currentChallOpen = self.content:GetName();
	end
	
	local function CreateChallenge(challenge, difficulty)
		local challengeBtn = CreateFrame("Button", difficulty.."challengeBtnTab"..challenge.id, tabDiff[difficulty].content);
		challengeBtn:SetID(challenge.id);
		challengeBtn:SetSize(512,306);
		challengeBtn:SetScript("OnClick", ListChallenge_OnClick);
		challengeBtn:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/challenge_button"});
		challengeBtn:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/challenges/challenge_button");
													
		challengeBtn.bg = CreateFrame("Frame", "challengeBgFrame", challengeBtn);
		challengeBtn.bg:SetSize(532,256);
		challengeBtn.bg:SetPoint("TOP", challengeBtn, "TOP", 0, -5);
		challengeBtn.bg:SetBackdrop({bgFile = challenge.bg});
		
		challengeBtn.title = CreateFrame("Frame", difficulty.."challenge"..challenge.id.."TitleFrame", challengeBtn);
		challengeBtn.title:SetSize(520,64);
		challengeBtn.title:SetFrameLevel(8);
		challengeBtn.title:SetPoint("BOTTOM", challengeBtn.bg, "BOTTOM", 0, 10);
		challengeBtn.title:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/title_bg"});
		
		challengeBtn.titleText = challengeBtn.title:CreateFontString(nil, "OVERLAY");
		if GetLocale() == "ruRU" then
			challengeBtn.titleText:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 20, "OUTLINE");
		else
			challengeBtn.titleText:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE");
		end
		challengeBtn.titleText:SetPoint("CENTER", challengeBtn.title, "CENTER", 0, 8);
		challengeBtn.titleText:SetText(challenge.title);
		
		challengeBtn.loot = CreateFrame("Frame", "challengeLootFrame", challengeBtn);		
		challengeBtn.loot:SetSize(490,55);
		challengeBtn.loot:SetPoint("BOTTOM", challengeBtn, "BOTTOM", 5, 5);
		challengeBtn.loot:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/progressBar"});
		
		challengeBtn.loot.progressBar = CreateFrame("Frame", difficulty.."challenge"..challenge.id.."ProgressBarLootFrame", challengeBtn.loot);
		challengeBtn.loot.progressBar:SetSize(core.libs.progressPourcent(challenge)*(328/100), 15);
		challengeBtn.loot.progressBar:SetPoint("LEFT", challengeBtn.loot, "LEFT", 75, 0);
		challengeBtn.loot.progressBar:SetBackdrop({bgFile = "Interface/GLUES/LoadingBar/Loading-BarFill"});
		challengeBtn.loot.progressBar:SetFrameLevel(7);
		
		challengeBtn.loot.progressText = challengeBtn.loot:CreateFontString(difficulty.."challenge"..challenge.id.."ProgressBarText", "OVERLAY");
		if GetLocale() == "ruRU" then
			challengeBtn.loot.progressText:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 11, "OUTLINE");
		else
			challengeBtn.loot.progressText:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE");
		end
		challengeBtn.loot.progressText:SetPoint("CENTER", challengeBtn.loot, "CENTER");
		challengeBtn.loot.progressText:SetText(math.floor(core.libs.progressPourcent(challenge)).."%");
		
		challengeBtn.loot.icon = CreateFrame("Frame", difficulty.."challenge"..challenge.id.."IconLootFrame", challengeBtn.loot);
		challengeBtn.loot.icon:SetSize(50,50);
		challengeBtn.loot.icon:SetPoint("RIGHT", challengeBtn.loot, "RIGHT", -50, 0);
		if (math.floor(core.libs.progressPourcent(challenge)) == 100) then
			challengeBtn.loot.icon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/icon_loot_done"});
		else
			challengeBtn.loot.icon:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/icon_loot"});
		end
		challengeBtn.loot.icon.challForTooltip = difficulty.."challenge"..challenge.id.."IconLootFrame";
		challengeBtn.loot.icon.diffForTooltip = difficulty;
		
		challengeBtn.loot.icon:SetScript("OnEnter", lootOnEnter)
		challengeBtn.loot.icon:SetScript("OnLeave", OnLeave)
		
		if (challenge.id == 1) then
			challengeBtn:SetPoint("TOP", tabDiff[difficulty].content, "TOP", 0, -15);
		else
			challengeBtn:SetPoint("TOP", _G[difficulty.."challengeBtnTab"..(challenge.id - 1)], "TOP", 0, -315);
		end		
		
		challengeBtn.content = CreateFrame("Frame", difficulty.."ChallengeFrame"..challenge.id, UIListMissions);
		challengeBtn.content:SetSize(574,410);
		challengeBtn.content:Hide();
		challengeBtn.content.numTabs = core.libs.tablelength(challenge.tab);
		challengeBtn.content:SetPoint("BOTTOM", UIListMissions, "BOTTOM", 0, 0);
		challengeBtn.content:SetFrameLevel(6);
		challengeBtn.content:SetBackdrop({bgFile = "Interface/Store/receipt-parchment-middle",
								edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
								tile = false, edgeSize = 16, 
								insets = { left = 3, right = -202, top = 3, bottom = 3 }});
		challengeBtn.content.btn = CreateFrame("Button", "btnBackFrame", challengeBtn.content, "UIPanelButtonTemplate");
		challengeBtn.content.btn:SetSize(75, 25);
		challengeBtn.content.btn:SetText(core.L["Retour"]);
		challengeBtn.content.btn:SetPoint("TOPRIGHT", challengeBtn.content, "TOPRIGHT", -20, 30);
		challengeBtn.content.btn:SetScript("OnClick", function(self)
			PlaySound("21967", "sfx");
			UIListMissions.ScrollFrame:Show();
			self:GetParent():Hide();
			currentChallOpen = nil;
		end);
		challengeBtn.content.ScrollFrame = CreateFrame("ScrollFrame", difficulty.."ChallengeFrame"..challenge.id.."ScrollFrame", challengeBtn.content, "UIPanelScrollFrameTemplate");
		challengeBtn.content.ScrollFrame:SetPoint("TOPLEFT", challengeBtn.content, "TOPLEFT", 1, -3);
		challengeBtn.content.ScrollFrame:SetPoint("BOTTOMRIGHT", challengeBtn.content, "BOTTOMRIGHT", 1, 3);
		challengeBtn.content.ScrollFrame:SetClipsChildren(true);
		local child = CreateFrame("Frame", nil, challengeBtn.content.ScrollFrame);
		child:SetSize(570, 420);

		challengeBtn.content.ScrollFrame:SetScrollChild(child);
		challengeBtn.content.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
				
		local function TabChallenge_OnClick(self)
			PanelTemplates_SetTab(self:GetParent(), self:GetID());
			local scrollChild = self:GetParent().ScrollFrame:GetScrollChild();
			if (scrollChild) then
				scrollChild:Hide();
			end
			self:GetParent().ScrollFrame:SetScrollChild(self.content);
			self.content:Show();
		end
								
		local function CreateTabChallenge(challengeTab, frameParent, challengeId, difficulty)
			local frameParentName = frameParent:GetName();
			frameParent.tab = CreateFrame("Button", frameParentName.."Tab"..challengeTab.id, frameParent, "OptionsFrameTabButtonTemplate");
			frameParent.tab:SetID(challengeTab.id);
			frameParent.tab:SetFrameLevel(5);
			frameParent.tab:SetText(challengeTab.name);
			frameParent.tab:SetScript("OnClick", TabChallenge_OnClick);
			frameParent.tab.content = CreateFrame("Frame", frameParentName.."Tab"..challengeTab.id.."Page", frameParent.ScrollFrame);
			frameParent.tab.content:SetSize(570, 350);
		
			if (challengeTab.id == 1) then
				frameParent.tab:SetPoint("TOPLEFT", frameParentName, "TOPLEFT", 10, 22); 	
			else
				frameParent.tab:SetPoint("TOPLEFT", _G[frameParentName.."Tab"..(challengeTab.id - 1)], "TOPRIGHT");
			end
			
			local function PageChallenge_OnClick(self)
			
				for i = 1, core.libs.tablelength(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page) do
					_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i.."Content"]:Hide();
					if (core.libs.ifDonePage(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page[i]) == true) then
						_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i]:SetNormalTexture(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page[i].icon.."_done");
						_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i]:SetHighlightTexture(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page[i].icon.."_done");
					else
						_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i]:SetNormalTexture(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page[i].icon);
						_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i]:SetHighlightTexture(core.DBchallenge.data[difficulty][challengeId].tab[challengeTab.id].page[i].icon);
					end
					_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page"..i].selected = false;
				end
				PlaySound("681", "sfx");
				self.content.step.ScrollFrame:SetScrollChild(self.content.step.ScrollFrame.child);
				self:SetNormalTexture(self.saveIcon.."_selected");
				self:SetHighlightTexture(self.saveIcon.."_selected");
				self.selected = true;
				self.content:Show();
			end
			
			local function CreatePageChallenge(challengePage, frameParent, challengeTab)
				local frameParentName = frameParent:GetName();

				frameParent.page = CreateFrame("Button", frameParentName..challengePage.id, frameParent);
				frameParent.page:SetID(challengePage.id);
				frameParent.page.selected = false;
				frameParent.page:SetPoint("TOP", frameParent, "TOP", 0, -15);
				if (core.libs.ifDonePage(challengePage) == true) then
					frameParent.page:SetNormalTexture(challengePage.icon.."_done");
					frameParent.page:SetHighlightTexture(challengePage.icon.."_done");
					frameParent.page.saveIcon = challengePage.icon.."_done";
				else
					frameParent.page:SetNormalTexture(challengePage.icon);
					frameParent.page:SetHighlightTexture(challengePage.icon);
					frameParent.page.saveIcon = challengePage.icon;
				end
				frameParent.page:SetSize(32,32);
				local lootOnEnter = function(self, motion)
					GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 5);
					GameTooltip:AddLine(challengePage.name, 1, 1, 1);
					GameTooltip:Show();
				end
				frameParent.page:SetScript("OnEnter", lootOnEnter);
				frameParent.page:SetScript("OnLeave", OnLeave);
				if (challengePage.id == 1) then
					frameParent.page:SetPoint("TOP", frameParent, "TOP", core.libs.centerListObjectX(challengePage.id, core.libs.tablelength(challengeTab.page), 40), -15);
				else
					frameParent.page:SetPoint("TOP", frameParent, "TOP", core.libs.centerListObjectX(challengePage.id, core.libs.tablelength(challengeTab.page), 40), -15);
				end
				frameParent.page.content = CreateFrame("Frame", frameParentName..challengePage.id.."Content", frameParent);
				frameParent.page.content:SetPoint("LEFT", frameParent, "LEFT", 3, -60);
				frameParent.page.content:SetSize(570, 335);
				
				frameParent.page.content.characterEdge = CreateFrame("Frame", frameParentName..challengePage.id.."ContentPctEdge", frameParent.page.content);
				frameParent.page.content.characterEdge:SetSize(235, 350);
				frameParent.page.content.characterEdge:SetPoint("BOTTOMLEFT", frameParent.page.content, "BOTTOMLEFT", 0, -1);
				frameParent.page.content.characterEdge:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/edge_character_pct"});
				frameParent.page.content.characterEdge:SetFrameLevel(7);
				frameParent.page.content.characterEdge.picture = CreateFrame("Frame", frameParentName..challengePage.id.."ContentPct", frameParent.page.content.characterEdge);
				frameParent.page.content.characterEdge.picture:SetSize(227, 344);
				frameParent.page.content.characterEdge.picture:SetPoint("CENTER", frameParent.page.content.characterEdge, "CENTER", -2, -1);
				frameParent.page.content.characterEdge.picture:SetBackdrop({bgFile = challengePage.character});
				frameParent.page.content.characterEdge.picture:SetFrameLevel(6);
				
				frameParent.page.content.step = CreateFrame("Frame", frameParentName..challengePage.id.."ContentStep", frameParent.page.content);
				frameParent.page.content.step:SetSize(336, 345);
				frameParent.page.content.step:SetPoint("BOTTOMRIGHT", frameParent.page.content, "BOTTOMRIGHT", -4, 0);
				frameParent.page.content.step:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/bg_step_challenge"});
				
				frameParent.page.content.step.ScrollFrame = CreateFrame("ScrollFrame",  frameParentName..challengePage.id.."ContentStepFauxScroll", frameParent.page.content.step, "UIPanelScrollFrameTemplate");
				frameParent.page.content.step.ScrollFrame:SetPoint("TOPLEFT", frameParent.page.content.step, "TOPLEFT", 0, -34);
				frameParent.page.content.step.ScrollFrame:SetPoint("BOTTOMRIGHT", frameParent.page.content.step, "BOTTOMRIGHT");
				frameParent.page.content.step.ScrollFrame:SetClipsChildren(true);
				frameParent.page.content.step.ScrollFrame.ScrollBar:ClearAllPoints();
				frameParent.page.content.step.ScrollFrame.ScrollBar:SetPoint("TOPRIGHT", frameParent.page.content.step.ScrollFrame, "TOPRIGHT", -1, -18);
				frameParent.page.content.step.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", frameParent.page.content.step.ScrollFrame, "BOTTOMRIGHT", -1, 18);
				
				frameParent.page.content.step.ScrollFrame.child = CreateFrame("Frame", frameParentName..challengePage.id.."ContentStepScrollChild", frameParent.page.content.step.ScrollFrame);
				frameParent.page.content.step.ScrollFrame.child:SetSize(335, 300);

				frameParent.page.content.step.ScrollFrame:SetScrollChild(frameParent.page.content.step.ScrollFrame.child);
				frameParent.page.content.step.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
				
				frameParent.page.content.step.Bigtitle = frameParent.page.content.step:CreateFontString(nil, "OVERLAY");
				if GetLocale() == "ruRU" then
					frameParent.page.content.step.Bigtitle:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 20, "OUTLINE");
				else
					frameParent.page.content.step.Bigtitle:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE");
				end
				frameParent.page.content.step.Bigtitle:SetText(challengePage.name);
				frameParent.page.content.step.Bigtitle:SetPoint("TOP", frameParent.page.content.step, "TOP", 0, -10);
				
				local function CreateStepChallenge(challengeStep, frameParent, challengePage, stepPrevDone)
					local frameParentName = frameParent:GetName();
					
					frameParent.title = CreateFrame("Frame", frameParentName..challengeStep.id, frameParent);
					frameParent.title:SetSize(256, 32);
					frameParent.title:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_title_bg"});
					frameParent.title.text = frameParent.title:CreateFontString(frameParentName..challengeStep.id.."TitleText", "OVERLAY");
					frameParent.title.text:SetFontObject("GameFontWhite");
					frameParent.title.text:SetText(core.L["Etape"].." "..challengeStep.id.."/"..core.libs.tablelength(challengePage.step)..core.L["2points"]);
					frameParent.title.text:SetPoint("LEFT", frameParent.title, "LEFT", 10, 0);

					frameParent.title.iconLoot = CreateFrame("Frame", frameParentName..challengeStep.id.."iconLoot", frameParent);
					frameParent.title.iconLoot:SetSize(20, 20);
					frameParent.title.iconLoot:SetPoint("RIGHT", frameParent.title, "RIGHT", -5, 0);
					frameParent.title.iconLoot:SetFrameLevel(15);
					frameParent.title.iconLoot.stepForTooltip = frameParentName..challengeStep.id.."iconLoot";
					frameParent.taskBg = CreateFrame("Frame", frameParentName..challengeStep.id.."Content", frameParent.title);
					frameParent.title.iconHelp = CreateFrame("Frame", frameParentName..challengeStep.id.."iconHelp", frameParent);
					frameParent.title.iconHelp:SetPoint("RIGHT", frameParent.title, "RIGHT", -25, 0);
					frameParent.title.iconHelp:SetFrameLevel(15);
					frameParent.title.iconHelp.stepForTooltip = frameParentName..challengeStep.id.."iconHelp";
					if (stepPrevDone == true) then
						if (core.libs.ifDoneStep(challengeStep) == true) then
							frameParent.title.text:SetText(core.L["Etape"].." "..challengeStep.id.."/"..core.libs.tablelength(challengePage.step)..core.L["2points"]..core.L["Acheve"]);
						end
						frameParent.taskBg:SetSize(252, core.libs.heightBgTask(challengeStep.task)+10);
						frameParent.taskBg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_task_bg"});
						frameParent.taskBg:SetPoint("TOP", frameParent.title, "BOTTOM", 0, 5);
						if (challengeStep.AccessLoot == true) then
							frameParent.title.iconLoot:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lootIconStep"});
							frameParent.title.iconLoot:SetScript("OnEnter", lootOnEnterIcon)
							frameParent.title.iconLoot:SetScript("OnLeave", OnLeave)
							frameParent.title.iconHelp:SetPoint("RIGHT", frameParent.title, "RIGHT", -25, 0);
						else
							frameParent.title.iconHelp:SetPoint("RIGHT", frameParent.title, "RIGHT", -5, 0);
						end
						if (challengeStep.help ~= nil) then
							frameParent.title.iconHelp:SetSize(20, 20);
							frameParent.title.iconHelp:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
							frameParent.title.iconHelp:SetScript("OnEnter", lootOnEnterIconHelp);
							frameParent.title.iconHelp:SetScript("OnLeave", OnLeave);
						end
					else
						frameParent.taskBg:SetSize(252, 50);
						frameParent.taskBg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_task_bg"});
						frameParent.taskBg:SetPoint("TOP", frameParent.title, "BOTTOM", 0, 5);
						frameParent.title.iconLoot:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep"});
						frameParent.title.iconLoot:SetScript("OnEnter", lootOnEnterIconLock);
						frameParent.title.iconLoot:SetScript("OnLeave", OnLeave);
					end			
					
					if (challengeStep.id == 1) then
						frameParent.title:SetPoint("TOP", frameParent, "TOP", 0, -15);
					else
						frameParent.title:SetPoint("BOTTOM", _G[frameParentName..(challengeStep.id - 1).."Content"], "BOTTOM", 0, -40);
					end
					
					local function CreateTaskChallenge(challengeTask, frameParent, stepPrevDone)
						local frameParentName = frameParent:GetName();
						frameParent.obj = CreateFrame("Frame", frameParentName.."Task"..challengeTask.id, frameParent);
						if (challengeTask.id == 1) then
								frameParent.obj:SetPoint("TOPLEFT", frameParent, "TOPLEFT", 0, -5);
							else
								frameParent.obj:SetPoint("LEFT", _G[frameParentName.."Task"..(challengeTask.id - 1)], "LEFT", 0, -40);
						end
						frameParent.obj.text = frameParent.obj:CreateFontString(frameParentName.."Task"..challengeTask.id.."Text", "OVERLAY", frameParent.obj);
						frameParent.obj.checkImg = CreateFrame("Frame", frameParentName.."Task"..challengeTask.id.."CheckImg", frameParent.obj);
						frameParent.obj.checkImg:SetSize(32, 32);
						frameParent.obj.checkImg:SetPoint("LEFT", frameParent.obj, "LEFT", -3, 0);
						frameParent.obj.text:SetJustifyH("LEFT");
						frameParent.obj.text:SetSize(frameParent:GetWidth()-15, 0);
						frameParent.obj.text:SetPoint("LEFT", frameParent.obj, "LEFT", 25, 0);
						frameParent.obj.taskForTooltip = frameParentName.."Task"..challengeTask.id.."Text";
						if (stepPrevDone == true) then
						frameParent.obj:SetSize(frameParent:GetWidth(), 40);
							if (challengeTask["type"] == "hf") then
								if (challengeTask["hf"][4] == true) then
									frameParent.obj.checkImg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
									frameParent.obj.text:SetFontObject("GameFontGreenLarge");
								else
									frameParent.obj.text:SetFontObject("GameFontHighlightLarge");
								end
								frameParent.obj.text:SetText(challengeTask["hf"][2]);
							elseif (challengeTask["type"] == "quest") then
								if (challengeTask["quest"].completed == true) then
									frameParent.obj.checkImg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
									frameParent.obj.text:SetFontObject("GameFontGreenLarge");
								else
									frameParent.obj.text:SetFontObject("GameFontHighlightLarge");
								end
								if (challengeTask["quest"].title == nil) then
									frameParent.obj.text:SetText("|c007D7D7d"..core.L["QuestNotDis"].."|r");
								else
									frameParent.obj.text:SetText(challengeTask["quest"].title);
								end
							end
							frameParent.obj:SetScript("OnEnter", lootOnEnterTask);
							frameParent.obj:SetScript("OnLeave", OnLeave);							
						else
							if (challengeTask.id == 1) then
								_G[frameParentName.."Task1Text"]:SetFontObject("GameFontHighlight");
								_G[frameParentName.."Task1Text"]:SetText(core.L["EtapeLock"]);
								_G[frameParentName.."Task1Text"]:SetJustifyH("LEFT");
								_G[frameParentName.."Task1Text"]:SetSize(frameParent:GetWidth(), 0);
								_G[frameParentName.."Task1Text"]:SetPoint("LEFT", frameParent, "LEFT", 25, 0);
							end
						end
					end				
					if (challengeStep.task ~= nil) then
						for i = 1, core.libs.tablelength(challengeStep.task) do
							CreateTaskChallenge(challengeStep.task[i], frameParent.taskBg, stepPrevDone);
						end
					end
				end
				if (challengePage.step ~= nil) then
					for i = 1, core.libs.tablelength(challengePage.step) do
						if (i == 1 or core.libs.ifDoneAllPrevStep(challengePage, challengePage.step[i].id) == true) then
							CreateStepChallenge(challengePage.step[i], frameParent.page.content.step.ScrollFrame.child, challengePage, true);
						else
							CreateStepChallenge(challengePage.step[i], frameParent.page.content.step.ScrollFrame.child, challengePage, false);
						end
					end
				end

				frameParent.page.content:Hide();
				frameParent.page:SetScript("OnClick", PageChallenge_OnClick);
			end
			if (challengeTab.name == core.L["Alliance"] or challengeTab.name == core.L["Horde"]) then
				local factionPlayer = UnitFactionGroup("player")
				if (factionPlayer == challengeTab.faction) then
					for i = 1, core.libs.tablelength(challengeTab.page) do
						CreatePageChallenge(challengeTab.page[i], frameParent.tab.content, challengeTab);
					end
					if (core.libs.tablelength(challengeTab.page) ~= 0) then
						PageChallenge_OnClick(_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page1"]);
					end
				else
					local invalidFactionText = frameParent.tab.content:CreateFontString(nil, "OVERLAY");
					invalidFactionText:SetFontObject("GameFontNormalLarge");
					invalidFactionText:SetPoint("CENTER");
					invalidFactionText:SetSize(frameParent.tab.content:GetWidth(), 0);
					invalidFactionText:SetText(core.L["OtherFaction"]);
				end
			else
				for i = 1, core.libs.tablelength(challengeTab.page) do
					CreatePageChallenge(challengeTab.page[i], frameParent.tab.content, challengeTab);
				end
				if (core.libs.tablelength(challengeTab.page) ~= 0) then
					PageChallenge_OnClick(_G[difficulty.."ChallengeFrame"..challengeId.."Tab"..challengeTab.id.."Page1"]);
				end
			end
		end
		for i = 1, core.libs.tablelength(challenge.tab) do
			CreateTabChallenge(challenge.tab[i], challengeBtn.content, challenge.id, difficulty);
		end
		if (core.libs.tablelength(challenge.tab) ~= 0) then
			local haveFaction = false;
			for i = 1, core.libs.tablelength(challenge.tab) do
				if (challenge.tab[i].name == core.L["Alliance"] or challenge.tab[i].name == core.L["Horde"]) then
					local factionPlayer = UnitFactionGroup("player")
					haveFaction = true;
					if (factionPlayer == challenge.tab[i].faction) then
						TabChallenge_OnClick(_G[difficulty.."ChallengeFrame"..challenge.id.."Tab"..i]);
					end
				elseif (haveFaction == false) then
					TabChallenge_OnClick(_G[difficulty.."ChallengeFrame"..challenge.id.."Tab1"]);
				end
			end
		end
	end
	for i = 1, core.libs.tablelength(challengesDB) do
		if (challengesDB[i] ~= nil) then
			for i2 = 1, (core.libs.tablelength(challengesDB[i]) - 1) do
				CreateChallenge(challengesDB[i][i2], challengesDB[i].id);
			end
		end
	end
	core.CampaignTab();
	core.MountsTab();
	core.PetsTab();
	core.ThanksTab();
	UIConfig:Hide();
	return UIConfig;
end

local MiniMapOnEnter = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, -15);
	GameTooltip:AddLine("WoW Challenger");
	GameTooltip:AddLine(setStringProgressionCampaign());
	GameTooltip:AddLine(setStringProgressionHF());
	GameTooltip:AddLine(setStringProgressionMounts());
	GameTooltip:AddLine(setStringProgressionPets());
	GameTooltip:Show();
end
local MiniMapOnLeave = function(self, motion)
	GameTooltip:Hide()
end

function Config:CreateMinimap()
	local minibtn = CreateFrame("Button", nil, Minimap, "UICheckButtonTemplate")
	minibtn:SetFrameLevel(8)
	minibtn:SetSize(35, 35)
	minibtn:SetMovable(true)
	 
	minibtn:SetNormalTexture("Interface\\CHARACTERFRAME\\TotemBorder")
	minibtn:SetPushedTexture("Interface\\CHARACTERFRAME\\TotemBorder")
	minibtn:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/difficulties/icon_wc", 
											tile = false,
											insets = { left = 4, right = 4, top = 4, bottom = 4 }
											});

	local myIconPos = Preferences["IconPos"];
	minibtn:ClearAllPoints();
	minibtn:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 52 - (80 * cos(myIconPos)),(80 * sin(myIconPos)) - 52);
	 
	local function UpdateMapBtn()
		local Xpoa, Ypoa = GetCursorPosition();
		local Xmin, Ymin = Minimap:GetLeft(), Minimap:GetBottom();
		Xpoa = Xmin - Xpoa / Minimap:GetEffectiveScale() + 70;
		Ypoa = Ypoa / Minimap:GetEffectiveScale() - Ymin - 70;
		myIconPos = math.deg(math.atan2(Ypoa, Xpoa));
		minibtn:ClearAllPoints();
		minibtn:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 52 - (80 * cos(myIconPos)), (80 * sin(myIconPos)) - 52);
		Preferences["IconPos"] = myIconPos;
	end
	 
	minibtn:RegisterForDrag("LeftButton");
	minibtn:SetScript("OnDragStart", function()
		minibtn:StartMoving();
		minibtn:SetScript("OnUpdate", UpdateMapBtn);
	end)
	 
	minibtn:SetScript("OnDragStop", function()
		minibtn:StopMovingOrSizing();
		minibtn:SetScript("OnUpdate", nil);
		UpdateMapBtn();
	end)
	minibtn:SetScript("OnClick", core.Config.Toggle);
	minibtn:SetScript("OnEnter", MiniMapOnEnter);
	minibtn:SetScript("OnLeave", MiniMapOnLeave);
	return minibtn;
end

local minimap;
local loadmap = CreateFrame("FRAME");
loadmap:RegisterEvent("ADDON_LOADED");
function loadmap:OnEvent(event, arg1)
	if event == "ADDON_LOADED" and arg1 == "WoWChallenger" then
		minimap = Config:CreateMinimap();
		if (Preferences["Show"] == true) then
			minimap:Show();
		else
			minimap:Hide();
		end
	end
end
loadmap:SetScript("OnEvent", loadmap.OnEvent);

SLASH_WC1 = "/wc";
SLASH_WC2 = "/wowchallenger";
SLASH_MAP1 = "/wcmap";
SlashCmdList["WC"] = function(msg)
   Config:Toggle();
end
SlashCmdList["MAP"] = function(msg)
	if (minimap:IsShown()) then
		minimap:SetShown(false);
		Preferences["Show"] = false;
		print(core.L["ModifText1H"]);
	elseif (not minimap:IsShown()) then
		minimap:SetShown(true);
		Preferences["Show"] = true;
		print(core.L["ModifText1S"]);
	end
end 