if (!window.jQuery){
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'http://www.bistromkt.com.br/ptwitter/js/jquery-1.3.2.callback.js';
	document.getElementsByTagName("head")[0].appendChild(script);
} else {
	jQueryReady(jQuery);
}

pTweets = {
	body: '<ul class="ptmb_tweets"><li><a class="foto" href="#" target="_blank"><img src=""/></a><span class="user"></span><p></p><span class="date"></span></li></ul><a class="ptmb_siga IR" target="_blank" href="http://twitter.com/ptbrasil">Siga @ptbrasil</a><a class="ptmb_vertodos IR" href="#">Ver todos</a><input type="text" value="&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.bistromkt.com.br/ptwitter/js/minhabandeira.js&quot; id=&quot;ptminhabandeira&quot;&gt;&lt;/script&gt;" />'
};

function jQueryReady($){

	$.fn.linkify = function(){
	/* ricardobeat at gmail.com */
		return this.each(function(){
			var t = $(this).text().replace(' ww.', ' www.').replace(/(http:\/\/|www\.)[^\s]+/, function(a,b){
			var txt = a.substring(0, 50) + (a.length > 50 ? '...' : '');
				return '<a target="_blank" href="http://'+a.replace('http://','')+'">'+txt+'</a>';
			});

			$(this).html( t );
		});
	};

	function tempoRelativo(dat){
		var s = ~~( (+new Date-Date.parse(dat)) / 1000 );
		// unidade de tempo
		var un = (s<60) ? 'segundo'
			: (s /= 60)<60 ? 'minuto'
			: (s /= 60)<24 ? 'hora'
			: (s /= 24)<30.4 ? 'dia'
			: (s /= 30.4)<365 ? 'mes'
			: 'ano';
		un += (s=~~s)>1 ? 's' : '';
		return s+' '+un;
	};

	function atualizar(tweets){
		$.each( tweets, function(){

			if ($('#ptmb_twitter li[id=t'+this.id+']').length)
				return true;

			var tempo = tempoRelativo( this.created_at );
			var $twee = tpl.clone().attr('id', 't'+this.id);

			$twee
				.find('a')
					.attr('href', 'http://twitter.com/'+this.screen_name)
					.find('img').attr('src', this.profile_image_url).end()
					.end()
				.find('.user')
					.text(this.screen_name).end()
				.find('p')
					.html(this.text).linkify().end()
				.find('.date')
					.html(tempo + ' atr&aacute;s').end()
				.hide()
				.prependTo('#ptmb_twitter ul');

		});
	};

	var delay = 0;

	function updateTweets(tweets){
		if (tweets && tweets.results && tweets.results.length)
			atualizar( tweets.results.reverse() );
		else
			delay = !delay;
		$tweets = $('#ptmb_twitter li');
		$tweets.eq(0).fadeIn();
		var i = 0;

		function roll(){
			var atual = $tweets.eq(i), next = atual.next();
			atual.hide();
			if (next.length){
				next.fadeIn();
				i++;
				setTimeout(roll, 7000);
			} else {
				if (delay) {
					updateTweets();
				} else {
					var last = $tweets.eq(0).attr('id').substring(1);
					$.getJSON('http://www.bistromkt.com.br/ptwitter/tweets_json.php?callback=?', { since: last }, updateTweets);
					i = 0;
				}
			}
		};

		setTimeout(roll, 7000);

	};

	var tpl;

	$('head').append('<link rel="stylesheet" type="text/css" href="http://www.bistromkt.com.br/ptwitter/css/minhabandeira.css" />');
	$.getJSON('http://www.bistromkt.com.br/ptwitter/tweets_json.php?callback=?', function(tweets){
		$('#ptmb_twitter').remove(); //evitar duplicata
		var div = $('<div id="ptmb_twitter" />').insertAfter('#ptminhabandeira');
		div.html( pTweets.body );
		tpl = $('#ptmb_twitter li:first').remove();
		updateTweets( tweets );
		$('#ptmb_twitter')
			.find('.ptmb_codigo').click(function(){
				if (window.clipboardData)
					window.clipboardData.setData('Text', $('#ptmb_twitter input').val());
				return false;
			}).end()
			.find('input').bind('focus click', function(){
				$(this)[0].select();
		});
	});

};