/*----------------------------------------------------------------//
	slideshow.js
	Author: Peter Kosar  
	Last Update: June 10, 2010
	   
	Requires javascript data section with the following variables:
		var slide = new Array ();
		var notes = new Array();
		slide[0] = "images/Slide0001.gif";
		notes[0] = "This caption...';
		...
		// --- optional controls ---//
		var autoplay = true;	// automatically start on load
		var autostop = false;	// stop on last slide
		var delay = 6000;		// microseconds
		var firstslide = 0;
		var maxheight = 480;	// maximum display height of image in pixels
		var maxwidth = 640;		// maximum display width of image in pixels
		var showcaption = true;	// true = place notes[n] in 'caption' container
		var showpages = false;	// true = place "pagelabel n of nn" in 'slidenr' container
		var pagelabel = "Slide"	// part of showpages output

//----------------------------------------------------------------*/
var auto = true;			// <= autoplay
var autoend = false;		// <= autostop
var timer = 6000;			// <= delay
var index = 0;				// <= firstslide
var imgheight = 480;		// <= maxheight
var imgwidth = 640;			// <= maxwidth
var captions = true;		// <= showcaption
var slidenrs = false;		// <= showpages
var slidelabel = "Slide"	// <= pagelabel
var lastslide;

if (document.images){
	nextimage= new Image(1,1);  
	}
    
function Step(i){
	if(auto) clearTimeout(showtimer);
	GoTo(index + i);
	if(auto && (index < lastslide || !autoend)) showtimer = setTimeout("Step(1)", timer);
	else if(autoend){
		auto = false;
		document.getElementById('ppbtn').value = 0;
		document.getElementById('ppbtn').src = '../images/play.png';
		}
	}

function GoTo(newIndex){
	if(newIndex < 0) newIndex = slide.length-1;
	if(newIndex >= slide.length) newIndex = 0;
	
	var newImg = new Image();
	
	// wait until image is loaded before showing it
	newImg.onload = showNextImage(newImg,newIndex);
	
	newImg.src = slide[newIndex];
	}

function showNextImage(newImg,newIndex){
	newImg.src = slide[newIndex];
	var height = newImg.height;
	var width = newImg.width;

	if(height == 0 || width == 0){
		showtimer = setTimeout("GoTo("+newIndex+")", 1000);
		return;
		}

	if(height > imgheight) {
		r = imgheight/height;
		height = imgheight;
		width = Math.floor(width*r);
		}
	
	if(width > imgwidth){
		r = imgwidth/width;
		width = imgwidth;
		height = Math.floor(height*r);
		}
	
	img = document.getElementById('slideshow');
	img.height = height; //if(height == 0) img.removeAttribute("height");
	img.width = width;   //if(width == 0) img.removeAttribute("width");
	img.src = slide[newIndex];
	if(slidenrs) document.getElementById('slidenr').innerHTML = slidelabel+" "+(newIndex+1)+" of "+slide.length;
	if(captions) document.getElementById('caption').innerHTML = notes[newIndex];
	
	if(newIndex > index && newIndex < lastslide && document.images) nextimage.src = slide[newIndex+1];
	index = newIndex;
	}	
	

//Automate Presentation

function addEvent(toElement,verb,funct,tf){
	if(navigator.userAgent.match(/MSIE [0-8]/)) toElement.attachEvent("on"+verb, funct);
	else toElement.addEventListener(verb,funct,tf);
	}

addEvent(window,"load",startpresentation,false);

function startpresentation(){
	if(typeof(autoplay) != 'undefined') auto = autoplay;
	if(typeof(autostop) != 'undefined') autoend = autostop;
	if(typeof(delay) != 'undefined') timer = delay;
	if(typeof(firstslide) != 'undefined') index = firstslide;
	if(typeof(maxheight) != 'undefined') imgheight = maxheight;
	if(typeof(maxwidth) != 'undefined') imgwidth = maxwidth;
	if(typeof(showcaption) != 'undefined') captions = showcaption;
	if(typeof(showpages) != 'undefined') slidenrs = showpages;
	if(typeof(pagelabel) != 'undefined') slidelabel = pagelabel;
	lastslide = slide.length-1;
	if(auto) showtimer = setTimeout("Step(1)", timer);
	}

function ToggleShow(b){	
	clearTimeout(showtimer);
	if(b.value == 1){
		auto = false;
		b.value = 0;
		b.src = '../images/play.png';
		}
	else {
		auto = true;
		b.value = 1;
		b.src = '../images/pause.png';
		if(index == lastslide) { index = 0; Step(0); }
		else showtimer = setTimeout("Step(1)", 1000);
		}
	}
	
function beginShow(){
	index = 0;
	Step(0);
	}
	
function gotoEnd(){
	index = lastslide;
	Step(0);
	}
	
function pauseNshow(i){
	if(auto) {
		clearTimeout(showtimer);
		auto = false;
		document.getElementById('ppbtn').value = 0;
		document.getElementById('ppbtn').src = '../images/play.png';
		}
	GoTo(i);
	}