const ITEMS_INCREMENT = 5;

Template.home.onCreated(function() {
  const text = 'The place João Bordalo calls home';
  DocHead.removeDocHeadAddedTags();
  DocHead.setTitle(text);
  DocHead.addMeta({ name: 'description', content: text });
  Session.set('postsLimit', ITEMS_INCREMENT);
});

Template.home.onRendered(function() {
  this.autorun(() => {
    const sub = this.subscribe('posts', Session.get('postsLimit'));
    if (sub.ready()) {
      Tracker.afterFlush(makeYoutubeResponsive);
    }
  });
});

Template.home.events({
  'click .showMore'(event) {
    const postID = event.currentTarget.id;
    const array  = Session.get('showMore') || [];
    const clone  = array.slice(0);
    clone.push(postID);
    Session.set('showMore', clone);
  }
});

Template.home.helpers({
  posts() {
    return Posts.find().fetch().map(function (p) {
      const post = p;
      post.body = post.body.replace('<iframe', '<div class="placeholder"><iframe');
      post.body = post.body.replace('</iframe>', '</iframe></div>');
      return post;
    });
  },
  moreResults() {
    return !(Posts.find().count() < Session.get('postsLimit'));
  },
  showMore(postID) {
    const auxArray = Session.get('showMore') || [];
    if (auxArray.indexOf(postID.toString()) === -1) {
      return false;
    }
    return true;
  }
});

// http://www.meteorpedia.com/read/Infinite_Scrolling
function showMoreVisible() {
  const target = $('#showMoreResults');
  if (!target.length) return;
  const threshold = ($(window).scrollTop() + $(window).height()) - target.height();
  if (target.offset().top < threshold) {
    if (!target.data('visible')) {
      target.data('visible', true);
      Session.set('postsLimit', Session.get('postsLimit') + ITEMS_INCREMENT);
    }
  } else if (target.data('visible')) {
    target.data('visible', false);
  }
}

// run the above func every time the user scrolls
$(window).scroll(showMoreVisible);

@@ -495,16 +495,74 @@
 %7D%0A  %7D);%0A
+  setTimeout(function() %7B Meteor.Gists.render() %7D, 1000);%0A
 %7D);%0A%0ATem
@@ -794,24 +794,84 @@
 e', clone);%0A
+    setTimeout(function() %7B Meteor.Gists.render() %7D, 1000);%0A
   %7D%0A%7D);%0A%0ATem

fix bug on gist render