// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//extracted and modified from the typo code.
function show_dates_as_local_time() {
	$('p.published span').each(function(i) { $(this).replaceWith(get_local_time_for_date($(this).text()));});
}

function get_local_time_for_date(time) {
  system_date = new Date(time);
  user_date = new Date();
  delta_minutes = Math.floor((user_date - system_date) / (60 * 1000));
  if (Math.abs(delta_minutes) <= (3*7*24*60)) { // only last three weeks
    distance = distance_of_time_in_words(delta_minutes);
    return distance + ((delta_minutes < 0) ? ' from now' : ' ago')
  } else {
    return 'on ' + system_date.toLocaleDateString();
  }
}

// a vague copy of rails' inbuilt function,
// but a bit more friendly with the hours.
function distance_of_time_in_words(minutes) {
  if (minutes.isNaN) return "";
  minutes = Math.abs(minutes);
  if (minutes < 15) return ('a few minutes');
  if (minutes < 90) return ('about one hour');
  if (minutes < 1080) return (Math.round(minutes / 60) + ' hours');
  if (minutes < 1440) return ('one day');
  if (minutes < 2880) return ('about one day');
  else return (Math.round(minutes / 1440) + ' days')
}

