<?php if (!defined('PmWiki')) exit();
/*  Copyright 2006 Hans Bracker. 
    This file is togglelink.php; 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.
    
    (:toggle div=divname :) creates a toggle link, which can show or hide 
    a division or other object on the page, for instance a div created with
    >>id=divisionname<< 
    text can be hidden/shown 
    >><< 
    Necessary parameters: (:toggle div=divisionname:) 
    Optional parameters:
    init=hide  hides the division initially (default is show)
    lshow=labelname  label of button when div is hidden (default is Show)
    lhide=labelname label of button when div is shown (default is Hide)
*/ 
# Version date
$RecipeInfo['ToggleLink']['Version'] = '2009-02-16';

# declare $ToggleLink for (:if enabled $ToggleLink:) recipe installation check
global $ToggelLink; $ToggleLink = 1;

SDVA($ToggleLinkConfig, array(
	'init' => 'show',       //show div 
	'lshow' => XL("Show"),  //link text 'Show'
	'lhide' => XL("Hide"),  //link text 'Hide'
	'set' => false,         //set no cookie to remeber toggle state
	'div' => '',            //no default div name
	'div2' => '',           //no default div2 name
	'set' => false,         //set no cookie to remeber toggle state
	'printhidden' => true,  // hidden divs get printed
));

Markup('toggle', 'directives',
  '/\\(:toggle\\s*(.*?):\\)/ei',
  "ToggleLink(\$pagename, PSS('$1'))");
  
# all in one function
function ToggleLink($pagename,$opt) {
   # javascript for toggling and cookie setting
   global $HTMLFooterFmt, $ToggleLinkConfig;
   $HTMLFooterFmt['toggleobj'] = "
   <script type=\"text/javascript\">
      function toggleObj(obj,init,lshow,lhide,swap,set,cname) {
         var Ar = new Array(obj,init,lshow,lhide,swap,set,cname);
         var elstyle = document.getElementById(obj).style;
         if (Ar[4]) var swstyle = document.getElementById(swap).style;
         var text    = document.getElementById(obj + \"-tog\");
         if (Ar[1]=='hide') { //hide object
            if (Ar[5]=='1') document.cookie=Ar[6]+'=hide; path=/';
            Ar[1]='show';
            elstyle.display = 'none';
            if(Ar[4]) swstyle.display = 'block';
            copy='<a class=\"wikilink\" ';  
            copy+='href=\"javascript:toggleObj(\''+Ar[0]+'\',\''+Ar[1]+'\',\''+Ar[2]+'\',\'';
            copy+= Ar[3]+'\',\''+Ar[4]+'\',\''+Ar[5]+'\',\''+Ar[6]+'\');\">'+Ar[2]+'</a>'; 
            text.innerHTML = copy;
            }
         else if (Ar[1]=='show') { //show object
            if (Ar[5]=='1') document.cookie=Ar[6]+'=show; path=/';
            Ar[1]='hide';
            elstyle.display = 'block';
            if(Ar[4]) swstyle.display = 'none';
            copy='<a class=\"wikilink\" ';  
            copy+='href=\"javascript:toggleObj(\''+Ar[0]+'\',\''+Ar[1]+'\',\''+Ar[2]+'\',\'';
            copy+= Ar[3]+'\',\''+Ar[4]+'\',\''+Ar[5]+'\',\''+Ar[6]+'\');\">'+Ar[3]+'</a>'; 
            text.innerHTML = copy;               
         }
      }
   </script> "; 
   
   $opt = array_merge($ToggleLinkConfig, ParseArgs($opt));
   if($opt['init']=='show') $tog = 'show'; 
   if($opt['init']=='hide') $tog = 'hide';
   $opt['lshow'] = str_replace("'","&rsquo;",$opt['lshow']);
   $opt['lhide'] = str_replace("'","&rsquo;",$opt['lhide']); 
   
   #  check cookie  if set=1
   if($opt['set']==1) {
      global $CookiePrefix, $SkinName;
      $Cookie = $CookiePrefix.$SkinName.'_set'.$opt['div'];
      if (isset($_COOKIE[$Cookie])) $tog = $_COOKIE[$Cookie];
   }      
   
   # toggle state 
   if($tog=='show') { 
     $label = $opt['lhide'];
     $tog = 'hide';
   } 
   elseif($tog=='hide') {
     $label = $opt['lshow'];
     $tog = 'show';
   }
   
   # initial toggle link (later link is build with javascript)
   $out = "<span id=\"{$opt['div']}-tog\" class=\"toggle\"><a class=\"wikilink\" ";
   $out.= "href=\"javascript:toggleObj('{$opt['div']}','{$tog}',";
   $out.= "'{$opt['lshow']}','{$opt['lhide']}','{$opt['div2']}','{$opt['set']}','{$Cookie}')\" ";
   $out.= ">{$label}</a></span>";
   if($tog=='show') {
      $out.= "<style type='text/css'>#{$opt['div']} { display:none; } </style>";
   }
   if($tog=='show' && $opt['printhidden']==1) {
      $out.= "<style type='text/css'>@media print {
      #{$opt['div']} { display:block; } 
      }</style>";
   }
   
   if($tog=='hide' && $opt['div2']) {
      $out.= "<style type='text/css'>#{$opt['div2']} {display:none} </style>";
   }
   if($tog=='hide' && $opt['div2'] && $opt['printhidden']==1) {
      $out.= "<style type='text/css'>@media print {
      #{$opt['div2']} {display:none} 
      }</style>";
   }   
   return Keep($out);
}
# end of ToggleLink function