$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=apratabr&count=2&callback=?", function(tweetData) {

	var tl = $("#tweet-list");
	
	$.each(tweetData, function(i,tweet) {
		
		tl.append("<div class=\"tweet\"><p>"+urlToLink(tweet.text)+"</p><div class=\"tweet_info\"><img width=\"10\" height=\"10\" src=\"../images/twitter_icon.png\"/><p class=\"hour\">"+relTime(tweet.created_at)+"</p></div><div class=\"clear\"></div></div>");
	});
});

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 'menos de um minuto atr&aacute;s';
	else if(timeago < 120) return 'h&aacute; um minuto atr&aacute;s';
	else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutos atr&aacute;s';
	else if(timeago < (90*60)) return 'h&aacute; uma hora atr&aacute;s';
	else if(timeago < (24*60*60)) return 'h&aacute; ' + (parseInt(timeago / 3600)).toString() + ' horas atr&aacute;s';
	else if(timeago < (48*60*60)) return '1 dia atr&aacute;s';
	else return (parseInt(timeago / 86400)).toString() + ' dias atr&aacute;s';	
}
