// 
// twitter_statuses.js
// 
// A modification of the official Twitter JavaScript badge found here:
//
//    http://twitter.com/account/badge
//
// This modification provides insertion of the user's image (linking to their
// twitter page), displaying one or more of the user's recent tweets (including
// time posted, linking to that tweet's permalink), plus a bunch of CSS hooks
// for the list items.
//
// This package contains an html file and associated CSS & image files, but they 
// are merely an example of usage and only this JavaScript file is needed (it
// can be embeded directly in any HTML document, if you prefer).
// 
// One might also want to integrate Jon Aquino's date parsing fix for IE:
//
//    http://jonaquino.blogspot.com/2006/12/twitter-increasing-number-of-twitters.html
// 
// v0.1   2007-05-02 - Morgan Aldridge <morgant@makkintosshu.com>
//                     Initial release.
// 

var elapsedTime = function(createdAt) {
    var ageInSeconds = (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
    var s = function(n) { return n == 1 ? '' : 's' };
    if (ageInSeconds < 0) {
        return 'just now';
    }
    if (ageInSeconds < 60) {
        var n = ageInSeconds;
        return n + ' second' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60) {
        var n = Math.floor(ageInSeconds/60);
        return n + ' minute' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24) {
        var n = Math.floor(ageInSeconds/60/60);
        return n + ' hour' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 7) {
        var n = Math.floor(ageInSeconds/60/60/24);
        return n + ' day' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 31) {
        var n = Math.floor(ageInSeconds/60/60/24/7);
        return n + ' week' + s(n) + ' ago';
    }
    if (ageInSeconds < 60 * 60 * 24 * 365) {
        var n = Math.floor(ageInSeconds/60/60/24/31);
        return n + ' month' + s(n) + ' ago';
    }
    var n = Math.floor(ageInSeconds/60/60/24/365);
    return n + ' year' + s(n) + ' ago';
}

// Make date parseable in IE [Jon Aquino 2007-03-29]
function fixDate(d) {
    var a = d.split(' ');
    var year = a.pop();
    return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' ');
}

function twitterCallback(obj) {
    var id = obj[0].user.id;
    var statuses_html = '';
    
    document.getElementById('my_twitter').innerHTML = '<a href="http://twitter.com/' + obj[0].user.screen_name + '"><img src="' + obj[0].user.profile_image_url + '" alt="' + obj[0].user.name + '" /></a>';
    
    for ( var i = 0; i < obj.length; i++ ) {
    	statuses_html += '<li class="';
    	if ( i == 0 ) {
    		statuses_html += 'first';
    	} else if ( i == (obj.length - 1) ) {
    		statuses_html += 'last';
    	}
    	if ( ((i+1) % 2) == 0 ) {
    		statuses_html += ' even';
    	}
    	else {
    		statuses_html += ' odd';
    	}
    	statuses_html += '">';
    	statuses_html += obj[i].text;
    	statuses_html +=' <span>(<a href="http://twitter.com/' + obj[i].user.screen_name + '/statuses/' + obj[i].id + '">' + elapsedTime(fixDate(obj[i].created_at)) + '</a>)</span></li>';
    }
    document.getElementById('my_twitter_statuses').innerHTML = statuses_html;
}