<!DOCTYPE html>
<html lang="en">
<head>
    <title>OSL Report: Subjects</title>
    <style>
        body { background-color: White; }
        p { color: #fff; }
        div.figbox { width: 50%; display: table-cell; text-align: center; }
        div.tablebox { width: 30%; display: table-cell; text-align: left; margin; 20px }
        th, td { border-bottom: 1px solid #ddd; }
        table { border-collapse: collapse; width: 75%; }
        .button1 {
              background-color: #4CAF50; /* Green */
              border: none;
              border-bottom-style: solid;
              border-color: black;
              color: white;
              padding: 15px 32px;
              text-align: center;
              text-decoration: none;
              display: inline-block;
              font-size: 16px;
            }
        .button1:active {
             color: #f6f6f6;
        }
    </style>
</head>

<body>

<div style='width:90%; margin: auto; text-align: center' >
    <h2>OSL Report: Subjects</h2>
</div>

<div class="float-container">
    <div style='width:70%; margin: auto; text-align: center' >
        <form onsubmit="return false">
          <label for="fid" id="">Navigate to file:</label>
          <input type="text" id="fid" name="fid">
          <button class="button" type="button" onclick="navigateToFif()">Go</button>
          <br><br>
        </form>
        <div style='max-width: 70%; margin: auto; padding: 0px 25px 20px 25px;'>
            The panels below summarise each fif file (full list on the right). Use arrow keys to navigate tabs.
        </div>
    </div>
    <div style='width: 80%; margin: auto; text-align: center; float: left'>
        {% for panel in panels %}
            {{ panel }}
        {% endfor %}
    </div>
    <div style="width: 20%; text-align: left; float: right">
        <div>
            {{ filenames }}
        </div>
    </div>
</div>

<script>

// Blocks scrolling with arrow keys
window.addEventListener("keydown", function(e) {
    if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
        e.preventDefault();
    }
}, false);

// All fif panels and buttons
fifs = document.getElementsByClassName("fif");
buttons = document.getElementsByClassName("button1");

// Text input box for navigating files
var fnav = document.getElementById("fid");

// Hide all panels apart from the first
hideFifs(1);

function hideFifs(start) {
    for (i = start; i < fifs.length; i++) {
        fifs[i].style.display = "none";
    }
}

// Variables to track which panel we're showing
var numButtons = buttons.length / fifs.length;
var currentButton = 0;
var currentFif = 0;

clickButton(0);

// Actions when we press an arrow key
document.onkeydown = checkKey;
function checkKey(e) {
    e = e || window.event;
    if (e.keyCode == '38') {
        // up arrow
        if (currentButton > 0) {
            currentButton--;
        }
        clickButton(currentButton + currentFif * numButtons);
    }
    else if (e.keyCode == '40') {
        // down arrow
        if (currentButton < numButtons - 1) {
            currentButton++;
        }
        clickButton(currentButton + currentFif * numButtons);
    }
    else if (e.keyCode == '37') {
        // left arrow
        if (currentFif > 0) {
            currentFif--;
            switchFif(currentFif);
        }
        clickButton(currentButton + currentFif * numButtons);
    }
    else if (e.keyCode == '39') {
        // right arrow
        if (currentFif < fifs.length - 1) {
            currentFif++;
            switchFif(currentFif);
        }
        clickButton(currentButton + currentFif * numButtons);
    }
    else if (e.keyCode == '13') {
        // enter
        navigateToFif();
    }
}

function clickButton(ind) {
    document.getElementsByClassName('button1')[ind].focus();
    document.getElementsByClassName('button1')[ind].click();
}

function switchFif(ind) {
    hideFifs(0);
    fifs[ind].style.display = "block";
    fnav.value = ind + 1;    
}

function navigateToFif() {
    fif = fnav.value - 1;
    if (fif > fifs.length - 1) {
        fif = fifs.length - 1;
    }
    if (fif < 0) {
        fif = 0;
    }
    if (fif == null) {
        fif = currentFif;
    }
    currentFif = fif;
    switchFif(currentFif);
    clickButton(currentButton + currentFif * numButtons);
}

</script>

</body>
