local _, core = ...;
local mountsDB;

local LoadMountsDB = CreateFrame("Frame");
LoadMountsDB:RegisterEvent("ADDON_LOADED");
LoadMountsDB:SetScript("OnEvent", function()
	core.Loadmounts();
	mountsDB = core.DBmounts;
end)

--- LIBS FUNCTION ---
local function getNbMaxMount()
	local MountMax = 0;
	for i = 1, core.libs.tablelength(core.DBmounts) do
		local factionPlayer = UnitFactionGroup("player")
		if (factionPlayer == core.DBmounts[i].faction or core.DBmounts[i].faction == "Neutral") then
			MountMax = MountMax + 1;
		end
	end
	return MountMax;
end
core.libs.getNbMaxMount = getNbMaxMount;

local function getNbMountDone()
	local MountDone = 0;
	for i = 1, core.libs.tablelength(core.DBmounts) do
		local factionPlayer = UnitFactionGroup("player")
		if (factionPlayer == core.DBmounts[i].faction or core.DBmounts[i].faction == "Neutral") then
			if (core.DBmounts[i]["infoMount"][11] == true) then
				MountDone = MountDone + 1;
			end
		end
	end
	return MountDone;
end
core.libs.getNbMountDone = getNbMountDone;

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

local function printNPCDroppedLoot(names)
	local stret = "";
	for i = 1, core.libs.tablelength(names) do
		stret = stret..names[i];
		if (i ~= core.libs.tablelength(names)) then
			stret = stret.."|cffffffff, |r";
		end
	end
	return stret;
end


local function printCompoCreate(create)
	local stret = "";
	for i = 1, core.libs.tablelength(create) do
		local compoName = "";
		local item = Item:CreateFromItemID(create[i].compoId)
		item:ContinueOnItemLoad(function()
			compoName = GetItemInfo(create[i].compoId);
		end)
		if (compoName == "") then
			stret = stret.."Loading";
		else
			stret = stret..compoName;
		end
		stret = stret.." ("..create[i].nbNeed..")";
		if (i ~= core.libs.tablelength(create)) then
			stret = stret.."|cffffffff, |r";
		end
	end
	return stret;
end

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

local function SetAllDoneNeed(MountId)
	for i = 1, core.libs.tablelength(core.DBmounts[MountId].page) do
		for j = 1, core.libs.tablelength(core.DBmounts[MountId].page[i].step) do		
			for k = 1, core.libs.tablelength(core.DBmounts[MountId].page[i].step[j].task) do
				if (core.DBmounts[MountId].page[i].step[j].task[k]["need"] ~= nil) then
					if (core.DBmounts[MountId].page[i].step[j].task[k]["need"].path ~= nil) then
						core.DBmounts[MountId].page[i].step[j].task[k]["need"].done = true;
						local pathSaved = {};
						for z = 1, #core.DBmounts[MountId].page[i].step[j].task[k]["need"].path do
							pathSaved[z] = core.DBmounts[MountId].page[i].step[j].task[k]["need"].path:sub(z, z)
						end
						DoneMountTask[tonumber(pathSaved[1])]["page"][tonumber(pathSaved[2])]["step"][tonumber(pathSaved[3])]["task"][tonumber(pathSaved[4])]["done"] = true;
					end
				end
			end
		end
	end
end

local function OnClickTaskNeed(self)
	local pathSaved = {};
	if (self.path["need"].path ~= nil) then
		for i = 1, #self.path["need"].path do
			pathSaved[i] = self.path["need"].path:sub(i, i)
		end
		if (core.DBmounts[tonumber(pathSaved[1])].page[tonumber(pathSaved[2])].step[tonumber(pathSaved[3])].task[tonumber(pathSaved[4])]["need"].done == false) then
			core.DBmounts[tonumber(pathSaved[1])].page[tonumber(pathSaved[2])].step[tonumber(pathSaved[3])].task[tonumber(pathSaved[4])]["need"].done = true;
			DoneMountTask[tonumber(pathSaved[1])]["page"][tonumber(pathSaved[2])]["step"][tonumber(pathSaved[3])]["task"][tonumber(pathSaved[4])]["done"] = true;
		else
			core.DBmounts[tonumber(pathSaved[1])].page[tonumber(pathSaved[2])].step[tonumber(pathSaved[3])].task[tonumber(pathSaved[4])]["need"].done = false;
			DoneMountTask[tonumber(pathSaved[1])]["page"][tonumber(pathSaved[2])]["step"][tonumber(pathSaved[3])]["task"][tonumber(pathSaved[4])]["done"] = false;
		end
		OnLeave();
		core.UpdateMounts();
	end
end

--- GAME TOOLTIP ---
local OnEnterBtnMount = function(self, motion)
	GameTooltip:SetOwner(self, self.anchor, 0, 0);
	GameTooltip:AddLine(self.infoMountForTooltip["infoMount"][1], 1, 1, 1);
	if (self.infoMountForTooltip["infoMount"][11] == true) then
		GameTooltip:AddLine(core.L["MountCollected"], 0, 1, 0);
	else
		GameTooltip:AddLine(core.L["MountNotCollected"], 1, 0, 0);
	end
	GameTooltip:AddLine("\""..self.infoMountForTooltip["extraInfoMount"][2].."\"", 1, 0.82, 0, true);
	GameTooltip:AddLine(self.infoMountForTooltip["extraInfoMount"][3], 1, 1, 1);
	GameTooltip:AddLine(core.L["Expansion"].."|cffffffff"..self.infoMountForTooltip.expansion.."|r");
	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 mountTask = mountsDB[tonumber(words[1])].page[tonumber(words[2])].step[tonumber(words[3])].task[tonumber(words[4])];

	if (mountTask["hf"] ~= nil) then
		if (mountTask["hf"][4] == true) then
			if (mountTask["hf"][14] ~= "") then
				GameTooltip:AddLine(core.L["AcheveBy"].." "..mountTask["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(mountTask["hf"][8], 1, 1, 1);
	elseif (mountTask["quest"] ~= nil) then
		if (mountTask["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 (mountTask["MA"] ~= nil) then
			GameTooltip:AddLine(mountTask["quest"].desc);
		elseif (mountTask["quest"].completed == true) then
			GameTooltip:AddLine(core.L["DescQuestComplete"], 1, 1, 1);
		else
			GameTooltip:AddLine(core.L["DescQuest"], 1, 1, 1);
		end
	elseif (mountTask["need"] ~= nil) then
		if (mountTask["need"].done == true) then
			GameTooltip:AddLine(core.L["Acheve"], 0, 1, 0);
		else
			GameTooltip:AddLine(core.L["NoAcheve"], 0.5, 0.5, 0.5);
		end
		if (mountTask["need"].path ~= nil) then
			GameTooltip:AddLine(core.L["Obtenir"]..tostring(mountTask["need"].nbNeed).." "..mountTask["need"].itemInfo[1]..".", 1, 1, 1);
		else
			GameTooltip:AddLine(core.L["ObtenirM"]..mountTask["need"].itemInfo[1]..".", 1, 1, 1);
		end
		GameTooltip:AddLine(" ");
		local count = GetItemCount(mountTask["need"].itemId)
		if (mountTask["need"].itemInfo[1] == nil) then
			mountTask["need"].itemInfo[1] = "|cffff0000Loading...|r";
		end
		if (mountTask["need"].path ~= nil) then
			if (count >= mountTask["need"].nbNeed) then
				GameTooltip:AddLine(core.L["HaveItem"]..count.."/"..tostring(mountTask["need"].nbNeed).." "..mountTask["need"].itemInfo[1]..".", 0, 1, 0);		
			else
				GameTooltip:AddLine(core.L["HaveItem"]..count.."/"..tostring(mountTask["need"].nbNeed).." "..mountTask["need"].itemInfo[1]..".", 1, 1, 1);
			end
		end
		if (mountTask["need"]["loot"] ~= nil) then
			GameTooltip:AddLine("|cffffffff"..core.L["DroppedBy"].."|r"..printNPCDroppedLoot(mountTask["need"]["loot"]).."|cffffffff.|r", 1, 0.82, 0, true);
		elseif (mountTask["need"]["vendor"] ~= nil) then
			GameTooltip:AddLine("|cffffffff"..core.L["SoldBy"].."|r"..printNPCDroppedLoot(mountTask["need"]["vendor"]).."|cffffffff.|r");
		elseif (mountTask["need"]["create"] ~= nil) then
			GameTooltip:AddLine("|cffffffff"..core.L["CreatedBy"].."|r"..printCompoCreate(mountTask["need"]["create"]).."|cffffffff.|r", 1, 0.82, 0, true);
		end
		if (mountTask["need"].path ~= nil) then
			GameTooltip:AddLine(" ");
			if (mountTask["need"].done == true) then
				GameTooltip:AddLine(core.L["PutNotComplete"], 0.6, 0.6, 0.6);
			else
				GameTooltip:AddLine(core.L["PutComplete"], 0, 1, 0);
			end
		end
	end
	if (mountTask["faction"] ~= nil and mountTask["MA"] == nil) then
		GameTooltip:AddLine(core.libs.changeReputToText(mountTask["faction"]), 1, 1, 1);
	end
	if (mountTask["hf"] ~= nil and GetAchievementNumCriteria(mountTask["hf"][1]) ~= 0) then
		GameTooltip:AddLine(core.libs.printCriteriaHF(mountTask["hf"][1]));
	end
	GameTooltip:Show();
end

local lootOnEnterTaskIconItem = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	if (self.itemLink ~= nil) then
		GameTooltip:SetHyperlink(self.itemLink)
	else
		GameTooltip:AddLine("|cffff0000Loading Error|r");
	end
	GameTooltip:Show();
end

local lootOnEnterIconHelpImp = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	GameTooltip:AddLine("|c00FF7F00"..core.L["HelpImportant"].."|r");
	GameTooltip:AddLine(self.helpForTooltipImp, 1, 1, 1, true);
	GameTooltip:Show();
end

local lootOnEnterIconHelp = function(self, motion)
	GameTooltip:SetOwner(self, "ANCHOR_LEFT", 0, 0);
	GameTooltip:AddLine(core.L["Aide"]);
	GameTooltip:AddLine(self.helpForTooltip, 1, 1, 1, true);
	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 function CreateMountsTab()
	local bgMounts = CreateFrame("Frame", "bgMountsFrame", core.Tab.t2.content, nil);
	bgMounts:SetSize(800,450);
	bgMounts:SetPoint("CENTER", core.Tab.t2.content, "CENTER");
	ListMounts = CreateFrame("Frame", "ListMountsFrame", bgMounts, nil);
	ListMounts:SetSize(245,450);
	ListMounts:SetPoint("LEFT", bgMounts, "LEFT", 1, 0);
	ListMounts:SetBackdrop({bgFile = "Interface/FrameGeneral/UI-Background-Marble",
							edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
							tile = false, edgeSize = 16, 
							insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	ListMounts.title_fond = CreateFrame("Frame", "UIListMountsTitleFrame", ListMounts, nil);
	ListMounts.title_fond:SetSize(ListMounts:GetWidth()+6, 65);
	ListMounts.title_fond:SetPoint("TOP", ListMounts, "TOP", 0, 4);
	ListMounts.title_fond:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/mounts/title_fond",
												tile = false,
												insets = { left = 0, right = 0, top = 0, bottom = 0 }
												});
	ListMounts.title_fond.text = ListMounts.title_fond:CreateFontString("UIListMountsTitleTextFrame", "OVERLAY", ListMounts.title_fond);
	ListMounts.title_fond.text:SetSize(ListMounts.title_fond:GetWidth(), 0);
	if (getNbMountDone() == 0) then
		ListMounts.title_fond.text:SetFontObject("GameFontRedLarge");
	elseif (getNbMountDone() == core.libs.tablelength(mountsDB)) then
		ListMounts.title_fond.text:SetFontObject("GameFontGreenLarge");
	else
		ListMounts.title_fond.text:SetFontObject("GameFontHighlightLarge");
	end
	ListMounts.title_fond.text:SetText(core.L["Mounts"].." "..getNbMountDone().."/"..getNbMaxMount());
	ListMounts.title_fond.text:SetPoint("CENTER", ListMounts.title_fond, "CENTER", 0, 0);
	
	ListMounts.ScrollFrame = CreateFrame("ScrollFrame", nil, ListMounts, "UIPanelScrollFrameTemplate");
	ListMounts.ScrollFrame:SetPoint("TOPLEFT", ListMounts, "TOPLEFT", 0, -50);
	ListMounts.ScrollFrame:SetPoint("BOTTOMRIGHT", ListMounts, "BOTTOMRIGHT", 0, 4);
	ListMounts.ScrollFrame:SetClipsChildren(true);
	ListMounts.ScrollFrame.ScrollBar:ClearAllPoints();
	ListMounts.ScrollFrame.ScrollBar:SetPoint("TOPRIGHT", ListMounts.ScrollFrame, "TOPRIGHT", -4, -20);
	ListMounts.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", ListMounts.ScrollFrame, "BOTTOMRIGHT", -4, 19);
	local child = CreateFrame("Frame", nil, ListMounts.ScrollFrame);
	child:SetSize(220, 396);

	ListMounts.ScrollFrame:SetScrollChild(child);
	ListMounts.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	local PageMount = CreateFrame("Frame", "PageMountFrame", bgMounts, nil);
	PageMount:SetSize(555,450);
	PageMount:SetBackdrop({bgFile = "Interface/TALENTFRAME/spec-paper-bg",
							edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
							tile = false, edgeSize = 16, 
							insets = { left = 0, right = -170, top = 0, bottom = -71 }});
	PageMount:SetPoint("BOTTOMRIGHT", bgMounts, "BOTTOMRIGHT", -2, 0);
	PageMount.ScrollFrame = CreateFrame("ScrollFrame", nil, PageMount, "UIPanelScrollFrameTemplate");
	PageMount.ScrollFrame:SetPoint("TOPLEFT", PageMount, "TOPLEFT", 0, 0);
	PageMount.ScrollFrame:SetPoint("BOTTOMRIGHT", PageMount, "BOTTOMRIGHT", 0, 0);
	PageMount.ScrollFrame:SetClipsChildren(true);
	local childPageMount = CreateFrame("Frame", nil, PageMount.ScrollFrame);
	childPageMount:SetSize(555, 450);
	PageMount.ScrollFrame:SetScrollChild(childPageMount);
	PageMount.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	PageMount.ScrollFrame.text = PageMount.ScrollFrame:CreateFontString(nil, "OVERLAY", PageMount.ScrollFrame);
	PageMount.ScrollFrame.text:SetFontObject("GameFontHighlightLarge");
	PageMount.ScrollFrame.text:SetText(core.L["SelectMountText"]);
	PageMount.ScrollFrame.text:SetPoint("CENTER", PageMount.ScrollFrame, "CENTER");
	
	local function TabDiff_Unselected_all()
		for i=1, core.libs.tablelength(mountsDB) do
			_G["MountBtn"..i]:SetNormalTexture("Interface/AddOns/WoWChallenger/images/mounts/button_mount");
			_G["MountBtn"..i]:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/mounts/button_highlight_mount");
		end
	end
		
	local function TabMount_OnClick(self)
		PlaySound("83", "sfx");
		TabDiff_Unselected_all();
		self:SetNormalTexture("Interface/AddOns/WoWChallenger/images/mounts/button_clicked_mount");
		self:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/mounts/button_highlight_mount");
		self:GetParent().content:Show();
		local scrollChild = PageMount.ScrollFrame:GetScrollChild();
		if (scrollChild) then
			scrollChild:Hide();
		end
		PageMount.ScrollFrame:SetScrollChild(self:GetParent().content);
		self:GetParent().content:Show();
	end
	
	local function CreateButtonMounts(MountInfo, frameParent, PageMount)
		local MountFrame = CreateFrame("Frame", "MountFrame"..MountInfo.id, frameParent);
		MountFrame:SetSize(frameParent:GetWidth(), 45);
		if (MountInfo.id == 1) then
			MountFrame:SetPoint("TOP", frameParent, "TOP", 8, -5);
		else
			MountFrame:SetPoint("BOTTOM", _G["MountFrame"..(MountInfo.id - 1)], "BOTTOM", 0, -50);
		end
		
		MountFrame.button = CreateFrame("Button", "MountBtn"..MountInfo.id, MountFrame);
		MountFrame.button:SetSize(175, 45);
		MountFrame.button:SetPoint("RIGHT", MountFrame, "RIGHT", -5, 0);
		MountFrame.button:SetNormalTexture("Interface/AddOns/WoWChallenger/images/mounts/button_mount");
		MountFrame.button:SetHighlightTexture("Interface/AddOns/WoWChallenger/images/mounts/button_highlight_mount");
		MountFrame.button:SetScript("OnClick", TabMount_OnClick);
		MountFrame.text = MountFrame.button:CreateFontString("MountBtn"..MountInfo.id.."Text", "OVERLAY", MountFrame.button);
		MountFrame.text:SetSize(MountFrame.button:GetWidth()-20, 0);
		if (MountInfo["infoMount"][11] == true) then
			MountFrame.text:SetFontObject("GameFontGreen");
			SetAllDoneNeed(MountInfo.id);
		else
			MountFrame.text:SetFontObject("GameFontNormal");
		end
		MountFrame.text:SetText(MountInfo["infoMount"][1]);
		MountFrame.text:SetJustifyH("LEFT");
		MountFrame.text:SetPoint("LEFT", MountFrame.button, "LEFT", 10, 0);
		
		MountFrame.icon = CreateFrame("Frame", nil, MountFrame);
		MountFrame.icon:SetSize(45, 45);
		MountFrame.icon:SetPoint("LEFT", MountFrame, "LEFT", -5, 0);
		MountFrame.icon:SetBackdrop({bgFile = MountInfo.icon});
		MountFrame.icon.infoMountForTooltip = MountInfo;
		MountFrame.icon.anchor = "ANCHOR_LEFT";
		MountFrame.icon:SetScript("OnEnter", OnEnterBtnMount);
		MountFrame.icon:SetScript("OnLeave", OnLeave);	

		MountFrame.content = CreateFrame("Frame", "MountFrame"..MountInfo.id.."Content", PageMount);
		MountFrame.content:SetSize(555,450);
		MountFrame.content:Hide();
		MountFrame.content:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/bg_step_challenge",
								edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
								tile = false, edgeSize = 16, 
								insets = { left = 3, right = 0, top = 0, bottom = 0 }});
		MountFrame.content.title = MountFrame.content:CreateFontString(nil, "OVERLAY", MountFrame.content);
		if GetLocale() == "ruRU" then
			MountFrame.content.title:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 20, "OUTLINE");
		else
			MountFrame.content.title:SetFont("Fonts\\FRIZQT__.TTF", 20, "OUTLINE");
		end
		MountFrame.content.title:SetText(MountInfo["infoMount"][1]);
		MountFrame.content.title:SetPoint("TOP", MountFrame.content, "TOP", 0, -15);
		
		MountFrame.content.bar_page = CreateFrame("Frame", nil, MountFrame.content);
		MountFrame.content.bar_page:SetSize(548, 55);
		MountFrame.content.bar_page:SetPoint("TOP", MountFrame.content, "TOP", -10, -60);
		MountFrame.content.bar_page:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/mounts/fond_progress_page"});
		local factionPlayer = UnitFactionGroup("player")
		if (factionPlayer ~= MountInfo.faction and MountInfo.faction ~= "Neutral") then
			MountFrame:Hide();
			MountFrame:SetPoint("BOTTOM", _G["MountFrame"..(MountInfo.id - 1)], "BOTTOM", 0, 0);
		end
		
		function PageMount_OnClick_Lock()
			return;
		end
		
		function PageMount_OnClick(self)
			_G["MountFrame"..self.mountId.."ContentPageGoalContent"]:Hide();
			for i = 1, core.libs.tablelength(core.DBmounts[self.mountId].page) do
				_G["MountFrame"..self.mountId.."ContentPage"..i.."Content"]:Hide();
				if (i == 1 or core.libs.ifDoneAllPrevPage(core.DBmounts[self.mountId], i) == true) then
					if (core.libs.ifDonePage(core.DBmounts[self.mountId].page[i]) == true) then
						_G["MountFrame"..self.mountId.."ContentPage"..i]:SetNormalTexture(core.DBmounts[self.mountId].page[i].icon.."_done");
						_G["MountFrame"..self.mountId.."ContentPage"..i]:SetHighlightTexture(core.DBmounts[self.mountId].page[i].icon.."_done");
						_G["MountFrame"..self.mountId.."ContentPage"..i].saveIcon = core.DBmounts[self.mountId].page[i].icon.."_done";
						_G["MountFrame"..self.mountId.."ContentPage"..i].selected = false;
					else
						_G["MountFrame"..self.mountId.."ContentPage"..i]:SetNormalTexture(core.DBmounts[self.mountId].page[i].icon);
						_G["MountFrame"..self.mountId.."ContentPage"..i]:SetHighlightTexture(core.DBmounts[self.mountId].page[i].icon);
						_G["MountFrame"..self.mountId.."ContentPage"..i].saveIcon = core.DBmounts[self.mountId].page[i].icon;
						_G["MountFrame"..self.mountId.."ContentPage"..i].selected = false;
					end
				else
					_G["MountFrame"..self.mountId.."ContentPage"..i]:SetNormalTexture(core.DBmounts[self.mountId].page[i].icon.."_lock");
					_G["MountFrame"..self.mountId.."ContentPage"..i]:SetHighlightTexture(core.DBmounts[self.mountId].page[i].icon.."_lock");
				end
			end
			if (core.DBmounts[self.mountId]["infoMount"][11] == true) then
				_G["MountFrame"..self.mountId.."ContentPageGoal"]:SetNormalTexture(core.DBmounts[self.mountId].icon.."_goal_done");
				_G["MountFrame"..self.mountId.."ContentPageGoal"]:SetHighlightTexture(core.DBmounts[self.mountId].icon.."_goal_done");
				_G["MountFrame"..self.mountId.."ContentPageGoal"].selected = false;
			else
				_G["MountFrame"..self.mountId.."ContentPageGoal"]:SetNormalTexture(core.DBmounts[self.mountId].icon.."_goal");
				_G["MountFrame"..self.mountId.."ContentPageGoal"]:SetHighlightTexture(core.DBmounts[self.mountId].icon.."_goal");
				_G["MountFrame"..self.mountId.."ContentPageGoal"].selected = false;
			end
			PlaySound("681", "sfx");
			self:SetNormalTexture(self.saveIcon.."_selected");
			self:SetHighlightTexture(self.saveIcon.."_selected");
			self.selected = true;
			self.content:Show();
		end
		
		local function CreatePageGoalMount(frameParent)
			local frameParentName = frameParent:GetName();
			frameParent.goal_page = CreateFrame("Button", frameParentName.."PageGoal", frameParent);
			frameParent.goal_page.selected = false;
			frameParent.goal_page.mountId = MountInfo.id;
			frameParent.goal_page:SetSize(58, 58);
			frameParent.goal_page:SetFrameLevel(7);
			frameParent.goal_page:SetPoint("RIGHT", frameParent.bar_page, "RIGHT", -5, -2);
			if (MountInfo["infoMount"][11] == true) then
				frameParent.goal_page:SetNormalTexture(MountInfo.icon.."_goal_done");
				frameParent.goal_page:SetHighlightTexture(MountInfo.icon.."_goal_done");
				frameParent.goal_page.saveIcon = MountInfo.icon.."_goal_done";
			else
				frameParent.goal_page:SetNormalTexture(MountInfo.icon.."_goal");
				frameParent.goal_page:SetHighlightTexture(MountInfo.icon.."_goal");
				frameParent.goal_page.saveIcon = MountInfo.icon.."_goal";
			end
			frameParent.goal_page.infoMountForTooltip = MountInfo;
			frameParent.goal_page.anchor = "ANCHOR_RIGHT";
			frameParent.goal_page:SetScript("OnEnter", OnEnterBtnMount);
			frameParent.goal_page:SetScript("OnLeave", OnLeave);
			frameParent.goal_page:SetScript("OnClick", PageMount_OnClick);
			
			frameParent.goal_page.content = CreateFrame("Frame", frameParentName.."PageGoalContent", frameParent);
			frameParent.goal_page.content:SetSize(544, 333);
			frameParent.goal_page.content:SetPoint("BOTTOM", frameParent, "BOTTOM", 0, 3);
			frameParent.goal_page.content:Hide();
			
			frameParent.goal_page.content.imgEdge = CreateFrame("Frame", "bite", frameParent.goal_page.content);
			frameParent.goal_page.content.imgEdge:SetSize(545, 340);
			frameParent.goal_page.content.imgEdge:SetPoint("BOTTOM", frameParent.goal_page.content, "BOTTOM", -6, 0);
			frameParent.goal_page.content.imgEdge:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/edge_character_pct"});
			frameParent.goal_page.content.imgEdge:SetFrameLevel(7);
			frameParent.goal_page.content.imgEdge.picture = CreateFrame("Frame", nil, frameParent.goal_page.content.imgEdge);
			frameParent.goal_page.content.imgEdge.picture:SetSize(530, 333);
			frameParent.goal_page.content.imgEdge.picture:SetPoint("CENTER", frameParent.goal_page.content.imgEdge, "CENTER", -2, -1);			
			frameParent.goal_page.content.imgEdge.picture:SetBackdrop({bgFile = MountInfo.imgMount});
			frameParent.goal_page.content.imgEdge.picture:SetFrameLevel(6);			
			
			frameParent.goal_page.content.imgEdge.picture.btnPlus = CreateFrame("Button", nil, frameParent.goal_page.content.imgEdge.picture, "UIPanelButtonTemplate");
			frameParent.goal_page.content.imgEdge.picture.btnPlus:SetSize(120, 25);
			frameParent.goal_page.content.imgEdge.picture.btnPlus:SetText(core.L["Next"]);
			frameParent.goal_page.content.imgEdge.picture.btnPlus:SetPoint("BOTTOM", frameParent.goal_page.content.imgEdge.picture, "BOTTOM", 0, 20);
			frameParent.goal_page.content.imgEdge.picture.btnPlus:SetScript("OnClick", function(self)
				PageMount_OnClick(_G["MountFrame"..MountInfo.id.."ContentPage1"]);
			end);
		end
		
		local function CreatePageMount(frameParent, MountPage, PagePrevDone)
			local frameParentName = frameParent:GetName();
			frameParent.page = CreateFrame("Button", frameParentName.."Page"..MountPage.id, frameParent);
			frameParent.page:SetSize(36, 36);
			frameParent.page:SetFrameLevel(7);
			frameParent.page.selected = false;
			frameParent.page.mountId = MountInfo.id;
			if (core.libs.ifDonePage(MountPage) == true) then
				frameParent.page:SetNormalTexture(MountPage.icon.."_done");
				frameParent.page:SetHighlightTexture(MountPage.icon.."_done");
				frameParent.page.saveIcon = MountPage.icon.."_done";
			else
				frameParent.page:SetNormalTexture(MountPage.icon);
				frameParent.page:SetHighlightTexture(MountPage.icon);
				frameParent.page.saveIcon = MountPage.icon;
			end
			local OnEnterPageMount = function(self, motion)
				GameTooltip:SetOwner(self, "ANCHOR_TOP", 0, 3);
				GameTooltip:AddLine(core.L["Chapter"]..MountPage.id);
				GameTooltip:Show();
			end
			if (PagePrevDone == true) then
				frameParent.page:SetScript("OnClick", PageMount_OnClick);
			else
				frameParent.page:SetNormalTexture(MountPage.icon.."_lock");
				frameParent.page:SetHighlightTexture(MountPage.icon.."_lock");
				frameParent.page:SetScript("OnClick", PageMount_OnClick_Lock);
			end
			frameParent.page:SetScript("OnEnter", OnEnterPageMount);
			frameParent.page:SetScript("OnLeave", OnLeave);
			
			frameParent.page.arrrow = CreateFrame("Frame", nil, MountFrame.content);
			frameParent.page.arrrow:SetSize(16, 32);
			frameParent.page.arrrow:SetFrameLevel(7);
			frameParent.page.arrrow:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/mounts/arrowRight"});
			frameParent.page:SetPoint("TOPLEFT", frameParent.bar_page, "TOPLEFT", core.libs.centerListObjectXByWidth((MountPage.id*2)-1, core.libs.tablelength(MountInfo.page)*2, 470, 36)+30, -10);
			frameParent.page.arrrow:SetPoint("TOPLEFT", frameParent.bar_page, "TOPLEFT", core.libs.centerListObjectXByWidth((MountPage.id*2), core.libs.tablelength(MountInfo.page)*2, 470, 36)+35, -13);

			frameParent.page.content = CreateFrame("Frame", frameParentName.."Page"..MountPage.id.."Content", frameParent);
			frameParent.page.content:SetSize(545, 333);
			frameParent.page.content:SetPoint("BOTTOM", frameParent, "BOTTOM", 0, 3);
			frameParent.page.content:Hide();
			
			frameParent.page.content.imgEdge = CreateFrame("Frame", nil, frameParent.page.content);
			frameParent.page.content.imgEdge:SetSize(235, 338);
			frameParent.page.content.imgEdge:SetPoint("BOTTOMLEFT", frameParent.page.content, "BOTTOMLEFT", -2, 0);
			frameParent.page.content.imgEdge:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/edge_character_pct"});
			frameParent.page.content.imgEdge:SetFrameLevel(7);
			frameParent.page.content.imgEdge.picture = CreateFrame("Frame", nil, frameParent.page.content.imgEdge);
			frameParent.page.content.imgEdge.picture:SetSize(227, 332);
			frameParent.page.content.imgEdge.picture:SetPoint("CENTER", frameParent.page.content.imgEdge, "CENTER", -2, -1);
			frameParent.page.content.imgEdge.picture:SetBackdrop({bgFile = MountPage.img});
			frameParent.page.content.imgEdge.picture:SetFrameLevel(6);
			
			frameParent.page.content.ScrollFrame = CreateFrame("ScrollFrame",  nil, frameParent.page.content, "UIPanelScrollFrameTemplate");
			frameParent.page.content.ScrollFrame:SetSize(315, 335);
			frameParent.page.content.ScrollFrame:SetPoint("BOTTOMRIGHT", frameParent.page.content, "BOTTOMRIGHT");
			frameParent.page.content.ScrollFrame:SetClipsChildren(true);
			frameParent.page.content.ScrollFrame.ScrollBar:ClearAllPoints();
			frameParent.page.content.ScrollFrame.ScrollBar:SetPoint("TOPRIGHT", frameParent.page.content.ScrollFrame, "TOPRIGHT", -1, -18);
			frameParent.page.content.ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", frameParent.page.content.ScrollFrame, "BOTTOMRIGHT", -1, 18);
			
			frameParent.page.content.ScrollFrame.child = CreateFrame("Frame", frameParentName.."Page"..MountPage.id.."ContentStep", frameParent.page.content.ScrollFrame);
			frameParent.page.content.ScrollFrame.child:SetSize(315, 335);
			
			frameParent.page.content.ScrollFrame:SetScrollChild(frameParent.page.content.ScrollFrame.child);
			frameParent.page.content.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
			
			local function CreateStepMount(MountStep, frameParent, nbStep, StepPrevDone)
				local frameParentName = frameParent:GetName();
				frameParent.title = CreateFrame("Frame", frameParentName..MountStep.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..MountStep.id.."TitleText", "OVERLAY");
				frameParent.title.text:SetFontObject("GameFontWhite");
				frameParent.title.text:SetText(core.L["Etape"].." "..MountStep.id.."/"..nbStep..core.L["2points"]);
				frameParent.title.text:SetPoint("LEFT", frameParent.title, "LEFT", 10, 0);
				
				frameParent.taskBg = CreateFrame("Frame", frameParentName..MountStep.id.."Content", frameParent.title);
				frameParent.title.iconHelp = CreateFrame("Frame", frameParentName..MountStep.id.."iconHelp", frameParent);
				frameParent.title.iconHelp:SetPoint("RIGHT", frameParent.title, "RIGHT", -5, 0);
				frameParent.title.iconHelp:SetFrameLevel(15);
				frameParent.title.iconHelp:SetSize(20, 20);
				if (MountStep.help ~= nil) then
					frameParent.title.iconHelp.helpForTooltip = MountStep.help;
				elseif (MountStep.helpI ~= nil) then
					frameParent.title.iconHelp.helpForTooltipImp = MountStep.helpI;
				end
				if (StepPrevDone == true) then
					if (core.libs.ifDoneStep(MountStep) == true) then
						frameParent.title.text:SetText(core.L["Etape"].." "..MountStep.id.."/"..nbStep..core.L["2points"]..core.L["Acheve"]);
					end
					frameParent.taskBg:SetSize(252, core.libs.heightBgTask(MountStep.task)+10);
					frameParent.taskBg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/step_task_bg"});
					frameParent.taskBg:SetPoint("TOP", frameParent.title, "BOTTOM", 0, 5);
					if (MountStep.help ~= nil) then
						frameParent.title.iconHelp:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
						frameParent.title.iconHelp:SetScript("OnEnter", lootOnEnterIconHelp);
						frameParent.title.iconHelp:SetScript("OnLeave", OnLeave);
					elseif (MountStep.helpI ~= nil) then
						frameParent.title.iconHelp:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep2"});
						frameParent.title.iconHelp:SetScript("OnEnter", lootOnEnterIconHelpImp);
						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.iconHelp:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep"});
					frameParent.title.iconHelp:SetScript("OnEnter", lootOnEnterIconLock);
					frameParent.title.iconHelp:SetScript("OnLeave", OnLeave);
				end
				
				if (MountStep.id == 1) then
					frameParent.title:SetPoint("TOP", frameParent, "TOP", -8, -10);
				else
					frameParent.title:SetPoint("BOTTOM", _G[frameParentName..(MountStep.id - 1).."Content"], "BOTTOM", 0, -40);
				end
				
				local function CreateTaskMount(MountTask, frameParent, StepPrevDone)
					local frameParentName = frameParent:GetName();
					frameParent.obj = CreateFrame("Button", frameParentName.."Task"..MountTask.id, frameParent);
					if (MountTask.id == 1) then
							frameParent.obj:SetPoint("TOPLEFT", frameParent, "TOPLEFT", 0, -5);
						else
							frameParent.obj:SetPoint("LEFT", _G[frameParentName.."Task"..(MountTask.id - 1)], "LEFT", 0, -40);
					end
					frameParent.obj.text = frameParent.obj:CreateFontString(frameParentName.."Task"..MountTask.id.."Text", "OVERLAY", frameParent.obj);
					frameParent.obj.checkImg = CreateFrame("Frame", frameParentName.."Task"..MountTask.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 = frameParent.obj:GetName();
					frameParent.obj.itemImg = CreateFrame("Frame", frameParentName.."Task"..MountTask.id.."ItemImg", frameParent.obj);
					frameParent.obj.itemImg:SetSize(22, 22);
					frameParent.obj.itemImg:SetPoint("LEFT", frameParent.obj, "LEFT", 25, 0);
					if (MountTask["need"] ~= nil) then
						local _, itemLink = GetItemInfo(MountTask["need"].itemId)
						frameParent.obj.itemImg.itemLink = itemLink;
					end
					if (StepPrevDone == true) then
					frameParent.obj:SetSize(frameParent:GetWidth(), 40);
						if (MountTask["hf"] ~= nil) then
							if (MountTask["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(MountTask["hf"][2]);
						elseif (MountTask["quest"] ~= nil) then
							if (MountTask["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 (MountTask["quest"].title == nil) then
								frameParent.obj.text:SetText("|c007D7D7d"..core.L["QuestNotDis"].."|r");
							else
								frameParent.obj.text:SetText(MountTask["quest"].title);
							end
						elseif (MountTask["need"] ~= nil) then
							frameParent.obj.path = MountTask;
							if (MountTask["need"].done == true) then
								frameParent.obj.checkImg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
								frameParent.obj.itemImg:SetBackdrop({bgFile = MountTask["need"].itemIcon});
								frameParent.obj.itemImg.taskForTooltip = MountTask;
								frameParent.obj.itemImg:SetScript("OnEnter", lootOnEnterTaskIconItem);
								frameParent.obj.itemImg:SetScript("OnLeave", OnLeave);
								local _, itemLink = GetItemInfo(MountTask["need"].itemId)
								frameParent.obj.itemImg.itemLink = itemLink;
								frameParent.obj.text:SetSize(frameParent:GetWidth()-20, 0);
								frameParent.obj.text:SetPoint("LEFT", frameParent.obj.itemImg, "LEFT", 25, 0);
								frameParent.obj.text:SetFontObject("GameFontGreenLarge");
								frameParent.obj.text:SetText("x"..tostring(MountTask["need"].nbNeed)..core.L["Required"]);
								local item = Item:CreateFromItemID(MountTask["need"].itemId)
								item:ContinueOnItemLoad(function()
									MountTask["need"].itemInfo[1] = GetItemInfo(MountTask["need"].itemId);
								end)
							else							
								frameParent.obj.itemImg:SetBackdrop({bgFile = MountTask["need"].itemIcon});
								frameParent.obj.itemImg.taskForTooltip = MountTask;
								frameParent.obj.itemImg:SetScript("OnEnter", lootOnEnterTaskIconItem);
								frameParent.obj.itemImg:SetScript("OnLeave", OnLeave);
								frameParent.obj.text:SetSize(frameParent:GetWidth()-20, 0);
								frameParent.obj.text:SetPoint("LEFT", frameParent.obj.itemImg, "LEFT", 25, 0);
								frameParent.obj.text:SetFontObject("GameFontHighlightLarge");							
								frameParent.obj.text:SetText("x"..tostring(MountTask["need"].nbNeed)..core.L["Required"]);
								local item = Item:CreateFromItemID(MountTask["need"].itemId)
								item:ContinueOnItemLoad(function()
									MountTask["need"].itemInfo[1] = GetItemInfo(MountTask["need"].itemId);
								end)
							end
							frameParent.obj:SetScript("OnClick", OnClickTaskNeed);
						end
						frameParent.obj:SetScript("OnEnter", lootOnEnterTask);
						frameParent.obj:SetScript("OnLeave", OnLeave);
					else
						if (MountTask.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 (MountStep.task ~= nil) then
					for i = 1, core.libs.tablelength(MountStep.task) do
						CreateTaskMount(MountStep.task[i], frameParent.taskBg, StepPrevDone);
					end
				end
			end
			
			if (MountPage.step ~= nil) then
				for i = 1, core.libs.tablelength(MountPage.step) do
					if (i == 1 or core.libs.ifDoneAllPrevStep(MountPage, MountPage.step[i].id) == true) then
						CreateStepMount(MountPage.step[i], frameParent.page.content.ScrollFrame.child, core.libs.tablelength(MountPage.step), true);
					else
						CreateStepMount(MountPage.step[i], frameParent.page.content.ScrollFrame.child, core.libs.tablelength(MountPage.step), false);
					end
				end
			end
		end
		CreatePageGoalMount(MountFrame.content);
		for i = 1, core.libs.tablelength(MountInfo.page) do
			if (i == 1 or core.libs.ifDoneAllPrevPage(MountInfo, i) == true) then
				CreatePageMount(MountFrame.content, MountInfo.page[i], true);
			else
				CreatePageMount(MountFrame.content, MountInfo.page[i], false);
			end
		end
		PageMount_OnClick(_G["MountFrame"..MountInfo.id.."ContentPageGoal"]);
	end
	
	for i = 1, (core.libs.tablelength(mountsDB)) do
		CreateButtonMounts(mountsDB[i], child, PageMount);
	end
	
end
core.MountsTab = CreateMountsTab;

local function UpdateMounts()
	loaded = UIParentLoadAddOn("WoWChallenger");
	if (loaded == nil) then
		print("Error to refresh addon");
	else
		core.Loadmounts();
		mountsDB = core.DBmounts;
		for i = 1, core.libs.tablelength(mountsDB) do
			_G["MountFrame"..i].icon.infoMountForTooltip = mountsDB[i];
			_G["MountFrame"..i.."ContentPageGoal"].infoMountForTooltip = mountsDB[i];
			_G["UIListMountsTitleTextFrame"]:SetText(core.L["Mounts"].." "..getNbMountDone().."/"..getNbMaxMount(mountsDB));
			if (getNbMountDone() == 0) then
				_G["UIListMountsTitleTextFrame"]:SetFontObject("GameFontRedLarge");
			elseif (getNbMountDone() == core.libs.tablelength(mountsDB)) then
				_G["UIListMountsTitleTextFrame"]:SetFontObject("GameFontGreenLarge");
			else
				_G["UIListMountsTitleTextFrame"]:SetFontObject("GameFontHighlightLarge");
			end
			_G["MountBtn"..i.."Text"]:SetFontObject("GameFontNormal");
			if (mountsDB[i]["infoMount"][11] == true) then
				_G["MountBtn"..i.."Text"]:SetFontObject("GameFontGreen");
				SetAllDoneNeed(mountsDB[i].id);
				if (_G["MountFrame"..i.."ContentPageGoal"].selected == true) then
					_G["MountFrame"..i.."ContentPageGoal"]:SetNormalTexture(mountsDB[i].icon.."_goal_done_selected");
					_G["MountFrame"..i.."ContentPageGoal"]:SetHighlightTexture(mountsDB[i].icon.."_goal_done_selected");
				else
					_G["MountFrame"..i.."ContentPageGoal"]:SetNormalTexture(mountsDB[i].icon.."_goal_done");
					_G["MountFrame"..i.."ContentPageGoal"]:SetHighlightTexture(mountsDB[i].icon.."_goal_done");	
				end
				_G["MountFrame"..i.."ContentPageGoal"].saveIcon = mountsDB[i].icon.."_goal_done";
			end
			for i2 = 1, core.libs.tablelength(mountsDB[i].page) do
				if (i2 == 1 or core.libs.ifDoneAllPrevPage(core.DBmounts[i], i2) == true) then
					_G["MountFrame"..i.."ContentPage"..i2]:SetScript("OnClick", PageMount_OnClick);
					if (core.libs.ifDonePage(core.DBmounts[i].page[i2]) == true) then
						if (_G["MountFrame"..i.."ContentPage"..i2].selected == true) then
							_G["MountFrame"..i.."ContentPage"..i2]:SetNormalTexture(mountsDB[i].page[i2].icon.."_done_selected");
							_G["MountFrame"..i.."ContentPage"..i2]:SetHighlightTexture(mountsDB[i].page[i2].icon.."_done_selected");								
						else
							_G["MountFrame"..i.."ContentPage"..i2]:SetNormalTexture(mountsDB[i].page[i2].icon.."_done");
							_G["MountFrame"..i.."ContentPage"..i2]:SetHighlightTexture(mountsDB[i].page[i2].icon.."_done");
						end
					else
						if (_G["MountFrame"..i.."ContentPage"..i2].selected == true) then
							_G["MountFrame"..i.."ContentPage"..i2]:SetNormalTexture(mountsDB[i].page[i2].icon.."_selected");
							_G["MountFrame"..i.."ContentPage"..i2]:SetHighlightTexture(mountsDB[i].page[i2].icon.."_selected");								
						else
							_G["MountFrame"..i.."ContentPage"..i2]:SetNormalTexture(mountsDB[i].page[i2].icon);
							_G["MountFrame"..i.."ContentPage"..i2]:SetHighlightTexture(mountsDB[i].page[i2].icon);
						end
					end
				else
					_G["MountFrame"..i.."ContentPage"..i2]:SetNormalTexture(mountsDB[i].page[i2].icon.."_lock");
					_G["MountFrame"..i.."ContentPage"..i2]:SetHighlightTexture(mountsDB[i].page[i2].icon.."_lock");
					_G["MountFrame"..i.."ContentPage"..i2]:SetScript("OnClick", PageMount_OnClick_Lock);
				end
				for i3 = 1, core.libs.tablelength(mountsDB[i].page[i2].step) do
					if (i3 == 1 or core.libs.ifDoneAllPrevStep(mountsDB[i].page[i2], mountsDB[i].page[i2].step[i3].id) == true) then
						if (core.libs.ifDoneStep(mountsDB[i].page[i2].step[i3]) == true) then
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."TitleText"]:SetText(core.L["Etape"].." "..mountsDB[i].page[i2].step[i3].id.."/"..core.libs.tablelength(mountsDB[i].page[i2].step)..core.L["2points"]..core.L["Acheve"]);
						else
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."TitleText"]:SetText(core.L["Etape"].." "..mountsDB[i].page[i2].step[i3].id.."/"..core.libs.tablelength(mountsDB[i].page[i2].step)..core.L["2points"]);
						end
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."Content"]:SetSize(252, core.libs.heightBgTask(mountsDB[i].page[i2].step[i3].task)+10);
						if (mountsDB[i].page[i2].step[i3].help ~= nil) then
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep"});
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetSize(20, 20);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconHelp);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnLeave", OnLeave);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetPoint("RIGHT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3], "RIGHT", -5, 0);
						elseif (mountsDB[i].page[i2].step[i3].helpI ~= nil) then
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/helpIconStep2"});
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetSize(20, 20);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconHelpImp);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnLeave", OnLeave);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetPoint("RIGHT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3], "RIGHT", -5, 0);
						else
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetBackdrop({bgFile = ""});
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnEnter", OnLeave);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnLeave", OnLeave);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetPoint("RIGHT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3], "RIGHT", -10000, 0);
						end
						for i4 = 1, core.libs.tablelength(mountsDB[i].page[i2].step[i3].task) do
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:SetSize(_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3]:GetWidth(), 40);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetSize(_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."Content"]:GetWidth()-15, 0);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetPoint("LEFT", "MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4, "LEFT", 25, 0);
							if (mountsDB[i].page[i2].step[i3].task[i4]["hf"] ~= nil) then
								if (mountsDB[i].page[i2].step[i3].task[i4]["hf"][4] == true) then
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontGreenLarge");
								else
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontHighlightLarge");
								end
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetText(mountsDB[i].page[i2].step[i3].task[i4]["hf"][2]);
							elseif (mountsDB[i].page[i2].step[i3].task[i4]["quest"] ~= nil) then
								if (mountsDB[i].page[i2].step[i3].task[i4]["quest"].completed == true) then
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontGreenLarge");
								else
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontHighlightLarge");
								end
								if (mountsDB[i].page[i2].step[i3].task[i4]["quest"].title == nil) then
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetText("|c007D7D7d"..core.L["QuestNotDis"].."|r");
								else
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetText(mountsDB[i].page[i2].step[i3].task[i4]["quest"].title);
								end
							elseif (mountsDB[i].page[i2].step[i3].task[i4]["need"] ~= nil) then
								if (mountsDB[i].page[i2].step[i3].task[i4]["need"].done == true) then
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."CheckImg"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/greenCheckButton"});
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontGreenLarge");
								else
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."CheckImg"]:SetBackdrop({bgFile = ""});
									_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetFontObject("GameFontHighlightLarge");
								end
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."ItemImg"]:SetPoint("LEFT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4], "LEFT", 25, 0);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."ItemImg"]:SetBackdrop({bgFile = mountsDB[i].page[i2].step[i3].task[i4]["need"].itemIcon});
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetPoint("LEFT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."ItemImg"], "LEFT", 25, 0);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4].path = mountsDB[i].page[i2].step[i3].task[i4];
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:SetScript("OnClick", OnClickTaskNeed);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetSize(_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:GetWidth()-20, 0);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."Text"]:SetText("x"..tostring(mountsDB[i].page[i2].step[i3].task[i4]["need"].nbNeed)..core.L["Required"]);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."ItemImg"]:SetScript("OnEnter", lootOnEnterTaskIconItem);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4.."ItemImg"]:SetScript("OnLeave", OnLeave);
								_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:SetScript("OnClick", OnClickTaskNeed);
							end
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:SetScript("OnEnter", lootOnEnterTask);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:SetScript("OnLeave", OnLeave);
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..i4]:Show();
						end
					else
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."TitleText"]:SetText(core.L["Etape"].." "..mountsDB[i].page[i2].step[i3].id.."/"..core.libs.tablelength(mountsDB[i].page[i2].step)..core.L["2points"]);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."TitleText"]:SetPoint("LEFT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3], "LEFT", 10, 0);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1Text"]:SetFontObject("GameFontHighlight");
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1Text"]:SetText(core.L["EtapeLock"]);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1Text"]:SetPoint("LEFT", "MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1", "LEFT", 25, 0);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1"]:SetSize(_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3]:GetWidth(), 40);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1"]:SetScript("OnEnter", OnLeave);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1"]:SetScript("OnLeave", OnLeave);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1CheckImg"]:SetBackdrop({bgFile = ""});
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1ItemImg"]:SetBackdrop({bgFile = ""});
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1ItemImg"]:SetScript("OnEnter", OnLeave);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetPoint("RIGHT", _G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3], "RIGHT", -5, 0);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/challenges/lockIconStep"});
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnEnter", lootOnEnterIconLock);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."iconHelp"]:SetScript("OnLeave", OnLeave);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."Content"]:SetSize(252, 50);
						_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask1"]:SetScript("OnClick", PageMount_OnClick_Lock);
						for z = 2, core.libs.tablelength(mountsDB[i].page[i2].step[i3].task) do
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..z]:Hide();
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..z.."ItemImg"]:SetBackdrop({bgFile = ""});
							_G["MountFrame"..i.."ContentPage"..i2.."ContentStep"..i3.."ContentTask"..z]:SetScript("OnClick", PageMount_OnClick_Lock);
						end
					end
				end
			end
		end
	end
end
core.UpdateMounts = UpdateMounts;