(function($){
	$.fn.sharer = function(options) {
		var defaults = { 
			sharetitle: "", // sharing page's title
			shareurl: "", // URL to share			
			tweetVia: "", // via twitter account
			twitterRelated: "SiteYou", // related twitter account
			icons: "facebook,twitter,linkedin,viadeo,email,favoris"//,imprimer
	  	};
	 	var opts = $.extend(defaults, options);			
		if(opts.tweetVia){
			var tweet_via = '&amp;via='+ encodeURL(opts.tweetVia);
		} else {
			var tweet_via = '';
		}
	 	
	 	var iformat				=	new Object();
	 	iformat.facebook		=	"http://www.facebook.com/share.php?u={URL}&t={DESCRIPTION}";
	 	iformat.twitter			=	"http://twitter.com/share?lang=fr&text={DESCRIPTION}&url={URL}&related="+ encodeURL(opts.twitterRelated) + tweet_via;
	 	iformat.linkedin		=	"http://www.linkedin.com/shareArticle?mini=true&url={URL}&title={TITLE}&summary={DESCRIPTION}&source=";
		iformat.viadeo			= 	"http://www.viadeo.com/shareit/share/?url={URL}&title={TITLE}&overview={DESCRIPTION}&urllanguage=fr&encoding=UTF-8";
		iformat.email			=	"mailto:?subject="+ encodeURIComponent("Jette un oeil sur "+ document.title) +"&body="+ encodeURIComponent("Salut !\r\nTu devrais te rendre sur ce site :\r\n"+ document.location.href);
	 	iformat.favoris			=	'javascript:void(0);" onclick="alert(\'Pressez ctrl+D pour ajouter ce site à vos favoris !\'); return false';
		//iformat.imprimer		=	"{URL}?imprimer";
		
		var sharer_container = $('<div id="sharer_container"></div>');
		var jbutton = $('<a href="javascript:void(0);" id="sharer_button" title="Partagez ce site ou ajoutez-le à vos favoris"><img src="http://www.siteyou.com/js/sharer/images/jbutton.png"></a>');
		var sharer = $('<div id="sharer_holder"></div>');
		var title = $('<div id="sharer_title">Partagez ce site !</div>');
		var icons = $('<div id="sharer_icons"></div>');
		var plusone = '<g:plusone size="medium" href="'+ sharer_url() +'"></g:plusone>';
		var fbLikebox = '<iframe src="http://www.facebook.com/plugins/like.php?href='+ encodeURL(sharer_url()) +'&amp;send=false&amp;layout=button_count&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;width=90&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:21px;" allowTransparency="true"></iframe>';
		var tweets = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?lang=fr&amp;url='+ encodeURL(sharer_url()) +'&amp;text='+ encodeURL(sharer_metadescription()) + '&amp;related='+ encodeURL(opts.twitterRelated) + tweet_via +'" style="width:120px; height:21px"></iframe>';
		var extras = $('<div id="sharer_extras">'+ fbLikebox + tweets + plusone +'</div>'); // <p>Signalez que vous l\'aimez !</p>
		
		sharer.html(title); // add the title
		sharer.append(icons, extras); // add the icons and extras
		sharer_container.append(sharer, jbutton);		
		$('body').append(sharer_container).fadeIn(1000);
		
		var share = opts.icons.split(","); // Get buttons
		for ( var key in share ) {
			var services = share[key];
			var name = services.replace(/_/gi, " ");
			var url = iformat[services];
			if(url){
				url = url.replace("{TITLE}", encodeURL(sharer_title()));
				url = url.replace("{URL}", encodeURL(sharer_url()));
				url = url.replace("{KEYWORDS}", encodeURL(sharer_metakeywords()));
				url = url.replace("{DESCRIPTION}", encodeURL(sharer_metadescription()));				
				var sharelink = '<a href="' + url + '" target="_blank"><div class="sharer_icon"><img src="http://www.siteyou.com/js/sharer/icons/'+ name + '.png"><span>' + name + '</span></div></a>';
				$(sharelink).appendTo(icons);
			}
		}	
		
		return this.each(function() {
			$("#sharer_container").hoverIntent({
				over: function(){$("#sharer_container").animate({left: -5}, 400, function(){})}, 
				timeout: 600, 
				out: function(){$("#sharer_container").animate({left: -320}, 400, function(){})}
			});
		});
		
		function encodeURL(string) {
			if(!string){
				return "";
			} else {				
				return encodeURIComponent(string);
			}
		}
		
		function sharer_title() {
			if(opts.sharetitle) { 
				return opts.sharetitle;
			} else { 
				return document.title;
			}
		}
		
		function sharer_url(){
			if(opts.shareurl) {
				return opts.shareurl;
			} else {
				return document.location.href;
			}
		}

		var sharer_keywords = '';
		function sharer_metakeywords() { 
			if(sharer_keywords === undefined){
				metaCollection = document.getElementsByTagName('meta'); 
				for (i=0; i<metaCollection.length; i++) { 
					nameAttribute = metaCollection[i].name.search(/keywords/);
					if (nameAttribute!= -1) { 
						sharer_keywords = metaCollection[i].content;
						return sharer_keywords; 
					} 
				} 
			} else {
				return sharer_keywords;
			}
		} 

		var sharer_description = '';
		function sharer_metadescription() { 
			if(sharer_description === undefined){
				metaCollection = document.getElementsByTagName('meta'); 
				for (i=0; i<metaCollection.length; i++) { 
					nameAttribute = metaCollection[i].name.search(/description/);
					if (nameAttribute!= -1) { 
						sharer_description = metaCollection[i].content;
						return sharer_description; 
					} 
				} 
			} else {
				return sharer_description;
			}
		} 
	}
})(jQuery);	
