/*
 * timeago: a jQuery plugin, version: 0.7.2 (2009-07-30)
 * @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($(timestamp).attr("title")));
	};
	var $t = $.timeago;

	$.extend($.timeago, {
		settings: {
			refreshMillis: 60000,
			allowFuture: false,
			strings: {
				prefixAgo: null,
				prefixFromNow: null,
				suffixAgo: "ago",
				suffixFromNow: "from now",
				ago: null, // DEPRECATED, use suffixAgo
				fromNow: null, // DEPRECATED, use suffixFromNow
				seconds: "just now",
				minutes: "%d minutes",
				hour: "an hour",
				hours: "%d hours",
				dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
				monthNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
			}
		},
		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 now = new Date();
			var abs = new Date();
			abs.setTime(now.getTime() - distanceMillis);
			
			var justTime = function(date) {
				var ampm = "am";
				var hrs = date.getHours();
				var mins = date.getMinutes();
				if (mins < 10) {
					mins = "0" + mins;
				}
				if (hrs == 0) {
					hrs = 12;
				} else if (hrs > 11) {
					if (hrs > 12)
						hrs -= 12;
					
					ampm = "pm";
				}
				
				return hrs + ":" + mins + ampm;
			};
			var justDate = function(date) {
				var yearPart = "";
				if (date.getFullYear() != now.getFullYear())
					yearPart = " " + date.getFullYear();
				return $l.monthNames[date.getMonth()] + " " + date.getDate() + yearPart;
			};
			
			var dayDist = Math.floor(distanceMillis / (24 * 60 * 60 * 1000));
			if (dayDist >= 6) {
				// full date format
				return justDate(abs) + " at " + justTime(abs);
			} else {
				if (now.getDate() == abs.getDate()) {
					var words = "";
					// earlier today
					if (minutes < 1) {
						return "Just now";
					} else if (hours < 1) {
						words = substitute($l.minutes, Math.round(minutes));
					} else if (hours <= 1.9) {
						words = substitute($l.hour, 1);
					} else { 
						words = substitute($l.hours, Math.round(hours));
					}

					return $.trim([prefix, words, suffix].join(" "));
				} else if (now.getDay() - 1 == abs.getDay() || (now.getDay() == 0 && abs.getDay() == 7)) {
					// yesterday
					return "Yesterday at " + justTime(abs);
				} else {
					// earlier this week -- day at ..
					return $l.dayNames[abs.getDay()] + " at " + justTime(abs);
				}
			}
		},
		parse: function(iso8601) {
			var s = $.trim(iso8601);
			s = s.replace(/-/,"/").replace(/-/,"/");
			s = s.replace(/Z/," UTC");
			s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
			return new Date(s);
		}
	});

	$.fn.timeago = function() {
		var self = this;
		self.each(refresh);

		var $s = $t.settings;
		if ($s.refreshMillis > 0) {
			setInterval(function() { self.each(refresh); }, $s.refreshMillis);
		}
		return self;
	};

	function refresh() {
		var date = $t.parse(this.title);
		if (!isNaN(date)) {
			$(this).text(inWords(date));
		}
		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
	document.createElement('abbr');
})(jQuery);

