function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;

	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

function formatTitle() {
	$('#twitterFeed p').each(
		function() {
			if ($(this).text().length < 50) {
				$(this).css('font-size', '1.8em');
				$(this).css('line-height', '1.9em');
				$(this).css('margin', '15px 0 0 0');
			}
			else if ($(this).text().length > 50 && $(this).text().length < 100) {
				$(this).css('font-size', '1.3em');
				$(this).css('line-height', '1.5em');
				$(this).css('margin', '24px 0 0 0');
			}
			else {
				$(this).css('font-size', '1.2em');
				$(this).css('line-height', '1.4em');
				$(this).css('margin', '30px 0 0 0');
			}
		}
		
	);
}

function loadTweets() {

	$.getJSON("http://search.twitter.com/search.json?&q=fedcan&count=10&callback=?", function(data) {

		for(var i in data.results) {
			var tweet = data.results[i];
			//$('.tweets').append('<li><p><strong>@'+tweet.from_user+': </strong>'+tweet.text+'</p></li>');
			$('#twitterFeed').append('<div class="blog-tweet"><p>'+tweet.text+'</p><div class="tweet-from"><img src="'+tweet.profile_image_url+'" /><span class="tweet-user"><a href="http://twitter.com'+tweet.from_user+'">'+tweet.from_user+'</a></span><span class="tweet-time">'+prettyDate(tweet.created_at)+'</span></div></div>');
			
		}
		formatTitle();
		
		$('#twitterFeed').cycle({
			fx: 'fade',
			timeout: 5000
		});
		
	});
}

$(document).ready(function() {
	// Stuff to do as soon as the DOM is ready;
});

