var isFading = false;
arrFading = new Array();
var CurrentBigPicture = 0; //switches between 1 and 0 to keep track of which one is on top
var autoPlay = true;
//NewPictureIndex = 0;
var carousselTheBigPictureCount = 0;
var carousselTimeout = false;
var fadeTimeout = false;
var reattachedImg = false;
function startSlideshow(){
	if (window.loaded){
		carousselTheBigPicture(startTime);
	}else{
		setTimeout('startSlideshow();',900);
	}
}
startSlideshow();

function fade(el,target,step,current){ if (window.loaded){
	if (!el.id){ el = document.getElementById(el); } // input strings also
	if (true){ //ns doesn't fade
		(target != -1)?arrFading[el.id+'t'] = target:target = arrFading[el.id+'t'];
		(current)?arrFading[el.id+'c'] = current:current = arrFading[el.id+'c'];
		if (current > 0){ el.style.visibility='visible'; }
		if (Math.abs(target-(Math.abs(current)+step)) < Math.abs(target-Math.abs(current))){
			el.style.filter='alpha(opacity='+current+')';
			el.style.opacity=current / 100;
			el.style.MozOpacity=current / 100;
			if (target - current == step){ step = step + 1; }//bug when reaching exactly zero: fade loops
			fadeTimeout = setTimeout('fade('+el.id+',-1,'+step+','+(current+step)+')',fadeSpeed);
		}else{
			current = target;
			el.style.filter='alpha(opacity='+target+')';
			el.style.opacity=target / 100;
			el.style.MozOpacity=target / 100;
			arrFading[el.id+'c']=target; }
		if (current == 0){ el.style.visibility='hidden'; }
		if (current == target){
			isFading = false;
			//document.getElementById('navPrevious').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Previous.gif';
			//document.getElementById('navNext').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Next.gif';
		}
	}else{
		(target==0)?el.style.visibility='hidden':el.style.visibility='visible'; }
} }
var NewPictureIndex = -1;
function carousselTheBigPicture(timeToWait,goBack){
	if (timeToWait == 0 || autoPlay){
		if (isFading){
			clearTimeout(carousselTimeout);
			carousselTimeout = setTimeout('carousselTheBigPicture('+timeToWait+','+goBack+');',100); //just a sec
		}else{
			if (timeToWait == 0){
				togglePlay(true);
				if (goBack){
					//document.getElementById('navPrevious').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Previous-ON.gif';
				}else{
					//document.getElementById('navNext').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Next-ON.gif';
				}
				//autoPlay = false;
				//clearTimeout(fadeTimeout);
			}
			if (goBack){
				NewPictureIndex--;
			}else{
				NewPictureIndex++;
			}
			if (NewPictureIndex >= arrBigPictures.length){
				NewPictureIndex = 0;
			}else if (NewPictureIndex < 0){
				NewPictureIndex = arrBigPictures.length - 1;
			}
			arrNewPicture = arrBigPictures[NewPictureIndex].split('|'); //0=src;1=fotograaf;2=type;3=titelNL;4=titelEN;
			myImage = new Image();
			//myImage.src = 'upload/'+SlideshowFolder+'/'+ReplaceAll(arrNewPicture[1],' ','_')+'/'+arrBigPictureTypes[arrNewPicture[2]][0]+'/'+arrNewPicture[0];
			myImage.src = 'upload/'+SlideshowFolder+'/'+arrNewPicture[0]; //types discontinued for now
			myImage.style.border = 'none';
			myImage.style.width = viewerImageWidth;
			myImage.style.height = viewerImageHeight;
			myImage.alt = arrNewPicture[2+languagePick];
			//BigPictureHtml = '<img src="'+myImage.src+'" alt="'+arrNewPicture[3]+'" border="0"/>';
			carousselInActiveDiv = document.getElementById('TheBigPicture'+Math.abs(CurrentBigPicture-1));
			while (carousselInActiveDiv.childNodes.length > 0){
				carousselInActiveDiv.removeChild(carousselInActiveDiv.childNodes[0]);
			}
			carousselInActiveDiv.appendChild(myImage);
			reattachedImg = false;
			//carousselInActiveDiv.innerHTML = BigPictureHtml;
			fadeTimeout = setTimeout('fadeTheBigPicture('+Math.abs(CurrentBigPicture-1)+',\''+arrBigPictureTypes[arrNewPicture[2]][0]+'\');',timeToWait);
		} // don't do it when still fading!
	} //do not carousselTheBigPicture if autoPlay was toggled off
}
function fadeTheBigPicture(BigPictureIndex,BigPictureType){
	carousselActiveDiv = document.getElementById('TheBigPicture'+BigPictureIndex);
	isFading = true;
	if (carousselActiveDiv.childNodes[0].complete){
		//highlight correct button
		for (iBigPictureTypes = 0;iBigPictureTypes < arrBigPictureTypes.length;iBigPictureTypes++){
			currentType = arrBigPictureTypes[iBigPictureTypes];
			try{
				//document.getElementById('nav'+currentType[0]).src = 'plugins/slideshow/button-'+TypeOfViewer+'-'+currentType[languagePick]+((BigPictureType == currentType[0])?'-ON':'')+'.gif';
			}catch(e){ }
		}
		//handle fade
		carousselActiveDiv.style.zIndex = 2;
		carousselInActiveDiv = document.getElementById('TheBigPicture'+CurrentBigPicture);
		carousselInActiveDiv.style.zIndex = 1;
		fade(carousselActiveDiv,128,fadeStep,2);//fadeSpeed);
		CurrentBigPicture = BigPictureIndex;
		if (autoPlay){
			carousselTimeout = setTimeout('carousselTheBigPicture('+showTime+');',parseInt(128/fadeStep*fadeSpeed)); //prepare next one after fade is completed
		}
	}else{
		if (!reattachedImg){
			temp = carousselActiveDiv.childNodes[0].src; //reattach because for some reason it often doesn't work the first time...
			carousselActiveDiv.childNodes[0].src = temp;
			reattachedImg = true;
		}
		fadeTimeout = setTimeout('fadeTheBigPicture('+BigPictureIndex+',\''+BigPictureType+'\');',100); //just a tenth of a sec!
//alert(document.getElementById('TheBigPicture'+BigPictureIndex).innerHTML);
	}
}
function togglePlay(stopPlay){
	if (isFading){
		//just ignore it for now
	}else{
		if (stopPlay){
			autoPlay = false;
			clearTimeout(fadeTimeout);
			//document.getElementById('navPlay').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Play.gif';
			//document.getElementById('navPause').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Pause-ON.gif';
		}else{
			autoPlay = true;
			carousselTheBigPicture(100);
			//document.getElementById('navPlay').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Play-ON.gif';
			//document.getElementById('navPause').src = 'plugins/slideshow/button-'+TypeOfViewer+'-Pause.gif';
		}
	}
}


function ReplaceAll(ReplaceAllIn,ReplaceAllWhat,ReplaceAllWith){
	if (ReplaceAllWith.indexOf(ReplaceAllWhat) != -1){
		alert('ReplaceAll function can\'t work.');
	}else{
		while (ReplaceAllIn.indexOf(ReplaceAllWhat) > 0){
			ReplaceAllIn = ReplaceAllIn.replace(ReplaceAllWhat,ReplaceAllWith);
		}
	}
	return ReplaceAllIn;
}



