/** function.js */

function displayPictureFadeOut(idImg,idTitle,idDesc,idDate,imgSrc,title,description,date,useTransition,timeToFade)
{
	// Do not reload an already opened picture.
	if (null != document.getElementById(idImg)) {
		var oldImgSrc = document.getElementById(idImg).src;
		
		oldImgSrc = oldImgSrc.replace(" ", "%20");
		if (oldImgSrc.match("/" + imgSrc+"$")=="/" + imgSrc) {
			return;
		}
	}
	
	if (useTransition) {
		fade(idImg,true,timeToFade);
		setTimeout(function(){displayPictureFadeIn(idImg,idTitle,idDesc,idDate,imgSrc,title,description,date,useTransition,timeToFade)}, timeToFade);
	} else {
		displayPictureFadeIn(idImg,idTitle,idDesc,idDate,imgSrc,title,description,date,useTransition,timeToFade);
	}
}

function displayPictureFadeIn(idImg,idTitle,idDesc,idDate,imgSrc,title,description,date,useTransition,timeToFade)
{
	if (null != document.getElementById(idImg)) {
		if (useTransition) {
			document.getElementById(idImg).style.opacity = 0;
		}
		
		document.getElementById(idImg).src = imgSrc;
		var objDiv = document.getElementById(idImg+'Panel');
		if (null != objDiv) {
			objDiv.scrollLeft = 0;
		}
		
		/*if (displayTooltip == 1) {
			document.getElementById(idImg).alt = title == "" ? imgSrc : title;
			document.getElementById(idImg).title = title == "" ? imgSrc : title;
		} else {
			document.getElementById(idImg).alt = "";
			document.getElementById(idImg).title = "";
		}*/
	}
	
	if (null != document.getElementById(idTitle)) {
		title = title.replace(/&lsquo;/g,"'");
		title = title.replace(/&quot;/g,"\"");
		document.getElementById(idTitle).innerHTML = title;
	}
	
	if (null != document.getElementById(idDesc)) {
		description = description.replace(/&lsquo;/g,"'");
		description = description.replace(/&quot;/g,"\"");
		document.getElementById(idDesc).innerHTML = description;
	}
	
	if (null != document.getElementById(idDate)) {
		date = date.replace(/&lsquo;/g,"'");
		date = date.replace(/&quot;/g,"\"");
		document.getElementById(idDate).innerHTML = date;
	}
	
	if (useTransition) {
		setTimeout(function(){fade(idImg,false,timeToFade)},timeToFade);
	}
}

function showThumbnail(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,currentIndex,selectedIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var index = 0;
	var newSelectedIndex = selectedIndex;
	if (newSelectedIndex + (nbThumbs-1) >= fileThumbs.length) {
		newSelectedIndex = fileThumbs.length - nbThumbs;
	}
	
	var newCurrentIndex = currentIndex;
	if (newCurrentIndex + (nbThumbs-1) < selectedIndex) {
		newCurrentIndex = selectedIndex - (nbThumbs-1);
		if (newCurrentIndex < 0) {
			newCurrentIndex = 0
		}
	} else if (newCurrentIndex > selectedIndex) {
		newCurrentIndex = selectedIndex;
	}
	
	displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,newCurrentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale);
	selectThumbnail(fileThumbs,nbThumbs,newCurrentIndex,selectedIndex,configId,useGrayscale);
	disableThumbNavigation(newCurrentIndex,newCurrentIndex+nbThumbs-1,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return newCurrentIndex;
}

function showNextThumbnail(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,currentIndex,selectedIndex,nbMove,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var index = 0;
	
	if (currentIndex + (nbThumbs-1) + nbMove >= fileThumbs.length) {
		nbMove = fileThumbs.length - (currentIndex + (nbThumbs));
	}
	var newCurrentIndex = currentIndex+nbMove;
	if (newCurrentIndex >= fileThumbs.length) {
		newCurrentIndex = fileThumbs.length-1;
	}
	
	displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,newCurrentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale);
	selectThumbnail(fileThumbs,nbThumbs,newCurrentIndex,selectedIndex,configId,useGrayscale);
	disableThumbNavigation(newCurrentIndex,newCurrentIndex+nbThumbs-1,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return newCurrentIndex;
}

function showPreviousThumbnail(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,currentIndex,selectedIndex,nbMove,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var index = 0;
	if (currentIndex - nbMove < 0) {
		nbMove = currentIndex;
	}
	var newCurrentIndex = currentIndex-nbMove;
	if (newCurrentIndex < 0) {
		newCurrentIndex = 0;
	}
	
	displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,newCurrentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale);
	selectThumbnail(fileThumbs,nbThumbs,newCurrentIndex,selectedIndex,configId,useGrayscale);
	disableThumbNavigation(newCurrentIndex,newCurrentIndex+nbThumbs-1,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return newCurrentIndex;
}

function showFirstThumbnail(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,selectedIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var currentIndex = 0
	var index = 0;
	
	displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,currentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale);	
	selectThumbnail(fileThumbs,nbThumbs,currentIndex,selectedIndex,configId,useGrayscale);
	disableThumbNavigation(currentIndex,currentIndex+nbThumbs-1,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return 0;
}

function showLastThumbnail(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,selectedIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var currentIndex = 0
	var index = 0;
	if (fileThumbs.length-nbThumbs > 0) {
		currentIndex = fileThumbs.length-nbThumbs;
	}
	
	displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,currentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale);
	selectThumbnail(fileThumbs,nbThumbs,currentIndex,selectedIndex,configId,useGrayscale);
	disableThumbNavigation(currentIndex,currentIndex+nbThumbs-1,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return currentIndex;
}

function displayThumbnails(idImg,idTitle,idDesc,idDate,files,fileTitles,fileDescs,fileDates,fileThumbs,nbThumbs,currentIndex,useTransition,configId,timeToFade,disabledNavigationOpacity,useGrayscale)
{
	var index = 0;
	
	for (var i=0; i<nbThumbs && currentIndex+i<fileThumbs.length; i++) {
		var str = "thumb" + configId + i;
		
		// If the thumbnail exists.
		if (null != document.getElementById(str)) {
			index = currentIndex+i;
			
			/*if (displayTooltip == 1) {
				document.getElementById(str).alt = fileTitles[index] == "" ? files[index] : fileTitles[index];
				document.getElementById(str).title = fileTitles[index] == "" ? files[index] : fileTitles[index];
			} else {
				document.getElementById(str).alt = "";
				document.getElementById(str).title = "";
			}*/
			
			document.getElementById(str).src = fileThumbs[index];
			document.getElementById(str).onclick = new Function("selectedIndex" + configId + " = " + index + "; displayPictureFadeOut('" + idImg + "','" + idTitle + "','" + idDesc + "','" + idDate + "','" + files[index] + "','" + fileTitles[index] + "','" + fileDescs[index] + "','" + fileDates[index] + "'," + useTransition + "," + timeToFade + "); selectThumbnail('" + fileThumbs + "'," + nbThumbs + "," + currentIndex + "," + index + ",'" + configId + "'," + useGrayscale + "); disableBigNavigation(selectedIndex" + configId + "," + fileThumbs.length + ",'" + configId + "'," + disabledNavigationOpacity + ");");
		}
	}
}

function showPreviousPicture(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,selectedIndex,nbMove,useTransition,configId,timeToFade,disabledNavigationOpacity)
{
	if (selectedIndex-nbMove < fileThumbs.length && selectedIndex-nbMove >= 0) {
		selectedIndex = selectedIndex-nbMove;
		if (fileThumbs.length > 0) {
			displayPictureFadeOut(idImg,idTitle,idDesc,idDate,files[selectedIndex],fileTitles[selectedIndex],fileDescs[selectedIndex],fileDates[selectedIndex],useTransition,timeToFade);
		}
	}
	disableBigNavigation(selectedIndex,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return selectedIndex;
}

function showNextPicture(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,selectedIndex,nbMove,useTransition,configId,timeToFade,disabledNavigationOpacity)
{
	if (selectedIndex+nbMove < fileThumbs.length && selectedIndex+nbMove >= 0) {
		selectedIndex = selectedIndex+nbMove;
		
		if (fileThumbs.length > 0) {
			displayPictureFadeOut(idImg,idTitle,idDesc,idDate,files[selectedIndex],fileTitles[selectedIndex],fileDescs[selectedIndex],fileDates[selectedIndex],useTransition,timeToFade);
		}
	}
	
	disableBigNavigation(selectedIndex,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return selectedIndex;
}

function showFirstPicture(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,useTransition,configId,timeToFade,disabledNavigationOpacity)
{
	var selectedIndex = 0;
	if (fileThumbs.length > 0) {
		displayPictureFadeOut(idImg,idTitle,idDesc,idDate,files[selectedIndex],fileTitles[selectedIndex],fileDescs[selectedIndex],fileDates[selectedIndex],useTransition,timeToFade);
	}
	
	disableBigNavigation(selectedIndex,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return selectedIndex;
}

function showLastPicture(fileThumbs,files,fileTitles,fileDescs,fileDates,idImg,idTitle,idDesc,idDate,nbThumbs,useTransition,configId,timeToFade,disabledNavigationOpacity)
{	
	var selectedIndex = 0;
	if (fileThumbs.length > 0) {
		selectedIndex = fileThumbs.length-1;
		displayPictureFadeOut(idImg,idTitle,idDesc,idDate,files[selectedIndex],fileTitles[selectedIndex],fileDescs[selectedIndex],fileDates[selectedIndex],useTransition,timeToFade);
	}
	
	disableBigNavigation(selectedIndex,fileThumbs.length,configId,disabledNavigationOpacity);
	
	return selectedIndex;
}

function disableThumbNavigation(firstIndex,lastIndex,length,configId,disabledNavigationOpacity) 
{
	var opacity = '1';
	var isEnabled = true;
	if (firstIndex == 0) {
		opacity = disabledNavigationOpacity;
		isEnabled = false;
	}
	
	// TODO : Pass the IDs.
	if (null != document.getElementById('previous' + configId)) {
		document.getElementById('previous' + configId).style.opacity = opacity;
		//document.getElementById('previous' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('previous' + configId).onmouseover = getOnMouseOver(isEnabled,'previousImgPath',configId);
	}
	if (null != document.getElementById('first' + configId)) {
		document.getElementById('first' + configId).style.opacity = opacity;
		//document.getElementById('first' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('first' + configId).onmouseover = getOnMouseOver(isEnabled,'firstImgPath',configId);
	}
	
	opacity = '1';
	isEnabled = true;
	if (lastIndex >= length-1) {
		opacity = disabledNavigationOpacity;
		isEnabled = false;
	}
	
	if (null != document.getElementById('next' + configId)) {
		document.getElementById('next' + configId).style.opacity = opacity;
		//document.getElementById('next' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('next' + configId).onmouseover = getOnMouseOver(isEnabled,'nextImgPath',configId);
	}
	if (null != document.getElementById('last' + configId)) {
		document.getElementById('last' + configId).style.opacity = opacity;
		//document.getElementById('last' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('last' + configId).onmouseover = getOnMouseOver(isEnabled,'lastImgPath',configId);
	}
}

function disableBigNavigation(selectedIndex,length,configId,disabledNavigationOpacity) 
{	
	var opacity = '1';
	var isEnabled = true;
	if (selectedIndex == 0) {
		opacity = disabledNavigationOpacity;
		isEnabled = false;
	}
	
	// TODO : Pass the IDs.
	if (null != document.getElementById('bigPrevious' + configId)) {
		document.getElementById('bigPrevious' + configId).style.opacity = opacity;
		//document.getElementById('bigPrevious' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('bigPrevious' + configId).onmouseover = getOnMouseOver(isEnabled,'bigPreviousImgPath',configId);
	}
	if (null != document.getElementById('bigFirst' + configId)) {
		document.getElementById('bigFirst' + configId).style.opacity = opacity;
		//document.getElementById('bigFirst' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('bigFirst' + configId).onmouseover = getOnMouseOver(isEnabled,'bigFirstImgPath',configId);
	}
	
	opacity = '1';
	isEnabled = true;
	if (selectedIndex >= length-1) {
		opacity = disabledNavigationOpacity;
		isEnabled = false;
	}
	
	if (null != document.getElementById('bigNext' + configId)) {
		document.getElementById('bigNext' + configId).style.opacity = opacity;
		//document.getElementById('bigNext' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('bigNext' + configId).onmouseover = getOnMouseOver(isEnabled,'bigNextImgPath',configId);
	}
	if (null != document.getElementById('bigLast' + configId)) {
		document.getElementById('bigLast' + configId).style.opacity = opacity;
		//document.getElementById('bigLast' + configId).style.filter = 'alpha(opacity=' + (opacity*100) + ')';
		document.getElementById('bigLast' + configId).onmouseover = getOnMouseOver(isEnabled,'bigLastImgPath',configId);
	}
}

function getOnMouseOver(isEnabled,varPrefix,configId) {
	return function () { 
		if (isEnabled == true) {
			this.style.cursor='pointer';
			this.src=window[varPrefix + configId] + window['imgPathHoverSuffix' + configId];
		} else {
			this.style.cursor='default';
			this.src=window[varPrefix + configId];
		}
    };
}

function selectThumbnail(fileThumbs,nbThumbs,currentIndex,selectedIndex,configId,useGrayscale)
{
	var index = 0;
	for (var i=0; i<nbThumbs && currentIndex+i<fileThumbs.length; i++) {
		var str = "thumb" + configId + i;
		
		// If the thumbnail exists.
		if (null != document.getElementById(str)) {
			index = currentIndex+i;
			
			if (index == selectedIndex) {
				document.getElementById(str).className = "diaporama_selectedThumbnail";
				if (useGrayscale == 1) {
					document.getElementById(str).src = removeGrayscale(document.getElementById(str).src);
				}
			} else {
				document.getElementById(str).className = "diaporama_thumbnail";
				if (useGrayscale == 1) {
					document.getElementById(str).src = addGrayscale(document.getElementById(str).src);
				}
			}
		}
	}
}

function preloader(imagesURL) 
{
	var temp = new Array(imagesURL.length);
	
	if (document.images)
	{
		var i = 0;
		for(i=0; i<imagesURL.length; i++) {
			var preload_image_object = new Image();
			preload_image_object.src = imagesURL[i];
			temp[i] = preload_image_object; 
		}
	}
	
	return temp;
}

function removeGrayscale(imagesURL) 
{
	return imagesURL.replace('__grayscale__', '');
}

function addGrayscale(imagesURL) 
{
	var index = imagesURL.lastIndexOf('/');
	var filename = imagesURL.substring(index+1);
	var path = imagesURL.substring(0,index+1)
	return path + '__grayscale__' + filename.replace('__grayscale__', '');
}

function fade(eid,fadeOut,timeToFade)
{
	var element = document.getElementById(eid);
	if (null == element) {
		return;
	}

	if(null == element.FadeState) {
		// First time
		element.FadeState = fadeOut ? -2 : 2;
	} else {
		// Not the first time
		element.FadeState = fadeOut ? -1 : 1;
	}

	if (element.FadeState == 1 || element.FadeState == -1) {
		element.FadeTimeLeft = timeToFade - element.FadeTimeLeft;
	} else {
		element.FadeState = element.FadeState == 2 ? 1 : -1;
		element.FadeTimeLeft = timeToFade;
		setTimeout(function(){animateFade(new Date().getTime(),eid,timeToFade)}, 33);
	}  
}

function animateFade(lastTick,eid,timeToFade)
{  
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var element = document.getElementById(eid);

	if(element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = ' 
		+ (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = null;
		
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/timeToFade;
	if(element.FadeState == 1) {
		newOpVal = 1 - newOpVal;
	}

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout(function(){animateFade(curTick,eid,timeToFade)}, 33);
}

