
// transformer les balises [...] en <div class="..."> si ça correspond aux classes prédéfinies
let baliseWords = [
    'labeur',
    'free',
    'temps',
    'moment',
    'latour',
    'lampe',
    'bibliographie',
    'imgsmall',
    'fullpage2imgs',
    'fullpageimage',
    'tripleimgs',
    'tripleimgs_bottom',
    'tripleimgs2',
    'tripleimgs2_bottom',
    'bottomimg',
    'imgfullspreadleft',
    'imgfullspreadright',
    'imgfullspreadright_bleedtop',
    'imgfullspreadright_bleed',
    'breakbefore',
    'breakafter'
];

var bodyContent = $('#body').html();

bodyContent = bodyContent.replace(/\[([^\/\]]+)\]/g, function(match, word) {
  if (baliseWords.includes(word)) {
    return '<div class="' + word + '">';
  } else {
    return match;
  }
}).replace(/\[\/([^\]]+)\]/g, function(match, word) {
  if (baliseWords.includes(word)) {
    return '</div>';
  } else {
    return match;
  }
});

$('#body').empty().append(bodyContent);


// isoler les chiffres des titres
let moments = document.getElementsByClassName("moment");

for(let i = 0; i < moments.length; i++) {
    moments[i].innerHTML = "<span class='first-letter'>" + moments[i].innerHTML.substring(0, moments[i].innerHTML.indexOf('—')) + "</span>" + moments[i].innerHTML.slice(moments[i].innerHTML.indexOf(' —'));
}

// cleaner le balisage des éléments contenant plusieurs paragraphes
let contentToParse = [];
let labeurs = document.getElementsByClassName("labeur");
for(let i = 0; i < labeurs.length; i++) {
    contentToParse.push(labeurs[i]);
}

// créer header icons pour encarts 
let latours = document.getElementsByClassName("latour");
for(let i = 0; i < latours.length; i++) {
    contentToParse.push(latours[i]);

    let headContent = latours[i].firstChild
    let headerEl = document.createElement('div');
    headerEl.setAttribute('class', 'latour-header')
    let iconImg = document.createElement('img');
    iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/map.png');
    headerEl.append(iconImg);
    headerEl.append(headContent);

    latours[i].firstChild?.remove();
    latours[i].prepend(headerEl);
}

let lampes = document.getElementsByClassName("lampe");
for(let i = 0; i < lampes.length; i++) {
    contentToParse.push(lampes[i]);

    let headContent = lampes[i].firstChild
    let headerEl = document.createElement('div');
    headerEl.setAttribute('class', 'lampe-header')
    let iconImg = document.createElement('img');
    iconImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/lampe.png');
    headerEl.append(iconImg);
    headerEl.append(headContent);

    lampes[i].firstChild?.remove();
    lampes[i].prepend(headerEl);
}

for(let i = 0; i < contentToParse.length; i++) {
    if (contentToParse[i].childNodes.length == 1) {
        contentToParse[i].innerHTML = '<p>' + contentToParse[i].innerHTML + '</p>';
    } else {
        contentToParse[i].innerHTML = "<p>" + contentToParse[i].innerHTML.replace("<p>", "");
    }
        
    // micro-typo
    for(j = 0; j < contentToParse[i].childNodes.length; j++) {
        let contentHTML = contentToParse[i].childNodes[j].innerHTML;
        if (contentHTML != undefined) {
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' :', '&nbsp;:');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' ?', '&nbsp;?');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' !', '&nbsp;!');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('« ', '«&nbsp;');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' »', '&nbsp;»');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('“', '«&nbsp;');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll('”', '&nbsp;»');
            contentToParse[i].childNodes[j].innerHTML = contentToParse[i].childNodes[j].innerHTML.replaceAll(' min', '&nbsp;min');
        }
    }
}

// PAGES TITRES
let h3s = document.querySelectorAll('h3');
for (let h3 of h3s) {
    if (h3.nextElementSibling.firstChild.tagName == 'IMG') {
        h3.style.width = "50%";
        h3.nextElementSibling.firstChild.style.width = "60%";
        h3.innerHTML = h3.innerHTML.replace(/[^\s]*/, `<div class="gros-chiffre">${1}</div>`);
    }
}
