// Delay between transitions (s * 1000ms)
var delay = 6 * 1000;
// Delay interval
var delayInt;
// Photo array (loaded by startSlideshow)
var photos;

// Frequency of effect (ms)
var effect_f = 50;
// Duration of effect (ms)
var effect_d = 500;
// Effect interval
var effectInt;

// Control variables
var fading = false;
var first = 1;
var current = first;

function startSlideshow() {
	// Find all photos.
	photos = getElementsByClassName("slideshow", "img");
	delayInt = window.setInterval(nextSlide, delay);
}

function trans(elA, elB) {
	// How many times should we fire this?
	var n = effect_d/effect_f;
	// How much should the alpha change each time?
	var jump = parseInt(100 / n);
	window.clearInterval(effectInt);
	
	// Initiate alpha
	elA.alpha = 100;
	elB.alpha = 0;
	// Standards Opacity
	elA.style.opacity = 1;
	elB.style.opacity = 0;
	// IE Opacity
	elA.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100);";
	elB.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0);";
	
	// Add the transition class
	elA.className = 'slideshow show transition dropshadow';
	elB.className = 'slideshow hide transition dropshadow';
	
	// Make sure both are visible
	elA.style.display = 'block';
	elB.style.display = 'block';
	
	effectInt = window.setInterval(function () {
		// Standards Opacity
		elA.style.opacity = (elA.alpha - jump) / 100;
		elB.style.opacity = (elB.alpha + jump) / 100;
		// IE Opacity
		elA.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (elA.alpha - jump) + ");";
		elB.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (elB.alpha + jump) + ");";
		
		// Save new alpha
		elA.alpha = elA.alpha - jump;
		elB.alpha = elB.alpha + jump;

		if (elA.alpha < jump) {
			elA.style.opacity = 0;
			elB.style.opacity = 1;
			elA.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0);";
			elB.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100);";
			elB.style.filter = ""; // Clearing this avoids an IE7 bug.

			elA.style.display = 'none';

			// Remove the transition class
			elA.className = 'slideshow hide';
			elB.className = 'slideshow show dropshadow';

			window.clearInterval(effectInt);
			fading = false;
		}
	}, effect_f);
}

function nextSlide() {
	if (fading != true) {
		fading = true;
				
		if (current >= photos.length - 1) {
			next = 0;
		} else {
			next = current + 1;
		}
		trans(photos[current], photos[next]);
		current = next;
	}
}