(function($){
	$.fn.jshare = function(custom) {
var defaults = {
				
				jsharedir			:	'jshare/',	// Set the directory for the jshare files like style.css & script.js
				
				highlight			:	true,		// Highlight share icon on hover?
			
				buttons				:	"facebook,twitter,digg,delicious,yahoo,email,live,stumbleupon,orkut,google,tumblr,linkedin,myspace,evernote,bebo,technorati,reddit,newsvine,mixx,netvibes",	// Default buttons to display
				iconsdir			:	'jshare/images/icons/',	//  Default is jsharedir + images/icons/
				imageextension		:	"png",		// Icon image format (jpg, png, gif, etc...)
		
				blanktarget			:	true,		// Open links on new tab/window?
				stickybutton		:	true,		// Add the sticky button on the side?
			
				shareurl			:	'',			// URL to share (Default is your current page's URL)
				sharetitle			:	'',			// Page title to share (Default is your current page's title)
			
				overlay				:	true,		// cover up the background?
				overlayopacity		:	0.5,
				overlaycolor		:	'#000',
		
				speedin				:	300,		// Set durations of fadein in milliseconds
				speedout			:	300			// Set durations of fadeout in milliseconds

		  };
		  
var settings	= $.extend({}, defaults, custom);

// Add the stylesheet to the head if using jshare
$("head").append("<link />");
$("head").children(":last").attr({
								"rel": "stylesheet",
								"type": "text/css",
								"href": settings.jsharedir + 'style.css'
});

var jshare_box = [ // jshare box HTML
				  '<div id="jshare_box">',
				  '<div id="jshare_close">',
				  '</div>',
				  '<div id="jshare_content"><div id="share_content">',
				  '<div class="jshare_title"><strong>Share or Bookmark</strong> on the top Social networking Sites </div>',
				  '<div id="jshare_main">',
				  '</div></div></div></div>'
				  ];

overlay	= $('<div id="jshare-overlay" />').click(function() {  // jshare close function
														  $('#jshare_box').fadeOut(settings.speedout);
														  overlay.fadeOut(settings.speedout);
														  });

if(settings.overlay) { // if overlay true, add opacity and colour or set opacity to 0
				overlay.css({'background-color' : settings.overlaycolour, 'opacity' : settings.overlayopacity});
} else {
				overlay.css({'opacity' : '0.0'});
}

		// sticky button settings
var jbutton = $('<a href="javascript:void(0;)" id="jshare_button" title="Share and Bookmark this page"><img src="' + settings.jsharedir + 'images/jbutton.png" border="0" /></a>');
jbutton.click(function() { // When jbutton is clicked
						$('#jshare_box').fadeIn(settings.speedin);
						overlay.fadeIn(settings.speedin);
						});

$('body').append(  // If using sticky button, add these HTMl to body
				jshare_box.join(''), // Join jshare box HTML and display
				$(settings.stickybutton ? jbutton : ' ').fadeIn('slow'), // Fade in sticky button if true
				overlay // Add overlay
);

close_button = $('<a href="javascript:void(0);"><img border="0" src="' + settings.jsharedir + 'images/jshare_close.png" alt="close" /></a>').click(function() {
														  $('#jshare_box').fadeOut(settings.speedout);
														  overlay.fadeOut(settings.speedout);
														  }).appendTo('#jshare_close');


$('.jshare').click(function() { // When jshare class is clicked
							$('#jshare_box').fadeIn(settings.speedin);
							overlay.fadeIn(settings.speedin);
							});

var target = settings.blanktarget ? 'target="_blank"' : ''; // If target _blank

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

		// Get URL to share
		function jshare_url(){
			if(settings.shareurl) { // if share url is set
				return settings.shareurl;
			} else { 
			return document.location.href;
			}
		}
		
		function jshare_title(){
			if(settings.sharetitle) {
				return settings.sharetitle;
			} else {
				return document.title;
			}
		}
		
		// Encode url
		function encodeURL(string) {
			if(string === undefined){
				return "";
			}
			return string.replace(/\s/g, '%20').replace('+', '%2B').replace('/%20/g', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
		}
		
// Sharing services URL format list
var jformat				=	Array();
jformat.digg			=	"http://digg.com/submit?phase=2&url={URL}&title={TITLE}";
jformat.linkedin		=	"http://www.linkedin.com/shareArticle?mini=true&url={URL}&title={TITLE}&summary={DESCRIPTION}&source=";
jformat.technorati		=	"http://www.technorati.com/faves?add={URL}";
jformat.delicious		=	"http://del.icio.us/post?url={URL}&title={TITLE}";
jformat.yahoo			=	"http://myweb2.search.yahoo.com/myresults/bookmarklet?u={URL}&t={TITLE}";
jformat.google			=	"http://www.google.com/bookmarks/mark?op=edit&bkmk={URL}&title={TITLE}";
jformat.newsvine		=	"http://www.newsvine.com/_wine/save?u={URL}&h={TITLE}";
jformat.reddit			=	"http://reddit.com/submit?url={URL}&title={TITLE}";
jformat.live			=	"https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={URL}&title={TITLE}&top=1";
jformat.facebook		=	"http://www.facebook.com/share.php?u={URL}";
jformat.twitter			=	"http://twitter.com/?status={TITLE}%20-%20{URL}";
jformat.stumbleupon		=	"http://www.stumbleupon.com/submit?url={URL}&title={TITLE}";
jformat.orkut			=	"http://promote.orkut.com/preview?nt=orkut.com&tt={TITLE}&du={URL}&cn={DESCRIPTION}";
jformat.bebo			=	"http://www.bebo.com/c/share?Url={URL}&title={TITLE}";
jformat.email			=	"mailto:?subject=Check out {TITLE}&body= You might like {TITLE}.%0D%0A {URL} %0D%0A {DESCRIPTION}";
jformat.evernote		=	"http://s.evernote.com/grclip?url={URL}&title={TITLE}";
jformat.mixx			=	"http://www.mixx.com/submit?page_url={URL}&title={TITLE}";
jformat.myspace			=	"http://www.myspace.com/Modules/PostTo/Pages/?u={URL}&title={TITLE}";
jformat.netvibes		=	"http://www.netvibes.com/share?title={TITLE}&url={URL}";
jformat.tumblr			=	"http://www.tumblr.com/share?v=3&u={URL}&t={TITLE}&s=";
jformat.googlebuzz		=	"http://www.google.com/reader/link?url={URL}&title={TITLE}&srcURL={URL}";

var buttons = settings.buttons.split(","); // Get buttons
		for ( var key in buttons ) {
			if (settings.buttons) {
			var name = buttons[key];
			var url = jformat[buttons[key]];
			if(url !== undefined){
				url = url.replace("{TITLE}"			, encodeURL(jshare_title()));
				url = url.replace("{URL}"			, encodeURL(jshare_url()));
				url = url.replace("{KEYWORDS}"		, encodeURL(jshare_metakeywords()));
				url = url.replace("{DESCRIPTION}"	, encodeURL(jshare_metadescription()));
				var sociallink = '<a ' + target + ' href="' + url + '" class="jshare_button" title="' + name + '"><img border="0" src="' + settings.iconsdir + name + '.' + settings.imageextension + '" alt="' + name + '" align="absmiddle" /> ' + name + '</a>';
				$(sociallink).appendTo('#jshare_main'); // Add buttons to jshare box
			}
			}
		}		
		
if(settings.highlight) { // If highlight true
	$("#jshare_main a").fadeTo("fast", 0.7).hover(
												   function(){$(this).fadeTo("fast", 1.0);},
												   function(){$(this).fadeTo("fast",0.7);}
												   );
}
		// returns the jQuery object to allow for chainability.
		return this;
	};
	
})(jQuery);

