<?php
/**
 * Plugin base links: Creates links relative to site root for all links beginning with "/"
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Jürgen A.Lamers <jaloma.ac@googlemail.com>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_INC.'inc/search.php');
require_once(DOKU_INC.'inc/JpegMeta.php');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_jalbum_slideshow extends DokuWiki_Syntax_Plugin {

  /**
   * default parameters of the jalbum tag
   */
  var $dflt = array(
		    'width' => 20,
		    'height' => 20,
		    'align' => 'top',
		    'albumdir' => 'off',
		    'albumtitle' => 'off'
		    );
  
  /**
   * return some info
   */
  function getInfo(){
    return array(
		 'author' => 'Juergen A.Lamers',
		 'email'  => 'jaloma.ac@googlemail.com',
		 'date'   => @file_get_contents(DOKU_PLUGIN . 'jalbum/VERSION'),
		 'name'   => 'jalbum',
		 'desc'   => 'Slideshow auf Thumbs von JALbum',
		 'url'    => 'http://wiki.splitbrain.org/plugin:jalbum'
		 );
  }
 
  /**
   * What kind of syntax are we?
   */
  function getType(){
    return 'substition';
  }

  function getPType(){
    return 'stack';
  }
  
  function getSort(){ return 305; }
  
  function connectTo($mode) {
    $this->Lexer->addSpecialPattern('~~jalbumshow.*?~~',$mode,'plugin_jalbum_slideshow');
  }
 
 
  /**
   * Handle the match
   */
  function handle($match, $state, $pos, &$handler){
    $match = str_replace("~~",null,$match);
    $match = str_replace("jalbumshow",null,$match);
    list($junk, $num) = explode(':', $match, 2);
    $dirs = explode('|', $junk);
    return array($num,$dirs);
  }
 
  /**
   * Create output
   */
  function render($mode, &$renderer, $data) {
    global $conf;
    if ($mode == 'xhtml') {
      list($num,$dirs) = $data;
      if (sizeof($dirs) == 0) { return true;}
      $jsscript ='<script type="text/javascript">'.
	'<!--//--><![CDATA[//><!--'."\n";
      foreach($dirs as $picdir) {
	$pics = $this->_search($picdir);
	if (sizeof($pics) == 0) { continue;}
	$cc = 1;
	foreach($pics as $p) {
	  $jsscript .= '';
	  $jsscript .= 'neu(';
	  $jsscript .= '"';
	  $jsscript .= $this->getConf('baseurl').'/'.$picdir.'thumbs/'.$p['file'];
	  $jsscript .= '"';
	  $jsscript .= ',';
	  $jsscript .= '"';
	  $jsscript .= '';
	  $jsscript .= '"';
	  $jsscript .= ',';
	  $jsscript .= '"';
	  $jsscript .= $this->getConf('baseurl').'/'.$picdir.'slides/'.str_replace('JPG','html',$p['file']);
	  $jsscript .= '"';
	  $jsscript .= ');'."\n";
	  if (isset($num) && $cc>=$num) { break; }
	  $cc++;
	}
      }
      $jsscript .= ''.
	'var changeTime 	= '.$this->getConf('changetime').';'.
	'//--><!]]>'."\n".'</script>'."\n";

      if (isset($this->getConf['target'])) {
	$target = 'target="'.$this->getConf['target'].'" ';
      } else {
	$target = '';
      }

      $body = ''.
	'<div class="jalbumslideshow">'.
	'<a '.$target.' href="javascript:transport()">'.
	'<img class="jalbumslide" src="'.$this->getConf('baseurl').'/'.$picdir.'thumbs/'.$pics[0]['file'].'"'.
	' width="'.$this->getConf('slidewidth').'"'.
	' height="'.$this->getConf('slideheight').'"'.
	' name="bild" alt="Bueld"/>'.
	'</a>'."\n".
	'</div>'."\n".
	'<script type="text/javascript">'."\n".
	'<!--//--><![CDATA[//><!--'."\n".
	'wechsel();'."\n".
	'//--><!]]>'."\n".'</script>'."\n".
	'';
      $renderer->doc .= $jsscript;
      $renderer->doc .= $body;
      return true;
    }
    return false;
  }

  function _search($dir) {
    $files = array();
    search($files,$this->getConf('albumdir'),'search_media',array(),$dir."/thumbs");
    return $files;
  }
}//class
?>
