/***********************************************************************************
*	Author: Hermes Gadella <hgadella@gmail.com>
*	Description: Class that makes an object 'social network sharer'
*	
*	Usage:
*		HTML:
*			<button type="button" class="shareLink" socialnet="facebook">
*			
*		JS: 
*			$('.shareLink').SocialShare();
*		
*		NOTES:
*			By default, the class will take the page title as title and the current url as
*			url to share.
*			
***********************************************************************************/
(function($)
{					
	$.fn.SocialShare = function(options)
	{
		$.fn.SocialShare.defaults = 
		{
			title_to_share : document.title, 					  /* Title to send. By default the page title. */
			url_to_share : location.href,	  					  /* Url to send. By default the current URL */
			social_net_attr : "socialnet"
		};
				
		var $opts = $.extend({}, $.fn.SocialShare.defaults, options); /* PUT DEFAULT VALUES IN A VARIABLE */

		return this.each(function()
		{
			//alert($opts.title_to_share + "\n" + $opts.url_to_share);
			var $social_network = $(this).attr($opts.social_net_attr);
			var $sharing_url;
			
			var strTtlReplace = "#TITLE#";
			var strUrlReplace = "#URL#";
			
			switch($social_network)
			{
				case "facebook":
					$sharing_url = "http://www.facebook.com/sharer.php?u=" + strUrlReplace + "&t=" + strTtlReplace;
					break;
				case "twitter":
					$sharing_url = "http://twitter.com/home?status=" + strTtlReplace + " " + strUrlReplace;
					break;
				case "buzz":
					$sharing_url = "http://www.google.com/buzz/post?url=" + strUrlReplace + "&message=" + strTtlReplace;
					break;
				case "bebo":
					$sharing_url = "http://www.bebo.com/c/share?Url=" + strUrlReplace + "&Title=" + strTtlReplace + "&TUUID=c583051f-6b2d-41ec-8dd0-a3a0ee1656c1&MID=8348657161";
					break;
				case "stumbleupon":
					$sharing_url = "http://www.stumbleupon.com/submit?url=" + strUrlReplace + "&title=" + strTtlReplace;
					break;					
				case "tuenti":
					$sharing_url = "http://www.tuenti.com/share?url=" + strUrlReplace;
					break;
				case "gmail":
					$sharing_url = "https://mail.google.com/mail/?ui=2&view=cm&fs=1&tf=1&su=" + strTtlReplace + "&body=" + strUrlReplace + "\n" + strTtlReplace + "&shva=1";
					break;
				case "blogger":
					$sharing_url = "http://www.blogger.com/blog-this.g?u=" + strUrlReplace + "&n=" + strTtlReplace;
					break;
			}
			
			$sharing_url = $sharing_url.replace(strUrlReplace,$opts.url_to_share);
			$sharing_url = $sharing_url.replace(strTtlReplace,$opts.title_to_share);
			
			$(this).click(function(){
				window.open($sharing_url);
			});
			
		});
		
	}
})(jQuery);
