/*!
 * jQuery news feed flicker
 */

var newsFeed = null;
var loopInterval = null;

function animateFirst() {
	if (!loopInterval) {
		loopInterval = setInterval(animateFirst, 6500);
	}
	$(newsFeed).find('div.nfitem.twitter.status:first').each(function() {
		$(this).css({'position': 'absolute', 'top': 0})
		$(this).animate({
			top: '65',
			opacity: 'show'
		}, 1000, 'easeOutBounce', function() {
			$(this).delay(4000).animate({
				top: '0',
				opacity: 'hide'
			}, 1000, 'easeInElastic');
		});
		appendLastShown(this, newsFeed);
	});
}

function appendLastShown(obj, parent) {
	$(parent).append($(obj).remove());
}

$(document).ready(function() {
	$('div.newsfeed:first').each(function() {
		newsFeed = this;
		$(this).addClass('flicker');
		var hasItems = false;
		$(this).find('div.nfitem').each(function() {
			$(this).hide();
			hasItems = true;
		});
		if (hasItems && newsFeed) {
			animateFirst();
		}
	});
});