//
// John Resig's implementation of pretty dates. without the jQuery plugin part.
// Tweaked it so that it accounts for receiving UTC time from server by using offsetInMilliseconds
//
/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// 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," ")),
    //  diff = (((new Date()).getTime() - date.getTime()) / 1000),
    //  day_diff = Math.floor(diff / 86400);
    var offsetInMilliseconds = (new Date()).getTimezoneOffset()*60*1000;
    // offsetInMilliseconds = (new Date()).getTimezoneOffset()*60*1000 - 7*60*60*1000; // uncomment this to test timestamps in development env
    
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
     diff = (((new Date()).getTime() - date.getTime() + offsetInMilliseconds) / 1000),
     day_diff = Math.floor(diff / 86400);
    
    if (diff < 0) {
        // in the future
        return (diff > -120 && "in about 1 minute" ||
			diff > -3600 && "in about " + Math.floor( diff / 60 )*-1 + " minutes" ||
			diff > -7200 && "in about an hour" ||
			diff > -86400 && "in about " + Math.floor( diff / 3600 )*-1 + " hours");
    }
    
	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 < 13 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}