;(function($){

	function bitly(url,callback)
	{
		var elem = this;

		url = htnlpecialchars_decode(url);

		// encodeURIComponent/decodeURIComponent = JS1.5
		// http://xkr.us/articles/javascript/encode-compare/
		url = decodeURIComponent(url);

		var restUri = 'http://api.bit.ly/shorten?version=2.0.1&login=yomomedia&apiKey=R_f8ec4aea598c4d48320d9a78b620117d&longUrl=' + encodeURIComponent(url) + '&callback=?';

		$.getJSON(restUri, function(data)
		{
			if ($.isFunction(callback)) callback.call(elem,data.results[url].shortUrl);
		});
	}

	$.fn.shortenUrl = function(callback,shorten)
	{
		if (!$.isFunction(shorten))
		{
			shorten = bitly;
		}

		return this.each(function()
		{
			var url = $(this).attr('href');

			if (url)
			{
				shorten.call(this,url,callback);
			}
		});
	};

	function htnlpecialchars_decode(str)
	{
		str = str.replace(/&amp;/gi,'&');
		str = str.replace(/&quot;/gi,'"');
		str = str.replace(/&#039;/gi,"'");
		str = str.replace(/&lt;/gi,'<');
		str = str.replace(/&gt;/gi,'>');
		return str;
	}

})(jQuery);