text: /*
  	Countdown
  	Repeat
  	Workout
  */
  
  var hiittimer = {
      shortBeep: new Audio("beep.wav"),
      slots: 1,
      countDownQueue: [],
      currentCountdownSeq: 0,
      loopCounts: 0,
      currentLoopSeq: 0,
      currentCounterDisplay: 0,
      pause: false,
      timerRef: null,
  
  };
  
  
  
  
  // clear history -1
  function doLogout() {
      var backlen = history.length;
      history.go(-backlen);
      window.location.replace("timer.html");
  }
  
  // onClick of first interaction on page before I need the sounds
  
  (function init() {
      document
          .querySelector("#add-time-slot")
          .addEventListener("click", addTimeSlot);
      document
          .querySelector("#loop-it")
          .addEventListener("click", loopAndDisplayToggle);
      document.querySelectorAll(".timer-type").forEach(function(el) {
          el.addEventListener("click", loadSavedTimer);
      });
  })();
  
  function addTimeSlot() {
      var counterRow = document.createElement("div");
      counterRow.classList.add("counter-row");
      counterRow.classList.add("counter-seq-" + ++hiittimer.slots);
  
      var ip = document.createElement("input");
      ip.classList.add("time-slot");
      ip.type = "number";
  
      counterRow.appendChild(ip);
      document.querySelector("#counter-rows").appendChild(counterRow);
      counterRow.querySelector("input").focus();
  }
  
  function loopAndDisplayToggle() {
      if (!hiittimer.countDownQueue.length) {
          prepData();
          removeEmptySlots();
  
          if (hiittimer.countDownQueue.length) {
              disableAddSlotsButton();
              activatePlayButton();
              disableLoopTimesInput();
              displayCounters();
              startLoop();
          }
      } else if (!hiittimer.pause) {
          hiittimer.pause = true;
          resetPlayButton();
          showPauseBtn();
          window.clearTimeout(hiittimer.timerRef);
      } else if (hiittimer.pause) {
          hiittimer.pause = false;
          activatePlayButton();
          startLoop();
      }
  }
  
  function activatePlayButton() {
      document.querySelector(".center-content").classList.remove("paused");
      document.querySelector(".center-content").classList.add("active");
  }
  
  function showPauseBtn() {
      document.querySelector(".center-content").classList.add("paused");
  }
  
  function resetPlayButton() {
      document.querySelector(".center-content").classList.remove("active");
  }
  
  function disableAddSlotsButton() {
      document.querySelector(".add-time-slot-btn").disabled = true;
      document.querySelector(".add-time-slot-btn").classList.add("disabled");
  }
  
  function enableAddSlotsButton() {
      document.querySelector(".add-time-slot-btn").disabled = false;
      document.querySelector(".add-time-slot-btn").classList.remove("disabled");
  }
  
  function disableLoopTimesInput() {
      document.querySelector("#loop-times").disabled = true;
      document.querySelector("#loop-times").classList.add("disabled");
  }
  
  function resetLoopTimesInput() {
      document.querySelector("#loop-times").disabled = false;
      document.querySelector("#loop-times").classList.remove("disabled");
      document.querySelector("#loop-times").value = hiittimer.loopCounts;
  }
  
  function prepData() {
      hiittimer.loopCounts = document.querySelector("#loop-times").value;
  
      document.querySelectorAll(".time-slot").forEach(function(timeSlotEL) {
          if (timeSlotEL.value > 0) {
              hiittimer.countDownQueue.push(timeSlotEL.value);
          }
      });
  }
  
  function startLoop(argument) {
      // start the counters
      if (hiittimer.currentCounterDisplay == 0) {
          printAndMoveNext(
              hiittimer.countDownQueue[hiittimer.currentCountdownSeq] - 1
          );
      } else {
          printAndMoveNext(hiittimer.currentCounterDisplay);
      }
  }
  
  function printAndMoveNext(count) {
      if (!hiittimer.pause) {
          hiittimer.timerRef = setTimeout(function() {
              updateDisplay(count);
              if (count > 0) {
                  // Countdown this slot
                  printAndMoveNext(--count);
              } else if (
                  hiittimer.currentCountdownSeq <
                  hiittimer.countDownQueue.length - 1
              ) {
                  // Move to next slot
                  hiittimer.currentCountdownSeq++;
                  printAndMoveNext(
                      hiittimer.countDownQueue[hiittimer.currentCountdownSeq] - 1
                  );
              } else if (hiittimer.loopCounts > hiittimer.currentLoopSeq + 1) {
                  // 1 Loop eneded
                  resetForNextLoop();
              } else {
                  // all done:
                  resetAllData();
              }
          }, 1000);
      }
  }
  
  function resetForNextLoop() {
      hiittimer.currentCountdownSeq = 0;
      hiittimer.currentLoopSeq++;
      document
          .querySelectorAll(".counter-display")
          .forEach(function(counterDisplay) {
              counterDisplay.parentNode.removeChild(counterDisplay);
          });
      document.querySelector("#loop-times").value =
          hiittimer.loopCounts - hiittimer.currentLoopSeq;
      displayCounters();
      printAndMoveNext(hiittimer.countDownQueue[hiittimer.currentCountdownSeq] - 1);
  }
  
  function resetAllData() {
      hiittimer.currentCountdownSeq = 0;
      hiittimer.countDownQueue = [];
      hiittimer.currentLoopSeq = 0;
      hiittimer.slots = 1;
      document
          .querySelectorAll(".counter-display")
          .forEach(function(counterDisplay) {
              counterDisplay.parentNode.removeChild(counterDisplay);
          });
      document.querySelector(".center-content").classList.remove("counter-started");
      document.querySelector(".center-content").classList.remove("paused");
      enableAddSlotsButton();
      resetLoopTimesInput();
      resetPlayButton();
  }
  
  function updateDisplay(count) {
      hiittimer.currentCounterDisplay = count;
      if (count > 0 && count <= 3) {
          hiittimer.shortBeep.playbackRate = 1;
          hiittimer.shortBeep.play();
      }
      if (count == 0) {
          hiittimer.shortBeep.playbackRate = 0.1;
          hiittimer.shortBeep.play();
      }
      var currentCounterDisplay =
          ".counter-seq-" + (hiittimer.currentCountdownSeq + 1) + " .counter-display";
      document.querySelector(currentCounterDisplay).textContent = count;
  }
  
  function displayCounters() {
      document.querySelectorAll("#counter-rows input").forEach(function(inputEl) {
          var counter = document.createElement("div");
          counter.classList.add("counter-display");
          counter.textContent = inputEl.value;
          inputEl.parentElement.insertBefore(counter, inputEl.nextSibling);
      });
      document.querySelector(".center-content").classList.add("counter-started");
  }
  
  function resetDom() {
      var timeSlots = document.querySelectorAll(".time-slot");
      for (var i = 1; i < timeSlots.length; i++) {
          timeSlots[i].parentElement.remove(timeSlots[i]);
      }
  }
  
  function loadSavedTimer(event) {
      resetAllData();
      resetDom();
  
      var formula = event.currentTarget
          .getAttribute("data-timer-formula")
          .replace("(", "")
          .replace(")", "");
      var formulaParts = formula.split("*");
      var timeSlots = formulaParts[0].split("+");
      var sets = formulaParts[1];
  
      if (sets) {
          document.querySelector("#loop-times").value = sets;
      }
  
      document.querySelector(".counter-seq-1 input").value = timeSlots[0];
      if (timeSlots.length > 1) {
          for (var i = 1; i < timeSlots.length; i++) {
              addTimeSlot();
              document.querySelector(".counter-seq-" + (i + 1) + " input").value =
                  timeSlots[i];
          }
      }
      console.log("called");
  }
  
  function removeEmptySlots() {
      var timeSlots = document.querySelectorAll(".time-slot");
      for (var i = 1; i < timeSlots.length; i++) {
          if (!timeSlots[i].value) {
              timeSlots[i].parentElement.remove(timeSlots[i]);
          }
      }
  }