$(function() {

  var description_lookup = new Array;
  description_lookup['about'] = 'About'
  description_lookup['invitatory'] = 'Invitatory';
  description_lookup['officeofreadings'] = 'Office of Readings';
  description_lookup['morningprayer'] = 'Morning Prayer';
  description_lookup['daytimeprayer'] = 'Daytime Prayer';
  description_lookup['eveningprayer'] = 'Evening Prayer';
  description_lookup['nightprayer' ] = 'Night Prayer';

  // display the first available post
  $('.do-post:first')
    .addClass('current')
    .fadeIn ('slow');

  // setup the right hand side navigation
  $('#navigation-sidebar li.page_item').each (function(i) {
    $(this).data ('division', 'division-' + (i % 5 + 1));

    $(this).hover(function() {
      $(this).addClass($(this).data ('division'));
    },
    function() {
      $(this).removeClass($(this).data('division')); 
    });
  });


  $('#amount,#amount-hidden').val('10.00');

  $('.amount').change(function () {
    $('#amount').val($(this).val());
    $('#amount-hidden').val($(this).val());
  });

  // setup the post navigation 
  $('#nav a,#day-nav-date a').click (function(e) {
    // follow the links on pages and other documents where
    // no DO content is loaded
    if (!$('.do-post').length) {
      return true;
    }

    e.preventDefault();
    // do nothing if we would be triggering the same content again
    if ($(this).is(".current")) 
      return false;

    var rel = $(this).attr('rel');

    // if there is no content we copy a place holder 
    if (!$('#'+rel).length) {
      var $el = $('#if-no-content-found').clone ();
      $el.attr ('id', rel).addClass ('do-post');
      $el.find ('span.description').html (description_lookup[rel] || rel);
      $('#posts').append ($el);
    }

    $('#nav .current').removeClass('current');
    $(this).addClass('current');
    $('.do-post:visible').fadeOut ('fast', function() {
      $('#'+rel).fadeIn();
    });
  });
});
