<?php if (!defined('PmWiki')) exit();
/**
  AddToggle - Turn selected text into a toggle link with a blank div below
  Written by (c) Randy Brown 2014-2015 
  
  Based on a (now obsolete) version of Petko's fixurl recipe, 
  which was based on GPLv2 insMarkup() by Patrick Michaud, pmichaud.com

  This text is written for PmWiki; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version. See pmwiki.php for full details
  and lack of warranty.
*/
# Version date

$RecipeInfo['AddToggle']['Version'] = '20150723';

# a custom markup expression to create a toggle that hides a div:  
# Usage: {(toggle N "This is my toggle link")} 
#  followed by (:divNend:), where N is a unique div number on the page
$MarkupExpr["toggle"] = 'toggle(@$args)'; 

function toggle($args) {
 
  $num = $args[0];
  $text = $args[1];

  $markupToReturn = "(:toggle id=toggle".$num." hide nojs=2 show=\"{\$Show} ".$text."\" hide=\"{\$Hide} ".$text."\":)
(:div".$num." id=toggle".$num.":)
";

 return $markupToReturn;
}

$GUIButtons['addtoggle'] = array(9991,'','','$[Toggle link]',
 "<img src='\$GUIButtonDirUrlFmt/addtoggle.png' title='$[Add a toggle link: Selected text will become a link that hides a section until clicked.]' onclick='AddToggle()'/>");

$HTMLFooterFmt['addtoggle'] = <<<EOF
<script type="text/javascript">
function AddToggle(){
 var r = 5000 + Math.round(Math.random()*1000);
 insMarkup(
    "{(toggle "+r+" \\"",
    "\\")}\\nHidden\\n(:div"+r+"end:)\\n",
    "$[Toggle link]");
}
</script>
EOF;