//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTeamRoundTimer::SendTimeWarning( int nWarning )
{
	// don't play sounds if the level designer has turned them off or if it's during the WaitingForPlayers time
	if ( !m_bTimerPaused && m_bAutoCountdown && !TeamplayRoundBasedRules()->IsInWaitingForPlayers() )
	{
		C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
		if ( pPlayer )
		{
			if ( ObjectiveResource() )
			{
				bool bShouldPlaySound = false;

				if ( TeamplayRoundBasedRules()->IsInTournamentMode() == true && TeamplayRoundBasedRules()->IsInStopWatch() == true )
				{
					int iActiveTimer = ObjectiveResource()->GetTimerToShowInHUD();
					int iStopWatchTimer = ObjectiveResource()->GetStopWatchTimer();

					if ( IsStopWatchTimer() == true && IsWatchingTimeStamps() == false )
					{
						CTeamRoundTimer *pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( iActiveTimer ) );

						if ( pTimer && pTimer->IsTimerPaused() == false && pTimer->GetTimeRemaining() > GetTimeRemaining() )
						{
							bShouldPlaySound = true;
						}
					}
					else
					{
						CTeamRoundTimer *pStopWatch = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( iStopWatchTimer ) );

						if ( ObjectiveResource()->GetTimerToShowInHUD() == entindex()  )
						{
							if ( pStopWatch )
							{
								if ( pStopWatch->IsTimerPaused() == true )
								{
									bShouldPlaySound = true;
								}

								if ( pStopWatch->GetTimeRemaining() > GetTimeRemaining() && pStopWatch->IsWatchingTimeStamps() == false )
								{
									bShouldPlaySound = true;
								}

								if ( pStopWatch->IsWatchingTimeStamps() == true )
								{
									bShouldPlaySound = true;
								}
							}
							else
							{
								bShouldPlaySound = true;
							}
						}
					}
				}
				else 
				{
					if( ObjectiveResource()->GetTimerToShowInHUD() == entindex())
					{
						bShouldPlaySound = true;
					}
				}

				if ( bShouldPlaySound == true )
				{
					pPlayer->EmitSound( GetTimeWarningSound( nWarning ) );
				}
			}
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudKothTimeStatus::Think( void )
{
	if ( !TFGameRules() )
		return;

	// check for an active timer and turn the time panel on or off if we need to
	if ( m_pBlueKothTimer && m_pRedKothTimer )
	{
		// Don't draw in freezecam, or when the game's not running
		C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
		bool bDisplayTimers = !( pPlayer && pPlayer->GetObserverMode() == OBS_MODE_FREEZECAM );
		if ( bDisplayTimers )
		{
			bool bDisplayBlueTimer = false;
			bool bDisplayRedTimer = false;
			bool bDisplayGreenTimer = false;
			bool bDisplayYellowTimer = false;

			// is the time panel still pointing at an active timer?
			CTeamRoundTimer *pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_pBlueKothTimer->GetTimerIndex() ) );

			// Check for the current active timer (used for the pulsating HUD animation)
			CTFHudTimeStatus *pActiveKothTimerPanel = NULL;

			if ( !pTimer )
			{
				pTimer = TFGameRules()->GetBlueKothRoundTimer();
				if ( pTimer && m_pBlueKothTimer->GetTimerIndex() != pTimer->index )
					m_pBlueKothTimer->SetTimerIndex( pTimer->index );
			}

			if ( pTimer && !pTimer->IsDormant() && !pTimer->IsDisabled() )
			{
				// the current timer is fine, make sure the panel is visible
				bDisplayBlueTimer = true;

				if ( !pTimer->IsTimerPaused() )
					pActiveKothTimerPanel = m_pBlueKothTimer;
			}

			// And now let's check the other timer
			// is the time panel still pointing at an active timer?
			pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_pRedKothTimer->GetTimerIndex() ) );

			if ( !pTimer )
			{
				pTimer = TFGameRules()->GetRedKothRoundTimer();
				if ( pTimer && m_pRedKothTimer->GetTimerIndex() != pTimer->index )
					m_pRedKothTimer->SetTimerIndex( pTimer->index );
			}

			if ( pTimer && !pTimer->IsDormant() && !pTimer->IsDisabled() )
			{
				// the current timer is fine, make sure the panel is visible
				bDisplayRedTimer = true;

				if ( !pTimer->IsTimerPaused() )
					pActiveKothTimerPanel = m_pRedKothTimer;
			}

			if ( TFGameRules()->IsFourTeamGame() )
			{
				// Check GRN timer
				pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_pGreenKothTimer->GetTimerIndex() ) );

				if ( !pTimer )
				{
					pTimer = TFGameRules()->GetGreenKothRoundTimer();
					if ( pTimer )
						m_pGreenKothTimer->SetTimerIndex( pTimer->index );
				}

				if ( pTimer && !pTimer->IsDormant() && !pTimer->IsDisabled() )
				{
					// the current timer is fine, make sure the panel is visible
					bDisplayGreenTimer = true;

					if ( !pTimer->IsTimerPaused() )
						pActiveKothTimerPanel = m_pGreenKothTimer;
				}

				// Check YLW timer
				pTimer = dynamic_cast< CTeamRoundTimer* >( ClientEntityList().GetEnt( m_pYellowKothTimer->GetTimerIndex() ) );

				if ( !pTimer )
				{
					pTimer = TFGameRules()->GetYellowKothRoundTimer();
					if ( pTimer )
						m_pYellowKothTimer->SetTimerIndex( pTimer->index );
				}

				if ( pTimer && !pTimer->IsDormant() && !pTimer->IsDisabled() )
				{
					// the current timer is fine, make sure the panel is visible
					bDisplayYellowTimer = true;

					if ( !pTimer->IsTimerPaused() )
						pActiveKothTimerPanel = m_pYellowKothTimer;
				}
			}

			if ( !m_pBlueKothTimer->IsVisible() || !m_pRedKothTimer->IsVisible() )
			{
				m_pBlueKothTimer->SetVisible( true ); // bDisplayBlueTimer
				m_pRedKothTimer->SetVisible( true ); // bDisplayRedTimer

				// If our spectator GUI is visible, invalidate its layout so that it moves the reinforcement label
				if ( g_pSpectatorGUI )
				{
					g_pSpectatorGUI->InvalidateLayout();
				}
			}

			if ( TFGameRules()->IsFourTeamGame() )
			{
				if ( !m_pGreenKothTimer->IsVisible() || !m_pYellowKothTimer->IsVisible() )
				{
					m_pGreenKothTimer->SetVisible( true ); 
					m_pYellowKothTimer->SetVisible( true );

					// If our spectator GUI is visible, invalidate its layout so that it moves the reinforcement label
					if ( g_pSpectatorGUI )
					{
						g_pSpectatorGUI->InvalidateLayout();
					}
				}
			}
			else
			{
				m_pGreenKothTimer->SetVisible( false ); 
				m_pYellowKothTimer->SetVisible( false );
			}

			// Set overtime panels active on our active panel (if needed)
			if ( m_pActiveKothTimerPanel )
				m_pActiveKothTimerPanel->SetExtraTimePanels();

			// Do NOT put a null check here, otherwise the white active timer BG will linger around after a round end
			if ( pActiveKothTimerPanel != m_pActiveKothTimerPanel )
			{
				m_pActiveKothTimerPanel = pActiveKothTimerPanel;
				UpdateActiveTeam();
			}
		}
		else
		{
			m_pBlueKothTimer->SetVisible( false );
			m_pRedKothTimer->SetVisible( false );
			m_pGreenKothTimer->SetVisible( false );
			m_pYellowKothTimer->SetVisible( false );

			m_pActiveTimerBG->SetVisible( false );
		}
	}
}