$(document).ready(function() {
  function initializeSyntaxHighlihting() {
    hljs.initHighlighting();
  }

  function initializeExpandablePreElements() {
    $("pre").hover(function(e) {
      /*
       * We want to remember the original width only once because during the
       * second invocation, we may be in the middle of the animation and the width
       * may not be correct.
       */
      if (!$(this).data("originalWidth")) {
        $(this).data("originalWidth", $(this).width());
      }

      var delta = $(this).children().width() - $(this).width();
      if (delta > 0) {
        $(this).css({
          position: "relative",
          zIndex:   1
        });
        $(this).animate({ width: $(this).children().width() + "px" }, "fast");
      }
    }, function(e) {
      if ($(this).data("originalWidth") < $(this).width()) {
        $(this).animate({ width: $(this).data("originalWidth") + "px" }, {
          duration: "fast",
          callback: function() {
            $(this).css({
              position: "static",
              zIndex:   0
            });
          }
        });
      }
    });
  }

  function initializeNiceImageLinks() {
    hs.graphicsDir = "/vendor/highslide/graphics/";
    hs.showCredits = false;

    $("a[href] img").each(function() {
      var link = $(this).parent();
      if (/\.(gif|jpg|jpeg|png)(\?.*)?$/.test(link.attr("href"))) {
        link.click(function() { return hs.expand(this); });
      }
    });
  }

  function initializeAddCommentCheck() {
    $("#add-comment-form-check").val("42");
    $("#add-comment-form-check").parent().parent().hide();
  }

  /* Main */

  initializeSyntaxHighlihting();
  initializeExpandablePreElements();
  initializeNiceImageLinks();

  if ($("#add-comment-form")) {
    initializeAddCommentCheck();
  }
});
