(function($) {
	/**
	 * A plugin to be able to share pages, or individual media, to social media networks. Written specifically for 
	 * Guild Wars 2
	 */
	$.fn.shareIt = function(options) {
			
		var options = $.extend({},$.fn.shareIt.defaults, options);		
		var siteDetails = $.extend($.sites, $.additionalSites);
		//var sites = options.sites;
		
		return this.each(function(i) {
			var $this = $(this);
			
			// use the meta plugin if it exists
			// see http://docs.jquery.com/Plugins/Metadata/metadata
			var o = $.meta ? $.extend({}, options, $this.data()) : options;
			var sites = o.sites;
			var lnks = [];
			if(o.killExisting) {
			 	$this.find('.' + o.wrapperClass).remove();
			}
			var loc = window.location;
			loc = loc.protocol + "//" + loc.host + loc.pathname;
			
			for(var i = 0; i < sites.length; i++) {
				if(siteDetails[sites[i]] !== undefined) {
					var site = siteDetails[sites[i]];
					var lnk = "<a href='" + site.url;
					// get rid of query string and hash (if they exist)
					lnk += site.key + "=" + encodeURIComponent(loc);
					if(o.shareType === 'media') {
						lnk += encodeURIComponent('#');
						if($this.attr(o.triggerType)) {
							lnk += $this.attr(o.triggerType);
						}
					}
					if(o.shareType === 'custom') {
						if(o.triggerID != '') {
							lnk += encodeURIComponent('#') + o.triggerID;
						}
					}
					if(site.title != '') {
						lnk += '&amp;' + site.title + '=';
						if(o.shareType === 'media' && o.manualTitle != '') {
							lnk += encodeURIComponent(o.manualTitle);
						}
						else { 
							lnk += encodeURIComponent(document.title);
						}
						lnk += "' ";
					}
					else {
						lnk += "' ";
					}
					if(o.blankTarget) {
						lnk += "target='_blank' ";
					}
					lnk += "class='" + site.cssClass + "'";
					lnk += " title='" + site.display + "' rel='" + o.rel + "'>" + site.display + "</a>";
					lnks.push(lnk);
				}
			}				
			_createEls($this, o, lnks);								  
		});
	};
	
	function _createEls(obj, o, lnks) {
		var els = "<div class='" + o.wrapperClass + "'>";
		els += "<div class='content'>";
		els += "<h3><span>Share this</span></h3>";
		els += "<ul>";
		for(var i = 0; i < lnks.length; i++) {
			els += "<li>" + lnks[i] + "</li>";	
		}
		els += "</ul><div class='clear'></div></div></div>";
		obj.append(els);
	};
	
	$.fn.shareIt.defaults = {
		shareType: 'page', // page|media|custom. Share the entire page, specific media based on trigger, or custom?
		triggerType: 'id', // what's the trigger (see above)? Should be an attribute (most likely ID, could also be name). In the case of fancybox this will be the id of the caller
		triggerID: '',
		rel: 'external', // useful for event tracking etc 
		manualTitle: '', // if the shareType is media, set the title manually... or leave blank to use page title
		killExisting: true, // should the plugin attempt to remove exisiting icons/links (within the container) 
		wrapperClass: 'share-it',
		blankTarget: true,   // Open the link in a new tab?
		sites: ['delicious', 'digg', 'facebook', 'reddit', 'twitter'] // default sites to use (and order)
	};
	
	$.sites = {
		delicious: {
			display: "Del.icio.us",
			cssClass: "delicious",
			url: "http://del.icio.us/post?v=2&amp;",
			key: "url",
			title: "title"
		},
		digg: {
			display: "Digg",
			cssClass: "digg",
			url: "http://digg.com/submit?",
			key: "url",
			title: "title"
		},
		facebook: {
			display: "Facebook",
			cssClass: "facebook",
			url: "http://www.facebook.com/share.php?",
			key: "u",
			title: "t"
		},
		reddit: {
			display: "Reddit",
			cssClass: "reddit",
			url: "http://reddit.com/submit?",
			key: "url",
			title: "title"
		},
		twitter: {
			display: "Twitter",
			cssClass: "twitter",
			url: "http://twitter.com/home?",
			key: "status",
			title: ""
		}
	};
	$.additionalSites = $.additionalSites || {};
	
})(jQuery);
