$(document).ready(function(){
	$('ul.tabs li').click(function(){
		$('.tweets, div.admin').hide();
		$('ul li').removeClass('active');
		$('.tweets[title="'+$(this).attr('title')+'"], div.admin[title="'+$(this).attr('title')+'"]').show();
		$(this).addClass('active');
	});
	
	// redirects all of the page link to a new window
	$('.tweets-container a').click(function() {
		this.target = '_blank';
	});
	$('#wib-link').click(function() {
		$(this).find('a').click();
	});
	setInterval('getContent();',10000);
});

function getContent(){
	$('.tweets').each(function(i,n){
		type =$(this).attr('title');
		lastID = LAST_IDS[$(this).attr('title')];

		$.get('/poll.php', {type:type,lastID:lastID}, function(data){
			if ( typeof(data.success) != 'undefined' && data.success == 1 ) {
				if( data.posts ){
					for ( var i in data.posts ){
					    $('.tweets[title="'+data.type+'"] .tweet:first').before('<div class="fade-me tweet" style="display:none;">'+data.posts[i]+'</div>');
					}					
				}	
				//fade them all in
				$('.tweets .tweet.fade-me').each(function(i,n){
					$(this).fadeIn(1000);
					$(this).removeClass('fade-me');
				});
			}
			if ( typeof(data.lastID) != 'undefined' && data.lastID ) {
				LAST_IDS[data.type] = data.lastID;
			}
			
		}, 'json');
		
	});
}