$(document).ready(function() {
  // Show calendar
  if ($('#index_posts, #show_post').length > 0) {
    $('#calendar').show();
    $('#calendar .month').each(function() {
      addLinksToCalendarMonth($(this));
      $(this).hide();
    });

    // Get date for calendar to show from URL or current date
    var viewingPost = false;
    if (date = window.location.toString().match(/blog\/(\d+)\/(\d+)\/(\d+)\//)) {
      viewingPost = true;
      var year = parseInt(date[1]);
      var month = parseInt(date[2]);
      var day = parseInt(date[3]);
    } else {
      var now = new Date();
      var year = now.getYear();
      if (year < 2000) {
        year += 1900;
      }
      var month = now.getMonth() + 1;
      var day = now.getDate();
    }

    // Try to find the calendar to show
    var tries = 0;
    do {
      var el = $('#calendar .month.' + year + '-' + month);
      if (el.length > 0) {
        el.show();
        if (viewingPost) {
          $('#calendar #day_' + year + '-' + month + '-' + day).addClass('current');
        }
      } else {
        month--;
        tries++;
      }
    } while (el.length == 0 && tries < 5)
  }

  // Show prev and next links
  if ($('#show_post, #show_picture, #show_portfolio').length > 0) {
    $('#prev').show().css('left', $('#prev').offset().left - 50);
    $('#next').show().css('left', $('#next').offset().left + 740);
  }

  // Ajaxify comments
  if ($('#show_post').length > 0) {
    $('.activate-js').hide();
    $('#comments form').show().append(
      '<p>' +
        '<label for="comment_name">Name</label>' +
        '<br />' +
        '<input id="comment_name" name="comment[name]" size="30" type="text" />' +
      '</p>' +
      '<p>' +
        '<label for="comment_email">Email address (optional)</label>' +
        '<br />' +
        '<input id="comment_email" name="comment[email]" size="30" type="text" />' +
      '</p>' +
      '<p>' +
        '<label for="comment_website">Website (optional)</label>' +
        '<br />' +
        '<input id="comment_website" name="comment[website]" size="30" type="text" />' +
      '</p>' +
      '<p>' +
        '<label for="comment_content">Comment</label>' +
        '<br />' +
        '<textarea cols="40" id="comment_content" name="comment[content]" rows="20"></textarea>' +
      '</p>' +
      '<p>' +
        '<label for="comment_notify_about_new_comments">' +
          '<input type="checkbox" value="1" id="comment_notify_about_new_comments" name="comment[notify_about_new_comments]"> Notify on new comments? (Email address required)' +
        '</label>' +
      '</p>' +
      '<p><input id="comment_submit" name="commit" type="submit" value="Create comment" /></p>'
    ).submit(function() {
      $.post($(this).attr('action'), $(this).serializeArray(), null, 'script');
      return false;
    });
  }

  ajaxifyShowOlderPostsLinks();
  showReadMoreLinks();
});

function ajaxifyShowOlderPostsLinks() {
  // Ajaxify [show older posts] links
  if ($('#index_posts').length > 0) {
    $('#show-older-posts').click(function() {
      $.get(this.href, { showing : $('.post-content').length }, null, 'script');
      return false;
    });
  }
}

function showReadMoreLinks() {
  // Show [read more] links
  if ($('#index_posts').length > 0) {
    $('.read-more-link').each(function() {
      $(this).show().appendTo($(this).prev().append('&nbsp;'));
    });
  }
}

function addLinksToCalendarMonth(el) {
  if (el.prev().length > 0) {
    var prev_month = $('<a href="#" title="Show previous month" class="prev_month">&laquo;</a>');
    $(prev_month).click(function() {
      el.hide().prev().show();
    });
  } else {
    var prev_month = '<span class="prev_month">&laquo;</div>';
  }
  if (el.next().length > 0) {
    var next_month = $('<a href="#" title="Show next month" class="next_month">&raquo;</a>');
    $(next_month).click(function() {
      el.hide().next().show();
    });
  } else {
    var next_month = '<span class="next_month">&raquo;</div>';
  }
  el.find('.caption').append(next_month).prepend(prev_month);
}
