window.onload = function(){
  // Fix button follow
  let follow = document.querySelector('#follow');
  feedUrl = location.href.replace(/^https?:/, 'feed:');
  follow.href = feedUrl;
  follow.onclick = window.open(feedUrl, "_self");
  // Fix button subtome
  document.querySelector('#subtome').href='https://www.subtome.com/#/subscribe?feeds='+location.href;
  // Convert ISO8601 To UTC
  for (let element of document.querySelectorAll('#articles > ul > li > div > h4, #feed > #header > h2#subtitle.date')) {
    let timeStamp = new Date(element.textContent);
    element.textContent = timeStamp.toUTCString();
  }
  // Parse Markdown
  for (let element of document.querySelectorAll('#articles > ul > li > div > p')) {
    let markDown = element.textContent
    element.innerHTML = marked.parse(markDown);
  }
  // Display a selection of suggested software.
  const selection = {
    'akregator' : {
      'name'  : 'Akregator',
      'image' : 'akregator.svg',
      'url'   : 'https://apps.kde.org/akregator/'
    },
    'leechcraft' : {
      'name'  : 'LeechCraft',
      'image' : 'leechcraft.png',
      'url'   : 'https://leechcraft.org/'
    },
    'liferea' : {
      'name'  : 'Liferea',
      'image' : 'liferea.svg',
      'url'   : 'https://lzone.de/liferea/'
    },
    'raven' : {
      'name'  : 'Raven Reader',
      'image' : 'raven.svg',
      'url'   : 'https://ravenreader.app/'
    },
    'rssguard' : {
      'name'  : 'RSS Guard',
      'image' : 'rssguard.png',
      'url'   : 'https://github.com/martinrotter/rssguard'
    },
    'rssowl' : {
      'name'  : 'RSSOwl',
      'image' : 'rssowl.svg',
      'url'   : 'http://www.rssowl.org/'
    },
    'tickr' : {
      'name'  : 'TICKR',
      'image' : 'tickr.png',
      'url'   : 'https://www.open-tickr.net/'
    }
  }
  let selectionLink = document.querySelector('#selection-link');
  selectionLink.addEventListener ('click', function() {
    let elementDiv = document.createElement('div');
    elementDiv.id = 'selection-page';
    let elementH1 = document.createElement('h1');
    elementH1.textContent = 'Get A News Reader';
    elementDiv.appendChild(elementH1);
    let elementH2 = document.createElement('h2');
    elementH2.textContent = 'Install Feed Reader Apps For Desktop And Mobile';
    elementDiv.appendChild(elementH2);
    let elementH3 = document.createElement('h3');
    elementH3.textContent = '' +
      'This is a selection of desktop applications, mobile apps and online ' +
      'services for you to choose from. This selection includes news ' +
      'readers, podcast managers, torrent clients, chat bots, HTML browsers ' +
      'and plugins which support syndication feeds.';
    elementDiv.appendChild(elementH3);
    const brands = Object.keys(selection);
    let elementDivSel = document.createElement('div');
    elementDivSel.id = 'selection';
    for (let i = 0; i < brands.length; i++) {
      let brand = brands[i];
      elementSpan = document.createElement('span');
      let elementA = document.createElement('a');
      elementA.href = selection[brand].url;
      elementA.textContent = selection[brand].name;
      let elementImg = document.createElement('img');
      elementImg.src = 'graphic/' + selection[brand].image;
      elementSpan.appendChild(elementImg);
      elementSpan.appendChild(elementA);
      elementDivSel.appendChild(elementSpan);
      elementDiv.appendChild(elementDivSel);
    }
    let elementDivReturn = document.createElement('div');
    elementDivReturn.id = 'return';
    elementDivReturn.textContent = 'Return To PubSub...';
    elementDivReturn.addEventListener ('click', function() {
      document.querySelector('#selection-page').remove();
    });
    elementDiv.appendChild(elementDivReturn);
    document.body.appendChild(elementDiv);
  });
}
