/**
 * main.js
 *
 * Author: Marian Friedmann
 *
 */


/**
 * Buzz function makes it possible to work with the disqus comment count.
 *
 * @param  {object} obj        containing relevant information
 * @param  {bool}   fullOutput triggers full output (including buzz-bar)
 *
 * obj.buzz        = wrapper element, containing disqus comment span
 * obj.steps       = steps for the buzz bar; optional
 * obj.buzzIcon    = character/string that is used to generate the
 *                   buzz-bar, according to obj.steps
 * obj.template    = template to output comments (and buzz-bar);
 *                   {{buzz}} and {{comments}} will be replaced
 *                   by buzz-bar and comment count
 * obj.activeClass = active class thats aplied to obj.buzz
 */

var Buzz = function(obj, fullOutput) {
  this.wrapper = obj.buzz;
  this.element = this.wrapper.childNodes[0];

  this.fullOutput = fullOutput || false;

  /*defaults*/
  this.steps = obj.steps || '20,50,100';
  this.buzzIcon = obj.buzzIcon || '💜';
  this.template = obj.template || '{{buzz}} {{comments}}';
  this.wrapperActiveClass = obj.activeClass || 'active';

  this.comments = false;
  this.progress = '';

  this.steps = this.steps.split(',');

  this.waitForContent();
};

Buzz.prototype.waitForContent = function(callback) {
  var buzz = this;
  var interval = setInterval(function() {
    if (buzz.element.innerHTML) {
      buzz.comments = buzz.element.innerHTML;
      clearInterval(interval);
      buzz.show();
    }
  }, 50);
};

Buzz.prototype.show = function () {
  var buzz = this;
  var content = this.template.replace('{{comments}}', this.comments);
  if (this.fullOutput) {
    buzz.steps.forEach(function(val,key) {
      if (buzz.comments >= parseFloat(val)) {
        buzz.progress += buzz.buzzIcon;
      }
    });
    content = content.replace('{{buzz}}', this.progress);
  }
  if (this.fullOutput && this.comments > this.steps[0] || !this.fullOutput) {
    this.wrapper.innerHTML = content;
    setTimeout(function() {
      buzz.wrapper.className = buzz.wrapper.className + ' ' + buzz.wrapperActiveClass;
    }, 100);
  }
};

$(function() {
  var $headerNav = $('.header-archive'),
      $archiveToggle = $('.archive-toggle'),
      $titleBar = $('#static-title-bar'),
      $body = $('body');

  $headerNav.headroom({
    offset: 180,
    tolerance: 5,
    classes : {
        pinned : 'header-archive--visible',
    },
    onPin : function() {
      if(!$headerNav.hasClass('header-archive')) $headerNav.addClass('header-archive');
      if(this.lastKnownScrollY < 200) $headerNav.removeClass('header-archive--visible');
    },
    onUnpin : function() {
      if(!$headerNav.hasClass('header-archive')) $headerNav.addClass('header-archive');
    },
    onTop : function() {
      if(!$headerNav.hasClass('header-archive')) $headerNav.addClass('header-archive');
      $headerNav.removeClass('header-archive--visible');
    },
    onNotTop : function() {
      if(!$headerNav.hasClass('header-archive')) $headerNav.addClass('header-archive');
    }
  });

  $archiveToggle.click(function() {
    $archiveToggle.toggleClass('archive-toggle--active');
    $headerNav.toggleClass('header-archive--active');
    $body.toggleClass('no-scroll');
    $titleBar.toggleClass('title-bar--hidden');
  });

  $('.post-content pre code').each(function() {
    $(this).parent().wrap('<div class="code-block"></div>');
  });

  $('.buzz-wrap').each(function() {
    var obj = {};
    obj.buzz = this;
    obj.buzzIcon = '🔥';
    obj.activeClass = 'buzz-wrap--active';
    obj.template = '<span class="buzz buzz--frontpage tooltip">{{buzz}}<span class="tooltip__content">{{comments}} Comments!</span></span>';
    var buzz = new Buzz(obj, true);
  });

  $('.comments').each(function() {
    var obj = {};
    obj.buzz = this;
    obj.activeClass = 'comments--active';
    obj.template = '{{comments}} Comments!';
    var buzz = new Buzz(obj);
  });

});

@@ -2111,16 +2111,847 @@
   %7D%0A%7D;%0A%0A
+var Disqus = function(callback) %7B%0A  var apiKey = '';%0A%0A  var xhr = new XMLHttpRequest();%0A  xhr.onreadystatechange = function() %7B%0A    if (xhr.readyState == 4) %7B%0A      var json = JSON.parse(xhr.responseText);%0A      callback(json);%0A    %7D%0A  %7D;%0A%0A  xhr.open('GET', 'https://disqus.com/api/3.0/forums/listThreads.json?forum=frdmn&limit=100&include=open&api_key='+apiKey, true);%0A  xhr.send(null);%0A%7D;%0A%0Avar test = new Disqus(function(data) %7B%0A  console.log(data.response);%0A  var r = 1;%0A  data.response.forEach(function(v,k) %7B%0A%0A    if(v.identifiers%5B0%5D) %7B%0A      console.log(r + ' ('+k+')');%0A      console.log();%0A      console.log(v.id, v.link);%0A      console.log('title: ' + v.title);%0A      console.log('identifier: ' + v.identifiers%5B0%5D);%0A      console.log('posts: ' + v.posts);%0A      console.log('--------------');%0A%0A      r++;%0A    %7D%0A  %7D);%0A%7D);%0A%0A
 $(functi

Add test function to work with disqus api