jQuery(document).ready(function($) { 
  $(".contact").click(function () {
    $("#contact").slideToggle("slow");
  });

  // Open the contact form when pressing 'c' unless inside an input or a textarea
  $(document.documentElement).keypress(function(e){
    if (!($(e.target).is('input, textarea'))) {
      if(e.keyCode==99) $("#contact").slideToggle("slow"); // c
    }
  });

  // Close the contact form when the 'esc' key is pressed
  // Also blur the textarea or textfield so the 'c' key can be used again.
  $(document.documentElement).keyup(function(e){
    if(e.keyCode==27) {
      $("#contact").slideUp("slow"); // esc
      $("textarea, input").blur();
    }
  });

  $(document.documentElement).keyup(function(e){
    if (!($(e.target).is('input, textarea'))) {
      if(e.keyCode==74) {
        // J has been pressed
        if($('section#posts article a:focus').size() == 0){
          // This will only happen the first time when nothing has been focused yet

          $('section#posts article:first-child a').focus();
          // Focus the first article link
        } else {
          // Somthing is already focused

          if($('section#posts article a:focus').parent().next('section#posts article').children('a').size() == 0) {
            // If there is no more childeren focus the first article link
            $('section#posts article:first-child a').focus();
          } else {
            // If there is a child, focus the next article link
            $('section#posts article a:focus').parent().next('section#posts article').children('a').focus();
          }
        }
      }
    }

    if(e.keyCode==75) {
      if (!($(e.target).is('input, textarea'))) {
        // K has been pressed
        if($('section#posts article a:focus').size() == 0){
          // This will only happen the first time when nothing has been focused yet

          $('section#posts article:last-child a').focus();
          // Focus the last article link
        } else {
          // Somthing is already focused

          if($('section#posts article a:focus').parent().prev('section#posts article').children('a').size() == 0) {
            // If there is no more childeren focus the first article link
            $('section#posts article:last-child a').focus();
          } else {
            // If there is a child, focus the next article link
            $('section#posts article a:focus').parent().prev('section#posts article').children('a').focus();
          }
        }
      }
    }
  });

  // ? was pressed
  $(document.documentElement).keydown(function(e){
    if(e.keyCode==16) {
      isShiftPressed = true;
      $(document.documentElement).keydown(function(e){
        if(e.keyCode==191 && isShiftPressed == true) {
          $('#keyboard_help').slideDown();
        }
      });
    }
  });

  $(document.documentElement).keyup(function(e){
    if(e.keyCode==16) {
      isShiftPressed = false;
    }
  });

});

