var twitter = {

	status : function() {
		var myTwitterRequest = new Request.JSONP({
			url: 'http://twitter.com/statuses/user_timeline/infectiousmedia.json',
			data: {
				count: '3'
			},
			noCache: true,
			onComplete: function(myTweets) {
				var el = document.id('twitter-post');
				el.empty();
				myTweets.each(function(tweet) {
					var myElement = new Element('li',{
						html:  '<p>' + convert(tweet.text) + '</p>'
					}).injectInside('twitter-post');

				});
			}
		}).send();		
		delete myTwitterRequest;

		function convert(text) {
   	 		text = text.replace(/@([^ ]\w*)/g,"<a href=\"http://twitter.com/$1\">@$1</a>");
    		text = text.replace(/\#([^ ]\w*)/g,"<a href=\"http://search.twitter.com/search?q=%23$1\">#$1</a>");
    		text = text.replace(/ http:\/\/([^ ]*)/g," <a href=\"http://$1\">http://$1</a>");

    		return text;
		}
		
	}

}

var delicious = {

	recent : function() {
		var myDeliciousRequest = new Request.JSONP({
			url: 'http://feeds.delicious.com/v2/json/infectiousmedia',
			data: {
				count: '5'
			},
			noCache: true,
			onComplete: function(myBookmarks) {
				//empty UL of default children
				var el = document.id('delicious-links');
				el.empty();
				//for each piece of data returned via JSON, create a new LI element and inject it inside the UL
				myBookmarks.each(function(bookmark) {
					var myElement = new Element('li',{
						html:  '<p><a href="' + bookmark.u + '">' + bookmark.d + '</a><br>' + bookmark.n + '</p>'
					}).injectInside('delicious-links');
				});
			}
		}).send();
		delete myDeliciousRequest;
	
	}

}

function initializer() {

	//run JSONP request for Twitter status
	twitter.status();
	//delicious JSONP callback
	delicious.recent();


	//make initializer available for garbage collection
	delete initializer;
}

//when the dom is ready, run initializer
window.addEvent('domready', initializer);