/* Array to hold preloaded images */
var pics = Array();

/* Number of increments to use when resizing */
var steps = 5;

/* Holds the DOM reference to the image */
var display;

/* Keeps track of where we are in the process */
var counter;

/* Total number of images in the gallery */
var totalimages = 14;

function init()
{
	counter = 1;
	display = document.getElementById('display_image');

	loadImage(1);
	loadImage(2);
	loadImage(totalimages);
}

function loadImage(index) {
	if (! pics[index]) {
		pics[index] = new Image();
		pics[index].src = '/slideshow/'+index+'.jpg';
	}
}

function advance()
{
	if (counter == totalimages) {
		previous = counter;
		counter = 1;
	} else if (counter == totalimages - 1) {
		previous = counter;
		counter++
	} else {
		previous = counter;
		counter++;
		loadImage(counter + 1);
	}
	
	initSlide();
}

function retreat()
{
	if (counter == 1) {
		previous = counter;
		counter = totalimages;
		loadImage(totalimages - 1);
		nextcounter = counter - 1;
	} else if (counter == 2) {
		previous = counter;
		counter--;
	} else {
		previous = counter;
		counter--;
		loadImage(counter - 1);
	}
	
	initSlide();
}

function initSlide()
{
	width_diff = pics[counter].width - pics[previous].width;
	width_increment = width_diff / steps;
	height_diff = pics[counter].height - pics[previous].height;
	height_increment = height_diff / steps;
	slide(100, width_increment, height_increment, 1);
}

function slide(opacity, width_increment, height_increment, tally_steps)
{
	if (tally_steps != steps) {
		tally_steps++;
		display.width += width_increment;
		display.height += height_increment;
		setOpacity(opacity)
		opacity -= 100/steps;
		window.setTimeout("slide("+opacity+","+width_increment+","+height_increment+","+tally_steps+")", 50);
	} else {
		display.style.backgroundImage = 'url(/slideshow/1.jpg)';
		setOpacity(0);
		display.src = '/slideshow/'+counter+'.jpg';
		display.width = pics[counter].width;
		display.height = pics[counter].height;
		fadeIn(10);
	}
}

function setOpacity(opacity) 
{
	opacity = (opacity == 100)?99.999:opacity;
 
	// IE/Win
	display.style.filter = "alpha(opacity:"+opacity+")";
 
	// Safari<1.2, Konqueror
	display.style.KHTMLOpacity = opacity/100;
 
	// Older Mozilla and Firefox
	display.style.MozOpacity = opacity/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	display.style.opacity = opacity/100;
}

function fadeIn(opacity) 
{
	if (opacity <= 100) 
	{
		setOpacity(opacity);
		opacity += 5;
		window.setTimeout("fadeIn("+opacity+")", 50);
	}
}

function fadeOut(opacity) 
{
	if (opacity <= 100) 
	{
		setOpacity(opacity);
		opacity -= 5;
		window.setTimeout("fadeOut("+opacity+")", 50);
	}
}
