window.onbeforeunload = () => { window.scrollTo(0, 0); }

setPlayerCount = (factionId, value) => {
    if (factionId == -1) {
        document.getElementById('player-count').innerHTML = value;
    } else if (factionId >= 0 && factionId <= 9) {
        document.getElementById(factionId).innerHTML = value;
    }
}

function showPlayerList() {
    document.getElementsByName("OnlineList")[0].classList.remove("hidden");
    mp.events.call("CEF:PlayerList_Loaded");
    window.scrollTo(0, 0);
}
function closePlayerList() {
    document.getElementsByName("OnlineList")[0].classList.toggle("hidden");
}

function clear_row() {
    document.getElementById('players').innerHTML = '';
}

function getFactionNameByFactionId(factionId) {
  switch (parseInt(factionId)) {
    case 1: return "LSPD";
    case 2: return "LSED";
    case 3: return "FIB";
    case 4: return "Trucker";
    case 5: return "Vagos";
    case 7: return "Grove Street";
    case 8: return "Ballas";
    case 9: return "Weazel News";
    default: return "Zivilisten";
  }
}

function addPlayerEntry(userId, userName, factionId, userPing) {
    var factionName = getFactionNameByFactionId(factionId);
    document.getElementById('players').innerHTML += `<tr><td>${userId}</td> <td>${userName}</td> <td class="group group--${factionId}">${factionName}</td> <td>${userPing}</td></tr>`;
}

$("table").stupidtable();
