function pad2(n) {
  return (( n < 10 ) ? '0' : '' ) + n;
}

Date.prototype.yesterday = function() {
  return new Date(this.getTime() - 86400000);
}

Date.prototype.tomorrow = function() {
  return new Date(this.getTime() + 86400000);
}

Date.prototype.asFormattedString = function() {
  return this.getFullYear() + pad2(this.getMonth() + 1) + pad2(this.getDate());
}

String.prototype.toDate = function() {
  var temp = this;
  var y = parseInt(temp.substr(0, 4)); 
  var m = (temp.charAt(4) == '0') ? temp.substr(5, 1) : temp.substr(4, 2);
  var d = (temp.charAt(6) == '0') ? temp.substr(7, 1) : temp.substr(6, 2);
  return new Date(y, m - 1, d);
}

function stringAsDate(s) {
  // set date label to the date we need to render
  var year = parseInt(s.substr(0, 4));
  var month = parseInt(s.substr(4, 2));
  var day = parseInt(s.substr(6, 2));
  return new Date(year,month, day);
}

function debug(s) {
}

$(function() {
  var abbreviated_months = ['Jan','Feb', 'Mar', 'Apr','May', 'Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  var day_prefix = $('meta[name=server-query-date]').attr('content');
  if (!day_prefix) {
    day_prefix = new Date().asFormattedString();
  }

  // update the date label
  newDate = day_prefix.toDate();
  label = abbreviated_months[newDate.getMonth()] + " " + newDate.getDate(); 

  $('#day-nav-date-label').html(label);


  $('#link-to-yesterday').attr('href', "?date="+new Date().yesterday().asFormattedString());
  $('#link-to-tomorrow').attr('href', '?date='+new Date().tomorrow().asFormattedString());


  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['middayprayer'] = 'Daytime Prayer';
  description_lookup['eveningprayer'] = 'Evening Prayer';
  description_lookup['nightprayer' ] = 'Night Prayer';

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

  // 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');
    var post_id = rel+'-'+day_prefix;

    debug("activating #" + rel + '-' + day_prefix);

    // if there is no content we copy a place holder 
    if (!$('#'+post_id).length) {
      var $el = $('#if-no-content-found').clone ();
      $el.attr ('id', post_id).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() {
      $('#'+post_id).fadeIn();
    });
  });

  // 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());
  });
});

