/*
*	CSS Photo Shuffler v1.0 by
*	Carl Camera
*	http://iamacamera.org 
*
*	SetOpacity Function and inpiration from Photo Fade by
*	Richard Rutter
*	http://clagnut.com
*
*	License: Creative Commons Attribution 2.5  License
*	http://creativecommons.org/licenses/by/2.5/
*
*	Customize your photo shuffle settings
*
* *	Surround the target < img /> with a < div >. specify id= in both
*
* *	The first and final photo displayed is in the html <img> tag
*
* *	The array contains paths to photos you want in the rotation. 
*  	If you want the first photo in the rotation, then it's best to
*   	put it as the final array image.  All photos must be same dimension
*
* *	The rotations variable specifies how many times to repeat array.
*  	images. zero is a valid rotation value.
*/

var gblPhotoShufflerDivId = "photodiv";
var gblPhotoShufflerImgId = "photoimg"; 

/*var gblImg = new Array(
						"fotos_cli/foto_366_1.jpg",
						"fotos_cli/foto_366_2.jpg",
						"fotos_cli/foto_366_3.jpg"
						);*/

var gblPauseSeconds 	= 3;
var gblFadeSeconds 		= .85;
var gblRotations 		= 1;

var gblStartImg;
var gblDeckSize 		= gblImg.length;
var gblOpacity 			= 100;
var gblOnDeck 			= 0;
var gblImageRotations 	= gblDeckSize * (gblRotations + 1);

// array que efetua um pré-carregamento das imagens
var arrayImages = new Array();

for(i = 0; i < gblImg.length; i++)
{
	arrayImages[i] 		= new Image();
	arrayImages[i].src 	= gblImg[i];
}

// id do menu com o texto 'anterior'
document.getElementById('l').onclick = function(){photoShufflerShuffle();}

// id do menu com o texto 'próximo'
document.getElementById('r').onclick = function(){photoShufflerShuffle(1);}

function photoShufflerFade()
{
	var theimg = document.getElementById(gblPhotoShufflerImgId);

	// determina o valor de delta baseado em número de segundos
	var fadeDelta = 100 / (30 * gblFadeSeconds);

	// aplica o efeito de fade até o valor de opacity chegar a 0.1
	if(gblOpacity < 0.1)
	{
		gblOpacity = 100;
	}
	else
	{
		gblOpacity -= fadeDelta;
		setOpacity(theimg, gblOpacity);

		// 1/25 de um segundo
		setTimeout("photoShufflerFade()", 25);
	}
}

function photoShufflerShuffle(v)
{
	photoShufflerFade();

	var thediv = document.getElementById(gblPhotoShufflerDivId);
	var theimg = document.getElementById(gblPhotoShufflerImgId);

	// a img recebe o src do array
	theimg.src = gblImg[gblOnDeck];

	// a img recebe opacidade de valor 100
	setOpacity(theimg, 100);

	// mudança de variavel de indice da imagem
	if(v == 1)
	{
		gblOnDeck = ++gblOnDeck % gblDeckSize;
	}
	else
	{
		if(gblOnDeck - 1 < 0)
		{
			gblOnDeck = gblImg.length - 1;
		}
		else
		{
			--gblOnDeck;
		}
	}

	// mudança de imagem
	thediv.style.backgroundImage = 'url('+ gblImg[gblOnDeck] +')';
}
	
function setOpacity(obj, opacity)
{
	opacity 		= (opacity == 100) ? 99.999 : opacity;

	opacityFinal 	= opacity / 100;

	// internet explorer
	obj.style.filter = "alpha(opacity:"+ opacity +")";

	// safari < 1.2, konqueror
	obj.style.KHTMLOpacity 	= opacityFinal;

	// mozilla antigo e firefox
	obj.style.MozOpacity 	= opacityFinal;

	// safari 1.2, firefox e mozilla em versões recentes, css3
	obj.style.opacity 		= opacityFinal;
}
