function InstanceInfo_SaveMyInstancesInfo()
	if (UnitLevel("player") < tonumber(InstanceInfoDB.Options.MinLevel)) then
		return;
	end
	if (GetTime()-InstanceInfoFrame.vTimeSinceLastUpdate) < 5 then
		return;
	end
	InstanceInfoFrame.vTimeSinceLastUpdate=GetTime();
	local player=UnitName("player");
	if not InstanceInfoDB.Info[player] then
		InstanceInfoDB.Info[player] = { };
	end
	InstanceInfoDB.Info[player].player=player;
	InstanceInfoDB.Info[player].uClass, InstanceInfoDB.Info[player].engClass=UnitClass("player");
	InstanceInfoDB.Info[player].server=GetRealmName("Player");
	InstanceInfoDB.Info[player].level=UnitLevel("player");
	InstanceInfoDB.Info[player].curtime=time();

	InstanceInfoDB.Info[player].instances={ };
	local numInstances = GetNumSavedInstances();
	for i = 1, numInstances do
		local instance = { };
		local fade = { };
		instance.name, instance.ID, instance.remaining, instance.difficulty, fade.locked, fade.extended, fade.instanceIDMostSig, fade.isRaid, instance.maxPlayers, instance.dName= GetSavedInstanceInfo(i);
		table.insert(InstanceInfoDB.Info[player].instances,instance);
	end
	table.sort(InstanceInfoDB.Info[player].instances, function(a,b) if a.remaining ~= b.remaining then return a.remaining < b.remaining; end return a.name < b.name; end);
	table.sort(InstanceInfoDB.Info[player], function(a,b) if a.server ~= b.server then return a.server < b.server; end return a.player < b.player; end);

	local e=0;
	local n = table.getn(InstanceInfoDB.Info[player].instances);
	for i = 1, n do
		if InstanceInfoDB.Info[player].instances[i].remaining == 0 then
		e = e + 1;
		end
	end
	InstanceInfoDB.Info[player].rNum=n;
	InstanceInfoDB.Info[player].eNum=e;
	InstanceInfo_setFrameTitle()
	return true;
end

function InstanceInfo_showPlayerInstancesInfo(player)
	if not InstanceInfoDB.Info[player] then
		return;
	end
	local info=InstanceInfoDB.Info[player];
	if (info.level < tonumber(InstanceInfoDB.Options.MinLevel)) then
		return;
	end
	GameTooltip:AddLine("|cff00ff00["..info.server.."] |cff"..InstanceInfo_getClassHEXColor(info.engClass)..info.player.." - "..info.level.."레벨|r");
	local nMax=table.getn(info.instances);
	for i = 1, nMax do
	local instance=info.instances[i];
	if time() < (info.curtime+instance.remaining) then
		GameTooltip:AddDoubleLine(" - "..instance.name.."  ["..instance.dName.."]", SecondsToTime((info.curtime+instance.remaining)-time()).."    (ID:"..instance.ID..")");
	else
	if InstanceInfoDB.Options.show_need_to_login then
		GameTooltip:AddDoubleLine(" - "..instance.name.."  ["..instance.dName.."]", L_NEED_TO_LOGIN, 1, 0, 0);
	end
	end
	end
end

function InstanceInfo_ShowMyInstancesInfo()
	local player=UnitName("player");
	GameTooltip:AddLine(L_MODE_TITLE..L_MODE_SHOW_MY_INSTANCE_INFO, 1,1,1);
	GameTooltip:AddLine("", 1,1,1);
	return InstanceInfo_showPlayerInstancesInfo(player);
end

function InstanceInfo_ShowAllPlayersInstancesInfo()
	GameTooltip:AddLine(L_MODE_TITLE..L_MODE_SHOW_INSTANCES_INFO_OF_ALL_PLAYERS, 1,1,1);
	GameTooltip:AddLine("", 1,1,1);
	for player, playerinfo in pairs(InstanceInfoDB.Info) do
	if (player ~= nil and playerinfo ~= {}) then
		InstanceInfo_showPlayerInstancesInfo(player);
	end
	end
end

function InstanceInfo_showPlayerInfoForFavoriteInstance(favoriteInstance)
	local secs_per_day=24*60*60;
	local showFlag=nil

	for player, info in pairs(InstanceInfoDB.Info) do
		local i;
		local bFound=false;
		local nMax=table.getn(info.instances);
		local n;

		for i = 1, nMax do
			local instance=info.instances[i];
			if ((instance.name == favoriteInstance.name) and (instance.difficulty == favoriteInstance.difficulty)) then

				if time() < (info.curtime+instance.remaining) then
					if (showFlag==nil) then
						GameTooltip:AddDoubleLine(" ", " ");
						GameTooltip:AddDoubleLine(favoriteInstance.name.." ["..favoriteInstance.dn.."]", SecondsToTime(favoriteInstance.resetTime-time()));
						showFlag="SHOW"
					end

					GameTooltip:AddDoubleLine(" - |cff00ff00["..info.server.."] |cff"..InstanceInfo_getClassHEXColor(info.engClass)..player, "(ID:"..instance.ID..")");
--				else
--					if InstanceInfoDB.Options.show_need_to_login then
--						GameTooltip:AddDoubleLine(" - |cff00ff00["..info.server.."~~] |cff"..InstanceInfo_getClassHEXColor(info.engClass)..player, L_NEED_TO_LOGIN, 1, 0, 0);
--					end
				end
--				bFound=true;
				break;
			end
		end
--		if bFound == false then
--			if InstanceInfoDB.Options.show_need_to_login then
--				if info.curtime < (favoriteInstance.resetTime-favoriteInstance.duration*secs_per_day) then
--					GameTooltip:AddDoubleLine(" - |cff00ff00["..info.server.."--] |cff"..InstanceInfo_getClassHEXColor(info.engClass)..player, L_NEED_TO_LOGIN, 1, 0, 0);
--				end
--			end
--		end
	end
end

function InstanceInfo_ShowFavoriteInstancesInfo()
	GameTooltip:AddLine(L_MODE_TITLE..L_MODE_SHOW_FAVORITE_INSTANCES_INFO, 1,1,1);
	GameTooltip:AddLine("", 1,1,1);
	local favoriteInstances=InstanceInfo_getFavoriteInstances();
	local idx;
	local nMax=table.getn(favoriteInstances);
	for idx = 1, nMax do
	local favoriteInstance=favoriteInstances[idx];
	InstanceInfo_showPlayerInfoForFavoriteInstance(favoriteInstance);
	end
end