function InstanceInfo_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED")
	this:RegisterEvent("PLAYER_LEAVING_WORLD")
	this:RegisterEvent("UPDATE_INSTANCE_INFO")
	this:RegisterEvent("ZONE_CHANGED")
	this:RegisterEvent("ZONE_CHANGED_NEW_AREA")
	this:RegisterEvent("ZONE_CHANGED_INDOORS")
	InstanceInfoFrame.vTimeSinceLastUpdate = 0;
	if not InstanceInfoDB then
		InstanceInfoDB = { };
	end
	if not InstanceInfoDB.Options then
		InstanceInfoDB.Options = {
		MinLevel = 80,
		show_my_info=true,
		show_all_player_info=false,
		show_instance_info=false,
		show_need_to_login=true,
	};  
	end
	if not InstanceInfoDB.Info then
		InstanceInfoDB.Info = { };
	end
	if not InstanceInfoDB.FrameInfo then
		InstanceInfoDB.FrameInfo = { };
	end
	InstanceInfoFrame:Hide();
end

function InstanceInfo_OnEvent(event)
	if (event == "VARIABLES_LOADED") or (event == "PLAYER_LEAVING_WORLD") or (event == "UPDATE_INSTANCE_INFO") or (event == "ZONE_CHANGED") or (event == "ZONE_CHANGED_NEW_AREA") or (event == "ZONE_CHANGED_INDOORS") then
		InstanceInfo_SaveMyInstancesInfo()
	end
	if (event == "VARIABLES_LOADED") then
		local player=UnitName("player");
		InstanceInfoFrame:Show();
		InstanceInfoFrame:ClearAllPoints();
		if not InstanceInfoDB.FrameInfo[player] then
			InstanceInfoDB.FrameInfo[player] = { };
			InstanceInfoDB.FrameInfo[player].frame_pos_x=300;
			InstanceInfoDB.FrameInfo[player].frame_pos_y=300;
		end
		InstanceInfoFrame:SetPoint("CENTER", "UIParent", "BOTTOMLEFT", InstanceInfoDB.FrameInfo[player].frame_pos_x, InstanceInfoDB.FrameInfo[player].frame_pos_y);
		InstanceInfo_setFrameTitle()
	end
end

function InstanceInfo_ClickHandler(btn, updown)
	local isAlt = IsAltKeyDown();
	local isShift = IsShiftKeyDown();
	local isControl = IsControlKeyDown();
	if (btn == "LeftButton") then
		if (InstanceInfoFrame.moving) then
			if (updown == "CLICK") then
				local player=UnitName("player");
				InstanceInfoFrame.moving = nil;
				InstanceInfoFrame:StopMovingOrSizing();
				InstanceInfoDB.FrameInfo[player].frame_pos_x, InstanceInfoDB.FrameInfo[player].frame_pos_y = InstanceInfoFrame:GetCenter();
				InstanceInfoFrame:SetMovable(0);
			end
		else
			if (isShift and updown == "DOWN") then
				InstanceInfoFrame.moving = true;
				InstanceInfoFrame:SetMovable(1);
				InstanceInfoFrame:StartMoving();
			elseif (isAlt and updown == "CLICK") then
				DEFAULT_CHAT_FRAME:AddMessage(L_CHATFRAME_HELP_TEXT0);
				DEFAULT_CHAT_FRAME:AddMessage(L_CHATFRAME_HELP_TEXT1);
				DEFAULT_CHAT_FRAME:AddMessage(L_CHATFRAME_HELP_TEXT2);
				DEFAULT_CHAT_FRAME:AddMessage(L_CHATFRAME_HELP_TEXT3);
				DEFAULT_CHAT_FRAME:AddMessage(L_CHATFRAME_HELP_TEXT4);
			end
		end
	elseif (btn == "RightButton") then
		if (isShift and updown == "CLICK") then
			if InstanceInfoDB.Options.show_instance_info then
				InstanceInfoDB.Options.show_my_info = true;
				InstanceInfoDB.Options.show_all_player_info = false;
				InstanceInfoDB.Options.show_instance_info = false;
				DEFAULT_CHAT_FRAME:AddMessage(L_MODE_TITLE..L_MODE_SHOW_MY_INSTANCE_INFO);
			elseif InstanceInfoDB.Options.show_my_info then
				InstanceInfoDB.Options.show_my_info = false;
				InstanceInfoDB.Options.show_all_player_info = true;
				InstanceInfoDB.Options.show_instance_info = false;
				DEFAULT_CHAT_FRAME:AddMessage(L_MODE_TITLE..L_MODE_SHOW_INSTANCES_INFO_OF_ALL_PLAYERS);
			elseif InstanceInfoDB.Options.show_all_player_info then
				InstanceInfoDB.Options.show_my_info = false;
				InstanceInfoDB.Options.show_all_player_info = false;
				InstanceInfoDB.Options.show_instance_info = true;
				DEFAULT_CHAT_FRAME:AddMessage(L_MODE_TITLE..L_MODE_SHOW_FAVORITE_INSTANCES_INFO);
			end
			InstanceInfo_SaveMyInstancesInfo()
			InstanceInfo_setFrameTitle()
			InstanceInfo_ShowTooltip()
		elseif (isControl and updown == "CLICK") then
			InstanceInfoDB.Options.show_need_to_login = not InstanceInfoDB.Options.show_need_to_login;
			InstanceInfo_SaveMyInstancesInfo()
			InstanceInfo_setFrameTitle()
			InstanceInfo_ShowTooltip()
		elseif (isAlt and updown == "CLICK") then
			ToggleFriendsFrame(5);
			InstanceInfo_SaveMyInstancesInfo()
			InstanceInfo_setFrameTitle()
			InstanceInfo_ShowTooltip()
		end
	end
end

function InstanceInfo_ShowTooltip()
	GameTooltip:Hide();
	InstanceInfo_SaveMyInstancesInfo()
	GameTooltip:SetOwner(InstanceInfoFrame, "ANCHOR_NONE");
	GameTooltip:SetPoint("TOP", "InstanceInfoFrame", "TOP", 0, -32);
	if InstanceInfoDB.Options.show_my_info then 
		InstanceInfo_ShowMyInstancesInfo()
	elseif InstanceInfoDB.Options.show_all_player_info then 
		InstanceInfo_ShowAllPlayersInstancesInfo()
	elseif InstanceInfoDB.Options.show_instance_info then
		InstanceInfo_ShowFavoriteInstancesInfo()
	end
	GameTooltip:AddLine("", 1,1,1);
	GameTooltip:AddLine(L_TOOLTIP_HELP_TEXT1, 0.5, 0.5, 0.5);
	GameTooltip:Show();
end

function InstanceInfo_setFrameTitle()
	if not InstanceInfoDB.Info or not InstanceInfoDB.Info[UnitName("player")] then
		numeInstances=0;
		numInstances=GetNumSavedInstances();
	else
		numrInstances=InstanceInfoDB.Info[UnitName("player")].rNum;
		numeInstances=InstanceInfoDB.Info[UnitName("player")].eNum;
		numInstances=numrInstances - numeInstances;
	end
	local strText="";
	if numInstances <= 0 then
		strText="|cff00ff00"..L_NO_SAVED_INSTANCE.."|r";
	else
		strText="|cff00ff00"..L_SAVED_INSTANCE_COUNT.."|r"..numInstances.." ";
	end
	if InstanceInfoDB.Options.show_need_to_login then
		strText=strText.." |c00888888"..L_ABB_FOR_SHOW_NEED_TO_LOGIN..numeInstances.."|r";
	end
	InstanceInfoTitle:SetText(strText)
end

function InstanceInfo_getClassHEXColor(engClass)
	local r, g, b = InstanceInfo_getClassRGBColor(engClass)
	return string.format("%02x%02x%02x", r * 255, g * 255, b * 255)
end

function InstanceInfo_getClassRGBColor(engClass)
	if RAID_CLASS_COLORS and RAID_CLASS_COLORS[engClass] then
		return RAID_CLASS_COLORS[engClass].r, RAID_CLASS_COLORS[engClass].g, RAID_CLASS_COLORS[engClass].b
	end
	return 0.63, 0.63, 0.63
end

