﻿var numpics = 31;
var timeperslide = 5000;
var frameinterval = 50;
var pixelsperframe = 60;
var curimgnum = 0;
var slideheight = 0;

var ssdiv;
var newimgnum;
var newdiv;
var animinterval;
var slide;

function annivslideshowinit() {
	ssdiv = document.getElementById('slideshow');
	preparenext();
	showifloaded();
}

function preparenext() {
	if (curimgnum == 0) newimgnum = 1;
	else {
		do {
			newimgnum = Math.floor(Math.random() * numpics) + 1;}
		while (newimgnum == curimgnum)
	}
	var newimg = new Image();
	newimg.setAttribute('src', 'slideshow/' + newimgnum + '.jpg');
	newimg.setAttribute('alt', 'Slideshow Pic');
	newdiv = document.createElement('div');
	newdiv.className = 'slide';
	newdiv.style.maxHeight = '0px';
	newdiv.appendChild(newimg);}

function showifloaded() {
	if (newdiv.firstChild.complete) shownext();
	else newdiv.firstChild.onload = shownext;}

function shownext() {
	if (curimgnum) animinterval = setInterval('hideslide();', frameinterval);
	else animinterval = setInterval('showslide();', frameinterval);}

function hideslide() {
	if (slideheight == 0) {
		clearInterval(animinterval);
		ssdiv.removeChild(slide);
		animinterval = setInterval('showslide();', frameinterval);}
	else {
		slideheight -= pixelsperframe;
		if (slideheight < 0) slideheight = 0;
		slide.style.maxHeight = slideheight + 'px';}}

function showslide() {
	if (slideheight == 0) {
		slide = newdiv;
		ssdiv.appendChild(slide);}
	slideheight += pixelsperframe;
	if (slideheight > 600) slideheight = 600;
	slide.style.maxHeight = slideheight + 'px';
	if (slideheight == 600) {
		clearInterval(animinterval);
		curimgnum = newimgnum;
		preparenext();
		setTimeout('showifloaded();', timeperslide)}}

//make sure a previous window.onload is not overwritten
if (window.onload) {
	var annivprevinit = window.onload;
	window.onload = function() {
		annivprevinit();
		annivslideshowinit();
	}
}
else window.onload = annivslideshowinit;