function relativeDate(date)
{
    var now = new Date();
    var offset = now.getTime() - date.getTime();
    var distanceInMinutes = Math.round(offset / 60000);
    if (distanceInMinutes == 0) { return 'less than a minute'; }
    else if (distanceInMinutes < 1) { return 'about a minute'; }
    else if (distanceInMinutes < 44) { return distanceInMinutes + ' minutes';}
    else if (distanceInMinutes < 89) { return 'about 1 hour';}
    else if (distanceInMinutes < 1439) { return 'about ' + Math.round(distanceInMinutes / 60) + ' hours'; }
    else if (distanceInMinutes < 2879) {return '1 day'; }
    else if (distanceInMinutes < 43199) {return 'about ' + Math.round(distanceInMinutes / 1440) + ' days'; }                    else if (distanceInMinutes < 86399) {return 'about a month' }
    else if (distanceInMinutes < 525599) {return 'about ' + Math.round(distanceInMinutes / 43200) + ' months'; }
    else if (distanceInMinutes < 1051199) {return 'about a year';}
    else return 'over ' + Math.round(distanceInMinutes / 525600) + ' years';
}

$(document).ready(function() {
        if ($('#twitter'))
        {
                var twitterApi = 'http://twitter.com/statuses/user_timeline/unscriptedone.json?count=2&callback=?';
                $.getJSON(twitterApi, function (data) {
                        var update = data[0];
                        $('#twitter_update').html(update.text);

                        // get relative date
                        var timestamp = 'posted ';
                        timestamp += relativeDate(new Date(update.created_at));
                        timestamp += ' ago from ' + update.source;
                        $('#twitter_timestamp').html(timestamp);
                });                                                                                                                 }
});

