$(document).ready(function(){
		
	String.prototype.tweetlinks = function() {
		return this.replace(/http:\/\/\S+/g,  '<a href="$&" target="_blank">$&</a>').
					replace(/\s(@)(\w+)/g,    ' <a href="http://twitter.com/$2" target="_blank">@$2</a>').
					replace(/\s(#)(\w+)/g,    ' <a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>').
					replace(/^\w+/,           '<a href="http://twitter.com/$&" target="_blank">$&</a>');
	};
	
	$.ajax({url: "feed.php", dataType: "text", success: function(xml) {
		xmlDoc = $.parseXML(xml);
		items = $(xmlDoc).find("item");
		html = "<ul id=\"marquee\" class=\"marquee\">";
		for(var i=0; i < items.length; i++) {
			tweet = $( items[i] ).find("description");
			tlink = $( items[i] ).find("link");
			tlink = $(tlink[0]).text();
			
			html += "<li class='twitlink' rel='"+tlink+"'>";
			html += $( tweet[0] ).text().tweetlinks();
			html += "</li>";
		}
		html += "</ul>";
		$("#twitterfeed").append(html);
		$('.twitlink').bind('click', function(event){
			var url = $(this).attr('rel');
			window.open(url, '_blank');
		});
		$('.twitlink a').bind('click', function(event){
			$(this).parent().unbind('click');
		});
		$("#marquee").marquee();
		console.log('twitter loaded');
	}
	});
});

