/**
 * Social bookmarks widget.
 * @author Alex Jilkin
 */
(function($)
{
	$.fn.socialbookmarks = function(options)
	{
		options = $.extend({}, {
			bookmarks: "twitter|facebook|vkontakte|mail_ru|livejournal|bobrdobr|friendfeed|yandex|memori|stozakladok|moemesto",
			urls: {
				"twitter": "http://twitter.com/home?status=%T%",
				"facebook": "http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=%U%&amp;t=%T%",
				"vkontakte": "http://vkontakte.ru/share.php?url=%U%",
				"mail_ru": "http://connect.mail.ru/share?share_url=%U%",
				"livejournal": "http://www.livejournal.com/update.bml?event=%U%&subject=%T%",
				"bobrdobr": "http://bobrdobr.ru/addext.html?url=%U%&amp;title=%T%",
				"friendfeed": "http://friendfeed.com/?title=%T%&url=%U%",
				"yandex": "http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&amp;lurl=%U%&amp;lname=%T%",
				"memori": "http://memori.ru/link/?sm=1&amp;u_data[url]=%U%&amp;u_data[name]=%T%",
				"stozakladok": "http://www.100zakladok.ru/save/?bmurl=%U%&amp;bmtitle=%T%",
				"moemesto": "http://moemesto.ru/post.php?url=%U%&amp;title=%T%"
			},
			labels: {
				"twitter": "Tweet This!",
				"facebook":	"Share this on Facebook",
				"vkontakte": "Share this on Vkontakte",
				"mail_ru": "Share this on mail.ru",
				"livejournal": "Share this on LiveJournal",
				"bobrdobr": "Share this on BobrDobr",
				"friendfeed": "Share this on FriendFeed",
				"yandex": "Add this to Yandex.Bookmarks",
				"memori": "Add this to Memory.ru",
				"stozakladok": "Add this to 100 bookmarks",
				"moemesto":	"Add this to MyPlace"
			}
    	}, options);

		var html = "";

		if(options.bookmarks.length > 0)
		{
			var names = options.bookmarks.split("|"), title = options.title, url = options.url;

			if(title == undefined || title.length < 1)
				title = document.title;
			if(url == undefined || url.length < 1)
				url = document.location.href;

			for(var n in names)
			{
				var name = names[n], u = options.urls[name], l = options.labels[name];
				if(u != undefined)
				{
					html += "<li><a href=\"" + u.replace("%T%", title).replace("%U%", escape(url)) + "\" rel=\"nofollow external\" target=\"_blank\"" +
						(l == undefined ? "" : " title=\"" + l + "\"") +
						" class=\"tip " + name + "\">" + (l == undefined ? "&nbsp;" : l) + "</a></li>";
				}
			}
			html = "<ul class=\"social-bookmarks\">" + html + "</ul>";
		}
		return this.each(function(){
			$(this).html(html);
			if($.fn.tipTip != undefined)
				$(".tip", this).tipTip({defaultPosition: 'top'});
		});
	};
})(jQuery);
