<?php
  // slideshow_via_invisibility_and_fade.php
  // RJM Programming
  // August, 2015
  
  $wordingurl="http://www.rjmprogramming.com.au/ITblog/developing-black-and-white-photos-primer-tutorial/";
  if (isset($_GET['wordingurl'])) $wordingurl=$_GET['wordingurl'];
  if (isset($_POST['wordingurl'])) $wordingurl=$_POST['wordingurl'];
  $wordcontent="";
  if ($wordingurl != "") $wordcontent=file_get_contents($wordingurl);
  $place="/Photography";
  if (isset($_GET['place'])) $place=$_GET['place'];
  if (isset($_POST['place'])) $place=$_POST['place'];
  $incrx="2750";
  if (isset($_GET['slideperiod'])) $incrx=$_GET['slideperiod'];
  if (isset($_POST['slideperiod'])) $incrx=$_POST['slideperiod'];
  $faderx="150";
  if (isset($_GET['fadercycle'])) $faderx=$_GET['fadercycle'];
  if (isset($_POST['fadercycle'])) $faderx=$_POST['fadercycle'];
  $istyle=" style='opacity:1.0;' ";
  $cwidth="";
  $cheight="";
  if (isset($_GET['width'])) $cwidth=$_GET['width']; // . "px";
  if (isset($_POST['width'])) $cwidth=$_POST['width']; // . "px";
  if (isset($_GET['height'])) $cheight=$_GET['height']; // . "px";
  if (isset($_POST['width'])) $cheight=$_POST['height']; // . "px";
  if (isset($_GET['width'])) $istyle=str_replace(";'", ";width:" . $_GET['width'] . "px;'", $istyle);
  if (isset($_POST['width'])) $istyle=str_replace(";'", ";width:" . $_POST['width'] . "px;'", $istyle);
  if (isset($_GET['height'])) $istyle=str_replace(";'", ";height:" . $_GET['height'] . "px;'", $istyle);
  if (isset($_POST['width'])) $istyle=str_replace(";'", ";height:" . $_POST['height'] . "px;'", $istyle);
  
  function wording($inimg) {
    global $wordingurl, $wordcontent;
    $cells=explode("<td>", $wordcontent);
    if (sizeof($cells) > 1) {
      for ($ij=0; $ij<sizeof($cells); $ij++) {
       $cellih=explode("</td>", $cells[$ij]);
       $cellnoti=explode(">", $cellih[0]);
       if (strpos($cellih[0], $inimg) !== false && sizeof($cellnoti) > 1) {
           return $cellnoti[sizeof($cellnoti) - 1];
       }
      }
    }
    return "";
  }
  
  function dojs($numimages, $ix, $fx, $wd, $ht) {
    $scris= " var iask=1;\n var iok=1;\n var iw='" . $wd . "';\n var ih='" . $ht . "';\n var lastslide=1;\n var lastix=" . $ix . ";\n var lastfx=" . $fx . ";\n var ix=" . $ix . ";\n var fx=" . $fx . ";\n var numimages=" . $numimages . "\n var way=1.0;\n var ncurid=0;\n var curopacity=1.0;\n function incr() { ncurid=eval((ncurid + 1) % numimages);\n if (lastix != ix) { document.getElementById('slidesecs').title=ix; document.getElementById('slidesecs').value=ix; document.title=document.title.replace('Slide Period: ' + lastix, 'Slide Period: ' + ix); lastix=ix; } \n  if (lastfx != fx) { document.getElementById('fadercycle').title=fx; document.getElementById('fadercycle').value=fx;  document.title=document.title.replace('Fader Cycle: ' + lastfx, 'Fader Cycle: ' + fx); lastfx=fx; } \n document.title=document.title.replace('Slideshow Slide ' + lastslide, 'Slideshow Slide ' + ncurid);\n lastslide=ncurid;  var was=document.getElementById('twording').innerHTML.replace('Start of slideshow ...',''); if (ncurid == 1) { document.getElementById('twording').innerHTML = 'Start of slideshow ...' + was;  } else if (was != document.getElementById('twording').innerHTML) { document.getElementById('twording').innerHTML=was; }  \n for (var ii=0; ii<numimages; ii++) { if (ii == ncurid) { if (iw != '') { document.getElementById('i' + ii).style.width=iw + 'px'; } if (ih != '') { document.getElementById('i' + ii).style.width=ih + 'px';   } document.getElementById('i' + ii).style.display='inline';  document.getElementById('wording').innerHTML=document.getElementById('i' + ii).title; } else { document.getElementById('i' + ii).style.display='none';   } }  if (iok == 1) { setTimeout(incr, ix); }   }\n function fader() { if (lastix != ix || lastfx != fx) { iok=0; incr(); iok=1; } curopacity=curopacity - (0.05 * way); if (curopacity <= 0.0 || curopacity >= 1.0) { way = -way; } document.getElementById('i' + ncurid).style.opacity=curopacity;  setTimeout(fader, fx);   } ";  
    $scris.=" \n function mclick(evt) { var mois=null; var minis=0; var maxis=0; if (evt.target.id) { mois=document.getElementById(evt.target.id); minis=mois.min; maxis=mois.max; } else if (evt.id) {  mois=document.getElementById(evt.target.id);  minis=mois.min; maxis=mois.max; } if (mois != null) { if (evt.clientY) { if (mois.id == 'slidesecs') { ix=verify(eval(Math.floor(((evt.clientY - 20) / 200.0) * (maxis - minis)) + minis)); this.value=ix; } else {  fx=verify(eval(Math.floor(((evt.clientY - 20) / 200.0) * (maxis - minis)) + minis)); this.value=fx;   }  } else if (evt.pageY) {  if (mois.id == 'slidesecs') {  ix=eval(Math.floor(((evt.pageY - 20) / 200.0) * (maxis - minis)) + minis); this.value=ix; } else {  fx=eval(Math.floor(((evt.pageY - 20) / 200.0) * (maxis - minis)) + minis); this.value=fx;  }  } } } ";
    $scris.=" \n function verify(inidea) { if (iask != 0) { var valis=prompt('If you are not happy with Calculated value, please change it as required.  Cancel stops asking, like this.',inidea); if (valis != null) { return valis; } else { iask=0; } } return inidea; } ";
    return $scris;
  } 
  
  $inline = "inline";
  $bits=explode("/", getcwd());
  $diris=str_replace("/" . $bits[(sizeof($bits) - 1)], $place, getcwd()) . "/";
  
  $myarray = glob($diris . "*.jpg");  // Thanks to http://stackoverflow.com/questions/124958/glob-sort-by-date
  usort($myarray, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));

  echo "<!doctype html><html><head><title>" . str_replace("/", " ", $place) . " Slideshow Slide 1 Slide Period: " . $incrx . " Fader Cycle: " . $faderx . "</title><script type='text/javascript'> " . dojs(sizeof($myarray), $incrx, $faderx, $cwidth, $cheight) . " </script></head><body onload=' setTimeout(incr," . $incrx . "); setTimeout(fader," . $faderx . "); '><table><tr><td>"; 
  for ($i=0; $i<sizeof($myarray); $i++) {
    $bits=explode("/", $myarray[$i]);
    echo "<img " . $istyle . " title='" . wording($bits[(sizeof($bits) - 1)])  . "' id='i" . $i . "' style='display:" . $inline . "' src='.." . $place . "/" . $bits[(sizeof($bits) - 1)] . "' title='" . $bits[(sizeof($bits) - 1)] . "' />";
    $inline = "none";
  }
  echo "</td><td id='twording' style=' margin-top:0px; background-color: yellow;'><div style=' position:absolute; top: 70px; -webkit-transform: rotate(90deg);  -ms-transform: rotate(90deg);  transform: rotate(90deg);'><br><br><meter onclick=' mclick(event); ' onchange=' ix=this.value; ' style=' width: 200px; ' id='slidesecs' title='2750' value='2750' min='1000' max='60000'>Slide Time in milliseconds</meter><br>Slide Time in milliseconds<br>(short) 1000 to 60000 (long)<br><meter onclick='  mclick(event); ' onchange=' fx=this.value; ' title='150' style=' width: 200px; ' id='fadercycle' value='150' min='50' max='360000'>Fader Cycle Period in milliseconds</meter><br>Fader Cycle Period in milliseconds<br> (lots) 50 to 3600000 (little)</div><br><br><br>Start of slideshow ...<div id='wording'></div></td></tr></table></body></html>";
?>
