<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speed Reading App</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }

        h1 {
            margin-bottom: 20px;
        }

        #controls {
            margin-bottom: 20px;
        }

        #progress-bar {
            display: flex;
            justify-content: center;
            margin: 10px 0;
            gap: 5px;
        }

        .bar-segment {
            width: 20px;
            height: 10px;
            background-color: #ccc;
        }

        .bar-segment.active {
            background-color: #007BFF;
        }

        #word-container {
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 20px 0;
            gap: 20px;
            position: relative;
            width: 100%;
        }

        .side-word {
            font-size: 1.5rem;
            color: #999;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }

        #prev-word {
            left: calc(50% - 15rem);
            text-align: right;
        }

        #next-word {
            left: calc(50% + 15rem);
            text-align: left;
        }

        #word-display {
            font-size: 3rem;
            font-weight: bold;
            color: #333;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            white-space: nowrap;
        }

        #info {
            margin-top: 20px;
            font-size: 1rem;
        }

        input[type="text"] {
            width: 80%;
            padding: 10px;
            font-size: 1.2rem;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            font-size: 1rem;
            border: none;
            background-color: #007BFF;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }

        button:disabled {
            background-color: #ccc;
        }
    </style>
</head>
<body>
    <h1>Speed Reading App</h1>
    <div id="controls">
        <input type="text" id="input-text" placeholder="Enter your text here...">
        <button id="start-btn">Start</button>
    </div>
    <div id="progress-bar">
        <div class="bar-segment"></div>
        <div class="bar-segment"></div>
        <div class="bar-segment"></div>
        <div class="bar-segment"></div>
        <div class="bar-segment"></div>
    </div>
    <div id="word-container">
        <span id="prev-word" class="side-word"></span>
        <div id="word-display">Your words will appear here</div>
        <span id="next-word" class="side-word"></span>
    </div>
    <div id="info">WPM: <span id="wpm-display">500</span>, Long Word Pause: <span id="pause-percentage">300%</span>, Punctuation Pause: <span id="punctuation-percentage">300%</span></div>
    <div>Press: UP/DOWN (faster,slower), LEFT/RIGHT(how long to pause for long words), W/S (how long to pause after end of a sentence), T/Z (go back/go forward in sentence. 
	The bars show which sentence you are on :-)</div>
	
	<script>
    const inputText = document.getElementById('input-text');
    const startBtn = document.getElementById('start-btn');
    const wordDisplay = document.getElementById('word-display');
    const prevWordDisplay = document.getElementById('prev-word');
    const nextWordDisplay = document.getElementById('next-word');
    const wpmDisplay = document.getElementById('wpm-display');
    const pausePercentageDisplay = document.getElementById('pause-percentage');
    const punctuationPercentageDisplay = document.getElementById('punctuation-percentage');
    const progressBar = document.getElementById('progress-bar');
    const barSegments = Array.from(progressBar.children);

    let words = [];
    let sentences = [];
    let currentWordIndex = 0;
    let currentSentenceIndex = 0;
    let wpm = 500; // Default words per minute
    let longWordMultiplier = 1.5; // Default multiplier for long words
    let punctuationMultiplier = 1.5; // Default multiplier for punctuation
    let intervalId;

    function calculateDisplayTime(word) {
        const baseTime = 60000 / wpm;
        const isLongWord = word.length > 7;
        const isComplexWord = /[^a-zA-Z0-9]/.test(word);
        const isPunctuation = /[,.]/.test(word);

        if (isPunctuation) {
            return baseTime * punctuationMultiplier;
        } else if (isLongWord || isComplexWord) {
            return baseTime * longWordMultiplier;
        } else {
            return baseTime;
        }
    }

    function updateProgressBar() {
        barSegments.forEach((segment, index) => {
            segment.classList.toggle('active', index === currentSentenceIndex % barSegments.length);
        });
    }

    function displayNextWord() {
        if (currentWordIndex >= words.length) {
            clearInterval(intervalId);
            wordDisplay.textContent = "Done!";
            prevWordDisplay.textContent = "";
            nextWordDisplay.textContent = "";
            startBtn.disabled = false;
            return;
        }

        const word = words[currentWordIndex];
        const prevWord = words[currentWordIndex - 1] || "";
        const nextWord = words[currentWordIndex + 1] || "";

        prevWordDisplay.textContent = prevWord;
        wordDisplay.textContent = word;
        nextWordDisplay.textContent = nextWord;

        if (sentences[currentSentenceIndex] && sentences[currentSentenceIndex].includes(word)) {
            updateProgressBar();
        }

        const displayTime = calculateDisplayTime(word);

        clearInterval(intervalId);
        intervalId = setTimeout(() => {
            currentWordIndex++;
            if (sentences[currentSentenceIndex] && !sentences[currentSentenceIndex].includes(words[currentWordIndex])) {
                currentSentenceIndex++;
            }
            displayNextWord();
        }, displayTime);
    }

    startBtn.addEventListener('click', () => {
        const text = inputText.value.trim();
        if (!text) {
            alert("Please enter some text to start speed reading.");
            return;
        }

        sentences = text.match(/[^.!?]+[.!?]/g) || [text];
        words = text.split(/\s+/);
        currentWordIndex = 0;
        currentSentenceIndex = 0;
        startBtn.disabled = true;
        updateProgressBar();
        displayNextWord();
    });

    document.addEventListener('keydown', (event) => {
        if (event.key === 'ArrowUp') {
            wpm = Math.min(wpm + 25, 1000);
            wpmDisplay.textContent = wpm;
        } else if (event.key === 'ArrowDown') {
            wpm = Math.max(wpm - 25, 50);
            wpmDisplay.textContent = wpm;
        } else if (event.key === 'ArrowRight') {
            longWordMultiplier = Math.min(longWordMultiplier + 0.1, 3);
            pausePercentageDisplay.textContent = `${Math.round(longWordMultiplier * 100)}%`;
        } else if (event.key === 'ArrowLeft') {
            longWordMultiplier = Math.max(longWordMultiplier - 0.1, 1);
            pausePercentageDisplay.textContent = `${Math.round(longWordMultiplier * 100)}%`;
        } else if (event.key === 'w') {
            punctuationMultiplier = Math.min(punctuationMultiplier + 0.1, 3);
            punctuationPercentageDisplay.textContent = `${Math.round(punctuationMultiplier * 100)}%`;
        } else if (event.key === 's') {
            punctuationMultiplier = Math.max(punctuationMultiplier - 0.1, 1);
            punctuationPercentageDisplay.textContent = `${Math.round(punctuationMultiplier * 100)}%`;
        } else if (event.key === 't') {
            currentSentenceIndex = Math.max(currentSentenceIndex - 1, 0);
            currentWordIndex = findWordIndex(currentSentenceIndex);
            updateProgressBar();
            displayNextWord();
        } else if (event.key === 'z') {
            currentSentenceIndex = Math.min(currentSentenceIndex + 1, sentences.length - 1);
            currentWordIndex = findWordIndex(currentSentenceIndex);
            updateProgressBar();
            displayNextWord();
        }
    });

    function findWordIndex(sentenceIndex) {
        const sentenceWords = sentences[sentenceIndex].trim().split(/\s+/);
        for (let i = 0; i < words.length; i++) {
            if (words.slice(i, i + sentenceWords.length).join(' ') === sentenceWords.join(' ')) {
                return i;
            }
        }
        return 0;
    }
</script>
</body>
</html>