// KMS gallery javascript functions
function openSlide(slideNumber, autoStart, mmId){
	bAutoStart = autoStart;
	
	if(bAutoStart == true){
		startOpacity = 0.0001;
	}
	else{
		startOpacity = 1;
	}
	
	if (bOpen != true){	
		bOpen = true; //set status as open
		fadePage(); //fade page before anything else
		playButton = document.getElementById('play'); //assign playbutton object
		photo = document.getElementById('photo'); //assign photo object
		
		//determine available opacity mechanism
		if (opacityType == ''){
			if (photo.style.opacity != undefined){
				opacityType = 'opacity';
			}
			else if (typeof(photo.filters.alpha.opacity) == 'number'){
				opacityType = 'filter';
			}
			else if (photo.style.MozOpacity != undefined){
				opacityType = 'mozOpacity';
			}
			else{
				opacityType = 'none';
			}
		}
	}
	
	//set variables used for slideshow
	timerId = 0;
	intervalId = 0;
	currentIndex = slideNumber;
	
	//assign startAt picture
	photo.src = imagesSlideShow[currentIndex].src;
	
	//log slide
	logSlide(currentIndex);
		
	//if autostart then startslideshow
	if(bAutoStart == true){
		startSlideShow();
	}
	else{
		pauseSlideShow();
	}
	
	//finally show slideshow placeholder
	if (document.getElementById('container_photo').style.display != 'block'){
		document.getElementById('container_photo').style.display = 'block';
	}
}

function nextSlide(){
	if(imagesSlideShow.length > 1){
		pauseSlideShow();
		if (currentIndex + 1 < imagesSlideShow.length){
			openSlide(currentIndex + 1, false);
		}
		else{
			openSlide(0, false);
		}
	}
}
            
function previousSlide(){
	if(imagesSlideShow.length > 1){
		pauseSlideShow();
		if (currentIndex > 0){
			openSlide(currentIndex - 1, false);
		}
		else{
			openSlide(imagesSlideShow.length - 1, false);
		}
	}
}

function startSlideShow(){
	if(imagesSlideShow.length > 1){
		playButton.src = imgPause;
		playButton.alt = imgPauseAlt;
		playButton.onclick = pauseSlideShow;
		
		if(bAutoStart == true){
			fadeOut = false;
		}
		else{
			fadeOut = true;
		}
		
		if (opacityType != 'none'){
			startSlideTransition();
		}
		else{
			timerId = window.setTimeout(startSlideTransition,2000)
		}
	}
}

function pauseSlideShow(){
	playButton.src = imgPlay;
	playButton.alt = imgPlayAlt;
	playButton.onclick = startSlideShow;
	
	//reset autostart variables after automatic start
	if (bAutoStart == true){
		startOpacity = 1;
		bAutoStart = false;
	}
	
	clearInterval(intervalId);
	clearTimeout(timerId);
	
	//Reset filter
	if (opacityType != 'none'){
		switch(opacityType)
		{
			case 'filter':
				photo.style.filter = '';
				break;
			case 'mozOpacity' : 
				photo.style.MozOpacity = 1;
				break;
			default : 
				photo.style.opacity = 1;
		}
	}
}

function startSlideTransition()  {
	//if called from timeout then clear timeout
	if(timerId > 0){
		window.clearTimeout(timerId);
	}
	
	//if opacitymechanism available then set opacity and fade image, else swap image
	if(opacityType != 'none')
	{
		switch(opacityType)
		{
			case 'filter':
				photo.style.filter = 'alpha(opacity=0)';
				photo.filters.alpha.opacity = startOpacity * 100;
				break;
			case 'mozOpacity' : 
				photo.style.MozOpacity = startOpacity;
				break;
			default : 
				photo.style.opacity = startOpacity;
		}
		intervalId = setInterval('fadeSlide()', (fadeTime/2) / (((fadeTime/1000)/2)* 20)); // number of seconds * 1000ms / ((number of seconds/2) * 20 steps) * 1000ms
	}
	else
	{
		if (currentIndex + 1 < imagesSlideShow.length){
			currentIndex = currentIndex + 1;
		}
		else{
			currentIndex = 0;
		}
		
		//assign picture
		photo.src = imagesSlideShow[currentIndex].src;
		
		//log slide
		logSlide(currentIndex);
		
		//set timout for next transition
		timerId = window.setTimeout(startSlideTransition, 3000);
	}
}

function fadeSlide(){
	var step = 1/(((fadeTime/1000)/2)* 20); //number of seconds/2 * 20 steps
	var currentOpacity;
	
	if (fadeOut == false){ //step needs to be negative
		step = 0 - step;
	}
	
	switch(opacityType)
	{
		case 'filter' :                  
			photo.filters.alpha.opacity = photo.filters.alpha.opacity - (step * 100);
			currentOpacity = photo.filters.alpha.opacity / 100;
			break;
		case 'mozOpacity' : 
			isf.obj.style.MozOpacity = photo.style.MozOpacity - step;
			currentOpacity = isf.obj.style.MozOpacity;
			break;
		default : 
			photo.style.opacity = photo.style.opacity - step;
			currentOpacity = photo.style.opacity;
	}
	
	if (currentOpacity < (2 * step) && bAutoStart == false){
		//stop fading
		clearInterval(intervalId);
		
		//switch image
		if (currentIndex + 1 < imagesSlideShow.length){
			currentIndex = currentIndex + 1;
		}
		else{
			currentIndex = 0;
		}
		photo.src = imagesSlideShow[currentIndex].src;
		
		//log slide
		logSlide(currentIndex);
		
		//reverse slide order
		fadeOut = false;
		
		//fade in new image
		intervalId = setInterval('fadeSlide()', 50);
	}
	
	//if fading in at end of sequence then
	if (currentOpacity > 0.99 && fadeOut == false){
		//remove filter due to IE bug!
		switch(opacityType)
		{
			case 'filter':
				photo.style.filter = '';
				break;
			case 'mozOpacity' : 
				photo.style.MozOpacity = 1;
				break;
			default : 
				photo.style.opacity = 1;
		}
		
		//stop fading
		clearInterval(intervalId);
		
		//reset autostart variables after automatic start
		if (bAutoStart == true){
			startOpacity = 1;
			bAutoStart = false;
		}
		
		//reset fading direction
		fadeOut = true;
		
		//call for next image
		timerId = window.setTimeout(startSlideTransition, 2000);
	}
}

function logSlide(nIndex){
	logMultimedia(document.getElementById('photo' + nIndex).mmId);
}


function closeWindow(){
	pauseSlideShow();
	photo.src = '';
	document.getElementById('container_photo').style.display = 'none';
	bOpen = false;
	fadePage();
}

function fadePage(){
	var fadeDIV = document.getElementById('photo_fade');
	
	if (bOpen == true){   
		fadeDIV.style.display = 'block';
		document.body.style.backgroundImage = 'url(Images/background_alpha.gif)';
	}
	else{
		fadeDIV.style.display = 'none';
		document.body.style.backgroundImage = 'url(Images/background.gif)';
	}
}
