 /*
 * Share+
 * Copyright 2011 Daniel Paul
 * www.danielpaul.me
 *
 * Share+ : http://www.binpress.com/dev/profile/326
 *
 * Version 1.0   -   Last Updated: Nov 11, 2011
 *
 * Licence: http://www.binpress.com/app/share%2B/627
 *
 */
   
(function($){
	$.fn.shareplus = function(options) {
		var defaults = { 
		
			height: '130px',
			
			speedIn: 400, // fadeIn speed
			speedOut: 600, // fadeOut speed
			
			/* effectIn: 'drop',
			effectOut: 'drop', */
			
			displayTitle: true, // true or flase
			title: "Share &amp; Bookmark", // shareplus box title
			
			sharetitle: "", // sharing page's title
			shareurl: "", // URL to share
			
			icons: "facebook,twitter,google,digg,delicious,stumble,tumblr,linkedin,live,myspace,bebo,email",
			targetBlank: true, // open link in new tab? true or false
			
			fbLikebox: true, // display facebook like box? true or false
			fbAction: "like", // like or recommend?
			fbcolorscheme: "light", // light or dark
			
			tweets: true, // diaplay tweet button? true or false
			tweetText: "Check out this URL",
			tweetVia: "", // via twitter account
			twitterRelated: "danielpaul94", // related twitter account
			tweetButton: "Tweet",
			
			gplus: true, // display Google Plus one button? true or false
			
		  	open: function(){}, // function to activate when open is started
		  	opened: function(){}, // function to activate when it is opened
		  	
		  	exit: function(){}, // function to activate when exit is starting
		  	exited: function(){}, // function to activate when it has exited
		  	
		  	sticker: true, // display share sticker?
		  	stickerAlign: 'left', // left or right?
		  	
		  	customFont: false // Use custom font? You can also add your own fonts in your stylesheets.
	  	};
	 	var opts = $.extend(defaults, options);
	 	
	 	
	 	
	 	
	 	// ***** Sharing services URL format list
	 	var iformat				=	new Object();
	 	iformat.email			=	"mailto:?subject={TITLE}&body={DESCRIPTION}%20{URL}";
	 	iformat.digg			=	"http://digg.com/submit?phase=2&url={URL}&title={TITLE}";
	 	iformat.linkedin		=	"http://www.linkedin.com/shareArticle?mini=true&url={URL}&title={TITLE}&summary={DESCRIPTION}&source=";
	 	iformat.technorati		=	"http://www.technorati.com/faves?add={URL}";
	 	iformat.delicious		=	"http://del.icio.us/post?url={URL}&title={TITLE}";
	 	iformat.yahoo			=	"http://myweb2.search.yahoo.com/myresults/bookmarklet?u={URL}&t={TITLE}";
	 	iformat.google			=	"http://www.google.com/bookmarks/mark?op=edit&bkmk={URL}&title={TITLE}";
	 	iformat.google_plus		=	"";
	 	iformat.newsvine		=	"http://www.newsvine.com/_wine/save?u={URL}&h={TITLE}";
	 	iformat.reddit			=	"http://reddit.com/submit?url={URL}&title={TITLE}";
	 	iformat.live			=	"https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={URL}&title={TITLE}&top=1";
	 	iformat.facebook		=	"http://www.facebook.com/share.php?u={URL}";
	 	iformat.twitter			=	"http://twitter.com/?status={TITLE}%20-%20{URL}";
	 	iformat.stumble		=	"http://www.stumbleupon.com/submit?url={URL}&title={TITLE}";
	 	iformat.orkut			=	"http://promote.orkut.com/preview?nt=orkut.com&tt={TITLE}&du={URL}&cn={DESCRIPTION}";
	 	iformat.bebo			=	"http://www.bebo.com/c/share?Url={URL}&title={TITLE}";
	 	iformat.evernote		=	"http://s.evernote.com/grclip?url={URL}&title={TITLE}";
	 	iformat.mixx			=	"http://www.mixx.com/submit?page_url={URL}&title={TITLE}";
	 	iformat.myspace			=	"http://www.myspace.com/Modules/PostTo/Pages/?u={URL}&title={TITLE}";
	 	iformat.netvibes		=	"http://www.netvibes.com/share?title={TITLE}&url={URL}";
	 	iformat.tumblr			=	"http://www.tumblr.com/share?v=3&u={URL}&t={TITLE}&s=";
	 	iformat.friendfeed		=	"http://friendfeed.com/share/bookmarklet/frame#title={TITLE}&url={URL}";
	 	iformat.design_moo		=	"http://www.designmoo.com/node/add/drigg/?url={URL}&title={TITLE}";
	 	iformat.designfloat	=	"http://www.designfloat.com/submit.php?url={URL}&title={TITLE}";
	 	iformat.design_bump		=	"http://www.designbump.com/node/add/drigg/?url={URL}&title={TITLE}";
	 	iformat.squidoo			=	"http://www.squidoo.com/lensmaster/bookmark?{URL}";
	 	iformat.yahoo_buzz		=	"http://buzz.yahoo.com/buzz?targetUrl={URL}&headline={TITLE}&summary={DESCRIPTION}";
	 	iformat.print			=	'javascript:void(0);" onClick="print_page();" target="_self';
	 	iformat.favorites		=	'javascript:void(0);" onClick="bookmark_us();" target="_self';
	 	
	 	
  	        
  	var shareplus = $('<div id="shareplus"></div>');
  	  	
  	// * if display shareplus title, set title 
  	var title = '';
  	if (opts.displayTitle) {
  	  var title = $('<div id="shareplus_title"><span>'+ opts.title +'</span><span id="shareplus_close"><a href="javascript:void(0);" class="shareplus_close">x</a></span></div>');
  	} 
  	  	        
  	// * add the share icons
  	var icons = $('<div id="shareplus_icons" style="height:'+ opts.height +';"></div>');
  	
  	
  	var fbLikebox = '';
  	if(opts.fbLikebox) {
  	  var fbLikebox = '<iframe src="http://www.facebook.com/plugins/like.php?href='+ encodeURL(sharing_url()) +'&amp;layout=button_count&amp;width=100&amp;action='+ opts.fbAction +'&amp;colorscheme='+ opts.fbcolorscheme +'&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>';
  	}
  	
  	
  	if(opts.tweetVia){
  	  var tweet_via = '&amp;via='+opts.tweetVia;
  	} else {
  	  var tweet_via = '';
  	}
  	
  	var tweets = '';
  	if(opts.tweets) {
  	  var tweets = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?url='+ encodeURL(sharing_url()) +'&amp;text='+ encodeURL(opts.tweetText) + '&amp;related='+ encodeURL(opts.twitterRelated) + tweet_via +'" style="width:100px; height:21px;"></iframe>';
  	}
  	
  	var gplus = '';
  	if(opts.gplus) {
  	  var gplus = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="https://plusone.google.com/u/0/_/+1/fastbutton?url='+ encodeURL(sharing_url()) +'&amp;size=medium" style="width:72px; height:21px;"></iframe>';
  	}
  	
  	// * if have any extras (fb likebox or tweet button)
  	var extras = '';
  	if (opts.fbLikebox || opts.tweets) {
  	  var extras = $('<div id="shareplus_extras">' + gplus + tweets + fbLikebox +'</div>');
  	}
  	
  	if(opts.customFont) {
  		$('head').append('<style>@import url(http://fonts.googleapis.com/css?family=Varela);#shareplus{font-family: "Varela", sans-serif;}</style>');
  	}
  	
  	
  	shareplus.html(title); // add the title
  	shareplus.append(icons, extras); // add the icons and extras
  	
  	$('body').append(shareplus); // Add share to body
  	
  	
  	var overlay = $('<div id="shareplus_overlay"></div>');
  	$('body').append(overlay);
  	
  	/***** Sticky Button START */
  	
  	if (opts.sticker) {
		var sticker = $('<a href="javascript:void(0);" onClick="open_shareplus();" class="shareplus"><div id="shareplus_sticker" class="'+ opts.stickerAlign +'"></div></a>');
		$('body').append(sticker);
  	}
  	
  	/* END Sticky Button *****/
  	
  	
  	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
  	               
	//Set the popup window to center
	$(shareplus).css('top',  winH/2-$(shareplus).height()/2);
	$(shareplus).css('left', winW/2-$(shareplus).width()/2);
	
	// centre share when window is resized
	$(window).resize(function() {
	  //Get the window height and width
	  var winH = $(window).height();
	  var winW = $(window).width();
	  	               
	  //Set the popup window to center
	  $(shareplus).css('top',  winH/2-$(shareplus).height()/2);
	  $(shareplus).css('left', winW/2-$(shareplus).width()/2);
	});
	
	/*
	// jQuery UI effects
	var ui_effects = ['blind','bounce','clip','drop','explode','fold','highlight','puff','pulsate','scale','shake','size','slide'];
	
	// Load jQuery UI if needed
	if(jQuery.inArray(opts.effectIn, ui_effects) > -1 || jQuery.inArray(opts.effectOut, ui_effects) > -1){
		if (!jQuery.ui) {
			var ui = document.createElement('script');
			ui.setAttribute("type","text/javascript");
			ui.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js");
			document.getElementsByTagName("head")[0].appendChild(ui);
		}
	}
	*/
	  	
  	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(opts.targetBlank){
  				  var target = '_blank';
  				} else {
  				  var target = '_self';
  				}
  				if(url){
  					url = url.replace("{TITLE}", encodeURL(sharing_title()));
  					url = url.replace("{URL}", encodeURL(sharing_url()));
  					url = url.replace("{KEYWORDS}", encodeURL(shareplus_metakeywords()));
  					url = url.replace("{DESCRIPTION}", encodeURL(shareplus_metadescription()));
  					
  					var sharelink = '<a href="' + url + '" target="' + target + '" class="shareplus_icon ' + name + '"><span>' + name + '</span></a>';
  					$(sharelink).appendTo(icons); // Add share buttons
  				}
  			}
	 	
		// *** Action!
	    window.open_shareplus = function() {
	    	opts.open.call(this);
	    	shareplus.stop();
	    	$("#shareplus_overlay").fadeIn(opts.speedIn);
	    	$(shareplus).fadeIn(opts.speedIn);
	    	opts.opened.call(this);
	    }
	    
		$(this).click(function() {
			open_shareplus();                      
		});
		// END action!
	            
	    // *** shows over! clean up time...  
	    window.close_shareplus = function() {
	    	opts.exit.call(this);
	    	$("#shareplus_overlay").fadeOut(opts.speedOut);
	    	$(shareplus).fadeOut(opts.speedOut);
	    	opts.exited.call(this);
	    }
	             
		$('#shareplus_close, #shareplus_overlay').click(function(){
	      	close_shareplus();
		});	
		// END clean up!
                      
    
       
    /***** Easy functions START */
    
    // * Encode url
    function encodeURL(string) {
    	if(!string){ // if there is nothing to process
    		return "";
    	}
    	return string.replace(/\s/g, '%20').replace('+', '%2B').replace('/%20/g', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
    	// Encode URL by replacing the string's special characters
    }
    
    // * Get sharing title
    function sharing_title() {
      if(opts.sharetitle) { // if share title is set
      	return opts.sharetitle;
      } else { // if not, get the page title
      	return document.title; // Get page title
      }
    }
    
    // * Get URL to share
    function sharing_url(){
    	if(opts.shareurl) {// if sharing url is set
    		return opts.shareurl;
    	} else { // if not, get the page URL
    	return document.location.href; //Get the page URL
    	}
    }
    
    // * Get meta keywords
    var shareplus_keywords = '';
    function shareplus_metakeywords() { 
    	if(shareplus_keywords === ''){
    		metaCollection = document.getElementsByTagName('meta'); 
    		for (i=0;i<metaCollection.length;i++) { 
    			nameAttribute = metaCollection[i].name.search(/keywords/);
    			if (nameAttribute!= -1) { 
    				shareplus_keywords = metaCollection[i].content;
    				return shareplus_keywords; 
    			} 
    		} 
    	}else{
    		return shareplus_keywords;
    	}
    } 
    
    // * Get meta description
    var shareplus_description = '';
    function shareplus_metadescription() { 
    	if(shareplus_description === ''){
    		metaCollection = document.getElementsByTagName('meta'); 
    		for (i=0;i<metaCollection.length;i++) { 
    			nameAttribute = metaCollection[i].name.search(/description/);
    			if (nameAttribute!= -1) { 
    				shareplus_description = metaCollection[i].content;
    				return shareplus_description; 
    			} 
    		} 
    	}else{
    		return shareplus_description;
    	}
    } 
    
    /* END Easy Functions *****/
    
    function print_page(){
      window.print();
    }
    function bookmark_us() {
    	alert('Press ctrl + D to bookmark');
    }


	}
})(jQuery);

/*
* Share+
* Copyright 2011 Daniel Paul
* www.danielpaul.me
* http://www.binpress.com/dev/profile/326
*/
