/*
* JavaScript Pretty Date
* Copyright (c) 2008 John Resig (jquery.com)
* Licensed under the MIT license.
http://ejohn.org/projects/javascript-pretty-date/
*/

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time) {
//	var date = new Date((time || "").replace(/-/g, "/").replace(/[TZ]/g, " ")),		//RE - was failing 5/9/2009 2:59PM EST
	var date = new Date((time || "").replace(/-/g, "/")), 
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
		 

	if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31)
		return;

	return day_diff == 0 && (
			diff <    60 && "just now" ||
			diff <   120 && "1 minute ago" ||
			diff <  3600 && Math.floor(diff / 60) + " minutes ago" ||
			diff <  7200 && "1 hour ago" ||
			diff < 86400 && Math.floor(diff / 3600) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff <  7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil(day_diff / 7) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if (typeof jQuery != "undefined") {
	jQuery.fn.prettyDate = function() {
		return this.each(function() {
			var date = (this.title || this.innerHTML);  //RE	- fallback on contained text if no title attribute
			var pd = prettyDate(date);
			
			//RE - replace TITLE's complete date with common date from text (assuming server TZ != client TZ, so TITLE=complete date, text=common date)
			jQuery(this).attr('title', jQuery(this).text());    
			
			if (pd) {   // only replace text date if pretty version is available (<1 month)
				jQuery(this).text(pd);
			}
			
			
		});
	};
}
