var list1;
var list2;
var list3;
var list4;
var list5;
var list6;
var list7;
var list8;

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(' ');
}
// Handle two separate Twitter feeds [Todd Kooser 2009-02-20]
function twitterCallback1(obj){
	list1 = obj;
}
function twitterCallback2(obj){
	list2 = obj;
}
function twitterCallback3(obj){
	list3 = obj;
}
function twitterCallback4(obj){
	list4 = obj;
}
function twitterCallback5(obj){
	list5 = obj;
}
function twitterCallback6(obj){
	list6 = obj;
}
function twitterCallback7(obj){
	list7 = obj;	
}
function twitterCallback8(obj){
	list8 = obj;
	var __timer = window.setTimeout("compile();", 1000); // hopefully the last callback finishes last, but we'll wait an extra second to be sure.		
}

function sortByDate(a,b){
	var x = new Date(fixDate(a.created_at)).getTime();
	var y = new Date(fixDate(b.created_at)).getTime();
	return ((x<y) ? 1 : ((x>y) ? -1 : 0));
}
function compile(){
	var list_final = list1.concat(list2);
	list_final = list_final.concat(list3);
	list_final = list_final.concat(list4);
	list_final = list_final.concat(list5);
	list_final = list_final.concat(list6);
	list_final = list_final.concat(list7);
	list_final = list_final.concat(list8);
	list_final.sort(sortByDate);
	var html = '';
	var regexstring = "(http:\/\/[a-zA-Z0-9\.\/]+)";
	var regex = new RegExp(regexstring, "gi");
	var userstring = "@([a-z0-9]+)";
	var userregex = new RegExp(userstring, "gi");
	for (var i = 0; i < 6; i++) {
	 var text1 = list_final[i].text.replace(regex,'<a href="$1" target="_blank">$1</a>');
	 var text = text1.replace(userregex, '@<a href="http://www.twitter.com/$1" target="_blank">$1</a>');
      html += '<li><img src="' + list_final[i].user.profile_image_url + '" alt="" style="width:48px; height:48px"><a href="http://www.twitter.com/' + list_final[i].user.screen_name + '" target="_blank"><span class="twitname">' + list_final[i].user.screen_name +  '</span></a> ' + text + ' (' + elapsedTime(fixDate(list_final[i].created_at)) + ')</li>';
    }
    document.getElementById('twitter_update_list').innerHTML = html;
}