/*****************************************************************************
 * Easter 2006 - slideshow.js
 * v 1.0 
 * Author: Jason Meade (jemeade@gmail.com)
 * Last update: Apr 10, 2006
 *
 * 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, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 *****************************************************************************/

// INSTRUCTIONS, or HOW THE HELL DO I MAKE THIS WORK?
// 
// Before you can use slideshow.js, you must define the images that you wish 
// to display, and the default speed at which to display them. The images
// must be stored in an Array object called 'slideshow_images'. The display
// speed, unsurprisingly, must be stored in an Integer object named 
// 'slideshow_speed'.
// 
// For example:
// 
// var slideshow_images = new Array ("mypuppy.jpg", 
//                                   "pretty kitty.jpg",
//                                   "mom.jpg",
//                                   "dad.jpg");
// 
// var slideshow_speed = 5;
// 
// Here we have declared that our slideshow will contain four images, and
// will rotate a new image every five seconds.
// 
// These variables must be declare BEFORE you load this library, so you 
// will need to include two separate JavaScript tags. 
// 
// The following example shows the proper way to setup slideshow.js:
// 
// <!-- 
// Initialize the slideshow_images and slideshow_speed.
// Make certain that this code goes in the <HEAD> section of the page!
// -->
// <script language="javascript">
//    var slideshow_images = new Array ("mypuppy.jpg", 
//                                      "pretty kitty.jpg",
//                                      "mom.jpg",
//                                      "dad.jpg");
//    var slideshow_speed = 5;
// </script>
// 
// <!--
// Now it is safe to load the slideshow.js library.
// -->
// <script language="javascript" src="scripts/slideshow.js"></script>
// 
// Once this has been accomplished, you must embed the slideshow somewhere
// within the <BODY> of the html web page. This naturally requires yet another
// <script> tag. Within this tag you must call the 'slideshow_draw()' method.
// This method takes two true/false arguments. The first determines whether
// the toggle speed buttons are displayed, and the second whether the jump 
// button is displayed. The toggle speed buttons allow the user to adjust the 
// speed of the image rotation. The jump button allows the users to move from 
// any image to any other arbitrary image.
// 
// For example: 
// 
// <!--
// Draw the initial slideshow image along with the navigation buttons
// -->
// <script language="javascript">
//    slideshow_draw(false,true);
// </script>
// 
// This says to hide the toggle speed buttons, but allow the jump button.
// 

var slideshow_img     = 0; // The current image
var slideshow_previmg = new Image();
var slideshow_nextimg = new Image();

/* Cache the previous and next images for faster rendering */
function slideshow_cacheimages()
{
    slideshow_cachenextimg();
    slideshow_cacheprevimg();
}

/* Cache the next image in the series */
function slideshow_cachenextimg()
{
    var img;
    if (slideshow_img +1 >= slideshow_images.length) {
	img = slideshow_images [0];
    }
    else {
	img = slideshow_images[slideshow_img +1];
    }

    slideshow_nextimg.src = img;
}

/* Cache the previous image in the series */
function slideshow_cacheprevimg()
{
    var img;
    if (slideshow_img -1 < 0) {
	img = slideshow_images[slideshow_images.length -1];
    }
    else {
	img = slideshow_images[slideshow_img -1];
    }

    slideshow_previmg.src = img;
}

/* This writes the first image of your slideshow to the screen */
function slideshow_draw(allow_toggle_speed, allow_jump)
{
    /* Validate the library input */ 
    if (typeof slideshow_images == 'undefined') {
	window.alert ("Cannot find images for slideshow!");
    }

    if (typeof slideshow_speed == 'undefined') {
	window.alert ("Cannot find slideshow toggle speed!");
    }

    slideshow_cacheimages(); // cache one image forwards/backwards



    var html = new String();

	html += "<TABLE width=610><TR valign=bottom><TD align=left><FONT FACE='arial'>Saint Augustine By-the-Sea Episcopal Church</font></TD><TD align=right>";

    html += "<button onClick='javascript:slideshow_prev()'>Back</button> ";
    html += "<button onClick='javascript:slideshow_slide()'>Play</button> ";
    html += "<button onClick='javascript:slideshow_pause()'>Stop</button> ";
    html += "<button onClick='javascript:slideshow_next()'>Next</button> ";

    if (allow_toggle_speed == true) {
	html += "<button onClick='javascript:slideshow_step(-1)'>Faster</button> ";
	html += "<button onClick='javascript:slideshow_step(1)'>Slower</button> ";
    }

    if (allow_jump == true) {
	html += "<button onClick='javascript:slideshow_jump()'>Jump...</button> ";
    }

	html += "</TD></TR><TR><TD colspan=2 ALIGN=CENTER>";


    html += "<img name='slideshow' src='" + slideshow_images[0] + "' border='0'>";

	html += "</TD></TR></TABLE>";

    document.write(html);
}

/* Move to the next image in the slideshow */
function slideshow_next() {
    document.images["slideshow"].src = slideshow_nextimg.src;
    slideshow_img += 1;

    if (slideshow_img >= slideshow_images.length) {
	slideshow_img = 0;
    }

    window.status = "Image no. " + (slideshow_img +1) + " of " + slideshow_images.length;
    slideshow_cacheimages();
}

/* Move to the previous image in the slideshow */
function slideshow_prev() {
    document.images["slideshow"].src = slideshow_previmg.src;
    slideshow_img -= 1;

    if (slideshow_img < 0) {
	slideshow_img = slideshow_images.length -1;;
    }

    window.status = "Image no. " + slideshow_img + " of " + slideshow_images.length;
    slideshow_cacheimages();
}

/* Jump to an arbitrary image in the slideshow */
/* This will pause the slideshow, requiring the user to resume manually. */
function slideshow_jump() {
    slideshow_pause();
    img = window.prompt("There are " + slideshow_images.length + " images. Which one do you want to jump to?",1);
    img -= 1;
    if (img < 0) {
	img = 0;
	window.alert("That number is too small. Moving to the beginning.");
    }
    if (img >= slideshow_images.length) {
	img = slideshow_images.length -1;
	window.alert("That number is too large. Moving to the end.");
    }

    slideshow_img = img;
    document.images["slideshow"].src = slideshow_images[slideshow_img];
    slideshow_cacheimages();
}

/* Pause the slideshow. */
var slideshow_timeout;
function slideshow_pause() {
    clearTimeout(slideshow_timeout);
}

/* Set or reset the default time-step between image changes */
function slideshow_step(val) {
    slideshow_speed += val;
    if (slideshow_speed < 1)
	slideshow_speed = 1;
    window.status = slideshow_speed + " seconds betwen images";
}

/* This starts the show! */
function slideshow_slide() {
    slideshow_pause();
    slideshow_next();
    slideshow_timeout = setTimeout("slideshow_slide()", (slideshow_speed * 1000));
}

