/* ============================================
   DANGEROUS WONDER
   A Field Guide to Everything Worth Knowing
   Main JavaScript
   ============================================ */

(function () {
  document.addEventListener('DOMContentLoaded', function () {

    /* --- Visitor Counter --- */
    var counterEl = document.getElementById('visitor-count');
    if (counterEl) {
      var stored = localStorage.getItem('dw_visits');
      var count;
      if (!stored) {
        var base = 247893;
        var offset = Math.floor(Math.random() * 200);
        count = base + offset;
      } else {
        count = parseInt(stored, 10) + 1;
      }
      localStorage.setItem('dw_visits', count.toString());
      counterEl.textContent = 'You are visitor #' + count.toLocaleString();
    }

    /* --- "Last Updated" datestamp --- */
    var updatedEl = document.getElementById('last-updated');
    if (updatedEl) {
      var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
      var d = new Date();
      updatedEl.textContent = months[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
    }

    /* --- Easter Egg: Konami Code --- */
    var konamiCode = ['ArrowUp','ArrowUp','ArrowDown','ArrowDown','ArrowLeft','ArrowRight','ArrowLeft','ArrowRight','b','a'];
    var konamiPos = 0;

    document.addEventListener('keydown', function (e) {
      if (e.key === konamiCode[konamiPos]) {
        konamiPos++;
        if (konamiPos === konamiCode.length) {
          konamiPos = 0;
          activateEasterEgg();
        }
      } else {
        konamiPos = 0;
      }
    });

    function activateEasterEgg() {
      var body = document.body;
      body.style.transition = 'filter 0.5s';
      body.style.filter = 'sepia(0.6) saturate(1.4) brightness(1.05)';
      var msg = document.createElement('div');
      msg.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:#1a3a5c;color:#fffff5;padding:30px 40px;font-family:Georgia,serif;font-size:1.2rem;z-index:9999;box-shadow:4px 5px 14px rgba(0,0,0,0.4);text-align:center;line-height:1.6;';
      msg.innerHTML = '<strong style="color:#b8860b;font-variant:small-caps;letter-spacing:0.1em;">Secret Found!</strong><br><br>You have discovered a hidden corner of<br>Dangerous Wonder. The world is full of<br>things you were never meant to see.<br><br><span style="font-size:0.85rem;color:#a09878;">(Click anywhere to dismiss)</span>';
      document.body.appendChild(msg);
      msg.addEventListener('click', function () {
        msg.remove();
        body.style.filter = '';
      });
    }

    /* --- Easter Egg: Triple-click title --- */
    var title = document.querySelector('.site-title a');
    if (title) {
      title.addEventListener('dblclick', function (e) {
        e.preventDefault();
        var secret = document.querySelectorAll('.easter-egg');
        for (var i = 0; i < secret.length; i++) {
          secret[i].style.color = '#b8860b';
          secret[i].style.fontSize = '0.85rem';
          secret[i].style.lineHeight = '1.4';
          secret[i].style.display = 'inline';
        }
      });
    }

    /* --- Gentle random "tip" in console --- */
    var tips = [
      'The oldest known star is older than the universe. Think about that.',
      'Octopuses have three hearts. Two for the gills, one for the rest.',
      'The Voyager Golden Record is currently 15 billion miles from Earth.',
      'A group of flamingos is called a "flamboyance."',
      'Honey never spoils. Archaeologists have found 3,000-year-old honey in Egyptian tombs that was still edible.',
      'There are more possible iterations of a game of chess than there are atoms in the observable universe.',
      'The shortest war in history lasted 38 to 45 minutes.',
      'A day on Venus is longer than a year on Venus.'
    ];
    var tip = tips[Math.floor(Math.random() * tips.length)];
    console.log('%c🦉 Dangerous Wonder says: ' + tip, 'font-family:Georgia,serif;font-size:14px;color:#1a3a5c;');

  });
})();