//Used to set customer id, Reading language and preferred voice and if default audio should be silenced.
let customerID = 13655;
let lang = 'es_co';    
let voice = 'Carlos';
let stopAudio = true;

//DO NOT EDIT below this line!

//Adds CSS container
var rsCSS = document.createElement('style');
rsCSS.type = 'text/css';
rsCSS.id = 'rsCSS'    
var css = `
.rsbtn {
position:absolute!important;
top:25px;
left:435px;
width:385px!important
}
.rsbtn.rs_mobile {
z-index: 10;
top: 75px;
left: 129px;
margin-top: 0px;
width: 140px !important;
}
#ReadSpeakerPresContainer .rsbtn {
top:25px;
left:425px;
width:385px!important;
z-index: 5!important;
}`;

if (rsCSS.styleSheet) {   // IE
        rsCSS.styleSheet.cssText = css;
    } else {                // the world
        rsCSS.appendChild(document.createTextNode(css));
    }
document.getElementsByTagName('head')[0].appendChild(rsCSS);
                          
//Adds config script
var rsConf = document.createElement('script');
rsConf.id = 'rsConf';    
rsConf.type = 'text/javascript';
rsConf.text = '\nwindow.rsConf = { general: { usePost:true, readidOrder: true, skipHiddenContent:true }, ui: { mobileVertPos: "bottom=115" } };';
document.head.appendChild(rsConf);

//Adds DocReader config script
var docReaderConf = document.createElement('script');
docReaderConf.id = 'docReaderConf';    
docReaderConf.type = 'text/javascript';
docReaderConf.text = '\nwindow.rsDocReaderConf = {lang: \''+ lang +'\', img_href: \'https://media.readspeaker.com/buttons/images/listen_icons/icon_16px_black.gif\'};';

document.head.appendChild(docReaderConf);

//Adds webReader script (CustomerID)
var rsScript = document.createElement('script');
rsScript.type = 'text/javascript';
rsScript.id = 'rs_req_Init';  
rsScript.src = 'https://cdn1.readspeaker.com/script/' + customerID + '/webReader/webReader.js?pids=wr&dload=DocReader.AutoAdd';
document.head.appendChild(rsScript);

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

window.onpopstate = function() { setTimeout(window.pageSwap, 50); }
window.onload = window.onpopstate;

});

function pageSwap () {

//Onclick on Leer button
const leerList = document.getElementsByClassName('h5p-interactive-book-cover-readbutton');
for (var i = 0; i < leerList.length; i++) {
leerList[i].onclick = function () { setTimeout(afterClick, 50); }
    }

//Stopping default autoplay audio
if (stopAudio) {
    const audioList = document.getElementsByClassName('h5p-audio');
    for (var i = 0; i < audioList.length; i++) {
        audioList[i].pause();
        audioList[i].currentTime = 0;
        }
    }

    //Stopping player
    ReadSpeaker.q(function() {if (rspkr.ui.getActivePlayer()) {rspkr.ui.getActivePlayer().close();}});
    const playerExist = document.getElementById('ReadSpeakerContainer');
    const targetList = document.getElementsByClassName('h5p-interactive-book-status-header');
    for (var i = 0; i < targetList.length; i++) {
    
    if (!playerExist) {
    //Adds Player code
    const player = document.createElement('div');
    player.id = 'ReadSpeakerContainer';
    player.innerHTML = '<div id="readspeaker_button1" class="rs_skip rsbtn rs_preserve">\n<a rel="nofollow" class="rsbtn_play" accesskey="L" title="Escucha esta p&aacute;gina utilizando ReadSpeaker webReader" href="https://app-eu.readspeaker.com/cgi-bin/rsent?customerid=' + customerID +'&lang=' + lang + '&voice=' + voice + '&readclass=h5p-advanced-text,h5p-question,h5p-interactive-box-summary-progress,h5p-interactive-book-summary-list&url=">\n<span class="rsbtn_left rsimg rspart"><span class="rsbtn_text"><span>Escuchar</span></span></span>\n<span class="rsbtn_right rsimg rsplay rspart"></span></a></div>';
    targetList[i].prepend(player);
        }
    }

const prestationList = document.getElementsByClassName('h5p-presentation-wrapper');
const presPlayerExist = document.getElementById('ReadSpeakerPresContainer');
for (var i = 0; i < prestationList.length; i++) {

if (!presPlayerExist) {
    const presPlayer = document.createElement('div');
    presPlayer.id = 'ReadSpeakerPresContainer';
    presPlayer.innerHTML = '<div id="readspeaker_button2" class="rs_skip rsbtn rs_preserve">\n<a rel="nofollow" class="rsbtn_play" accesskey="L" title="Escucha esta p&aacute;gina utilizando ReadSpeaker webReader" href="https://app-eu.readspeaker.com/cgi-bin/rsent?customerid=' + customerID +'&lang=' + lang + '&voice=' + voice + '&readclass=h5p-slide&url=">\n<span class="rsbtn_left rsimg rspart"><span class="rsbtn_text"><span>Escuchar</span></span></span>\n<span class="rsbtn_right rsimg rsplay rspart"></span></a></div>';
    prestationList[i].prepend(presPlayer);

const footerBtnList = document.getElementsByClassName('h5p-footer-button');
for (var i = 0; i < footerBtnList.length; i++) {
footerBtnList[i].onclick = function () { setTimeout(afterClick, 250); }
            }
        }
    }
rs.init();
}

function afterClick () {
    console.log('Click!');
    ReadSpeaker.q(function() {if (rspkr.ui.getActivePlayer()) {rspkr.ui.getActivePlayer().close();}});
    //Stopping default autoplay audio
    if (stopAudio) {
    const audioList = document.getElementsByClassName('h5p-audio');
    for (var i = 0; i < audioList.length; i++) {
        audioList[i].pause();
        audioList[i].currentTime = 0;
        }
    }
}

H5Pscript = {
meta: {version: 'Beta',date: '2024-10-29'}
}