(function (){
'use strict';
var activeButton=null;
var currentUtterance=null;
function stopSpeaking(){
if('speechSynthesis' in window){
window.speechSynthesis.cancel();
}
if(activeButton){
activeButton.setAttribute('aria-pressed', 'false');
var label=activeButton.querySelector('.emh-story-speaker__text');
if(label){
label.textContent='Listen';
}
activeButton.classList.remove('is-speaking');
}
activeButton=null;
currentUtterance=null;
}
function speak(button){
if(!('speechSynthesis' in window)){
return;
}
var text=button.getAttribute('data-emh-speech');
if(!text){
return;
}
if(activeButton===button){
stopSpeaking();
return;
}
stopSpeaking();
currentUtterance=new SpeechSynthesisUtterance(text);
currentUtterance.rate=0.95;
currentUtterance.pitch=1.05;
currentUtterance.onend=function (){
stopSpeaking();
};
currentUtterance.onerror=function (){
stopSpeaking();
};
activeButton=button;
activeButton.setAttribute('aria-pressed', 'true');
activeButton.classList.add('is-speaking');
var label=activeButton.querySelector('.emh-story-speaker__text');
if(label){
label.textContent='Stop';
}
window.speechSynthesis.speak(currentUtterance);
}
document.addEventListener('click', function (event){
var button=event.target.closest('.emh-story-speaker');
if(!button){
return;
}
event.preventDefault();
speak(button);
});
window.addEventListener('beforeunload', stopSpeaking);
})();