var gPhotos = new Array(
 		new Array(
    	"images/Slideshow/Photo1.jpg",
    	"images/Slideshow/Photo2.jpg",
		"images/Slideshow/Photo3.jpg",
		"images/Slideshow/Photo4.jpg"
    ),
 		new Array(
		"images/Slideshow/Photo5.jpg",
    	"images/Slideshow/Photo6.jpg",
		"images/Slideshow/Photo7.jpg",
		"images/Slideshow/Photo8.jpg"
    	
    ),
 		new Array(
    	"images/Slideshow/Photo9.jpg",
    	"images/Slideshow/Photo10.jpg",
		"images/Slideshow/Photo11.jpg",
		"images/Slideshow/Photo12.jpg"
    )
);

var gPause = 2000;
var gFade = 1000;

var gCurrentPhoto = gPhotos.length;
var gNextPhotoURL = "";
var gOpacity = 100;

window.onload = StartPhotoShuffle;

function FileName( path )
{
	var start = path.lastIndexOf("/");
	if (start == -1) return s;
	return path.substring(start+1, path.length);
}

function StartPhotoShuffle()
{
	gCurrentPhoto++;
	if ( gCurrentPhoto > gPhotos.length ) {
		gCurrentPhoto = 1;
	}
	var front = document.getElementById("Photo"+gCurrentPhoto+"Front");
	var back = document.getElementById("Photo"+gCurrentPhoto+"Back");
	
	var current = FileName( front.src );
	var p = gPhotos[gCurrentPhoto-1];
	do {
		gNextPhotoURL = p[Math.floor(Math.random()*p.length)];
	} while ( current == FileName( gNextPhotoURL ) );
	back.src=gNextPhotoURL;
	gOpacity = 100;
	setTimeout( "FadeOldPhoto()", gPause );
}

function FadeOldPhoto()
{
	var front = document.getElementById("Photo"+gCurrentPhoto+"Front");
	var back = document.getElementById("Photo"+gCurrentPhoto+"Back");
	
	var fade = 100 / (gFade / 30);
	if ( gOpacity < fade ) { // Done
		front.src = back.src;
		SetOpacity( front, 100 );
		StartPhotoShuffle();
	} else {
		gOpacity -= fade;
		SetOpacity( front, gOpacity );
		setTimeout( "FadeOldPhoto()",33 );
	}
}

function SetOpacity(elem, opacityAsInt)
{
    var opacityAsDecimal = opacityAsInt;
    
    if (opacityAsInt > 100)
        opacityAsInt = opacityAsDecimal = 100; 
    else if (opacityAsInt < 0)
        opacityAsInt = opacityAsDecimal = 0; 
    
    opacityAsDecimal /= 100;
    if (opacityAsInt < 1)
        opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
    
    elem.style.opacity = (opacityAsDecimal);
    elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

// JavaScript Document