$(document).ready(function() {
	$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=carlinscuderi&count=1&callback=?&include_rts=1", function(tweetdata) {
		var tl = $("#tweet-list");
		$.each(tweetdata, function(i,tweet) {
			tl.append("<li>“" + urlToLink(tweet.text) + "” " + relTime(tweet.created_at) + "</li>");
		});
		function urlToLink(text) {
			var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
			return text.replace(exp,"<a href='$1'>$1</a>");
		}
		function relTime(time_value) {
			time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,"");
			var parsed_date = Date.parse(time_value);
			var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
			var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000);
			if (timeago < 60) return '<br /><em style="font-size: 12px;">less than a minute ago</em>';
			else if(timeago < 120) return '<br /><em style="font-size: 12px;">about a minute ago</em>';
			else if(timeago < (45*60)) return '<br /><em style="font-size: 12px;">' + (parseInt(timeago / 60)).toString() + ' minutes ago</em>';
			else if(timeago < (90*60)) return '<br /><em style="font-size: 12px;">about an hour ago</em>';
			else if(timeago < (24*60*60)) return '<br /><em style="font-size: 12px;">about ' + (parseInt(timeago / 3600)).toString() + ' hours ago</em>';
			else if(timeago < (48*60*60)) return '<br /><em style="font-size: 12px;">1 day ago</em>';
			else return '<br /><em style="font-size: 12px;">' + (parseInt(timeago / 86400)).toString() + ' days ago</em>';
		}
	});
});
