/**
 * socialBadges - jQuery plugin to display submit badges for sites that support them
 *
 * @author Vance Lucas <vance@vancelucas.com>
 * @version 1.1.0
 * @requires jQuery v1.4.2 or above
 */
(function($) {
	$.fn.socialBadges = function(o) {
		// Default settings
		var o = o || {};
		if(typeof(o.sites) == "undefined") {
			o.sites = ['digg', 'stumbleupon', 'tweetmeme', 'facebook', 'delicious'];
		}
		
		// Return so call is chainable
		return this.each(function() {
			var tEl = $(this);
			var sb = {
				appendScriptTo: function(el, url) {
					// Uses the 'writeCapture' JS library as a dependency because the badges all use document.write()
					// @link http://github.com/iamnoah/writeCapture
					el.writeCapture().append('<div class="jqSBadge"><script type="text/javascript" src="'+url+'"></script></div>');
				},
				digg: function() {
					this.appendScriptTo(tEl, "http://digg.com/tools/diggthis.js");
				},
				stumbleupon: function() {
					this.appendScriptTo(tEl, "http://www.stumbleupon.com/hostedbadge.php?s=5");
				},
				tweetmeme: function() {
					// var tweetmeme_style = 'compact';
					this.appendScriptTo(tEl, "http://tweetmeme.com/i/scripts/button.js");
				},
				facebook: function() {
					this.appendScriptTo(tEl, "http://widgets.fbshare.me/files/fbshare.js");
				},
				delicious: function() {
					$.getJSON("http://feeds.delicious.com/v2/json/urlinfo/data?url="+encodeURIComponent(window.location)+"&callback=?", function(items) {
						var itemCount = 0;
						if(null !== items && items.length > 0) {
							itemCount = items[0].total_posts;
						}
						// Build the badge HTML ourselves
						var html = "<div class='jqSBadge' style='text-align:center;'>" +
							"<a href='http://delicious.com/save?url="+encodeURIComponent(window.location)+"&title="+document.title+"'>" +
							"<img src='https://static.delicious.com/14012407/img/logo/delicious.32px.gif' src='Save on Delicious'/></a><br />" +
							"<a href='http://delicious.com/save?url="+encodeURIComponent(window.location)+"&title="+document.title+"'>" +
							"<small>"+itemCount+" Saves</small></a></div>";
						tEl.append(html);
					});
				}
			};
			
			// For each site specififed, display badge
			for(var si in o.sites) {
				var site = o.sites[si];
				sb[site]();
			}
		});
	};
})(jQuery);
