var photoCacheSrc = [];
var photoCacheImg = [];

var photoDelay = 5000;
var photoShown = 1;
var photoSrc;

function transitionOut () {
	photoImg2.src = photoSrc;
	photoImg2.show();
	photoShown = 0;
	if(typeof(photoLoading) !== 'undefined') photoLoading.style.visibility = 'hidden';
	new Effect.Opacity(photoImg1, {from: 0.999, to: 0, duration: 2.0});
	window.setTimeout(photoRotation,photoDelay);
}
function transitionIn () {
	photoImg1.src = photoSrc;
	photoImg1.show();
	photoShown = 1;
	if(typeof(photoLoading) !== 'undefined') photoLoading.style.visibility = 'hidden';
	new Effect.Opacity(photoImg1, {from: 0, to: 0.999, duration: 2.0});
	window.setTimeout(photoRotation,photoDelay);
}

photoRotation = function () {

	if (photos && photos.length && photoShown) {

		photoSrc = photoImg1.src.replace(/.*\//,'');
		var photoIndex = photos.indexOf(photoSrc);
		var photoCacheIndex = photoCacheSrc.indexOf(photoSrc);
		if(photoCacheIndex == -1)
			photoCacheSrc.push(photoSrc);

		photoSrc = photoImg1.src.replace(/\/[^\/]*$/,'');

		if(photos.length > photoIndex+1)
			photoSrc = photoSrc + '/' + photos[photoIndex+1];
		else
			photoShown = 0;
//			photoSrc = photoSrc + '/' + photos[0];


		if(photoCacheIndex == -1) {
			if(typeof(photoLoading) !== 'undefined') photoLoading.style.visibility = '';
			photoCacheImg.push(new Image());
			photoCacheImg[photoCacheImg.length-1].onload = transitionOut;
			photoCacheImg[photoCacheImg.length-1].src = photoSrc;

		} else {
			transitionOut();
		}

	} else {

		photoSrc = photoImg2.src.replace(/.*\//,'');
		var photoIndex = photos.indexOf(photoSrc);
		var photoCacheIndex = photoCacheSrc.indexOf(photoSrc);
		if(photoCacheIndex == -1)
			photoCacheSrc.push(photoSrc);

		photoSrc = photoImg2.src.replace(/\/[^\/]*$/,'');

		if(photos.length > photoIndex+1)
			photoSrc = photoSrc + '/' + photos[photoIndex+1];
		else
			photoShown = 0;
//			photoSrc = photoSrc + '/' + photos[0];


		if(photoCacheIndex == -1) {
			if(typeof(photoLoading) !== 'undefined') photoLoading.style.visibility = '';
			photoCacheImg.push(new Image());
			photoCacheImg[photoCacheImg.length-1].onload = transitionIn;
			photoCacheImg[photoCacheImg.length-1].src = photoSrc;

		} else {
			transitionIn();
		}

	}
}

if(window.addEventListener)
	window.addEventListener("load",photoRotation,false);
else if (window.attachEvent)
	window.attachEvent("onload",photoRotation);
