window.addEvent('domready', function()
{
	new Request.JSON({url: '/twitter/alc277.json', onSuccess: printTweets, 'method': 'GET' }).send();
});

function printTweets(resp)
{
	if($defined(resp))
	{
		var tweetContainer = $('tweets');
		tweetContainer.setStyle('display','');
		
		var show = 10;
		resp.each(function(tweet)
		{
			if(show > 0)
			{
				var inner = new Element('div',{ 'class' : 'tweet'}).inject(tweetContainer);
				
				var image = new Element('img',{ 'src' : tweet.user.profile_image_url }).inject(inner);
				var sp = new Element('span').inject(inner);
				var author = new Element('a',{ 'class' : 'author', 'text' : 'alc277', 'href' : 'http://twitter.com/alc277' }).inject(sp);
				var tweetText = tweet.text;
				
				tweetText = tweetText.replace(/\B#([_a-z0-9]+)/ig, function(hashtag) {
				      return '<a href="http://twitter.com/search?q=%23'+hashtag.substring(1)+'">'+hashtag+'</a>';
			    });
				
				tweetText = tweetText.replace(/\B@([_a-z0-9]+)/ig, function(hashtag) {
				      return '@<a href="http://twitter.com/'+hashtag.substring(1)+'">'+hashtag.substring(1)+'</a>';
			    });

				
				sp.innerHTML = sp.innerHTML + tweetText;
				
				var cb = new Element('div',{ 'class' : 'cb' }).inject(inner);
				
				postTime = new Date(tweet.created_at);
				
				var time = new Element('div',{ 'class': 'time', 'text' : postTime.timeDiffInWords()}).inject(inner);
				
			}
			else
			{
				return;
			}
			show--;
		});
		
		
		new Element('img',{ 'src' : '/img/twitter_logo_s.png' }).inject(new Element('div',{'class':'twitterLogo'}).inject(tweetContainer));
	}
}

