local sX, sY = guiGetScreenSize( )
function dxSpectate( )
	local camTarget = getCameraTarget( )
	if ( camTarget ) then
	    dxDrawText( 'Player: #FFA500'..tostring( getPlayerName( camTarget ) )..'', sX*0.5, sY*0.9, sX*0.5, sY*0.9, tocolor ( 150, 150, 150, 222 ), 0.8, 'bankgothic', "center", "top", false, false, true, true, false )
	    local teamName = getTeamName( getPlayerTeam( camTarget ) )
		local playerKills = getElementData( camTarget, 'Kills' )
		local playerDeaths = getElementData( camTarget, 'Deaths' )
		dxDrawText( '|\n|\n|\n|\n', sX*0.78, sY*0.02, sX*0.78, sY*0.02, tocolor ( 255, 150, 0, 222 ), 0.6, 'bankgothic', "left", "top", false, false, true, true, false )
		dxDrawText( '#FFA500Team: #FFFFFF'..teamName..'\n#FFA500Kills: #FFFFFF'..playerKills..'\n#FFA500Deaths: #FFFFFF'..playerDeaths..'', sX*0.8, sY*0.02, sX*0.8, sY*0.02, tocolor ( 255, 255, 255, 222 ), 0.6, 'bankgothic', "left", "top", false, false, true, true, false )
	end
end
function getAlivePlayers( )
    local table = { };
    for i,v in ipairs( getElementsByType( "player" ) ) do
		if not isPedDead( v ) then
			table.insert( table, v );
		end 
    end
    return table;
end
function getAlivePlayersInTeam( theTeam )
    --[[local theTable = { }
    local players = getPlayersInTeam( theTeam )
    for i,v in pairs( players ) do
        if getElementHealth( v ) > 0 then
            theTable[#theTable+1]=v
        end
    end
    return theTable]]
	local table = { };
	local players = getPlayersInTeam( theTeam );
	for i,v in pairs( players ) do
		if not isPedDead( v ) then
			table.insert( table, v );
		end
	end
	return table;
end
function spectateStart( )
    if ( g_Players ) then
	    g_Players = nil
	end
	local localTeamName = getTeamName( getPlayerTeam( getLocalPlayer( ) ) )
	if ( localTeamName == "SPECTATORS" ) then
		g_Players = getElementsByType( "player" )
		for i,aPlayer in ipairs( g_Players ) do
			if ( aPlayer == getLocalPlayer( ) ) then
			    g_CurrentSpectated = i
				break
			end
		end
	else
		g_Players = getPlayersInTeam( getTeamFromName( localTeamName ) )
		for i,aPlayer in ipairs( g_Players ) do
			if ( aPlayer == getLocalPlayer( ) ) then
			    g_CurrentSpectated = i
				break
			end
		end
	end
	bindKey( "arrow_l", "down", spectatePrevious )
	bindKey( "arrow_r", "down", spectateNext )
	bindKey( "r", "down", spectateNext )
	guiSetVisible( gui.images['intro'], true )
	showPlayerHudComponent( 'radar', false )
	spectateNext( )
	--outputChatBox( 'Press (R) to spectate next player.', 222, 222, 222, true )
	createInfoBoxClient( 20, "(sy/3.5)*2", 'Press (R) to spectate next player.', 7000 )	
	showCursor( false )
end
function spectateStop( )
	unbindKey( "arrow_l", "down", spectatePrevious )
	unbindKey( "arrow_r", "down", spectateNext )
	unbindKey( "r", "down", spectateNext )
	guiSetVisible( gui.images['intro'], false )
end
function spectatePrevious( )
    if g_CurrentSpectated == 1 then
        g_CurrentSpectated = #g_Players
    else
        g_CurrentSpectated = g_CurrentSpectated - 1
    end
    setCameraTarget( g_Players[g_CurrentSpectated] )
end
 
function spectateNext( )
    if g_CurrentSpectated == #g_Players then
        g_CurrentSpectated = 1
    else
        g_CurrentSpectated = g_CurrentSpectated + 1
    end
    setCameraTarget( g_Players[g_CurrentSpectated] )
end
addEvent( 'togglePlayerSpectate', true )
addEventHandler( 'togglePlayerSpectate', localPlayer,
    function( bool )
	    if ( bool == true ) then
		    spectateStart( )
			addEventHandler( 'onClientRender', getRootElement( ), dxSpectate )
		end
		if ( bool == false ) then
		    spectateStop( )
			removeEventHandler( 'onClientRender', getRootElement( ), dxSpectate )
		end
	end
)