var presentationRunning = false;
var presentationTimeout = 0;
var presentationIndex = 0;

function switchPresentation() {
	$('presentation_link').innerHTML = presentationRunning ? presentationStartTitle : presentationStopTitle;
	
	presentationRunning = !presentationRunning;
	
	if (presentationTimeout != 0) {
		clearTimeout(presentationTimeout);
		presentationTimeout = 0;
	}else{
		if (presentationIndex == picturesArray.length - 1){
			presentationIndex = -1;
		}
		playPresentation();
	}
}

function playPresentation(){
	presentationTimeout = setTimeout('playPresentation()',presentationDelay * 1000);
	showNext();
}

function showGalleryPicture(){
	picUrl = $('gallery_image').src;
	$('gallery_image').src = picUrl.substr(0,picUrl.lastIndexOf('/')) + '/' + picturesArray[presentationIndex]['filename'];
	$('gallery_image').title = picturesArray[presentationIndex]['name'];
	$('phototitle').innerHTML = picturesArray[presentationIndex]['name'];
	$('gallerydesc').innerHTML = picturesArray[presentationIndex]['description'];
}

function showNext(){
	presentationIndex++;
	if (presentationIndex == picturesArray.length - 1){
		if (presentationRunning){
			switchPresentation();
		}
	}
	showGalleryPicture();
	checkLinkVisibility();
}

function showPrevious(){
	presentationIndex--;
	checkLinkVisibility()
	$('photofwrd').style.display = '';
	showGalleryPicture();
}

function checkLinkVisibility(){
	if (presentationIndex == picturesArray.length - 1){
		$('photofwrd').style.display = 'none';
		$('photofwrd_disabled').style.display = '';
	}else{
		$('photofwrd').style.display = '';
		$('photofwrd_disabled').style.display = 'none';
	}
	
	if (presentationIndex <= 0){
		$('photoback').style.display = 'none';
		$('photoback_disabled').style.display = '';
	}else{
		$('photoback').style.display = '';
		$('photoback_disabled').style.display = 'none';
	}
}
