
// shamlessly ripped from http://gist.github.com/58761
var DateHelper = {
  // Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
  // Ruby strftime: %b %d, %Y %H:%M:%S GMT
  time_ago_in_words: function(from) {
    then = Date.parse(from)/1000;
    now = Date.parse(new Date())/1000;
    return this.difference_in_words(now - then);
  },

  difference_in_words: function(secs) {
      mins = Math.floor(secs / 60);
      if (mins == 0) return "less than a minute ago.";
      else if (mins == 1) return "a minute ago.";
      else if (mins < 45) return mins + " minutes ago.";
      else if (mins < 90) return "about an hour ago.";
      else if (mins < 1440) return "about " + Math.floor(mins/60) + " hours ago.";
      else if (mins < 2880) return "about a day ago.";
      else if (mins < 43200) return "about " + Math.floor(mins/1440) + " days ago.";
      else if (mins < 86400) return "about a month ago.";
      else if (mins < 525960) return Math.floor(mins/43200) + " months ago.";
      else if (mins < 1051199) return "about a year ago.";
      else return "over " + (mins / 525950) + " years ago.";
  }
};

// fix the date fields as soon as the document is loaded. My technique is to
// to prefix the html of any date I wish to replace with "difftime_". The text
// after "difftime_" is the GMT timestamp that gets parsed.
$(document).ready(function(){
   // set the difference dates for elements where id starts with difftime_
   $("span").find("span:contains('difftime_')").each(function(){
       var from = this.innerHTML.substring(9);
       this.innerHTML = DateHelper.time_ago_in_words(from);
   });
 });
 