(function(){'use strict';let speechSynthesis=window.speechSynthesis,currentUtterance=null,isPlaying=false,isPaused=false,articleText='',totalCharacters=0,charactersRead=0,speechStartTime=0,speechDuration=0,pausedElapsed=0,progressIntervalId=null,audioLink,audioCircle,audioIcon,listenText,activeLine,barLine;function findMaleVoice(voices){const femaleKeywords=['Female','Zira','Samantha','Susan','Hazel','Linda','Karen','Heather','Michelle','Stephanie','Monica','Nancy','Allison','Victoria','Serena','Amanda','Lisa','Anna','Maria','Helena','Katya','Irina','Kate','Natasha','Tessa','Fiona','Moira','Veena','Karen','Kyoko','Sora','Yuna','Sin-ji','Mei-Jia','Ting-Ting','Sora','Yuna','Sin-ji','Mei-Jia','Ting-Ting'];const maleKeywords=['Male','David','Mark','Daniel','James','Richard','George','Paul','John','Michael','Robert','William','Thomas','Charles','Joseph','Christopher','Daniel','Matthew','Anthony','Donald','Steven','Andrew','Joshua','Kenneth','Kevin','Brian','Edward','Ronald','Timothy','Jason','Jeffrey','Ryan','Jacob','Gary','Nicholas','Eric','Jonathan','Stephen','Larry','Justin','Scott','Brandon','Benjamin','Samuel','Frank','Gregory','Raymond','Alexander','Patrick','Jack','Dennis','Jerry','Tyler','Aaron','Jose','Henry','Adam','Douglas','Nathan','Zachary','Kyle','Noah','Ethan','Mason','Lucas','Oliver','Aiden','Elijah','Jameson','Carter','Sebastian','David','Microsoft David','Google UK English Male','Google US English','en-US Male','en-GB Male'];const isFemale=voice=>femaleKeywords.some(keyword=>voice.name.toLowerCase().includes(keyword.toLowerCase()));const isMale=voice=>maleKeywords.some(keyword=>voice.name.toLowerCase().includes(keyword.toLowerCase()));const englishVoices=voices.filter(voice=>voice.lang.includes('en')&&!isFemale(voice));const neuralVoices=englishVoices.filter(voice=>voice.name.includes('Neural')&&isMale(voice));if(neuralVoices.length>0){console.log('Found Neural male voice:',neuralVoices[0].name);return neuralVoices[0];}const premiumVoices=englishVoices.filter(voice=>(voice.name.includes('Premium')||voice.name.includes('Natural')||voice.name.includes('Enhanced'))&&isMale(voice));if(premiumVoices.length>0){console.log('Found Premium male voice:',premiumVoices[0].name);return premiumVoices[0];}const explicitMaleVoices=englishVoices.filter(voice=>isMale(voice));if(explicitMaleVoices.length>0){console.log('Found explicit male voice:',explicitMaleVoices[0].name);return explicitMaleVoices[0];}const usMaleVoices=englishVoices.filter(voice=>voice.lang.includes('en-US')&&!isFemale(voice));if(usMaleVoices.length>0){console.log('Found US English voice (assuming male):',usMaleVoices[0].name);return usMaleVoices[0];}const anyEnglishMale=englishVoices.find(voice=>!isFemale(voice));if(anyEnglishMale){console.log('Found English voice (assuming male):',anyEnglishMale.name);return anyEnglishMale;}console.warn('No suitable male voice found, using first available');return voices[0];}function initAudioPlayer(){console.log('=== Initializing audio player ===');audioLink=document.querySelector('.audio_link');audioCircle=document.querySelector('.audio_circle');audioIcon=document.querySelector('.audio_circle img');listenText=document.querySelector('.listen_audio');activeLine=document.querySelector('.active_line.active');barLine=document.querySelector('.bar_line');if(!audioLink){console.warn('Audio link not found, retrying...');setTimeout(initAudioPlayer,100);return;}console.log('Elements found:',{audioLink:!!audioLink,audioCircle:!!audioCircle,audioIcon:!!audioIcon,listenText:!!listenText,activeLine:!!activeLine,barLine:!!barLine});const articleElement=document.getElementById('audio-article')||document.querySelector('.body_article_text.w-richtext')||document.querySelector('.body_article_text')||document.querySelector('article')||document.querySelector('.article-content');if(articleElement){articleText=articleElement.innerText||articleElement.textContent||'';console.log('Article text found, length:',articleText.length);}else{console.warn('Article element not found, using body text');const body=document.body.cloneNode(true);body.querySelectorAll('nav, header, footer, .nav, .header, .footer, .menu, script, style, .audio_link').forEach(el=>el.remove());articleText=body.innerText||body.textContent||'';console.log('Using body text, length:',articleText.length);}if(!articleText||articleText.trim().length<10){console.error('Article text is too short or empty!');if(listenText)listenText.textContent='No text found';return;}totalCharacters=articleText.length;charactersRead=0;speechDuration=estimateSpeechDuration(articleText);if(!speechSynthesis){console.error('Speech synthesis not supported');if(listenText)listenText.textContent='Audio not supported';return;}if(activeLine)activeLine.style.width='0%';if(barLine){barLine.style.cursor='pointer';barLine.style.userSelect='none';barLine.addEventListener('click',function(e){e.preventDefault();e.stopPropagation();const rect=barLine.getBoundingClientRect();const clickX=e.clientX-rect.left;seekToPosition(Math.max(0,Math.min(100,(clickX/rect.width)*100)));});}const handleClick=function(e){e.preventDefault();e.stopPropagation();console.log('Play/pause clicked, isPlaying:',isPlaying,'isPaused:',isPaused);if(isPlaying)pauseAudio();else if(isPaused)resumeAudio();else startAudio();};if(audioLink)audioLink.addEventListener('click',handleClick);if(audioCircle)audioCircle.addEventListener('click',handleClick);if(audioIcon)audioIcon.addEventListener('click',handleClick);console.log('Audio player initialized successfully');}function startAudio(){console.log('startAudio called');if(!speechSynthesis)return;speechSynthesis.cancel();clearIntervals();charactersRead=0;pausedElapsed=0;speechDuration=estimateSpeechDuration(articleText);if(activeLine){activeLine.style.width='0%';activeLine.style.transition='none';}currentUtterance=new SpeechSynthesisUtterance(articleText);currentUtterance.lang='en-US';currentUtterance.rate=0.92;currentUtterance.pitch=0.88;currentUtterance.volume=1;const voices=speechSynthesis.getVoices();const selectedVoice=findMaleVoice(voices);if(selectedVoice){currentUtterance.voice=selectedVoice;console.log('Using voice:',selectedVoice.name,'lang:',selectedVoice.lang);}let startCharIndex=0;currentUtterance.onboundary=function(event){if(event.name==='word'||event.name==='sentence'){const charIndex=event.charIndex;if(charIndex!==undefined&&charIndex!==null)charactersRead=Math.min(startCharIndex+charIndex,totalCharacters);}};currentUtterance.onstart=function(){console.log('Speech started');isPlaying=true;isPaused=false;speechStartTime=Date.now();updateUI(true);startProgressUpdate();};currentUtterance.onend=function(){console.log('Speech ended');charactersRead=totalCharacters;isPlaying=false;isPaused=false;updateUI(false);if(activeLine)activeLine.style.width='100%';clearIntervals();};currentUtterance.onerror=function(event){console.error('Speech error:',event);isPlaying=false;isPaused=false;updateUI(false);clearIntervals();};speechSynthesis.speak(currentUtterance);}function pauseAudio(){console.log('pauseAudio called');if(speechSynthesis&&isPlaying){speechSynthesis.pause();isPlaying=false;isPaused=true;const elapsed=Date.now()-speechStartTime;const progress=Math.min((pausedElapsed+elapsed)/speechDuration,1);charactersRead=Math.floor(progress*totalCharacters);pausedElapsed+=elapsed;updateUI(false);clearIntervals();}}function resumeAudio(){console.log('resumeAudio called');if(speechSynthesis&&isPaused){const remainingText=articleText.substring(charactersRead);if(remainingText.trim().length===0){if(activeLine)activeLine.style.width='100%';isPlaying=false;isPaused=false;updateUI(false);return;}speechSynthesis.cancel();clearIntervals();currentUtterance=new SpeechSynthesisUtterance(remainingText);currentUtterance.lang='en-US';currentUtterance.rate=0.92;currentUtterance.pitch=0.88;currentUtterance.volume=1;const voices=speechSynthesis.getVoices();const selectedVoice=findMaleVoice(voices);if(selectedVoice)currentUtterance.voice=selectedVoice;let resumeStartCharIndex=charactersRead;currentUtterance.onboundary=function(event){if(event.name==='word'||event.name==='sentence'){const charIndex=event.charIndex;if(charIndex!==undefined&&charIndex!==null)charactersRead=Math.min(resumeStartCharIndex+charIndex,totalCharacters);}};currentUtterance.onstart=function(){isPlaying=true;isPaused=false;speechStartTime=Date.now();updateUI(true);startProgressUpdate();};currentUtterance.onend=function(){charactersRead=totalCharacters;isPlaying=false;isPaused=false;updateUI(false);if(activeLine)activeLine.style.width='100%';clearIntervals();};speechSynthesis.speak(currentUtterance);}}function seekToPosition(percentage){console.log('seekToPosition called:',percentage+'%');if(!speechSynthesis)return;speechSynthesis.cancel();clearIntervals();const targetCharacters=Math.floor((percentage/100)*totalCharacters);const remainingText=articleText.substring(targetCharacters);if(remainingText.trim().length===0){if(activeLine)activeLine.style.width='100%';isPlaying=false;isPaused=false;charactersRead=totalCharacters;updateUI(false);return;}charactersRead=targetCharacters;pausedElapsed=(percentage/100)*speechDuration;if(activeLine){activeLine.style.width=percentage+'%';activeLine.style.transition='none';}currentUtterance=new SpeechSynthesisUtterance(remainingText);currentUtterance.lang='en-US';currentUtterance.rate=0.92;currentUtterance.pitch=0.88;currentUtterance.volume=1;const voices=speechSynthesis.getVoices();const selectedVoice=findMaleVoice(voices);if(selectedVoice)currentUtterance.voice=selectedVoice;let seekStartCharIndex=charactersRead;currentUtterance.onboundary=function(event){if(event.name==='word'||event.name==='sentence'){const charIndex=event.charIndex;if(charIndex!==undefined&&charIndex!==null)charactersRead=Math.min(seekStartCharIndex+charIndex,totalCharacters);}};currentUtterance.onstart=function(){isPlaying=true;isPaused=false;speechStartTime=Date.now();updateUI(true);startProgressUpdate();};currentUtterance.onend=function(){charactersRead=totalCharacters;isPlaying=false;isPaused=false;updateUI(false);if(activeLine)activeLine.style.width='100%';clearIntervals();};speechSynthesis.speak(currentUtterance);}function updateUI(playing){console.log('updateUI called, playing:',playing);if(listenText)listenText.textContent=playing?'Stop Listening':'Listen Article';if(audioIcon){const pauseIconSrc='https://cdn.prod.website-files.com/6939a31d6f0751cc94b4a574/695c9fb4a53dbb4513f977f8_stop-icon.png';const originalIconSrc='https://cdn.prod.website-files.com/6939a31d6f0751cc94b4a574/695c9eb269cdd4ffda71deab_listen-icon.png';audioIcon.src=playing?pauseIconSrc:originalIconSrc;}}function startProgressUpdate(){if(progressIntervalId){clearInterval(progressIntervalId);progressIntervalId=null;}if(!activeLine)return;progressIntervalId=setInterval(function(){if(!isPlaying||!activeLine){if(progressIntervalId){clearInterval(progressIntervalId);progressIntervalId=null;}return;}const progress=charactersRead/totalCharacters;const percentage=Math.min(progress*100,100);activeLine.style.width=percentage+'%';if(percentage>=100){activeLine.style.width='100%';charactersRead=totalCharacters;if(progressIntervalId){clearInterval(progressIntervalId);progressIntervalId=null;}}},50);}function clearIntervals(){if(progressIntervalId){clearInterval(progressIntervalId);progressIntervalId=null;}}function estimateSpeechDuration(text){const words=text.split(/\s+/).filter(w=>w.trim().length>0).length;return(words/150)*60*1000;}if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',initAudioPlayer);}else{setTimeout(initAudioPlayer,200);}if(speechSynthesis){if(speechSynthesis.getVoices().length===0){speechSynthesis.addEventListener('voiceschanged',function(){console.log('Voices loaded');},{once:true});}}})();