// the markdown updater
$(document).ready(function() {
  console.log(docCookies.keys());

  if (docCookies.getItem("fileLocation") && docCookies.getItem("fileContent") && docCookies.getItem("fileLocation") === "README.md") {
    $('.code-edit').html(decodeURIComponent(docCookies.getItem("fileContent")));
    docCookies.removeItem("fileLocation");
    docCookies.removeItem("fileContent");
  }

  $('.code-edit').attr('contenteditable', true)
  .on('focus', function() {
      var $this = $(this);
      $this.data('before', $this.html());
      return $this;
  }).on('blur keyup paste input', function() {
      var $this = $(this);
      if ($this.data('before') !== $this.html()) {
          $this.data('before', $this.html());
          $this.trigger('change');
      }
      return $this;
  }).on('change', function (e) {
    // console.log('updated');
    $('.btn.saveChanges').removeClass('hide');
  });

  $('.btn.saveChanges').click(function () {
    $('.code-edit .anchor').remove();

    var adjustHTML = '';
    $('.code-edit > div, .code-edit > p, .code-edit > pre').map(function (i, section) {
      adjustHTML += '<br/><br/>' + $(section).html();
    });

    if($("#repo-header-fork-btn").attr("href")) {
      // fork someone else's repo
      var two_hours = new Date((new Date() * 1) + 48 * 60 * 60 * 1000);
      docCookies.setItem("fileLocation", encodeURIComponent("README.md"), two_hours);
      docCookies.setItem("fileContent", encodeURIComponent(adjustHTML), two_hours);
      window.location.href = $("#repo-header-fork-btn").attr("href");
      return;
    }

    // save to own repo
    $.post('#save', {
      content: toMarkdown(adjustHTML, {
        gfm: true
      })
    }, function (response) {
      // error if not repo owner / authorized
      if (response === "OK") {
        window.location.reload();
      }
    });
  });
});

@@ -48,16 +48,306 @@
 ion() %7B%0A
+  function addCookie(cookieName, cookieValue) %7B%0A    cookieValue = encodeURIComponent(cookieValue);%0A    var myDate = new Date();%0A    myDate.setMonth(myDate.getMonth() + 12);%0A    document.cookie = cookieName +%22=%22 + cookieValue + %22;expires=%22 + myDate + %22;domain=.codemingler.com;path=/%22;%0A  %7D%0A%0A
   consol
@@ -1549,157 +1549,45 @@
     
-var two_hours = new Date((new Date() * 1) + 48 * 60 * 60 * 1000);%0A      docCookies.setItem(%22fileLocation%22, encodeURIComponent(%22README.md%22), two_hours
+addCookie(%22fileLocation%22, %22README.md%22
 );%0A 
@@ -1595,26 +1595,17 @@
     
-doc
+add
 Cookie
-s.setItem
 (%22fi
@@ -1620,49 +1620,18 @@
 t%22, 
-encodeURIComponent(adjustHTML), two_hours
+adjustHTML
 );%0A 

set cookie domain directly