(function($){
	$.fn.social = function(custom) {
var defaults = {
			buttons: "Facebook,Twitter,Linkedin,Digg,Technorati,Delicious,Yahoo,Google,Live,Newsvine,Reddit,Stumbleupon,Misterwong,Mail",
			imagedir: "images/16/",
			imageextension: "png",
		  	highlight: true,
			blanktarget: true,
			shorturl: ""
		  };
var settings 	= $.extend({}, defaults, custom);
var social 		= this;

		// Set target attribute
var target = settings.blanktarget ? 'target="_blank"' : '';
		
		// Write social icons to browser
var buttons = settings.buttons.split(",");
		for ( key in buttons ) {
			var name = buttons[key];
			var url = jformat[buttons[key]];
			if(url != undefined){
				url = url.replace("{TITLE}"			, urlencode(social_title()));
				url = url.replace("{URL}"			, urlencode(social_url()));
				url = url.replace("{SHORTURL}"		, urlencode(social_short_url()));
				url = url.replace("{KEYWORDS}"		, urlencode(social_metakeywords()));
				url = url.replace("{DESCRIPTION}"	, urlencode(social_metadescription()));
				var sociallink = '<a ' + target + ' href="' + url + '" class="social_button social_tip" title="Add to ' + name + '"><img border="0" src="' + settings.imagedir + name + '.' + settings.imageextension + '" rel="noindex,nofollow" /></a>';
				this.append(sociallink);
			}
			
		}
		
		// If highlight bind mousehover and mouseout
		if(settings.highlight){
			// Trigger focus animation
			this.find(".social_button").bind("mouseover", function(){
				$(this).siblings().stop().animate({"opacity": 0.2}, 500);
			});
			
			this.find(".social_button").bind("mouseout", function(){
				$(this).siblings().stop().animate({"opacity": 1}, 500);
			});
		}
		
		// Meta keywords
var social_keywords;
		function social_metakeywords() { 
			if(social_description == undefined){
				metaCollection = document.getElementsByTagName('meta'); 
				for (i=0;i<metaCollection.length;i++) { 
					nameAttribute = metaCollection[i].name.search(/keywords/);
					if (nameAttribute!= -1) { 
						social_keywords = metaCollection[i].content;
						return social_keywords; 
					} 
				} 
			}else{
				return social_keywords;
			}
		} 
		
		// Meta description
		var social_description;
		function social_metadescription() { 
			if(social_description == undefined){
				metaCollection = document.getElementsByTagName('meta'); 
				for (i=0;i<metaCollection.length;i++) { 
					nameAttribute = metaCollection[i].name.search(/description/);
					if (nameAttribute!= -1) { 
						social_description = metaCollection[i].content;
						return social_description; 
					} 
				} 
			}else{
				return social_description;
			}
		} 
		
		// Title
		function social_title(){
			return document.title;
		}
		
		// Url
		function social_url(){
			var temp = document.location.href;
			return temp;
		}
		
		// Short url
		function social_short_url(){
			if(settings.shorturl == ""){
				return social_url();
			}else{
				return settings.shorturl;
			}
		}
		
		// Encode url
		function urlencode(string) {
			if(string == undefined){
				return "";
			}
			return string.replace(/\s/g, '%20').replace('+', '%2B').replace('/%20/g', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
		}
		
		function highlight(element, state){
			
			if(state){
				element.style.opacity = 1;
				element.childNodes[0].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100);";
			}else{
				element.style.opacity = highlight_opacity/100;
				element.style.filter = "alpha(opacity=20)";
				element.childNodes[0].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + highlight_opacity + ");";
			}
		}
		
		// returns the jQuery object to allow for chainability.
		return this;
	}
	
})(jQuery);

// Format list
var jformat				= Array();
jformat['Facebook'] 	= "http://www.facebook.com/share.php?u={URL}";
jformat['Twitter'] 		= "http://www.twitter.com/?status={TITLE}%20-%20{SHORTURL}";
jformat['Linkedin'] 	= "http://www.linkedin.com/shareArticle?mini=true&url={URL}&title={TITLE}&summary={DESCRIPTION}&source=";
jformat['Digg'] 		= "http://www.digg.com/submit?phase=2&url={URL}&title={TITLE}";
jformat['Technorati'] 	= "http://www.technorati.com/faves?add={URL}";
jformat['Delicious'] 	= "http://www.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['Live']		 	= "http://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={URL}&title={TITLE}&top=1";
jformat['Newsvine'] 	= "http://www.newsvine.com/_wine/save?u={URL}&h={TITLE}";
jformat['Reddit'] 		= "http://www.reddit.com/submit?url={URL}&title={TITLE}";
jformat['Stumbleupon'] 	= "http://www.stumbleupon.com/submit?url={URL}&title={TITLE}";
jformat['Misterwong']   = "http://www.mister-wong.com/add_url/?bm_url={URL}&bm_title={TITLE}&bm_comment=&bm_tags={KEYWORDS}";
jformat['Mail']			= "mailto:someone@email.com?SUBJECT={TITLE}&BODY={DESCRIPTION} - {URL}";

///////////////////////////////////////////////////

$(function() {
  $('.social_tip').tipsy({
	delayIn: 0,      // delay before showing tooltip (ms)
	delayOut: 0,     // delay before hiding tooltip (ms)
	fade: false,     // fade tooltips in/out?
	fallback: '',    // fallback text to use when no tooltip text
	gravity: 's',    // gravity - nw | n | ne | w | e | sw | s | se
	//gravity: $.fn.tipsy.autoSN, // auto gravity - autoSN or autoWE
	html: true,      // is tooltip content HTML?
	live: false,     // use live event support?
	offset: 0,       // pixel offset of tooltip from element
	opacity: 0.8,    // opacity of tooltip
	title: 'title',  // attribute/callback containing tooltip text
	trigger: 'hover' // how tooltip is triggered - hover | focus | manual
  });
});


