// Edouard Nojaroff
// jQuery ShareService
//
// Description:
// Use this plugin in order to have links to Link sharing Services, like reddit
//
// usage:
// There are 2 ways:
// 1) Direct Use:
// 		$('#mylink').shareitem('reddit');
//		In this case element with Id "mylink" will become a link to reddit service
// 2) Global use:
//		$("#myblock").shareservice(options);
//		plugin will look for all links in element #myblock that have a class name
//		like "share_xxx", where xxx is the name of the service
//

(function(jQuery) {

	jQuery.fn.shareservice = function (options) {
		var options = jQuery.extend({}, jQuery.fn.shareservice.defaults, options);
 		jQuery.fn.shareservice.title = options['title'];
		return this.each(function() {
			var $cont = this;

//			updateItems($cont);

//			jQuery(document).unbind('set.share').bind('set.share', function(e, data) {
			jQuery(document).bind('set.share', function(e, data) {
			    jQuery.fn.shareservice.title = data;
			    updateItems($cont);
			});

		});

		function updateItems($cont) {
			var reg = /share_(\w+)/;

			jQuery("[class*='share_']", $cont).each(function() {
				var sres = reg.exec(this.className);
				if (sres) {
					jQuery(this).shareitem(sres[1], options);
				}
			});
		}
	};

    jQuery.fn.shareservice.title = "SportWeekTV";

	jQuery.fn.shareitem = function(service, options) {
		var options = jQuery.extend({}, jQuery.fn.shareservice.defaults, options);

		return this.each(function() {
			var $shCont = this;

			if (jQuery.fn.shareservice.services[service]===undefined)
				throw "Unrecognized service: " + service;

			var surl = jQuery.fn.shareservice.services[service].url.replace('{{url}}', encodeURIComponent(options['url'])).replace('{{title}}', encodeURIComponent(jQuery.fn.shareservice.title));

			var $elem;

			if (jQuery(this).attr('href')) {
				jQuery(this).attr('href', surl);
				$elem = $shCont;
			} else {
				jQuery(this).wrap('<a href="'+surl+'"></a>');
				$elem = jQuery(this).parent().get(0);

			}

			if (!!jQuery.fn.shareservice.services[service].func) {
				jQuery($elem).bind("click", function(e) { 
					return jQuery.fn.shareservice.services[service].func(surl, jQuery.fn.shareservice.title);
				});
			} else {
				if (!!options['callback'])
					jQuery($elem).bind('click', function(){ options['callback'].call(this);});
				if (options['new_window'])
					jQuery($elem).bind('click', function(e){ e.preventDefault(); window.open(this.href);} );
			}
		});
	};
 
    //default settings
    jQuery.fn.shareservice.defaults = {
        new_window: true,
        title: encodeURIComponent(document.title),
        url: encodeURIComponent(window.location.href),
        callback: null,
        builtin : false
    };


    //default services
    jQuery.fn.shareservice.services = {
		facebook: {
			name: "FaceBook",
			css: "facebook", 
			url: "http://www.facebook.com/sharer.php?u={{url}}&title={{title}}",
			func: function(url, title) {
				var u=location.href;
				window.open(
					'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(title),
					"FaceBook",
					'toolbar=0,status=0,width=626,height=436'
				);
				return false;
			}
		},

		twitter: {
			name: "TwitThis",
			css: "twitthis", 
			url: "http://twitthis.com/twit?url={{url}}&title={{title}}",
			func: function(url, title) {
				var u=location.href;
				window.open(
					'http://twitthis.com/twit?url='+encodeURIComponent(u)+'&title='+encodeURIComponent(title),
					'TwitThisPop', 
					'width=600, height=500, location, status, scrollbars, resizable'
				);
				return false;
			}
		},

			digg: { 
				name: "digg", 
				img: "digg", 
				url: "http://digg.com/submit?phase=3&url={{url}}&title={{title}}" 
			}, 

			delicious: { 
				name: "del.icio.us", 
				css: "delicious", 
				url: "http://del.icio.us/post?v=2&url={{url}}&title={{title}}",
				func: function(url, title) {
					var u=location.href;
					window.open(
						'http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='
						+encodeURIComponent(u)+'&amp;title='+encodeURIComponent(title),
						'delicious', 
						'toolbar=no,width=550,height=550'
					);
					return false;
				}
			},

			reddit: { 
				title: "reddit", 
				css: "reddit", 
				url: "http://reddit.com/submit?url={{url}}&title={{title}}" 
			}, 

			myspace: { 
				title: "mySpace", 
				css: "myspace", 
				url: "http://www.myspace.com/Modules/PostTo/Pages/?l=3&u={{url}}&t={{title}}&c=" 
			}, 

			google: { 
				title: "Google", 
				css: "google", 
				url: "http://www.google.com/bookmarks/mark?op=edit&bkmk={{url}}&title={{title}}" 
			}, 

			backflip: {
				title: "Backflip",
				css: "backflip", 
				url: "http://www.backflip.com/add_page_pop.ihtml?url={{url}}&title={{title}}" 
			},
			blinkbits: {
				title: "BlinkBits", 
				css: "blinkbits",
				url: "http://blinkbits.com/bookmarklets/save.php?v=1&source_url={{url}}&title={{title}}" 
			}, 
			blinklist: { 
				title: "Blinklist", 
				css: "blinklist", 
				url: "http://www.blinklist.com/index.php?Action=Blink/addblink.php&Title={{title}}&Url={{url}}" 
			}, 
			blogmarks: { 
				title: "blogmarks", 
				css: "blogmarks", 
				url: "http://blogmarks.net/my/new.php?mini=1&simple=1&url={{url}}&title={{title}}" 
			}, 
			buddymarks: { 
				title: "Buddymarks", 
				css: "buddymarks", 
				url: "http://buddymarks.com/add_bookmark.php?bookmark_title={{title}}&bookmark_url={{url}}" 
			}, 
			cityulike: { 
				title: "CiteUlike", 
				css: "citeulike", 
				url: "http://www.citeulike.org/posturl?url={{url}}&title={{title}}" 
			}, 
			diigo: { 
				title: "Diigo", 
				css: "diigo", 
				url: "http://www.diigo.com/post?url={{url}}&title={{title}}" 
			}, 
			dzone: { 
				title: "dzone", 
				css: "dzone", 
				url: "http://www.dzone.com/links/add.html?url={{url}}&title={{title}}" 
			}, 
			feedmarker: { 
				title: "FeedMarker", 
				css: "feedmarker", 
				url: "http://www.feedmarker.com/admin.php?do=bookmarklet_mark&url={{url}}&title={{title}}" 
			}, 
			feedmelinks: { 
				title: "Feed Me Links!", 
				css: "feedmelinks", 
				url: "http://feedmelinks.com/categorize?from=toolbar&op=submit&name={{title}}&url={{url}}&version=0.7" 
			}, 
			furl: { 
				title: "Furl", 
				css: "furl", 
				url: "http://www.furl.net/storeIt.jsp?t={{title}}&u={{url}}" 
			}, 
			givealink: { 
				title: "Give a Link", 
				css: "givealink", 
				url: "http://www.givealink.org/cgi-pub/bookmarklet/bookmarkletLogin.cgi?&uri={{url}}&title={{title}}" 
			}, 
			gravee: { 
				title: "Gravee", 
				css: "gravee", 
				url: "http://www.gravee.com/account/bookmarkpop?u={{url}}&t={{title}}" 
			}, 
			hyperlinkomatic: { 
				title: "Hyperlinkomatic", 
				css: "hyperlinkomatic", 
				url: "http://www.hyperlinkomatic.com/lm2/add.html?LinkTitle={{title}}&LinkUrl={{url}}" 
			}, 
			igooi: { 
				title: "igooi", 
				css: "igooi", 
				url: "http://www.igooi.com/addnewitem.aspx?self=1&noui=yes&jump=close&url={{url}}&title={{title}}" 
			}, 
			kinja: { 
				title: "kinja", 
				css: "kinja", 
				url: "http://kinja.com/id.knj?url={{url}}" 
			}, 
			lilisto: { 
				title: "Lilisto", 
				css: "lilisto", 
				url: "http://lister.lilisto.com/?t={{title}}&l={{url}}" 
			}, 
			linkagogo: { 
				title: "Linkagogo", 
				css: "linkagogo", 
				url: "http://www.linkagogo.com/go/AddNoPopup?title={{title}}&url={{url}}" 
			}, 
			linkroll: { 
				title: "Linkroll", 
				css: "linkroll", 
				url: "http://www.linkroll.com/index.php?action=insertLink&url={{url}}&title={{title}}" 
			}, 
			looklater: { 
				title: "looklater", 
				css: "looklater", 
				url: "http://api.looklater.com/bookmarks/save?url={{url}}&title={{title}}" 
			}, 
			magnolia: { 
				title: "Magnolia", 
				css: "magnolia", 
				url: "http://ma.gnolia.com/bookmarklet/add?url={{url}}&title={{title}}" 
			}, 
			maple: { 
				title: "maple", 
				css: "maple", 
				url: "http://www.maple.nu/bookmarks/bookmarklet?bookmark[url]={{url}}&bookmark[name]={{title}}" 
			}, 
			mesfavs: { 
				title: "MesFavs", 
				css: "", 
				url: "http://mesfavs.com/bookmarks.php/?action=add&address={{url}}&title={{title}}" 
			}, 
			netscape: { 
				title: "Netscape", 
				css: "netscape", 
				url: "http://www.netscape.com/submit/?U={{url}}&T={{title}}" 
			}, 
			netvouz: { 
				title: "netvouz", 
				css: "netvouz", 
				url: "http://netvouz.com/action/submitBookmark?url={{url}}&title={{title}}&popup=no" 
			}, 
			newsvine: { 
				title: "Newsvine", 
				css: "newsvine", 
				url: "http://www.newsvine.com/_wine/save?u={{url}}&h={{title}}" 
			}, 
			rawsugar: { 
				title: "Raw Sugar", 
				css: "rawsugar", 
				url: "http://www.rawsugar.com/tagger/?turl={{url}}&tttl={{title}}&editorInitialized=1" 
			}, 
			rojo: { 
				title: "Rojo", 
				css: "rojo", 
				url: "http://www.rojo.com/add-subscription/?resource={{url}}" 
			}, 
			scuttle: { 
				title: "Scuttle", 
				css: "scuttle", 
				url: "http://scuttle.org/bookmarks.php/?action=add&address={{url}}&title={{title}}" 
			}, 
			segnalo: { 
				title: "Segnalo", 
				css: "segnalo", 
				url: "http://segnalo.com/post.html.php?url={{url}}&title={{title}}" 
			}, 
			shadows: { 
				title: "Shadows", 
				css: "shadows", 
				url: "http://www.shadows.com/shadows.aspx?url={{url}}" 
			}, 
			simpy: { 
				title: "Simpy", 
				css: "simpy", 
				url: "http://simpy.com/simpy/LinkAdd.do?title={{title}}&href={{url}}&v=6&src=bookmarklet" 
			}, 
			spurl: { 
				title: "Spurl",
				css: "spurl", 
				url: "http://www.spurl.net/spurl.php?v=3&title={{title}}&url={{url}}" 
			}, 
			squidoo: { 
				title: "Squidoo", 
				css: "squidoo", 
				url: "http://www.squidoo.com/lensmaster/bookmark?{{url}}" 
			}, 
			tagtooga: { 
				title: "tagtooga", 
				css: "tagtooga", 
				url: "http://www.tagtooga.com/tapp/db.exe?c=jsEntryForm&b=fx&title={{title}}&url={{url}}" 
			}, 
			tailrank: { 
				title: "Tailrank", 
				css: "tailrank", 
				url: "http://tailrank.com/share/?title={{title}}&link_href={{url}}" 
			}, 
			technorati: { 
				title: "Technorati", 
				css: "technorati", 
				url: "http://technorati.com/faves/?add={{url}}" 
			}, 
			unalog: { 
				title: "unalog", 
				css: "unalog", 
				url: "http://unalog.com/my/stack/link?url={{url}}&title={{title}}" 
			}, 
			wink: { 
				title: "Wink", 
				css: "wink", 
				url: "http://www.wink.com/_/tag?url={{url}}&doctitle={{title}}" 
			}, 
			wists: { 
				title: "wists", 
				css: "wists", 
				url: "http://www.wists.com/r.php?r={{url}}&title={{title}}" 
			}, 
			yahoo: { 
				title: "Yahoo My Web", 
				css: "yahoo", 
				url: "http://myweb2.search.yahoo.com/myresults/bookmarklet?u={{url}}&t={{title}}" 
			}, 
			zurpy: { 
				title: "zurpy", 
				css: "zurpy", 
				url: "http://tag.zurpy.com/?box=1&url={{url}}&title={{title}}" 
			}
		};


})(jQuery);