
/** converts timestamps to relative english terms.*/
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.";
  }
};

/** convert all the timestamps in element #id (tagged with id=difftime_generated) to use relative dates.*/
function fix_dates(id) {
  $("#"+id).find("#difftime_generated").each(function(){
      var from = this.innerHTML.substring(9);
      this.innerHTML = DateHelper.time_ago_in_words(from);
  });
}

function setup_init_focus_handlers(id, key_value, set_value) {
  $(id).bind("focus", function(e) {
      if ($(id).attr("value") == key_value) {
          $(id).attr("value", set_value);
      }
  });
  $(id).bind("blur", function(e) {
      if ($(id).attr("value") == set_value) {
          $(id).attr("value", key_value);
      }
  });
}

function maybe_search(formid) {
  // get the query string.
  query = $(formid).children("#q").attr("value");
  // send to the search ajax function.
  // for now, pretent the query is the results.
  // show in an overlay iframe or something.
  $("#popper").dialog();
}