jQuery.fn.swapWith = function(to) {
    return this.each(function() {
        var copy_to = $(to).clone(true);
        var copy_from = $(this).clone(true);
        $(to).replaceWith(copy_from);
        $(this).replaceWith(copy_to);
    });
};


$(function() {
  $("tr:even").addClass("stripe");
  $('a[href="#add_comment"]').click(function() {
    $('#comments').hide();
    $('#add_comment, .pagenav_dup').show();
  });
  $('a[href="#cancel"]').click(function() {
    $('#add_comment, .pagenav_dup').hide();
    $('#comments').show().each(function() {
      $('.pagenav_dup').show();
    });
  });
  $('#comment_frm').submit(function(e) {
    if ($('#comment_name').val().length < 1) {
      alert("You need to enter your name.");
      e.preventDefault();
      return false;
    }
    if ($('#comment_comment').val().length < 1) {
      alert("You need to enter a comment.");
      e.preventDefault();
      return false;
    }
    if ($('#comment_qseca').val() != "15") {
      alert("You didn't answer our math question correctly.");
      e.preventDefault();
      return false;
    }
  });
  $('.screenshot').each(function() {
    prev = $(this).prev()
    if (!prev.is(".literallayout, .programlisting, h1, h2, h3, h4, h5, h6")) {
      $(this).swapWith(prev);
    } else {
      $(this).removeClass("screenshot").addClass("screenshot2");
    }
  });
  $('.screenshot').before("<div class='clearscreenshot'></div>");
  $('.literallayout, .programlisting').prev().before("<div class='clearscreenshot'></div>");
  $('.screenshot .imageobject img').wrap("<a>");
  $('.screenshot .imageobject a').each(function() {
    img = $(this).find("img");
    $(this).attr("href", img.attr("src"));
  }).lightBox();
});
