// bof dom ready
$(function() {

	// some variables
	var		
		username = 'RevDoors', // twitter username
		src_tpl = 'http://twitter.com/statuses/user_timeline/'+username+'.json?callback=twitterCallback2&amp;count='
		max = false, // maximum tweets to display
		cur = 3, // number of tweets to display
		min = 1, // minimum tweets to display
		e = {}; // store cached elements

	e.box = $('#twitterbox'); // cache box element
	
	// attempt to load the twitter blogger file
	$.getScript('http://twitter.com/javascripts/blogger.js', function() {

		// build markup
		e.box.prepend(
			$('<h2>').attr('id', 'twitterheader').append(
				$('<span>').text('Twitter')
			),
			$('<div>').attr('id', 'twittercontrols').append(
				$('<span>').text('More twitter feeds'),
				$('<a>').attr('id', 'twitterless').attr('href','#lesstweets').html('  ').click(less_tweets),
				$('<a>').attr('id', 'twittermore').attr('href','#moretweets').html('  ').click(more_tweets)
			),
				$('<div>').addClass('clear'),
			$('<ul>').attr('id',  'twitter_update_list')
		);
		
		// cache some of the created elements
		e.list = $('#twitter_update_list');
		e.more = $('#twittermore');
		e.less = $('#twitterless');
		
		// load the tweets (maximum number)
		$.getScript('http://twitter.com/statuses/user_timeline/RevDoors.json?callback=twitterCallback2', function() {
			e.tweets = e.list.children();
			max = e.tweets.size();
			tweet_update();
		});
	
	});
	
	// show one more tweet in the list
	function more_tweets() {
		return tweet_update(cur++);
	};
	
	// show one less tweet in the list
	function less_tweets() {
		return tweet_update(cur--);
	};
	
	// update list and interface
	function tweet_update() {
		e.tweets.each(function(i) {
			if (i < cur) $(this).show();
			else $(this).hide();
		});
		if (cur === max) e.more.hide();
		else e.more.show();
		if (cur == min) e.less.hide();
		else e.less.show();
		return false;
	};
	
// eof dom ready
});
