<?php if (!defined('PmWiki')) exit ();
/*
Copyright 2015 Adam Overton

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, Version 2, as
published by the Free Software Foundation.
http://www.gnu.org/copyleft/gpl.html
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

The program allows quick embedding of the 'AddThis' widget from http://addthis.com, 
which enables easy sharing of webpages on facebook, delicious.com, and other social networking sites.

the 'AddThis' widget Custom Markup looks like:
(:addThis username="myUsername" btn="sm-share":)

Where to Embed:
   According to the AddThis.com instructions, the following script should be embedded 
   somewhere in the <BODY> of your page, so feel free to add it anywhere...

Current Arguments:
   * username -- created on addthis.com site - 
      if you sign up for a username on addthis.com, you can access addthis.com's sharing statistics.
      even anonymous users seem to have usernames assigned in the form of a long string of 
      random-ish characters (i.e. "49fd93d418b90e5e")... it may or may not work without it (i haven't 
      checked), so to get the anonymous username just hit the "get your button code" button on the 
      addthis.com site, and look for the part of the script that says something like:
   		var addthis_pub="49fd93d418b90e5e"; 
   	use the number generated for you in quotes for your username if you don't have a username...
   * btn - button styles - 
   	-- the standard ones currently enabled are "plus", "sm-share", "lg-share", "sm-bookmark", "lg-bookmark", 
   	and "lg-addthis"; the default is "sm-share"
   	-- you can also create your own custom image and use it instead - just 
   		1. add your custom image to your pub directory
   		2. declare it in config.php:    	$AddThis_customBtn = $PubDirUrl."/AddThisWidget/AddThis_myCustomBtn.png";
		3. call it in the Markup:    (:addThis username="myusername" btn="custom":)
		- there are currently two versions: "custom" & "custom_NoText" - custom adds $AddThis_defaultText after it as a text link
	-- and finally, you can just use text, which is set by $AddThis_defaultText
   
More info on the 'Add This' widget: http://addthis.com

This file is a modification to version 2011-08-11. It should work under PHP 5.5 and higher
*/

# Version date
$RecipeInfo['AddThisWidget']['Version'] = '2015-08-011b'; 


# Defaults
SDV($AddThis_customBtn, ''); // no custom button defined
SDV($AddThis_defaultText, 'Share');


# 'AddThis' Markup -- accepts both lower-case and uppercase: addThis & AddThis
Markup_e('addThisWidget', 'directives',
  '/\\(:addThis (.*?):\\)/i',
   "addThisWidgetFunc(\$m[1],\$m[2])");
# OR
Markup_e('AddThisWidget', 'directives',
  '/\\(:addThis (.*?):\\)/i',
   "addThisWidgetFunc(\$m[1],\$m[2])");



# 'AddThis' Widget Function
function addThisWidgetFunc($args) {  
	global $pagename, $AddThis_customBtn, $AddThis_defaultText;

	// ARGS //
	$args = ParseArgs($args); // get parameters...
	$addThisUsername = $args['username']; // username
	$addThisButtonInput = $args['btn'];
	//$addThisButtonSize = $args['size'];


	// BUTTON TYPES //
	if ($addThisButtonInput=='custom') {
		 // USE CUSTOM BTN w/ TEXT LINK -- add your own custom button info here, and use btn="custom" to call it
		$addThisBtnOut = '<img src="'.$AddThis_customBtn.'" width="10" height="10" alt="" style="border:0"/> '.$AddThis_defaultText;
	} elseif ($addThisButtonInput=='custom_NoText') {
		 // USE CUSTOM BTN -- add your own custom button info here, and use btn="custom_NoText" to call it
		$addThisBtnOut = '<img src="'.$AddThis_customBtn.'" width="10" height="10" alt="" style="border:0"/>';

	} elseif ($addThisButtonInput=='text') {
		 // USE TEXT LINK, NO BUTTON -- add your own custom button info here, and use btn="custom_NoText" to call it
		$addThisBtnOut = $AddThis_defaultText;
				
	} elseif ($addThisButtonInput=='sm-share' || $addThisButtonInput=='' || $addThisButtonInput=='default') {
		 // SMALL SHARE BTN
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/>';
	} elseif ($addThisButtonInput=='lg-share') {
		// LARGE SHARE BTN
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/>';
	} elseif ($addThisButtonInput=='plus') {
		// PLUS (+) button
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/sm-plus.gif" width="16" height="16" alt="Bookmark and Share" style="border:0"/>';
	} elseif ($addThisButtonInput=='lg-bookmark') {
		// LARGE BOOKMARK BTN
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/>';
	} elseif ($addThisButtonInput=='sm-bookmark') {
		// SMALL BOOKMARK BTN
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/sm-bookmark-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/>';
	} elseif ($addThisButtonInput=='lg-addthis') {
		// LARGE ADD THIS BTN
		$addThisBtnOut = '<img src="http://s7.addthis.com/static/btn/lg-addthis-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/>';
	}


	// OUTPUT //
	if ($addThisButtonInput=='custom') {
		$customStyle = ' style="text-decoration:none;"'; // this is added to the <img> tag
	}

	// OUTPUT - STANDARD //
	$addThis_out = '<!-- AddThis Button BEGIN -->
	<script type="text/javascript">var addthis_pub="'.$addThisUsername.'";</script>
	<a href="http://www.addthis.com/bookmark.php?v=20"'.$customStyle.'" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()">'.$addThisBtnOut.'</a><script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script><!-- AddThis Button END -->';
	
	$addThis_out = Keep("$addThis_out");
	return "$addThis_out";
}