/******************************************************************************/
/* Site web nepascroquer.com
/*
/* Graphismes: Guillaume Ruan (ruan_guillaume@yahoo.com)
/* Code:       Loïc Le Page   (loic.le.page@gmail.com)
/*
/* File: /script/diaporama.js
/* Version: 1.0
/*
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
// Diaporama static variables
////////////////////////////////////////////////////////////////////////////////

//Time in ms between successive opacity changes during transitions
//Default period is 33 ms (about 30 images per second)
Diaporama.TRANSITION_STEP_PERIOD = 33;

////////////////////////////////////////////////////////////////////////////////
// Diaporama class
////////////////////////////////////////////////////////////////////////////////

function Diaporama(div)
{
	////////////////////////////////////////////////////////////////////////////
	// Diaporama public interface declaration
	////////////////////////////////////////////////////////////////////////////

	this.setImageArray = setImageArray;

	this.showImage = showImage;
	this.getDisplayedImage = getDisplayedImage;

	this.showNextImage = showNextImage;
	this.showPrevImage = showPrevImage;

	this.setTransitionDuration = setTransitionDuration;

	this.startSlideShow = startSlideShow;
	this.stopSlideShow = stopSlideShow;
	this.isSlideShowRunning = function()
	{
		return (timer > 0);
	}

	////////////////////////////////////////////////////////////////////////////
	// Diaporama private members and methods
	////////////////////////////////////////////////////////////////////////////

	var targetDiv = div;

	div.style.backgroundRepeat = "no-repeat";
	div.style.backgroundPosition = "center";
	div.innerHTML = '<img border="0" alt="">';

	var targetImg = div.firstChild;

	var imgArray = null;

	var imgIndex = 0;

	function setImageArray(array)
	{
		if (!array || !(array instanceof Array) || (array.length <= 0))
			imgArray = null;
		else
		{
			imgArray = array;
			if (imgIndex < 0)
				imgIndex = 0;
			else if (imgIndex >= imgArray.length)
				imgIndex = imgArray.length - 1;
		}

		blendImage();
	}

	function showImage(index)
	{
		if (!imgArray)
			return;

		if (index < 0)
			imgIndex = 0;
		else if (index >= imgArray.length)
			imgIndex = imgArray.length - 1;
		else
			imgIndex = index;

		blendImage();
	}

	function getDisplayedImage()
	{
		if (!imgArray)
			return null;

		return imgArray[imgIndex];
	}

	function showNextImage()
	{
		if (!imgArray)
			return;

		imgIndex++;
		if (imgIndex >= imgArray.length)
			imgIndex = 0;

		blendImage();
	}

	function showPrevImage()
	{
		if (!imgArray)
			return;

		imgIndex--;
		if (imgIndex < 0)
			imgIndex = imgArray.length - 1;

		blendImage();
	}

	var currentImgAlpha = 0;

	function setImgOpacity(alpha)
	{
		if (alpha < 0)
			currentImgAlpha = 0;
		else if (alpha > 1)
			currentImgAlpha = 1;
		else
			currentImgAlpha = alpha;

		targetImg.style.opacity = currentImgAlpha;
		targetImg.style.MozOpacity = currentImgAlpha;
		targetImg.style.KhtmlOpacity = currentImgAlpha;

		if (alpha < 1)
			targetImg.style.filter = "alpha(opacity=" + currentImgAlpha * 100 + ")";
		else
			targetImg.style.filter = "";
	}

	var alphaIncrement = Diaporama.TRANSITION_STEP_PERIOD / 500;

	function setTransitionDuration(d)
	{
		if (d <= 0)
			alphaIncrement = 0;
		else
			alphaIncrement = Diaporama.TRANSITION_STEP_PERIOD / (d * 500);
	}

	function transition()
	{
		if (alphaIncrement <= 0)
			setImgOpacity(1);
		else
			setImgOpacity(currentImgAlpha + alphaIncrement);

		if (currentImgAlpha >= 1)
			targetDiv.style.backgroundImage = "none";
		else
			setTimeout(transition, Diaporama.TRANSITION_STEP_PERIOD);
	}

	function blendImage()
	{
		if (!imgArray)
		{
			targetDiv.style.width = "0px";
			targetDiv.style.height = "0px";
			targetDiv.style.backgroundImage = "none";

			targetImg.src = "";
		}
		else if (alphaIncrement <= 0)
		{
			targetDiv.style.width = imgArray[imgIndex].width + "px";
			targetDiv.style.height = imgArray[imgIndex].height + "px";
			targetDiv.style.backgroundImage = "none";

			targetImg.width = imgArray[imgIndex].width;
			targetImg.height = imgArray[imgIndex].height;
			targetImg.src = imgArray[imgIndex].src;

			setImgOpacity(1);
		}
		else
		{
			targetDiv.style.width = imgArray[imgIndex].width + "px";
			targetDiv.style.height = imgArray[imgIndex].height + "px";

			if (!targetImg.src)
				targetDiv.style.backgroundImage = "none";
			else
				targetDiv.style.backgroundImage = 'url("' + targetImg.src + '")';

			setImgOpacity(0);
			targetImg.width = imgArray[imgIndex].width;
			targetImg.height = imgArray[imgIndex].height;
			targetImg.src = imgArray[imgIndex].src;

			setTimeout(transition, Diaporama.TRANSITION_STEP_PERIOD);
		}
	}

	var timer = 0;

	function startSlideShow(d)
	{
		if (d <= 0)
			return;

		if (timer > 0)
			clearInterval(timer);

		timer = setInterval(showNextImage, d * 2000);
	}

	function stopSlideShow()
	{
		if (timer > 0)
		{
			clearInterval(timer);
			timer = 0;
		}
	}
}

