// WebShow.js V1.5 12/1/09
// 12Jan09: Added code to correctly set picture size when the Viewport is smaller than the picture
// 25Oct08: Removed function Redraw. Was not being used. See 1.3 and 1.2 for older code.

// Function GENTHUMBS: Create thumbnail anchor tags 22/6/07.
// cols=0: original style (no table). cols=1+ columns, centred, with undertitle.
function GenThumbs(cols) {   
 if (cols==0) {
  for (var i=0; i<p.length; i++) {
   document.write('<a href="javascript:ShowPhoto('+i+');"><img src="thumbs/'+p[i].n+'.jpg" width="'
      +p[i].tw+'" height="'+p[i].th+'" title="'+p[i].n+'"></a>&nbsp;');
  }
 } else {
  w = (MaxTW+10)*cols;  // Table width
  n = Math.round((p.length/cols)+0.4);  
  document.write('<table width="'+w+'" border="0" align="center"><tr><td class=ta>'); // start table + col 1.
  for (var i=0; i<p.length; i++) {
   document.write('<a href="javascript:ShowPhoto('+i+');"><img src="thumbs/'+p[i].n+'.jpg" width="'
      +p[i].tw+'" height="'+p[i].th+'" title="'+p[i].n+'"></a><br>'
	  +p[i].n+'<br><br>');
   if ((i+1)%n == 0 ) { document.write('</td><td class=ta>'); }
  }
  document.write('</td></tr></table>');
 }  
} //--end GenThumbs--//


// Set delay. Invoked if timer value changed
function SetDelay() {   
 Delay = 1000*document.setdelay.delay.value;  // Set new delay time
} 

 
// ShowPhoto. Create child window to display photo. Called from top level window.
function ShowPhoto(j) {
 var RunShow = false;  
 if (j<0) {	j=0; RunShow = true; } ;  // straight to slideshow
 CP = j;    // Save cur. photo in global var
 var left = (screen.availWidth-MaxW-10)/2; // Centre
 var top = (screen.availHeight-MaxH-40)/2;
 if (left<1) { left=1;}
 if (top<1) { top=1;} 
 var winstyle = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=no,resizable=yes,copyhistory=no,width='
    +MaxW+',height='+MaxH+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
 pWin = window.open("","showWin", winstyle);
 pWinDoc(j);  // Create Document
 if (RunShow) { timerID = setTimeout("RunShow()",Delay); }
 CachePhoto(CP+1);   // Cache next photo if necessary
// alert('Just set timer to delay = '+Delay);
} // end ShowPhoto


// pWinDoc function. Called from ShowPhoto and NxtPhoto etc. Last updated 12/01/09
function pWinDoc(j) { // Create or update the Picture Window document.
 pWin.document.open("text/html");   // Optional
 var head = '<head><title>'+p[j].n+'<\/title><link href='+SS+' rel="stylesheet" type="text/css">'+
     '<script language="JavaScript">var T = window.opener;<\/script><\/head>';
 var boddiv = '<body class="pwin" onresize="javascript:T.pWinDoc(T.CP)">'+
     '<script>T.vpH = window.innerHeight;T.vpW = window.innerWidth;'+
	 'if (!T.vpH) {'+
       'if (document.documentElement && document.documentElement.clientWidth) {'+
	   'T.vpW = document.documentElement.clientWidth;'+
       'T.vpH = document.documentElement.clientHeight;} else if (document.body.clientWidth) {'+   
	   'T.vpW = document.body.clientWidth;'+
       'T.vpH = document.body.clientHeight; } }'+// 'alert("Viewport Width/Ht = "+T.vpW+"/"+T.vpH);'+
	 '<\/script>';
 pWin.document.write(head+boddiv);
 pW = p[j].w;    // pW is global - Picture width
 pH = p[j].h;    // + height  
 if (pW>vpW) {pH=pH*vpW/pW; pW=vpW;}  // Shrink to available width
 if (pH>vpH) {pW=pW*vpH/pH; pH=vpH;}  // Shrink to available height
 pMargin=((vpH-pH)/2).toFixed(0);
 pW=pW.toFixed(0);
 pH=pH.toFixed(0);
 // alert("Picture size="+pW+"/"+pH+"  Margin="+pMargin);
 var picdiv = '<div id="outer" style="margin: '+pMargin+'px 0px"><img src="images/'+p[j].n+'.jpg" width="'+pW+'" height="'+pH+'"></div>'; 	 
 var butdiv = '<div id="buttons">'+
  '<a href="javascript:T.PrvPhoto();"><img src="images/prv.gif" title="Previous Picture" border=0></a>'+
  '<a href="javascript:T.NxtPhoto();"><img src="images/nxt.gif" title="Next Picture" border=0></a>'+
  '<a href="javascript:T.RunShow();"><img src="images/run.gif" title="Start slide show" border=0></a>'+
  '<a href="javascript:T.StopShow();"><img src="images/stop.gif" title="Pause slide show" border=0></a></div>';	  
 pWin.document.write(picdiv+butdiv); 
 pWin.document.write('</body></html>');
 pWin.document.close(); // Now close. Stops "progress bar" in Firefox status bar from "hanging".
 pWin.status = p[j].n;
 }

 
// Show next photo (first picture=0) 17/2/07. 
function NxtPhoto() {  
 CP = CP+1;      // Increment picture number
 if (CP==p.length) { CP=0; }   // Reached max? If so reset to 0.   
 pWinDoc(CP);
 CachePhoto(CP+1);   // cache next photo if necessary
} // end function


// Show previous photo 17/2/07
function PrvPhoto() {  
 CP = CP-1;
 if (CP<0) CP=p.length-1;  	
 pWinDoc(CP);    
} // end function 


// Run slideshow. Called from pWin 18/2/07. 
function RunShow() {  
  CP = CP+1;      // Increment picture number
  if (CP==p.length) {CP=0;}  // Reached max? If so reset to 0.
  pWinDoc(CP); 
  timerID = setTimeout('RunShow()',Delay);  // Invoke function after specified delay
  CachePhoto(CP+1);   // Load next photo into cache if necessary
}


// Stop show (user clicked button)
function StopShow() { 
 clearTimeout(timerID); 
}


// Load jpg into cache.
function CachePhoto(i) {  // Load specified photo into the cache.
  if (i<p.length) {  // only if within array bounds
    if (!pCache[i]) {  // Only if not done already ie if undefined.
     // alert('Loading picture into cache. No='+i);
      pCache[i] = new Image(p[i].w,p[i].h);
      pCache[i].src = 'images/'+p[i].n+'.jpg';
    }
  }
}


// ShowVer 1.2 20/6/07  Print Ver & date last modified.
function ShowVer() {
   tdate = new Date(document.lastModified);
   document.write('Webshow '+Ver+' © Erw Wen Software '+tdate.toLocaleString());
}