(function(){ 'use strict'; var WHY = JSON.parse(document.getElementById('why').textContent); var LESSON = JSON.parse(document.getElementById('lessons').textContent), LDUR = JSON.parse(document.getElementById('ldur').textContent); var GDUR = JSON.parse(document.getElementById('gdur').textContent), HOW = JSON.parse(document.getElementById('how').textContent); var $ = function(id){ return document.getElementById(id); }; function el(tag, cls, text){ var e = document.createElement(tag); if(cls) e.className = cls; if(text != null) e.textContent = text; return e; } function btn(cls, text, fn){ var b = el('button', cls, text); b.type = 'button'; if(fn) b.addEventListener('click', fn); return b; } var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion:reduce)').matches; /* ---------- the ten stops ---------- */ var STOPS = JSON.parse(document.getElementById('stops').textContent); var TEST = JSON.parse(document.getElementById('testq').textContent); var BUILD = JSON.parse(document.getElementById('builddata').textContent); var LEVEL = JSON.parse(document.getElementById('leveldata').textContent); var TRK = { build: { list: BUILD, view: 'build', name: 'Build', road: 'broad', key: 'ball', word: 'step' }, level: { list: LEVEL, view: 'level', name: 'Level up', road: 'lroad', key: 'lall', word: 'step' } }; /* ---------- saved progress (this device only) ---------- */ var store = { stars: {}, test: 0, sound: true, all: false }; try{ var raw = JSON.parse(localStorage.getItem('maps-trip') || 'null'); if(raw && raw.stars){ store = raw; } }catch(e){} function save(){ try{ localStorage.setItem('maps-trip', JSON.stringify(store)); }catch(e){} } function doneCount(){ return STOPS.filter(function(s){ return store.stars[s.id]; }).length; } function starSum(){ return STOPS.reduce(function(a, s){ return a + (store.stars[s.id] || 0); }, 0); } function starsEl(n){ var s = el('span', 'stars'); for(var i = 0; i < 3; i++){ s.appendChild(i < n ? el('b', null, '★') : document.createTextNode('★')); } s.setAttribute('aria-label', n + ' of 3 stars'); return s; } function licence(){ if(store.test >= 4) return ['Road captain', starSum() === 30 ? 'Perfect record. Every stop, three stars.' : 'Test passed. Replay any stop to collect all 30 stars.']; var d = doneCount(); if(d === STOPS.length) return ['Licensed driver', 'One thing left: pass the driving test to become a Road captain.']; if(d === 0) return ['Learner’s permit', 'Ten short stops, about two minutes each. At every stop: listen, make one move, watch the map, keep a saved line.']; return ['Learner’s permit', (STOPS.length - d) + ' more ' + (STOPS.length - d === 1 ? 'stop' : 'stops') + ' to become a Licensed driver.']; } /* ---------- sound ---------- */ var clip = null, snd = $('snd'), player = null; function paintSound(){ snd.setAttribute('aria-pressed', String(store.sound)); snd.textContent = store.sound ? 'Sound on' : 'Sound off'; } function stopClip(){ if(player){ player.onended = null; player.onerror = null; } if(clip){ try{ clip.pause(); }catch(e){} clip = null; } } document.addEventListener('pointerdown', function prime(){ document.removeEventListener('pointerdown', prime, true); try{ if(!player) player = new Audio(); }catch(e){} }, true); /* iPhone: Web Audio (the chimes) is muted by the silent switch unless the page asks for "playback" audio. Narration uses