// $Id: slideshow.js,v 1.2 2005/09/22 04:14:44 John Exp $
// Javascript slideshow functions for slideshow.php

if (!window.total) {var total = 0;}
if (!window.loaded) {var loaded = 0;}
if (!window.fadeSpeed) {var fadeSpeed = 3;}
if (!window.showSpeed) {var showSpeed = 5000;}
if (!window.current) {var current = 1;}
if (!window.timer1) {var timer1 = null;}
if (!window.faderNext) {var faderNext = null;}
if (!window.faderCurrent) {var faderCurrent = null;}
if (!window.faderNextCaption) {var faderNextCaption = null;}
if (!window.faderCurrentCaption) {var faderCurrentCaption = null;}
if (!window.loop) {var loop = 1;}
if (!window.slidename) {var slidename = 'slide';}

// Initializes everything, counts the number of images, sets
// up and starts the progress and speed displays.
function init() {
	for(i=1;i<=999;i++) {
		var caption = document.getElementById('caption'+i);
		if (caption != null) {
			if (i == 1) {
				caption.style.visibility = 'visible';
			} else {
				caption.style.visibility = 'hidden';
			}
		}
		var slide = document.getElementById(slidename+i);
		if (slide != null) {
			if (i == 1) {
				slide.style.visibility = 'visible';
			} else {
				slide.style.visibility = 'hidden';
			}
			total = i;
		} else {
			break;
		}
	}
	new Fadomatic(document.getElementById("controls"), 2, 0, 0, 100).fadeIn();
	displaySpeed();
	displayProgress();
}

// Captures the image loading progress
function imageLoaded() {
	loaded++;
	displayProgress();
	if (loaded >= total) {
		new Fadomatic(document.getElementById("progress"), 10, 100, 0, 100).fadeOut();
	}
}

// Show the progress indicator
function displayProgress() {
	var e = document.getElementById('loaded');
	if (e != null) {
		e.value = loaded;
	}
	var e = document.getElementById('total');
	if (e != null) {
		e.value = total;
	}
}

// Show the slideshow speed
function displaySpeed() {
	var e = document.getElementById('speed');
	if (e != null) {
		e.value = showSpeed/1000;
	}
}

// Do a crossfade between the current image and the next one
function fadeTo(next) {
	stop();
	
	if (next > total) { next = 1; }
	if (next <= 0) { next = total; }
	
	var nextSlide = document.getElementById(slidename+next);
	var currentSlide = document.getElementById(slidename+current);
	var nextCaption = document.getElementById('caption'+next);
	var currentCaption = document.getElementById('caption'+current);
	
	if (nextSlide != null) {
		faderNext = new Fadomatic(nextSlide, fadeSpeed, 0, 0, 100);
		faderNext.fadeIn();
	}
	
	if (currentSlide != null) {
		faderCurrent = new Fadomatic(currentSlide, fadeSpeed, 100, 0, 100);
		faderCurrent.fadeOut();
	}
	
	if (nextCaption != null) {
		faderNextCaption = new Fadomatic(nextCaption, fadeSpeed, 0, 0, 100);
		faderNextCaption.fadeIn();
	}
	
	if (currentCaption != null) {
		faderCurrentCaption = new Fadomatic(currentCaption, fadeSpeed, 100, 0, 100);
		faderCurrentCaption.fadeOut();
	}
	
	current = next;
}

// Show the next image
function showNext() {
	if (current + 1 > total) {
		if (confirm("You are at the end.  Would you like to see it again?")) {
			fadeTo(current + 1);
		}
	} else {
		fadeTo(current + 1);
	}
}

// Show the previous image
function showPrevious() {
	fadeTo(current - 1);
}

// Play the slideshow
function play() {
	if (current + 1 > total) {
		stop();
		if (loop == 1 || loop == 0 && confirm("You are at the end.  Would you like to see it again?")) {
			if (loop == 0 && confirm("Would you like to play this show continuously?")) {
				loop = 1;
			}
			fadeTo(1);
			timer1 = setTimeout("play()", showSpeed);
		}
	} else {
		fadeTo(current + 1);
		timer1 = setTimeout("play()", showSpeed);
	}
	
	if (timer1 != null) {
		var playbutton = document.getElementById("playbutton");
		var stopbutton = document.getElementById("stopbutton");
		if (playbutton != null && stopbutton != null) {
			playbutton.style.display = "none";
			stopbutton.style.display = "inline";
		}
	}
}

// Decrease the slideshow speed
function slower() {
	showSpeed = showSpeed + 1000;
	if (showSpeed > 10000) { showSpeed = 10000; }
	updateTimer();
}

// Increase the slideshow speed
function faster() {
	showSpeed = showSpeed - 1000;
	if (showSpeed <= 0) { showSpeed = 1000; }
	updateTimer();
}

// Reset the slideshow timer after the speed has changed
function updateTimer() {
	displaySpeed();
	if (timer1 != null) {
		clearTimeout(timer1);
		timer1 = setTimeout("play()", showSpeed);
	}
}

// Stop the slideshow
function stop() {
	if (faderNext != null) {
		faderNext.show();
	}
	if (faderCurrent != null) {
		faderCurrent.hide();
	}
	if (faderNextCaption != null) {
		faderNextCaption.show();
	}
	if (faderCurrentCaption != null) {
		faderCurrentCaption.hide();
	}
	if (timer1 != null) {
		clearTimeout(timer1);
		timer1 = null;
	}
	var playbutton = document.getElementById("playbutton");
	var stopbutton = document.getElementById("stopbutton");
	if (playbutton != null && stopbutton != null) {
		playbutton.style.display = "inline";
		stopbutton.style.display = "none";
	}
}

// Delete this caption and associated data
function deleteCaption(n) {
	if (confirm("Are you sure?")) {
		var e = document.getElementById("caption"+n);
		if (e != null) {
			e.parentNode.removeChild(e);
			e = document.getElementById("hidden_image"+n);
			if (e != null) {
				e.parentNode.removeChild(e);
			}
		}
		var next = findNextCaption(0, 1);
		if (next == -1) {
			e = document.getElementById("finishbutton");
			if (e != null) { e.style.display = "none"; }
			e = document.getElementById("nocaptions");
			if (e != null) { e.style.display = "block"; }
		}
	}
}

// Swap all elements associated with a source (s) and target (t) caption index
function swap(s, t) {
	// Swap values
	var image, title, caption, url, preview;
	var s1, s2, s3, s4, s5;
	var t1, t2, t3, t4, t5;
	
	t1 = document.getElementById("hidden_image"+t);
	t2 = document.getElementById("imagetitle"+t);
	t3 = document.getElementById("imagecaption"+t);
	t4 = document.getElementById("imageurl"+t);
	t5 = document.getElementById("previewimage"+t);
	
	s1 = document.getElementById("hidden_image"+s);
	s2 = document.getElementById("imagetitle"+s);
	s3 = document.getElementById("imagecaption"+s);
	s4 = document.getElementById("imageurl"+s);
	s5 = document.getElementById("previewimage"+s);
	
	image = t1.value;
	title = t2.value;
	caption = t3.value;
	url = t4.value;
	preview = t5.src;
	
	t1.value = s1.value;
	t2.value = s2.value;
	t3.value = s3.value;
	t4.value = s4.value;
	t5.src = s5.src;
	
	s1.value = image;
	s2.value = title;
	s3.value = caption;
	s4.value = url;
	s5.src = preview;
}

// Swap this caption with the one above it
function moveCaptionUp(n) {
	var e = document.getElementById("caption"+n);
	var t = findNextCaption(n, -1);
	if (t == -1) {
		alert("This image can't be moved up.");
		return;
	}
	swap(n, t);
}

// Swap this caption with the one below it
function moveCaptionDown(n) {
	var e = document.getElementById("caption"+n);
	var t = findNextCaption(n, 1);
	if (t == -1) {
		alert("This image can't be moved down.");
		return;
	}
	swap(n, t);
}

// Find the next available caption id given a starting point n and a direction
// Returns the index of the next caption found in the given direction or
// -1 if nothing found.
function findNextCaption(n, dir) {
	for(i=n+dir;i>=0 && i<=99;i+=dir) {
		var e = document.getElementById("caption"+i);
		if (e != null) { return(i); }
	}
	return(-1);
}

