coorgAddLoadEvent( function () {
	timeoutValue = 5000;
	animationTime = 2;
	fps = 40; 
	InsertPlayPauseButton();
	SetPlaying(true);
});

function GoToPreviousPhoto()
{
	clearTimeout(currentTimeOut);
	currentPhoto--;
	if (currentPhoto < 0) {
		currentPhoto = photos.length - 1;
	}
	changePhoto();
}

function GoToNextPhoto()
{
	clearTimeout(currentTimeOut); // In case this was initiated by the user clear the timeout
	currentPhoto++;
	if (currentPhoto >= photos.length) {
		currentPhoto = 0;
	}
	changePhoto();
}

function changeDimension(img, orig)
{
	var repScreen = document.getElementById('photo-reportage');
	orig.parentNode.replaceChild(img, orig);
	if (big) {
		if (repScreen.clientHeight) {
			var wH;
			if (window.innerHeight) {
				wH = window.innerHeight;
			} else {
				wH = document.documentElement.clientHeight;
			}
			if (repScreen.clientHeight + 10 > wH) {
				var origHeight = img.height;
				var newHeight = origHeight - (repScreen.clientHeight - wH) - 30;
				var origWidth = img.width;
				img.height = newHeight;
				if (origWidth == img.width) { // Fix for IE
					img.width = img.width*newHeight/origHeight;
				}
			}
		} else {
			if (img.height > window.innerHeight - 120) {
				img.height = window.innerHeight - 120;
			}
		}
		//repScreen.style.width = img.width+40+'px';
		//repScreen.style.marginLeft = '-'+repScreen.clientWidth/2+'px';
		//repScreen.style.marginRight = '-'+repScreen.clientWidth/2+'px';
	} else {
		repScreen.style.width = 'auto';
	}
}

function startAnimation()
{
	/*animationPhase1 = true
	startFadeOut();
	startFadeIn();*/
	var orimg = document.getElementById('puntstudio-slideshow-current');
	var newimg = document.createElement('img');
	newimg.id = orimg.id;
	newimg.onclick = orimg.onclick;
	newimg.onload = function() { changeDimension(newimg, orimg); }
	if (big == false) {
		newimg.src = photos[currentPhoto];
	} else {
		newimg.src = bigPhotos[currentPhoto];
	}
}

function doFadeOut()
{
	img = document.getElementById('puntstudio-slideshow-current');
	opacityChange = 1/(fps*animationTime/2);
	opacity -= opacityChange;
	img.style.opacity = opacity;
	
	if (opacity >= 0) {
		setTimeout(doFadeOut, 1000/fps);
	} else {
		animationPhase1 = false;
	}
}

function startFadeOut()
{
	img = document.getElementById('puntstudio-slideshow-current');
	opacity = 1;
	img.style.opacity = opacity;
	setTimeout(doFadeOut, 1000/fps);
}

function doFadeIn()
{
	img = document.getElementById('puntstudio-slideshow-current');
	opacityChange = 1/(fps*animationTime/2);
	opacity += opacityChange;
	img.style.opacity = opacity;
	if (opacity <= 1) {
		setTimeout(doFadeIn, 1000/fps);
	}
}

function startFadeIn()
{
	if (animationPhase1 == false) {
		img = document.getElementById('puntstudio-slideshow-current');
		if (big == false) {
			img.src = photos[currentPhoto];
		} else {
			img.src = bigPhotos[currentPhoto];
		}
		opacity = 0;
		img.style.opacity = opacity;
		setTimeout(doFadeIn, 1000/fps);
	} else {
		setTimeout(startFadeIn, 1000/fps);
	}
}

function changePhoto()
{
	prevLink = document.getElementById('puntstudio-slideshow-keys-prev');
	prevLink.innerHTML = "<a href=\"#\" onclick=\"GoToPreviousPhoto(); return false;\">"+contentMediaPrev+"</a>";
	startAnimation();
	if (playing) {
		currentTimeOut = setTimeout(GoToNextPhoto, timeoutValue);
	}
	nextLink = document.getElementById('puntstudio-slideshow-keys-next');
	nextLink.innerHTML = "<a href=\"#\" onclick=\"GoToNextPhoto(); return false;\">"+contentMediaNext+"</a>";
}

function InsertPlayPauseButton()
{
	button = document.createElement('span');
	button.id = 'puntstudio-slideshow-keys-playpause';
	nextLink = document.getElementById('puntstudio-slideshow-keys-next');
	nextLink.parentNode.insertBefore(button, nextLink);
}

function SetPlaying(val)
{
	playing = val;
	playPauseKey = document.getElementById('puntstudio-slideshow-keys-playpause');
	if (val == true && currentPhoto != photos.length - 1) {
		playPauseKey.innerHTML = "<a href=\"#\" onclick=\"SetPlaying(false); return false;\">"+contentMediaPause+"</a>";
		currentTimeOut = setTimeout(GoToNextPhoto, timeoutValue);
	} else if (val == false) {
		clearTimeout(currentTimeOut);
		playPauseKey.innerHTML = "<a href=\"#\" onclick=\"SetPlaying(true); return false;\">"+contentMediaPlay+"</a>";
	} else {
		// We are on the last photo and play is clicked => We start over
		currentPhoto = 0;
		changePhoto();
	}
}

function SwitchSize()
{
	var img = document.getElementById('puntstudio-slideshow-current');
	var reportage = document.getElementById('photo-reportage');
	help = document.getElementById('photo-help-text');
	if (big == true) {
		big = false;
		startAnimation();
		reportage.setAttribute('class', 'normal');
		reportage.setAttribute('className', 'normal');
		help.firstChild.data = "Klik op de foto om deze te vergroten.";
	} else {
		big = true;
		reportage.setAttribute('class', 'fullscreen');
		reportage.setAttribute('className', 'fullscreen');
		startAnimation();
		help.firstChild.data = "Klik op de foto om deze terug te verkleinen.";
	}
}
