// controls reformatting of tweets
var formatTweet = function ( txt ) {
	// formatting 'http://www.site.com' to '<a href="http://www.site.com">http://www.site.com</a>'
	var urls = txt.match( /http[^\s]+/gi );
	
	if ( urls ) for( var i = 0; i < urls.length; i++ ) txt = txt.replace( urls[i], '<a href="' + urls[i] + '">' + urls[i] + '</a>');

	// formatting '@username' to '<a href="http://twitter.com/username">@username</a>'
	var users = txt.match( /^@[^\s]+|\s@[^\s]+\b/gi );
	
	if ( users ) {
		for( var i = 0; i < users.length; i++ ) {
			users[i] = users[i].replace( ' ', '' );
			users[i] = users[i].replace( ':', '' );
			var user = users[i].replace( /@/, '' );
			
			txt = txt.replace( users[i], '<a href="http://twitter.com/' + user + '">' + users[i] + '</a>');
		}
	}
	
	return txt;
};

var twitterFeed = function ( data ) {
	var tweets = '';
	
	$( data ).each(function ( i ) {
							  
			var txt = this.text;
			
			tweets += '<li>'  + formatTweet( txt ) + '</li>';

			if ( i == data.length - 1 ) {
				
				$( '#twitter-updates' ).html( tweets );
				return false;
				
			}
	});
	
};

var doPrint = function ()
{
	$( 'a.print-this' ).click( function ()
		{
			window.print();

			return false;
		}
	);
};

var bindShowTags = function()
{
	$( 'a[href="#show-tags"]' ).toggle(
		function( e )
		{
			e.preventDefault();
			
			$( 'li.minor-tag' ).show();
			
			$( this ).text( 'Show less …' ).trigger( 'blur' );
		},
		function( e )
		{
			e.preventDefault();
			
			$( 'li.minor-tag' ).hide();
			
			$( this ).text( 'Show more …' ).trigger( 'blur' );
		}
	);
};

$( document ).ready( 
	function ()
	{
		doPrint();
		bindShowTags();	
		
		$('input[placeholder], textarea[placeholder]').placeholder();
	}
);

