/*
 * timeago: a jQuery plugin, version: 0.7.1 (2009-02-18)
 * @requires jQuery v1.2 or later
 *
 * Timeago is a jQuery plugin that makes it easy to support automatically
 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
 *
 * For usage and examples, visit:
 * http://timeago.yarp.com/
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008-2009, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
 */
(function($) {
  $.timeago = function(timestamp) {
	if (timestamp instanceof Date) return inWords(timestamp);
	else if (typeof timestamp == "string") return inWords($.timeago.parse(timestamp));
	else return inWords($.timeago.parse($.timeago.getTimeInfo()));
  };
  var $t = $.timeago;
	var about = ""; //"&#8764;";
  $.extend($.timeago, {
	settings: {
	  refreshMillis: 60000,
	  interval: null,
	  allowFuture: false,
	  strings: {
		prefixAgo: null,
		prefixFromNow: null,
		suffixAgo: "", //"ago",
		suffixFromNow: "from now",
		ago: null, // DEPRECATED, use suffixAgo
		fromNow: null, // DEPRECATED, use suffixFromNow
		seconds: "< 1 min",
		minute: about+" 1 min",
		minutes: "%d min",
		hour: about+" 1 hr",
		hours: about+" %d hrs",
		day: "1 d",
		days: "%d d",
		month: about+" 1 mth",
		months: "%d mths",
		year: about+" 1 yr",
		years: "%d yrs"
	  }
	},
	inWords: function(distanceMillis) {
	  var $l = this.settings.strings;
	  var prefix = $l.prefixAgo;
	  var suffix = $l.suffixAgo || $l.ago;
	  if (this.settings.allowFuture) {
		if (distanceMillis < 0) {
		  prefix = $l.prefixFromNow;
		  suffix = $l.suffixFromNow || $l.fromNow;
		}
		distanceMillis = Math.abs(distanceMillis);
	  }

	  var seconds = distanceMillis / 1000;
	  var minutes = seconds / 60;
	  var hours = minutes / 60;
	  var days = hours / 24;
	  var years = days / 365;

	  var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
		seconds < 90 && substitute($l.minute, 1) ||
		minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
		minutes < 90 && substitute($l.hour, 1) ||
		hours < 24 && substitute($l.hours, Math.round(hours)) ||
		hours < 48 && substitute($l.day, 1) ||
		days < 30 && substitute($l.days, Math.floor(days)) ||
		days < 60 && substitute($l.month, 1) ||
		days < 365 && substitute($l.months, Math.floor(days / 30)) ||
		years < 2 && substitute($l.year, 1) ||
		substitute($l.years, Math.floor(years));

	  return $.trim([prefix, words, suffix].join(" "));
	},
	rParseDash: /-/,
	rParseT: /T/,
	rParseZ: /Z/,
	rParseTime: /([\+-]\d\d)\:?(\d\d)/,
	parseDate: function(iso8601) {
	  var s = $.trim(iso8601);
	  s = s.replace(this.rParseDash,"/").replace(this.rParseDash,"/");
	  s = s.replace(this.rParseT," ").replace(this.rParseZ," UTC");
	  s = s.replace(this.rParseTime," $1$2"); // -04:00 -> -0400
	  return s;
	},
	parse: function(iso8601) {
	  var s = this.parseDate(iso8601);
	  return new Date(s);
	},
	getDate: function(timestamp){
		var $timestamp = $(timestamp),
			datakey = 'ago-date',
			date = $timestamp.data(datakey);
		
		if (!date) {
			var time = $.timeago.getTimeInfo(timestamp);
			date = $t.parse(time);
			$timestamp.data(datakey, date);
		}
		
		return date;
	},
	getTimeInfo: function(timestamp){
		var $timestamp = $(timestamp);
		
		return $timestamp.attr('data-timestamp') || $timestamp.attr("timestamp") || $timestamp.attr("title");
	}
  });

  $.fn.timeago = function() {
	var self = this;
	self.each(refresh);

	var $s = $t.settings;
	if ($s.refreshMillis > 0 && !$s.interval) {
	  $s.interval = setInterval(function() { $('.ago').each(refresh); }, $s.refreshMillis);
	}
	return self;
  };

  function refresh() {
	var date = $.timeago.getDate(this);
	if (!isNaN(date)) {
	  this.innerHTML = inWords(date).replace(/</g, '&lt;');
	}
	return this;
  }

  function inWords(date) {
	return $t.inWords(distance(date));
  }

  function distance(date) {
	return (new Date().getTime() - date.getTime());
  }

  function substitute(stringOrFunction, value) {
	var string = $.isFunction(stringOrFunction) ? stringOrFunction(value) : stringOrFunction;
	return string.replace(/%d/i, value);
  }

  // fix for IE6 suckage
  if ($.browser.msie && $.browser.version < 7.0) {
	document.createElement('abbr');
  }
})(jQuery);

