/**
 * Simple relative date. Requires Prototype.js (tested with 1.6.0_rc0 and 1.6.0_rc1).
 *
 * Author: Erik Hanson http://www.eahanson.com/
 */
var RelativeDate = Class.create({
  initialize: function(olderDate, newerDate) {
    this.milliseconds = newerDate - olderDate;
  },

  seconds: function() {
    return this.milliseconds / 1000;
  },

  minutes: function() {
    return this.milliseconds / 60000;
  },

  hours: function() {
    return this.milliseconds / 3600000;
  },

  days: function() {
    return this.milliseconds / 86400000;
  },

  /** Assumes there are 30.4 days in a month */
  months: function() {
    return this.milliseconds / 2626560000;
  },

  years: function() {
    return this.milliseconds / 31518720000;
  },

  /**
   * Returns a string like "4 days ago".
   *
   * Prefers to return values >= 2. For example, it would return "26 hours ago" instead of "1 day ago" but would return
   * "2 days ago" instead of "49 hours ago".
   */
  toString: function() {
    var functions = [
      ["years", this.years],
      ["months", this.months],
      ["days", this.days],
      ["hours", this.hours],
      ["minutes", this.minutes],
      ["seconds", this.seconds]
    ];

    for (var i = 0; i < functions.length; i++) {
      var result = Math.floor(functions[i][1].call(this));
      if (result >= 2) {
        return result + " " + functions[i][0] + " ago";
      }
    }

    return "1 second ago";
  }
});