<?php if (!defined('PmWiki')) exit();
    /* popupwindow.php, a module written for pmwiki 2
    for creating popup windows.
    copyright 2007 Hans Bracker.
    
    This program is free software; 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.
    
    Simple syntax: %popwin%[[Group/PageName]] or %popwin%[[Group/PageName| Title]]
    Syntax with some parameters: %popwin height=300 width=400 top=20 left=20%[[link]]
    See list of available parameters below under $defaults
    For multiple popup-window links, each with some different parameters, 
    add id= parameter, for instance id=1, id=2 etc. 
    
    Additional markup:
    (:popclose Close Window:) creates a link for closing the popup window, 
    replace "Close Window" with whatever label text.
    (:input closewindow 'Close Window':) creates a 'Close Window' button.
    replace "Close Window" with whatever label text.
    add ?action=popopen to the %popwin%link
        %popwin%[[PageName?action=popopen| link text]]    
    opens the popup and sets a page variable {$PopOpen}. 
    Use it in the popup page in conditionals to trim side menus etc:
    (:if equal {$PopOpen} 1:)(:noleft:)(:popclose Close Window:)(:ifend:)
    */
$RecipeInfo['PopupWindow']['Version'] = '2008-04-17';

Markup_e('popwin1','<links','/%popwin\\s*(.*?)%\\[\\[(.*?)\\]\\]/i',
  "PopupWindow(\$pagename,\$m[1],\$m[2],NULL,'')");
 
Markup_e('popwin2','<popwin1',
  "/%popwin\\s*(.*?)%(?>\\[\\[([^|\\]]*)\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/i",
  "PopupWindow(\$pagename,\$m[1],\$m[2],\$m[3],\$m[4])"); 
  
function PopupWindow ($pagename, $opts, $tgt, $lbl, $sfx) {
   global $LinkPopupFmt, $FmtPV, $PopupLinkPage;
   static $cnt = 0; $cnt++;
   $defaults = array(
      'id' => '0',
      'width' => 500,
      'height' => 300,
      'left' => 'center',
      'top' => 'center',      
      'resizable' => 1,
      'scrollbars' => 1,
      'toolbar' => 0,
      'location' => 0,
      'directories' => 0,
      'statusbar' => 0, 
      'menubar' => 0,
      );
   $opt = array_merge($defaults, ParseArgs($opts));
   
   $FmtV['$PopupLinkPage'] = $PopupLinkPage = $pagename;
   $FmtPV['$PopupLinkPage'] = '$GLOBALS["PopupLinkPage"]'; 
   
   $LinkPopupFmt = "<a href='\$LinkUrl' onclick=\"return popWin('\$LinkUrl','popup".$opt['id'].
                "','".$opt['width']."','".$opt['height']."','".$opt['left']."','".$opt['top'].
                "','".$opt['resizable']."','".$opt['scrollbars']."','".$opt['toolbar'].
                "','".$opt['location']."','".$opt['directories']."','".$opt['statusbar'].
                "','".$opt['menubar']."')\">\$LinkText</a>".
	    ($cnt==1 ? "\n       <script language='javascript' type='text/javascript'><!--
	      function popWin(url,id,px,py,pl,pt,rs,sb,tb,lo,di,st,mb) {
	      if(pl=='center') var pl = (screen.width-px)/2; if(pt=='center') var pt = (screen.height-py)/2;
	      newwindow=window.open(url,id,'toolbar='+tb+',scrollbars='+sb+',location='+lo+',statusbar='+st+',menubar='+mb+',directories='+di+',resizable='+rs+',width='+px+',height='+py+',left='+pl+',top='+pt+'');
	      if (window.focus) {newwindow.focus()} return false;} // -->
       </script>" : "");
   return Keep(MakeLink($pagename,$tgt,$lbl,$sfx,$GLOBALS['LinkPopupFmt']),'L');
}

# set PV {$PopOpen} with action=popopen
$HandleActions['popopen'] = 'PopOpenHandle';
function PopOpenHandle($pagename) {
    global $FmtPV;
    $FmtPV['$PopOpen'] = '"1"';
    HandleBrowse($pagename);
}

# create link for closing popup with (:popclose Close Window:)
Markup_e('popclose','directives','/\\(:popclose\\s*(.*?):\\)/i', 
       "PopCloseMarkup(\$pagename,\$m[1])");
function PopCloseMarkup($pagename, $label='Close Window') {
	$out = "<a href=\"$PageUrl?action=popclose\" onclick=\"self.close()\">$label</a>";
	return Keep($out);	
}

# add markup (:input closewindow 'Close Window':)
SDVA($InputTags['closewindow'], array(
  ':args' => array('value'),
  'value' => XL('Close Window'),
  ':html' => "<input type='button' class='inputbutton' \$InputFormArgs onClick='window.close()' />",
 	));