$(document).ready(function() {	
	//create prettier hashtag and get tweets, er, comments
	$('.post_id span').each(function(){
		var commenthash = 'ldm' + $(this).text().slice(4);
		$(this).text(commenthash);
		$(this).wrap('<a href="http://search.twitter.com/search?q=%23' + commenthash + '" target="_blank"></a>');
		var comments = $(this).closest('.article').find('.comments');
		
		$.getJSON('http://search.twitter.com/search.json?q=%23' + commenthash + '&rpp=20&page=1&show_user=true&callback=?', function(data){
			if(data.results.length > 0){
				$(comments).append('<h3>Comments via Twitter</h3><ul id="' + commenthash + '"></ul>');
			}
			$.each(data.results, function(i, item) {
				var tweet = item.text.replace(/\#ldm\d+/, '');
				$('#' + commenthash).append("<li><a href='http://twitter.com/" + item.from_user + "'>@" + item.from_user + "</a>: " + tweet.linkify() + "</li>");
			}); 
		});
	});
	
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};
	
	$('img[align="left"]').css('margin', '0 0.5em 0.25em 0');
	$('img[align="right"]').css('margin', '0 0 0.25em 0.5em');
});

