
$(function() {
	
	// Flash ids
	var flash_id = 0;
	$.each($('object, embed'), function(i, flash_obj) {
		flash_id++;
		flash_obj.id = 'flash' + flash_id;
	});

	// Fetch data
	$.getJSON('http://www.buymeasoda.com/js/feeds/feeds.php?callback=?', function(data) {

		var output = '';
		
		// Twitter
		if (data.twitter) {
			var twitter = '';
			if (data.twitter.length > 0 && data.twitter[0].text) {
				twitter = data.twitter[0].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
					return '<a href="' + url + '">' + url + '</a>';
				}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
					return  reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
				});
			}
			if (twitter != '') {
				output += '<div id="twitter" class="box"><a href="http://www.twitter.com/buymeasoda/" class="follow">Follow Me</a><h3><a href="http://www.twitter.com/buymeasoda/"><span>Twitter</span></a></h3><div class="tweet"><p>&ldquo;' + twitter + '&rdquo;</p></div></div>';
			}
		}		
		
		// Flickr
		if (data.flickr) {
			var flickr = '';
			$.each(data.flickr, function(i, photo) {
				flickr += '<div class="photo">' + 
					'<a href="' + photo.url + '">' + 
					'<div class="photo_artwork">' + 
					'<img src="' + photo.image + '" width="75" height="75" alt="' + photo.title + '" />' + 
					'<div class="photo_frame"></div>' + 
					'</div>' +
					'</a>' + 
					'</div>';
			});
			if (flickr != '') {
				output += '<div id="flickr" class="box"><h3><a href="http://www.flickr.com/photos/buymeasoda/"><span>Flickr</span></a></h3>' + flickr + '</div>';
			}
		}
		
		// lastfm
		if (data.lastfm) {
			var lastfm = '';
			$.each(data.lastfm, function(i, album) {
				lastfm += '<div class="album">' + 
					'<a href="' + album.url + '">' + 
					'<div class="album_artwork">' + 
					'<img src="' + album.image + '" width="64" height="64" />' +
					'<div class="album_frame"></div>' + 
					'</div>' + 
					'</a>' + 
					'<ul class="album_details">' + 
					'<li class="album_artist"><a href="' + album.url.slice(0, album.url.lastIndexOf('/')) + '">' + album.artist + '</a></li>' + 
					'<li class="album_name"><a href="' + album.url + '">' + album.name + '</a></li>' + 
					'</ul>' + 
					'</div>';
			});
			if (lastfm != '') {
				output += '<div id="lastfm" class="box"><h3><a href="http://www.last.fm/user/evenmorebetter"><span>Last.fm</span></a></h3>' + lastfm + '</div>';
			}
		}

		// Pinboard
		
		if (data.pinboard) {
			var pinboard = '';
			$.each(data.pinboard, function(i, bookmark) {
				pinboard += '<p>' + 
					'<a href="' + bookmark.link + '">' + bookmark.title + '</a>' + 
					' &rsaquo; ' + bookmark.description + 
					'</p>';
			});
			if (pinboard != '') {
				output += '<div id="pinboard" class="box"><h3><a href="http://pinboard.in/u:buymeasoda/"><span>Pinboard</span></a></h3><div class="links">' + pinboard + '</div></div>';
			}
		}
		
		$('#sidebar').append(output);
		
	});

});
