
/*                 GENERIC SLIDE SHOW JAVASCRIPT  


    An original routine by Bob Rilling (email to rilling,
    and add @ucar.edu), National Center for Atmospheric Research

    Segments of code derived from any workers outside NCAR are
    attributed.

    This code is copyright NCAR/UCAR and Bob Rilling

    Here's the formal statement:

                  Copyright (C) 2008 by UCAR
           University Corporation for Atmospheric Research
                       All rights reserved

    No part of this work covered by the copyrights herein may be
    reproduced or used in any form or by any means -- graphic,
    electronic, or mechanical, including photocopying, recording,
    taping, or information storage and retrieval systems -- without
    permission of the copyright owner.

    Owner hereby gives permission for use by any not-for-profit entity
    or private individual, provided this copyright (or a condensed 
    attribution) is included with the code.  This permission is not
    extended for any commercial use.

    This software and any accompanying written materials are provided
    "as is" without warranty of any kind.  UCAR expressly disclaims
    all warranties of any kind, either express or implied, including
    but not limited to the implied warranties of merchantibility and
    fitness for a particular purpose.  UCAR does not indemnify any
    infringement of copyright, patent, or trademark through use or
    modification of this software.  UCAR does not provide maintenance
    or updates for its software.

*/

/*   Slide_show.js is a generic slide show viewer.  This is a nice,
     simple, minimalist viewer and removes some of the redundancies
     found in other viewers when each slide must have an additional
     html page.  This viewer uses very simple slide navigation, and
     allows a single "go back" browser history return to any previous
     index page.

     This routine assumes that there are multiple, named subdirectories
     below the current directory, each containing a complete set of 
     presentation slides, as may have been created from Microsoft Power
     Point.  The slides in the subdirectories must be named and numbered
     as slide1.png, slide2.png, slide3.png, ...  

     A widgets subdirectory must also be included with this script.

*/

var show_num=0;
var sl=0;
var old_sl=0;
var base_dir = "/projects/js_tools/";   // web-server referenced location of this script
var pattern = "wxyz123";  // a junk pattern, to start with

var args = getArgs();  // the particular subdirectory is referenced as a query string in the URL
                       // most often, the subdirectory is named after the slide show author

if(args.dir) {
    pattern = args.dir;
//    alert('pattern = ' + pattern );
}

function find_show() {  // determines which specific subdiretory to operate upon
                        // converts a named subdirectory to a numbered slideshow reference
    var j=0;
    for( j=0; j<slideshow.length; j++) {
      if( pattern == slideshow[j].author ) {
		show_num = j;
	}
    }
}

function list_slides() {  // creates a slide index in the leftmost column of a table contained on your html page
    var j=0;
    var k;
// begin creating your two-column table
    document.write('<table cellpadding=5>\n<tr>');
    document.write('\t<td width=200px>\n' +  slideshow[show_num].author + '<br><br>' );

// for each slide, create a blank, changeable pointer image, a conventional slide name, and use that name to reference your slide
// all slide changes are done through the javascript url reference to the show_slide function

    for( j=0,k=1; j<slideshow[show_num].len; j++,k++ ) {
	document.write( '\t<img src=\"' + base_dir + 'widgets/16tx_pix.gif\" name=\"slide' + j + '">&nbsp;&nbsp;<a href=\"javascript:sl=' + j + ';show_slide();\">Slide' + k + '</a><br>' );
    }
    document.write('\t</td>\n');
    document.write('<td align=center valign=top><IMG align=\"top\" border=\"0\" NAME=\"img_disp\" src=\"' + slideshow[show_num].slide[0] + '\"><br><br>');
    document.write('<a href="javascript:sl=sl-1;show_slide();"><img border=0 src="' + base_dir + 'widgets/Prev.gif" name="prev"></a>' +
                   '<a href="javascript:sl=sl+1;show_slide();"><img border=0 src="' + base_dir + 'widgets/Next.gif" name="next"></a></td>');
    document.write('</tr>\n</table>\n');
}

function show_slide() {  // set the current slide to the number given by sl; swap in and out the pointer image and a transparent image
   var old_st;
   var new_st;
   old_st = 'slide' + old_sl;
   document[old_st].src = base_dir + "widgets/16tx_pix.gif";
   if( sl < 0 ) {sl = 0;}
   if( sl >= slideshow[show_num].len ) {sl = slideshow[show_num].len -1;}
   new_st = 'slide' + sl;
   document[new_st].src = base_dir + "widgets/pointer.png";
   document.img_disp.src = slideshow[show_num].slide[sl];
   old_sl = sl;
}

function getArgs() {
//
//  routine taken straight from the book: JavaScript the Definitive Guide
//     by David Flanagan
   var args = new Object();
   var query = location.search.substring(1);
   var pairs = query.split("&");
   for( var i=0; i<pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
      if (pos == -1) continue;
      var argname = pairs[i].substring(0,pos);
      var value = pairs[i].substring(pos+1);
      args[argname] = unescape(value);
   }
   return args;
}
