
// last tweet
(function(global){
	jQuery(function(){
		var $ = function(id){
			return document.getElementById(id);
		};
		var lastTweetContainer = $('last-tweet-container');
		if (!lastTweetContainer) return;
		var tweetDateContainer = $('last_tweet-date');
	
		global.show_tweets = function(tweets){
			var tweet = tweets[0];
			if (!tweet) return;
			var username = tweet.user.screen_name;
			var date = tweet.created_at;
			var msg = tweet.text
				.replace(/(:?(:?https?|s?ftp|ssh)\:\/\/[^"\s<>]*[^.,;'">:<>!\)\]\s])/g, '<span class="link" title="$1">$1</span>')
				.replace(/(^|\s)@(\w+)/ig, '$1<span class="mention">@$2</span>')
				.replace(/(^|\s)#(\w+)/ig, '$1<span class="hash">#$2</span>');
			lastTweetContainer.set('html', msg);
			if (tweetDateContainer) tweetDateContainer.set('html', global.prettyDate(date, 'pt-br'));
		};

		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.async = true;
		script.src = 'http://api.twitter.com/1/statuses/user_timeline.json?include_rts=true&screen_name=neylopesjr25125&callback=show_tweets&count=1';
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(script, s);
	});
})(this);


// Takes an ISO time and returns a string representing how
// long ago the date represents.
// by John Resig
// date format: "2008-01-28T20:24:17Z"
// "Mon May 31 20:58:10 +0000 2010"

(function(global){
	
	var months = {
		'jan': '01',
		'feb': '02',
		'mar': '03',
		'apr': '04',
		'may': '05',
		'jun': '06',
		'jul': '07',
		'aug': '08',
		'sep': '09',
		'oct': '10',
		'nov': '11',
		'dec': '12'
	};
	
	var GMT = {
		'pt-br': -3
	};
	
	var reTwitterDate = /^\w+\s+(\w+)\s+(\d+)\s+([\d:]+)\s+[+\d]+\s+(\d+)$/i;
	
	global.prettyDate = function(time, gmt){
		if (typeof gmt == 'string') gmt = GMT[gmt.toLowerCase()];
		gmt = (gmt || 0) * 3600;
		var bigDate = '';
		var date = new Date((time || "").replace(reTwitterDate, function(a, mes, dia, horas, ano){
			var d = ano + '/' + months[mes.toLowerCase()] + '/' + dia;
			bigDate = d + ' ás ' + horas;
			return d + ' ' + horas;
		}));
		
		var diff = ((new Date().getTime() - date.getTime()) / 1000) - gmt,
			day_diff = Math.floor(diff / 86400);
		
		if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) return bigDate;

		return day_diff == 0 && (
			diff < 60 && 'agorinha' ||
			diff < 120 && 'há 1 minuto' ||
			diff < 3600 && 'há ' + Math.floor( diff / 60 ) + ' minutos' ||
			diff < 7200 && 'há 1 hora' ||
			diff < 86400 && 'há ' + Math.floor( diff / 3600 ) + ' horas') ||
			day_diff == 1 && 'Ontem' ||
			day_diff < 7 && 'há ' + day_diff + ' dias' ||
			day_diff < 31 && 'há ' + Math.ceil( day_diff / 7 ) + ' semanas';
	};
	
})(this);
