var twtCount = 0;
var twtLimit = 5;
$(document).ready(function() {
	
	var userName = '';
	$("p.twitter-link a").each(function(){
		showMinus();
		$t = $(this);
		//userName = $t.text();
		userName = $t.attr('rel');
		$t.parent().before('<ul class="block-nav twitter"></ul>');
		$('div.twitter h3').after('<div class="followers"></div><div class="following"></div>');
		$('ul.block-nav.twitter').hide();
		//$('div.twitter.followers').hide();
		$t.hide(); 
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
				$t.fadeIn();
				//$('div.twitter.followers').fadeIn();
			});	
		});
	});
	
	$('.twitter p.see-more a').click(function(){
		var $t = $(this);
		$('.twitter ul li').remove();
		$t.hide();
		$t.parent().parent().addClass('loading');
		twtCount+=twtLimit;
		showMinus();
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
				$t.fadeIn();
			});	
		});
		return false;
	});
	
	$('.twitter p.see-less a').click(function(){
		showMinus();
		var $t = $(this);
		$('.twitter ul li').remove();
		$t.parent().parent().addClass('loading');
		twtCount-=twtLimit;
		showMinus();
		if(twtCount < 0) {
			twtCount = 0;
		}
		$.getScript('http://twitter.com/statuses/user_timeline/'+userName+'.json?callback=twitterCallback&count='+twtCount+twtLimit, function() {			
			$('ul.block-nav.twitter').fadeIn('slow',function(){
				$('div.twitter.loading').removeClass('loading');
			});	
		});
		return false;
	});
});
// Taken from http://twitter.com/javascripts/blogger.js  //
// Modified by Ollie Relph 15/07/09                      //
function twitterCallback(twitters) {
	var statusHTML = [];
	$('div.twitter div.followers').html('Followers: ' +twitters[0].user.followers_count);
	$('div.twitter div.following').html('Following: ' +twitters[0].user.friends_count);
	for (var i=0; i<twitters.length; i++){
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		  return '<a href="'+url+'" target="_blank">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
		});
		if(i>=twtCount && i<twtCount+twtLimit) {
			statusHTML.push('<li style="display:none;"><p>'+status+' <a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'" target="_blank">*</a></p></li>');
		}
	}
	$('ul.block-nav.twitter').append(statusHTML.join(''));
	$('ul.block-nav.twitter li').fadeIn();
}
function showMinus() {
	if(twtCount > 0) {
		$('.twitter p.see-less a').show();
	} else {
		//$('.twitter').hide();
		$('.twitter p.see-less a').hide();
	}
	if(twtCount > twtLimit) {
		$('.twitter p.see-more a').hide();
	} else {
		$('.twitter p.see-more a').show();
	}

}
