(function(SLIDES) {

  var remaining = SLIDES.length;
  var slidesContent = [];

  SLIDES.forEach(function(slideName, index) {
    fetchSlide(slideName, slideLoadedHandler.bind(null, index));
  });

  function slideLoadedHandler(index, content) {
    remaining--;
    slidesContent[index] = content;
    if(remaining === 0) {
      createSlideShow();
    }
  }

  function createSlideShow() {
    remark.create({
      source: slidesContent.join('\n---\n'),
      highlightStyle: 'github',
      highlightLanguage: 'javascript'
    });
  }

  function fetchSlide(slideName, cb) {
    var req = new XMLHttpRequest();
    req.open('GET', slideName+'.md', true);
    req.onload = function (e) {
      req.onload = null;
      if (req.readyState === 4) {
        if (req.status === 200) {
          var content = req.responseText.replace(/\r\n/g, '\n');
          return cb(content);
        } else {
          console.error('Impossible de charger "'+slideName+'.md" !', req.statusText);
          return cb(slideName+'.md');
        }
      }
    };
    req.send(null);
  }

}(SLIDES = SLIDES || []));