--[[
	Quest Keeper Frame

	By sarf & Lash

	This file contains stuff to handle the UI elements.

	Thanks goes to Thott, for building the kick-ass database that we all know, love and abuse!
	
	Credits to vjeux for his nice list of zone names in CartoPlus - we copied it. Hope you don't mind!
	
	CosmosUI URL:
	http://www.cosmosui.org/forums/viewtopic.php?t=
	
   ]]

-- Constants
QUESTKEEPERFRAME_BASE_HEIGHT = 64;
QUESTKEEPERFRAME_TITLEBUTTON_HEIGHT = 18;

QUESTKEEPERFRAME_TITLE_START_X = 19;
QUESTKEEPERFRAME_TITLE_START_Y = -45;
QUESTKEEPERFRAME_TITLE_OFFSET_X = 0;
QUESTKEEPERFRAME_TITLE_OFFSET_Y = 1;

QUESTKEEPERFRAME_QUEST_HEIGHT = 16;

QUESTKEEPERFRAME_MODE_AVAILABLE = 1;
QUESTKEEPERFRAME_MODE_COMPLETED = 2;

QUESTKEEPERFRAME_MODE_MIN = QUESTKEEPERFRAME_MODE_AVAILABLE;
QUESTKEEPERFRAME_MODE_MAX = QUESTKEEPERFRAME_MODE_COMPLETED;

QuestKeeperFrame_CurrentMode = QUESTKEEPERFRAME_MODE_AVAILABLE;

	-- Variables
QuestKeeperFrame_CurrentlyOnlyOneZone = false;
QuestKeeperFrame_CurrentQuests = {};
QuestKeeperFrame_CurrentQuestsOffset = -1;
QuestKeeperFrame_IgnoreZone = true;

QuestKeeperFrame_Cosmos_Registered = 0;



-- executed on load, calls general set-up functions
function QuestKeeperFrame_OnLoad()
	QuestKeeperFrame_Register();
	QuestKeeperFrame_SliderSetup();
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
end

-- registers the mod with Cosmos
function QuestKeeperFrame_Register_Cosmos()
	if ( ( Cosmos_RegisterConfiguration ) and ( QuestKeeperFrame_Cosmos_Registered == 0 ) ) then
		Cosmos_RegisterButton(
			QUESTKEEPERFRAME_BUTTON_TEXT, 
			QUESTKEEPERFRAME_BUTTON_SUBTEXT, 
			QUESTKEEPERFRAME_BUTTON_TIP, 
			"Interface\\Icons\\INV_Misc_Map_01", 
			QuestKeeperFrame_Toggle
		);
		QuestKeeperFrame_Cosmos_Registered = 1;
	end
end

function QuestKeeperFrame_Toggle()
	if ( QuestKeeperFrame:IsVisible() ) then
		QuestKeeperFrame_Hide();
	else
		QuestKeeperFrame_Show();
	end
end

function QuestKeeperFrame_ShouldIgnoreZone()
	return QuestKeeperFrame_IgnoreZone;
end

function QuestKeeperFrame_PageDown()
	local maxButtons = QuestKeeperFrame_GetNumberOfButtons();
	QuestKeeperFrame_ChangeOffset(QuestKeeperFrame_CurrentQuestsOffset + maxButtons);
end

function QuestKeeperFrame_PageUp()
	local maxButtons = QuestKeeperFrame_GetNumberOfButtons();
	QuestKeeperFrame_ChangeOffset(QuestKeeperFrame_CurrentQuestsOffset - maxButtons);
end

function QuestKeeperFrame_ChangeOffset(newOffset)
	local maxButtons = QuestKeeperFrame_GetNumberOfButtons();
	local questMax = QuestKeeperFrame_GetQuestKeeperQuestNumberRelative()- maxButtons;
	local temp = newOffset + maxButtons;
	if ( newOffset < 1 ) then
		QuestKeeperFrame_CurrentQuestsOffset = 1;
	else
		if ( temp < questMax ) then
			questMax = temp;
		end
		QuestKeeperFrame_CurrentQuestsOffset = questMax;
	end
	QuestKeeperFrame_FillButtonsWithQuestInfo();
end

function QuestKeeperFrame_FillButtonsWithQuestInfo()
	if ( QuestKeeperFrame_CurrentQuestsOffset <= 0 ) then
		QuestKeeperFrame_CurrentQuestsOffset = 1;
	end
	local button = nil;
	local questMax = QuestKeeperFrame_GetQuestKeeperQuestNumberRelative();
	local maxButtons = QuestKeeperFrame_GetNumberOfButtons();
	local temp = QuestKeeperFrame_CurrentQuestsOffset + maxButtons;
	if ( temp < questMax ) then
		questMax = temp;
	end
	local buttonIndex = 1;
	local questInfo = nil;
	for i = QuestKeeperFrame_CurrentQuestsOffset, questMax do
		button = getglobal("QuestKeeperTitle"..buttonIndex);
		buttonIndex = buttonIndex + 1;
		questInfo = QuestKeeperFrame_GetQuestKeeperQuestInfoByIndexRelative(i);
		QuestKeeperFrame_FillButtonWithQuestInfo(button, questInfo);
	end
	QuestKeeperFrame_Setup(buttonIndex-1);
end


function QuestKeeperFrame_GetQuestName(questInfo)
	return string.format("[%d] %s", questInfo.level, questInfo.title);
end

QuestKeeperFrame_SelectedID = -1;

function QuestKeeperFrame_GetSelectedID()
	return QuestKeeperFrame_SelectedID;
end

function QuestKeeperFrame_SelectQuest(questInfo)
	if ( not questInfo ) then
		QuestKeeperFrame_SelectedID = -1;
		return true;
	end
	if ( not questInfo.isHeader ) then
		QuestKeeperFrame_SelectedID = questInfo.id;
		QuestKeeperQuestTitle:SetText(questInfo.title);
		QuestKeeperDescriptionText:SetText(QuestKeeper_GetQuestDescription(questInfo));
		return true;
	end
	return false;
end

function QuestKeeperFrame_FillButtonWithQuestInfo(button, questInfo)
	--local normalText = getglobal(button:GetName().."NormalText");
	if ( not button ) then
		QuestKeeperFrame_Print("WE GOT NIL BUTTON!");
		return;
	end
	if ( not questInfo ) then
		QuestKeeperFrame_Print("WE GOT NIL! "..button:GetName());
		return;
	end
	if ( questInfo.isHeader ) then
		if ( not questInfo.isExpanded ) then
			button:SetNormalTexture("Interface\\Buttons\\UI-PlusButton-Up");
		else
			button:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-Up"); 
		end
		getglobal(button:GetName().."Highlight"):SetTexture("Interface\\Buttons\\UI-PlusButton-Hilight");
		button:SetText(questInfo.name);
		local playerLevel = UnitLevel("player");
		-- Place the highlight and lock the highlight state
		if ( QuestKeeperFrame_GetSelectedID() == questInfo.id ) then
			QuestKeeperHighlightFrame:SetPoint("TOPLEFT", button:GetName(), "TOPLEFT", 0, 0);
			QuestKeeperHighlightFrame:Show();
			button:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
			button:LockHighlight();
		else
			button:UnlockHighlight();
		end
	else
		button:SetText("  "..QuestKeeperFrame_GetQuestName(questInfo));
		button:SetNormalTexture("");
		button.questId = questInfo.id;
	end
	local color = nil;
	if ( questInfo.isHeader ) then
		color = QuestDifficultyColor["header"];
	else
		color = GetDifficultyColor(questInfo.level);
	end
	button:SetTextColor(color.r, color.g, color.b);
	button.r = color.r;
	button.g = color.g;
	button.b = color.b;
end

function QuestKeeperFrame_GetQuestKeeperQuestNumber(relative)
	local counter = 0;
	for k, v in QuestKeeperFrame_CurrentQuests do
		if ( ( not relative ) or ( v.isExpanded) ) then
			for key, value in v.quests do
				counter = counter + 1;
			end
		end
		counter = counter + 1;
	end
	return counter;
end

function QuestKeeperFrame_GetQuestKeeperQuestNumberRelative()
	return QuestKeeperFrame_GetQuestKeeperQuestNumber(true);
end

function QuestKeeperFrame_GetQuestKeeperQuestInfoByIndex(index, relative)
	local counter = 0;
	for k, v in QuestKeeperFrame_CurrentQuests do
		counter = counter + 1;
		if ( counter == index ) then
			return v;
		end
		if ( ( not relative ) or ( v.isExpanded ) ) then
			for key, value in v.quests do
				counter = counter + 1;
				if ( counter == index ) then
					return value;
				end
			end
		end
	end
	return nil;
end

function QuestKeeperFrame_GetQuestKeeperQuestInfoByIndexRelative(index)
	return QuestKeeperFrame_GetQuestKeeperQuestInfoByIndex(index, true);
end

function QuestKeeperFrame_QuestComparator(quest1, quest2)
	if ( quest1.level < quest2.level ) then
		return true;
	elseif ( quest1.level > quest2.level ) then
		return false;
	elseif ( quest1.title < quest2.title ) then
		return true;
	else
		return false;
	end
end


function QuestKeeperFrame_RemakeQuestListByZone(questsByZone)
	if ( getn(questsByZone) <= 1 ) then
		QuestKeeperFrame_CurrentlyOnlyOneZone = true;
		for k,v in questsByZone do
			QuestKeeperFrame_CurrentQuests = v;
			break;
		end
	else
		QuestKeeperFrame_CurrentlyOnlyOneZone = false;
	end
	local tempArray = {};
	for k, v in questsByZone do
		local questZone = {};
		questZone.name = QuestKeeper_ZoneList[k];
		questZone.isHeader = true;
		questZone.isExpanded = true;
		questZone.id = k;
		questZone.quests = {};
		for key, value in v do
			value.id = key;
			table.insert(questZone.quests, value);
			--questZone.quests[key] = value;
			--questZone.quests[key].id = key;
		end
		table.sort(questZone.quests, QuestKeeperFrame_QuestComparator);
		table.insert(tempArray, questZone);
		--tempArray[k] = questZone;
	end
	return tempArray;
end

function QuestKeeperFrame_ChangeMode(newMode)
	if ( ( QuestKeeperFrame_CurrentMode ~= newMode ) 
		and ( newMode >= QUESTKEEPERFRAME_MODE_MIN ) 
		and ( newMode <= QUESTKEEPERFRAME_MODE_MAX ) ) then
		QuestKeeperFrame_CurrentMode = newMode;
		QuestKeeperFrame_CurrentQuestsOffset = 1;
		QuestKeeperFrame_UpdateQuests();
	end
end

function QuestKeeperFrame_UpdateQuests()
	if ( QuestKeeperFrame_CurrentMode == QUESTKEEPERFRAME_MODE_AVAILABLE ) then
		QuestKeeperFrame_CurrentQuests = QuestKeeperFrame_RemakeQuestListByZone(QuestKeeper_GetQuestsByZone(QuestKeeper_GetAppropriateQuests(QuestKeeperFrame_ShouldIgnoreZone())));
	elseif ( QuestKeeperFrame_CurrentMode == QUESTKEEPERFRAME_MODE_COMPLETED ) then
		QuestKeeperFrame_CurrentQuests = QuestKeeperFrame_RemakeQuestListByZone(QuestKeeper_GetQuestsByZone(QuestKeeper_GetCompletedQuestsExpanded()));
	end
	QuestKeeperFrame_ChangeOffset(QuestKeeperFrame_CurrentQuestsOffset);
end

function QuestKeeperFrame_Show()
	QuestKeeperFrame_UpdateQuests();
	QuestKeeperFrame:Show();
end

function QuestKeeperFrame_Hide()
	QuestKeeperFrame:Hide();
end

-- registers the mod with the system, integrating it with slash commands and "master" AddOns
function QuestKeeperFrame_Register()
	if ( Cosmos_RegisterConfiguration ) then
		QuestKeeperFrame_Register_Cosmos();
	else
		this:RegisterEvent("VARIABLES_LOADED");
	end
end

function QuestKeeperFrame_Extract_NextParameter(msg)
	local params = msg;
	local command = params;
	local index = strfind(command, " ");
	if ( index ) then
		command = strsub(command, 1, index-1);
		params = strsub(params, index+1);
	else
		params = "";
	end
	return command, params;
end

-- Handles chat - e.g. slashcommands - enabling/disabling the QuestKeeper
function QuestKeeperFrame_Main_ChatCommandHandler(msg)
	
	local func = QuestKeeperFrame_Toggle_Enabled;
	
	local toggleFunc = true;
	
	if ( ( not msg) or ( strlen(msg) <= 0 ) ) then
		QuestKeeperFrame_Print(QUESTKEEPER_CHAT_COMMAND_USAGE);
		return;
	end
	
	local commandName, params = QuestKeeperFrame_Extract_NextParameter(msg);
	
	if ( ( commandName ) and ( strlen(commandName) > 0 ) ) then
		commandName = string.lower(commandName);
	else
		commandName = "";
	end
	
	if ( strfind( commandName, "state" ) ) then
		func = QuestKeeperFrame_Toggle_Enabled;
	else
		QuestKeeperFrame_Print(QUESTKEEPER_CHAT_COMMAND_USAGE);
		return;
	end
	
	if ( toggleFunc ) then
		-- Toggle appropriately
		if ( (string.find(params, 'on')) or ((string.find(params, '1')) and (not string.find(params, '-1')) ) ) then
			func(1);
		else
			if ( (string.find(params, 'off')) or (string.find(params, '0')) ) then
				func(0);
			else
				func(-1);
			end
		end
	else
		func();
	end
end

-- Does things with the hooked function
function QuestKeeperFrame_Hooked_Function()
	if ( QuestKeeperFrame_Enabled == 1 ) then
	end
	QuestKeeperFrame_Saved_Hooked_Function();
end

-- Hooks/unhooks functions. If toggle is 1, hooks functions, otherwise it unhooks functions.
--  Hooking functions mean that you replace them with your own functions and then call the 
--  original function at your leisure.
function QuestKeeperFrame_Setup_Hooks(toggle)
	if ( toggle == 1 ) then
		if ( ( Hooked_Function ~= QuestKeeperFrame_Hooked_Function ) and (QuestKeeperFrame_Saved_Hooked_Function == nil) ) then
			QuestKeeperFrame_Saved_Hooked_Function = Hooked_Function;
			Hooked_Function = QuestKeeperFrame_Hooked_Function;
		end
	else
		if ( Hooked_Function == QuestKeeperFrame_Hooked_Function) then
			Hooked_Function = QuestKeeperFrame_Saved_Hooked_Function;
			QuestKeeperFrame_Saved_Hooked_Function = nil;
		end
	end
end

-- Handles events
function QuestKeeperFrame_OnEvent(event)
	if ( event == "VARIABLES_LOADED" ) then
		if ( QuestKeeperFrame_Cosmos_Registered == 0 ) then
			local value = getglobal("COS_QUESTKEEPER_ENABLED_X");
			if (value == nil ) then
				-- defaults to off
				value = 0;
			end
			--QuestKeeperFrame_Toggle_Enabled(value);
		end
	end
end

-- Toggles the enabled/disabled state of an option and returns the new state
--  if toggle is 1, it's enabled
--  if toggle is 0, it's disabled
--   otherwise, it's toggled
function QuestKeeperFrame_Generic_Toggle(toggle, variableName, CVarName, enableMessage, disableMessage, CosmosVarName)
	local oldvalue = getglobal(variableName);
	local newvalue = toggle;
	if ( ( toggle ~= 1 ) and ( toggle ~= 0 ) ) then
		if (oldvalue == 1) then
			newvalue = 0;
		elseif ( oldvalue == 0 ) then
			newvalue = 1;
		else
			newvalue = 0;
		end
	end
	setglobal(variableName, newvalue);
	setglobal(CVarName, newvalue);
	if ( newvalue ~= oldvalue ) then
		local text = "";
		if ( newvalue == 1 ) then
			if ( enableMessage ) then
				text = TEXT(getglobal(enableMessage));
			end
		else
			if ( disableMessage ) then
				text = TEXT(getglobal(disableMessage));
			end
		end
		if ( text ) and ( strlen(text) > 0 ) then
			QuestKeeperFrame_Print(text);
		end
	end
	QuestKeeperFrame_Register_Cosmos();
	if ( QuestKeeperFrame_Cosmos_Registered == 0 ) then 
		RegisterForSave(variableName);
	else
		QuestKeeperFrame_Generic_CosmosUpdateCheckOnOff(CVarName, newvalue);
		QuestKeeperFrame_Generic_CosmosUpdateCheckOnOff(CosmosVarName, newvalue);
	end
	return newvalue;
end

function QuestKeeperFrame_Generic_CosmosUpdateCheckOnOff(varName, value)
	if ( not Cosmos_UpdateValue ) then
		return;
	end
	local name = varName;
	if ( ( not name ) or ( strlen(name) <= 0 ) ) then
		return
	end
	if ( strfind(name, "_X" ) ) then
		name = strsub(name, 1, strlen(name)-2);
	end
	if ( ( name ) and ( strlen(name) > 0 ) ) then
		Cosmos_UpdateValue(name, CSM_CHECKONOFF, value);
	end
end

function QuestKeeperFrame_Generic_CosmosUpdateValue(varName, value)
	if ( not Cosmos_UpdateValue ) then
		return;
	end
	local name = varName;
	if ( ( not name ) or ( strlen(name) <= 0 ) ) then
		return
	end
	if ( strfind(name, "_X" ) ) then
		name = strsub(name, 1, strlen(name)-2);
	end
	if ( ( name ) and ( strlen(name) > 0 ) ) then
		Cosmos_UpdateValue(name, CSM_SLIDERVALUE, value);
	end
end



-- Toggles the enabled/disabled state of the QuestKeeper
--  if toggle is 1, it's enabled
--  if toggle is 0, it's disabled
--   otherwise, it's toggled
function QuestKeeperFrame_Toggle_Enabled(toggle)
	QuestKeeperFrame_DoToggle_Enabled(toggle, true);
end

-- does the actual toggling
function QuestKeeperFrame_DoToggle_Enabled(toggle, showText)
	local newvalue = 0;
	if ( showText ) then
		newvalue = QuestKeeperFrame_Generic_Toggle(toggle, "QuestKeeperFrame_Enabled", "COS_QUESTKEEPER_ENABLED_X", "QUESTKEEPER_CHAT_ENABLED", "QUESTKEEPER_CHAT_DISABLED");
	else
		newvalue = QuestKeeperFrame_Generic_Toggle(toggle, "QuestKeeperFrame_Enabled", "COS_QUESTKEEPER_ENABLED_X");
	end
	QuestKeeperFrame_Setup_Hooks(newvalue);
end

-- toggling - no text
function QuestKeeperFrame_Toggle_Enabled_NoChat(toggle)
	QuestKeeperFrame_DoToggle_Enabled(toggle, false);
end

-- Prints out text to a chat box.
function QuestKeeperFrame_Print(msg,r,g,b,frame,id,unknown4th)
	if ( Print ) then
		Print(msg, r, g, b, frame, id, unknown4th);
	end
	if(unknown4th) then
		local temp = id;
		id = unknown4th;
		unknown4th = id;
	end
				
	if (not r) then r = 1.0; end
	if (not g) then g = 1.0; end
	if (not b) then b = 1.0; end
	if ( frame ) then 
		frame:AddMessage(msg,r,g,b,id,unknown4th);
	else
		if ( DEFAULT_CHAT_FRAME ) then 
			DEFAULT_CHAT_FRAME:AddMessage(msg, r, g, b,id,unknown4th);
		end
	end
end

function QuestKeeperFrame_GetNumberOfButtons()
	return floor((getglobal("QuestKeeperChooseQuestFrame"):GetHeight() - QUESTKEEPERFRAME_BASE_HEIGHT) / QUESTKEEPERFRAME_TITLEBUTTON_HEIGHT);
end


function QuestKeeperFrame_Setup(numberOfQuestsAndHeadersToDisplay)
	local button = nil;
	local name = "QuestKeeperTitle";
	
	local maxButtons = QuestKeeperFrame_GetNumberOfButtons();
	
	local currentMax = numberOfQuestsAndHeadersToDisplay;
	if ( currentMax > maxButtons ) then
		currentMax = maxButtons;
	end
	
	if ( numberOfQuestsAndHeadersToDisplay > 0 ) then
		button = getglobal(name.."1");
		if ( button ) then
			button:ClearAllPoints();
			button:SetPoint("TOPLEFT", "QuestKeeperChooseQuestFrame", "TOPLEFT", QUESTKEEPERFRAME_TITLE_START_X, QUESTKEEPERFRAME_TITLE_START_Y);
			button:SetID(1);
			button:Show();
		end
	end
	
	for i = 2, currentMax do
		button = getglobal(name..i);
		if ( button ) then
			button:ClearAllPoints();
			button:SetPoint("TOPLEFT", name..(i-1), "BOTTOMLEFT", QUESTKEEPERFRAME_TITLE_OFFSET_X, QUESTKEEPERFRAME_TITLE_OFFSET_Y);
			button:SetID(i);
			button:Show();
		end
	end
	
	if ( maxButtons > currentMax ) then
		for i = currentMax+1, maxButtons do
			button = getglobal(name..i);
			if ( button ) then
				button:Hide();
			end
		end
	end
end


function QuestKeeperListScrollFrame_OnVerticalScroll()
	--FauxScrollFrame_OnVerticalScroll(QUESTKEEPERFRAME_QUEST_HEIGHT, QuestKeeperFrame_Update);
end


function QuestKeeperTitleButton_OnLoad()
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
end

function QuestKeeperTitleButton_OnClick(button)
	if ( button == "LeftButton" ) then
		if ( IsShiftKeyDown() and ChatFrameEditBox:IsVisible() ) then
			ChatFrameEditBox:Insert(this:GetText());
		end
		local questInfo = QuestKeeperFrame_GetQuestKeeperQuestInfoByIndexRelative(QuestKeeperFrame_CurrentQuestsOffset-1+this:GetID());
		if ( questInfo.isHeader ) then
			if ( questInfo.isExpanded ) then
				questInfo.isExpanded = false;
			else
				questInfo.isExpanded = true;
			end
			QuestKeeperFrame_ChangeOffset(QuestKeeperFrame_CurrentQuestsOffset);
		else
			QuestKeeperFrame_SelectQuest(questInfo);
		end
	end
end

function QuestKeeperFrame_GetQuestKeeperQuestInfoByQuestId(questId)
	for k, v in QuestKeeperFrame_CurrentQuests do
		if ( ( not relative ) or ( v.isExpanded ) ) then
			for key, value in v.quests do
				if ( value.id == questId ) then
					return value;
				end
			end
		end
	end
	return nil;
end

function QuestKeeperFrame_FinishButton_OnClick()
	local questId = QuestKeeperFrame_GetSelectedID();
	local questInfo = QuestKeeperFrame_GetQuestKeeperQuestInfoByQuestId(questId);
	if ( questInfo ) then
		QuestKeeper_AddCompletedQuest(UnitName("player"), questId, questInfo);
		QuestKeeperFrame_UpdateQuests();
	end
end


function QuestKeeperFrame_SliderChanged(value)
	this.sliderValue = value;
	QuestKeeper_SetRelativeLevel(value);
	QuestKeeperFrame_SliderSetup();
	QuestKeeperFrame_UpdateQuests();
end

function QuestKeeperFrame_IgnoreZoneCheckBox_OnClick()
	local checked = QuestKeeperFrameIgnoreZoneCheckbox:GetChecked();
	if ( checked ) then
		QuestKeeperFrame_IgnoreZone = true;
	else
		QuestKeeperFrame_IgnoreZone = false;
	end
	QuestKeeperFrame_UpdateQuests();
end

function QuestKeeperFrame_ToggleIgnoreZone()
	if ( QuestKeeperFrame_IgnoreZone ) then
		QuestKeeperFrame_IgnoreZone = false;
	else
		QuestKeeperFrame_IgnoreZone = true;
	end
	QuestKeeperFrame_UpdateQuests();
end

function QuestKeeperFrame_SliderSetup()
	local slider = getglobal ("QuestKeeperFrame".."Slider");
	local slidertext = getglobal("QuestKeeperFrame".."Slider".."Text");
	local slidervaluetext = getglobal("QuestKeeperFrame".."Slider".."ValueText");
	
	local currentValue = this.sliderValue;
	if ( not currentValue ) then
		currentValue = UnitLevel("player");
	end

	valuetext = "Level: "..currentValue;

	slidervaluetext:SetText ( valuetext );
	slidervaluetext:Show();
	slidertext:SetText ( "Base level" );
	slidertext:Show();
	slider:SetMinMaxValues( 1, 60 );
	slider:SetValueStep( 1 );
	slider:SetValue( currentValue );
	slider:Show();
end

function QuestKeeperTitleButton_GetTooltipName()
	--return "QuestKeeperFrameQuestTooltip";
	return "GameTooltip";
end

function QuestKeeperTitleButton_OnEnter()
	local questArray = QuestKeeper_GetQuestInfoTooltipArray(QuestKeeperFrame_GetQuestKeeperQuestInfoByIndexRelative(QuestKeeperFrame_CurrentQuestsOffset+this:GetID()-1));
	if ( not questArray ) then
		Print("Nop Quest ARray !");
	elseif ( questArray.isHeader ) then
		Print("Quest ARray headur!");
	end
	local tooltipName = QuestKeeperTitleButton_GetTooltipName();
	local tooltip = getglobal(tooltipName);
	if ( ( questArray ) and ( not questArray.isHeader) ) then
		tooltip:SetOwner(this, "ANCHOR_TOPRIGHT");
		local uiText = getglobal(tooltipName.."TextLeft1");
		uiText:SetText(questArray.title);
		uiText:Show();
		uiText = getglobal(tooltipName.."TextLeft3");
		if ( ( questArray.text ) and (strlen(questArray.text) > 0 ) ) then
			uiText:SetText(questArray.text);
			uiText:Show();
		else
			uiText:Hide();
		end
		tooltip:Show();
	end
end

function QuestKeeperTitleButton_OnLeave()
	local tooltipName = QuestKeeperTitleButton_GetTooltipName();
	local tooltip = getglobal(tooltipName);
	if ( tooltip:IsOwned(this) ) then
		tooltip:Hide();
	end
end
