var queue = 0;
var toBeLoaded = 0;
var loaded = 0;

function setTitle() {
  $('#title').html(config.title);
  document.title = config.title;
}

function setNavbar() {
  for (var i = 0; i < config.navbar.length; i++) {
    var dom = '<a href="' + config.navbar[i].link + '">' + config.navbar[i].text + '</a>';
    if (i != 0)
      $('<span> • </span>').appendTo('#navbar');
    $(dom).appendTo('#navbar');
  }
}

function createPlaceholders() {
  for (var i = 0; i < config.atOnce; i++) {
    if (toBeLoaded != config.posts.length) {
      var dom = '<div id="post' + config.posts[toBeLoaded] + '"></div>';
      $(dom).appendTo('#posts');
      toBeLoaded++;
    } else
      break;
  }
}

function loadPosts() {
  $('#loader').show();
  for (;loaded < toBeLoaded; loaded++) {
    queue ++;
    $.get('https://api.github.com/gists/' + config.posts[loaded], function (data) {
      var gist = '';
      for (var j in data.files)
        gist = gist + marked.parse(data.files[j].content);
      var dom = '<div class="post">' +
                '<div class="post-text">' + gist + '</div>' +
                '<span class="date"><a href="' + data.html_url + '">' + new Date(data.updated_at) + '</a></span>' +
                ' • <span class="author">Posted by <a href="https://github.com/' + data.user.login + '">' + data.user.login + '</a></span>' +
                '</div><hr>';
      $(dom).appendTo('#post' + data.id);
    }, 'json').done(function() {
      queue --;
      if (queue == 0)
        $('#loader').hide();
    });
  }
}

function loadLink(gist_id) {
  $.get('https://api.github.com/gists/' + gist_id, function (data) {
    var gist = '';
    for (var j in data.files)
      gist = gist + marked.parse(data.files[j].content);
      title = data.files[j].filename;
      updated_at = new Date(data.updated_at);
    var dom = '<div class="post">' + '<a href="' + data.html_url + '">' + title + '</a>' + ' •  <span class="author">last edited </span>' +
              '<span class="author">' + updated_at.getMonth() + '.' + updated_at.getDate() + '.' + updated_at.getFullYear() + '</span>' +
              // ' • <span class="author">posted by <a href="https://github.com/' + data.user.login + '">' + data.user.login + '</a></span>' +
              '</div>';
    $(dom).appendTo('#post' + data.id);
    }, 'json').done(function() {
  });
}

function loadAllLinks() {
  $('#loader').show();
  for (;loaded < toBeLoaded; loaded++) {
    queue ++;
    $.get('https://api.github.com/gists/' + config.posts[loaded], function (data) {
      // var gist = '';
      // for (var j in data.files)
      //   gist = gist + marked.parse(data.files[j].content);
      //   title = data.files[j].filename;
      //   updated_at = new Date(data.updated_at);
      // var dom = '<div class="post">' + title + ' •  ' +
      //           '<span class="date"><a href="' + data.html_url + '">' + updated_at.toDateString() + '</a></span>' +
      //           ' • <span class="author">Posted by <a href="https://github.com/' + data.user.login + '">' + data.user.login + '</a></span>' +
      //           '</div>';
      // $(dom).appendTo('#post' + data.id);
      gist_id = data.id
      loadLink(gist_id);
    }, 'json').done(function() {
      queue --;
      if (queue == 0)
        $('#loader').hide();
    });
  }
}

function loadFooter() {
  $('#footer-text').html(config.footerText);
}

function loadOlder() {
  createPlaceholders();
  loadPosts();
  if (toBeLoaded == config.posts.length)
    $('#old-posts').hide();
}

$('#loader').hide();
setTitle();
setNavbar();
loadFooter();
$('#old-posts').bind('click', loadOlder);
createPlaceholders();
loadAllLinks();
if (toBeLoaded == config.posts.length)
  $('#old-posts').hide();

@@ -1670,24 +1670,65 @@
 data.files)%0A
+      console.log('this one has files');%0A
       gist =
@@ -1768,24 +1768,24 @@
 %5D.content);%0A
-
       title 
@@ -2300,24 +2300,49 @@
 %3C/div%3E';%0A   
+ console.log(dom);%0A    //
  $(dom).appe
@@ -2358,32 +2358,63 @@
 st' + data.id);%0A
+    $(dom).appendTo('#posts');%0A
     %7D, 'json').d
@@ -3262,16 +3262,17 @@
  data.id
+;
 %0A      l
@@ -3285,24 +3285,265 @@
 k(gist_id);%0A
+      forks = data.forks;%0A      console.log(forks.length);%0A      for(var i in forks) %7B%0A        console.log('this fork id is  ' +forks%5Bi%5D.id);%0A        thisId = forks%5Bi%5D.id;%0A        loadLink(thisId);%0A        console.log('thisId is ');%0A      %7D%0A
     %7D, 'json

append all posts to nonspecific #posts div rather than #post[id] div