/**
 * jQ plugin adapted from 37s' relative date tool
 * Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
 * @see http://37signals.com/svn/posts/1557-javascript-makes-relative-times-compatible-with-caching
 */
(function($, window, undefined){

    $.fn.relativeDate = function(opts){
        var defaults = {
            dateGetter: function(el){
                return $(el).text().replace(/-/g,"/");
            }
        },
        options = $.extend({}, defaults, opts),
        time_ago_in_words_with_parsing = function(from) {
            var date = new Date;
            date.setTime(Date.parse(from));
            return time_ago_in_words(date);
        },
        time_ago_in_words = function(from) {
            return distance_of_time_in_words(new Date, from);
        },
        distance_of_time_in_words = function(to, from) {
            var distance_in_seconds = ((to - from) / 1000);
            var distance_in_minutes = Math.floor(distance_in_seconds / 60);

            if (distance_in_minutes < 0) { return message("jquery.relativedate.en.lessthanminuteago"); }
            if (distance_in_minutes == 1) { return message("jquery.relativedate.en.oneminuteago"); }
            if (distance_in_minutes < 45) { return distance_in_minutes + " " + message("jquery.relativedate.en.minutesago"); }
            if (distance_in_minutes < 90) { return message("jquery.relativedate.en.aboutonehourago"); }
            if (distance_in_minutes < 1440) { return message("jquery.relativedate.en.aboutnhoursago", [ Math.round(distance_in_minutes / 60)]); }
            if (distance_in_minutes < 2880) { return message("jquery.relativedate.en.onedayago"); }
            if (distance_in_minutes < 43200) { return Math.floor(distance_in_minutes / 1440) + " " + message("jquery.relativedate.en.daysago"); }
            if (distance_in_minutes < 86400) { return message("jquery.relativedate.en.aboutonemonthago"); }
            if (distance_in_minutes < 525960) { return Math.floor(distance_in_minutes / 43200) + " " + message("jquery.relativedate.en.monthsago"); }
            if (distance_in_minutes < 1051199) { return message("jquery.relativedate.en.aboutoneyearago"); }
            return message("jquery.relativedate.en.overnyearsago", [Math.floor(distance_in_minutes / 525960)]);
        }

        return $(this).each(function(){
            date_str = options.dateGetter(this);
            $(this).html(time_ago_in_words_with_parsing(date_str));
        });
    };

})(jQuery, window);

