﻿var FRAME_PAUSE = 5 * 1000;		// 5 seconds	
var FRAME_SPEED = 50;			// 0.5 seconds
var TRANSITION_STEP = 5;		// 5% transparency change per frame


var transitionTimer = null;
var faderTimer = null;

var imageList = [];
var currentImage = 0;
var currentOpacity = 0;

function StartRotator() {
	var i = 0;
	var thisImage;
	while(thisImage = document.getElementById("rotatingImage" + i)) {
		imageList.push(thisImage);
		i++;
		}
	currentImage = 0;

	if (document.getElementById("rotatingImage0").style.opacity || (navigator.platform=="Win32" && document.getElementById("rotatingImage0").style.filter)) {
		transitionTimer = setInterval(StartFader, FRAME_PAUSE);
		}
	else {
		transitionTimer = setInterval(StartFlipper, FRAME_PAUSE);
		}
	
	}



function StartFader() {
	currentImage++;
	if(currentImage < imageList.length) {
		// start fading in
		currentOpacity = 0;
		imageList[currentImage].style.visibility = "visible";
		faderTimer = setInterval(FadeIn, FRAME_SPEED);
		}
	else {
		// hide everything below and start fading out
		for(var i = 1; i < (imageList.length - 1); i++) {
			imageList[i].style.opacity = 0;
			imageList[i].style.filter = "alpha(opacity=0)";
			imageList[i].style.visibility = "hidden";
			}
		currentOpacity = 100;
		currentImage = imageList.length - 1;
		faderTimer = setInterval(FadeOut, FRAME_SPEED);
		}
	}



function FadeIn() {
	if (currentOpacity < 100) {
		currentOpacity += TRANSITION_STEP;
		imageList[currentImage].style.opacity = Math.min(currentOpacity,99) / 100;
		imageList[currentImage].style.filter = "alpha(opacity=" + currentOpacity + ")";
		}
	else {
		clearInterval(faderTimer);
		}
	}



function FadeOut() {
	if (currentOpacity > 0) {
		currentOpacity -= TRANSITION_STEP;
		imageList[currentImage].style.opacity = currentOpacity / 100;
		imageList[currentImage].style.filter = "alpha(opacity=" + currentOpacity + ")";
		}
	else {
		clearInterval(faderTimer);
		imageList[currentImage].style.visibility = "hidden";
		currentImage = 0;
		}
	}



function StartFlipper() {
	currentImage++;
	if(currentImage < imageList.length) {
		// flip to next photo
		imageList[currentImage].style.visibility = "visible";
		imageList[currentImage].style.opacity = 1;
		imageList[currentImage].style.filter = "alpha(opacity=100)";
		}
	else {
		// hide everything
		for(var i = 1; i < imageList.length; i++) {
			imageList[i].style.visibility = "hidden";
			}
		currentImage = 0;
		}
	}
