@@ -2,34 +2,42 @@
 -- PVE FRAME
 ---------------------------------------------------------------
 local panels = {
-	[1] = { name = "GroupFinderFrame", addon = nil },
-	[2] = { name = "ChallengesFrame", addon = "Blizzard_ChallengesUI" },
+	{ name = "GroupFinderFrame", addon = nil },
+	{ name = "PVPUIFrame", addon = "Blizzard_PVPUI" },
+	{ name = "ChallengesFrame", addon = "Blizzard_ChallengesUI" },
 }
 
 function PVEFrame_OnLoad(self)
 	RaiseFrameLevel(self.shadows);
-	PanelTemplates_SetNumTabs(self, 2);
+	PanelTemplates_SetNumTabs(self, #panels);
+	
+	self.maxTabWidth = (self:GetWidth() - 19) / #panels;
 end
 
 function PVEFrame_ToggleFrame(sidePanelName, selection)
-	if ( UnitLevel("player") < SHOW_LFD_LEVEL ) then
+	if ( UnitLevel("player") < math.min(SHOW_LFD_LEVEL,SHOW_PVP_LEVEL) ) then
 		return;
 	end
 	local self = PVEFrame;
 	if ( self:IsShown() ) then
 		if ( sidePanelName ) then
 			local sidePanel = _G[sidePanelName];
-			if ( sidePanel and sidePanel:IsShown() and sidePanel:getSelection() == selection ) then
-				HideUIPanel(self);
-			else
-				PVEFrame_ShowFrame(sidePanelName, selection);
+			if ( sidePanel ) then
+				--We know the panel is loaded, so try to dereference the selection
+				if ( type(selection) == "string" ) then
+					selection = _G[selection];
+				end
+				if ( sidePanel:IsShown() and (not selection or not sidePanel.getSelection or sidePanel:getSelection() == selection) ) then
+					HideUIPanel(self);
+					return;
+				end
 			end
 		else
 			HideUIPanel(self);
+			return;
 		end
-	else
-		PVEFrame_ShowFrame(sidePanelName, selection);
 	end
+	PVEFrame_ShowFrame(sidePanelName, selection);
 end
 
 function PVEFrame_ShowFrame(sidePanelName, selection)
@@ -60,6 +68,12 @@ function PVEFrame_ShowFrame(sidePanelNam
 		UIParentLoadAddOn(panels[tabIndex].addon);
 		panels[tabIndex].addon = nil;
 	end
+
+	-- we've loaded the AddOn, so try to dereference the selection if needed
+	if ( type(selection) == "string" ) then
+		selection = _G[selection];
+	end
+
 	-- show it
 	ShowUIPanel(self);
 	self.activeTabIndex = tabIndex;	
@@ -68,7 +82,9 @@ function PVEFrame_ShowFrame(sidePanelNam
 		local panel = _G[data.name];
 		if ( index == tabIndex ) then
 			panel:Show();
-			panel:update(selection);			
+			if( panel.update ) then
+				panel:update(selection);
+			end
 		elseif ( panel ) then
 			panel:Hide();
 		end
@@ -98,45 +114,86 @@ end
 ---------------------------------------------------------------
 
 SCENARIOS_SHOW_LEVEL = 85;
+SCENARIOS_HIDE_ABOVE_LEVEL = 90;
 RAID_FINDER_SHOW_LEVEL = 85;
-FLEX_RAID_SHOW_LEVEL = 90;
 
-local groupFrames = { "LFDParentFrame", "ScenarioFinderFrame", "RaidFinderFrame", "FlexRaidFrame" }
+local groupFrames = { "LFDParentFrame", "ScenarioFinderFrame", "RaidFinderFrame", "LFGListPVEStub" }
 
 function GroupFinderFrame_OnLoad(self)
 	SetPortraitToTexture(self.groupButton1.icon, "Interface\\Icons\\INV_Helmet_08");
 	self.groupButton1.name:SetText(LOOKING_FOR_DUNGEON_PVEFRAME);
+	SetPortraitToTexture(self.groupButton2.icon, "Interface\\Icons\\Icon_Scenarios");
+	self.groupButton2.name:SetText(SCENARIOS_PVEFRAME);
 	SetPortraitToTexture(self.groupButton3.icon, "Interface\\LFGFrame\\UI-LFR-PORTRAIT");
 	self.groupButton3.name:SetText(RAID_FINDER_PVEFRAME);
 	SetPortraitToTexture(self.groupButton4.icon, "Interface\\Icons\\Achievement_General_StayClassy");
-	self.groupButton4.name:SetText(FLEX_RAID);
-	SetPortraitToTexture(self.groupButton2.icon, "Interface\\Icons\\Icon_Scenarios");
-	self.groupButton2.name:SetText(SCENARIOS_PVEFRAME);
-	-- disable
-	if ( UnitLevel("player") < SCENARIOS_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton2, false);
-		self.groupButton2.tooltip = format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, SCENARIOS_SHOW_LEVEL);
-		GroupFinderFrame:SetScript("OnEvent", GroupFinderFrame_OnEvent);
-		GroupFinderFrame:RegisterEvent("PLAYER_LEVEL_UP");
-	end
-	if ( UnitLevel("player") < RAID_FINDER_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton3, false);
-		self.groupButton3.tooltip = format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, RAID_FINDER_SHOW_LEVEL);
-		GroupFinderFrame:SetScript("OnEvent", GroupFinderFrame_OnEvent);
-		GroupFinderFrame:RegisterEvent("PLAYER_LEVEL_UP");
-	end
-	if ( UnitLevel("player") < FLEX_RAID_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton4, false);
-		self.groupButton4.tooltip = format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, FLEX_RAID_SHOW_LEVEL);
-		GroupFinderFrame:SetScript("OnEvent", GroupFinderFrame_OnEvent);
-		GroupFinderFrame:RegisterEvent("PLAYER_LEVEL_UP");
-	end
+	self.groupButton4.name:SetText(LFGLIST_NAME);
+	
+	GroupFinderFrame_EvaluateButtonVisibility(self, UnitLevel("player"));
+	
+	self:RegisterEvent("PLAYER_LEVEL_UP");
+
+	GroupFinderFrameButton_SetEnabled(self.groupButton4, true);
+
 	-- set up accessors
 	self.getSelection = GroupFinderFrame_GetSelection;
 	self.update = GroupFinderFrame_Update;
 end
 
+function GroupFinderFrame_EvaluateButtonVisibility(self, level)
+	if ( level > SCENARIOS_HIDE_ABOVE_LEVEL ) then
+		self.groupButton2:Hide()
+		
+		if ( GroupFinderFrame_GetSelectedIndex(self) == self.groupButton2:GetID() ) then
+			-- Deselect this now hidden tab if it happened to be selected
+			self.selection = nil
+			GroupFinderFrame_ShowGroupFrame(nil)
+		end
+	else
+		if ( level < SCENARIOS_SHOW_LEVEL ) then
+			GroupFinderFrameButton_SetEnabled(self.groupButton2, false);
+			self.groupButton2.tooltip = self.groupButton2.tooltip or format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, SCENARIOS_SHOW_LEVEL);
+		else
+			GroupFinderFrameButton_SetEnabled(self.groupButton2, true);
+			self.groupButton2.tooltip = nil
+		end
+		
+		self.groupButton2:Show()
+	end
+
+	if ( level < SHOW_LFD_LEVEL ) then
+		GroupFinderFrameButton_SetEnabled(self.groupButton1, false);
+		self.groupButton1.tooltip = self.groupButton3.tooltip or format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, SHOW_LFD_LEVEL);
+	else
+		self.groupButton1.tooltip = nil;
+		GroupFinderFrameButton_SetEnabled(self.groupButton1, true);
+	end
+	
+	if ( level < RAID_FINDER_SHOW_LEVEL ) then
+		GroupFinderFrameButton_SetEnabled(self.groupButton3, false);
+		self.groupButton3.tooltip = self.groupButton3.tooltip or format(FEATURE_BECOMES_AVAILABLE_AT_LEVEL, RAID_FINDER_SHOW_LEVEL);
+	else
+		self.groupButton3.tooltip = nil;
+		GroupFinderFrameButton_SetEnabled(self.groupButton3, true);
+	end
+	
+	GroupFinderFrame_UpdateButtonAnchors(self);
+end
+
+function GroupFinderFrame_UpdateButtonAnchors(self)
+	local moveDown = not self.groupButton2:IsShown();
+	local spacing =  moveDown and -30 or -23
+	local button3RelativeTo = moveDown and self.groupButton1 or self.groupButton2
+	self.groupButton3:SetPoint("TOP", button3RelativeTo, "BOTTOM", 0, spacing);
+	self.groupButton4:SetPoint("TOP", self.groupButton3, "BOTTOM", 0, spacing);
+	self.groupButton1:SetPoint("TOPLEFT", self, "TOPLEFT", 10, moveDown and -101 or -70);
+end
+
 function GroupFinderFrameButton_SetEnabled(button, enabled)
+	if ( button:IsEnabled() == enabled ) then
+		return
+	end
+	
 	if ( enabled ) then
 		button.bg:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
 		button.name:SetFontObject("GameFontNormalLarge");
@@ -151,40 +208,17 @@ end
 
 function GroupFinderFrame_OnEvent(self, event, ...)
 	local level = ...;
-	local allAvailable = true;
-
-	if ( level >= SCENARIOS_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton2, true);
-		self.groupButton2.tooltip = nil;
-	else
-		allAvailable = false;
-	end
-
-	if ( level >= RAID_FINDER_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton3, true);
-		self.groupButton3.tooltip = nil;
-	else
-		allAvailable = false;
-	end
-
-	if ( level >= FLEX_RAID_SHOW_LEVEL ) then
-		GroupFinderFrameButton_SetEnabled(self.groupButton4, true);
-		self.groupButton4.tooltip = nil;
-		RequestLFDPlayerLockInfo();
-	else
-		allAvailable = false;
-	end
-	
-	if ( allAvailable ) then
-		GroupFinderFrame:SetScript("OnEvent", nil);
-		GroupFinderFrame:UnregisterEvent("PLAYER_LEVEL_UP");		
-	end
+	GroupFinderFrame_EvaluateButtonVisibility(self, level);
 end
 
 function GroupFinderFrame_GetSelection(self)
 	return self.selection;
 end
 
+function GroupFinderFrame_GetSelectedIndex(self)
+	return self.selectionIndex;
+end
+
 function GroupFinderFrame_Update(self, frame)
 	GroupFinderFrame_ShowGroupFrame(frame);
 end
@@ -195,7 +229,7 @@ function GroupFinderFrame_OnShow(self)
 end
 
 function GroupFinderFrame_ShowGroupFrame(frame)
-	frame = frame or GroupFinderFrame.selection or LFDParentFrame;
+	frame = frame or GroupFinderFrame.selection or (UnitLevel("player") >= SHOW_LFD_LEVEL and LFDParentFrame or LFGListPVEStub);
 	-- hide the other frames and select the right button
 	for index, frameName in pairs(groupFrames) do
 		local groupFrame = _G[frameName];
@@ -219,6 +253,8 @@ function GroupFinderFrame_SelectGroupBut
 			button.bg:SetTexCoord(0.00390625, 0.87890625, 0.75195313, 0.83007813);
 		end
 	end
+	
+	GroupFinderFrame.selectionIndex = index
 end
 
 function GroupFinderFrameGroupButton_OnClick(self)