/*
   MIS - Minimalist Image Slideshow
   Version 3
   January 16, 2011

   ---

   Marc Robledo
   http://usuaris.tinet.cat/mark/mis
*/

var WAIT=8; // SET THE PAUSE BETWEEN IMAGES IN SECONDS

var i=0;
var nexti=0;
var iMax=0;
var timer=0;
var changing=false;

function prepareNextImg(){
	clearTimeout(timer);

	document.getElementById('MIS-image'+i).style.zIndex=500;
	document.getElementById('MIS-image'+nexti).style.opacity=0;
	document.getElementById('MIS-image'+nexti).style.zIndex=501;

	var alpha=0;
	timer=window.setInterval(function(){
		alpha+=10;
		document.getElementById('MIS-image'+nexti).style.opacity=alpha/100;
		if(alpha==100){
			document.getElementById('MIS-image'+i).style.opacity=0;
			i=nexti;
			changing=false;
			clearInterval(timer);
			resetTimer();
		}
	}, 40);
}

function resetTimer(){
	timer=window.setTimeout(nextImage, WAIT*1000);
}

function nextImage(){
	if(!changing){
		changing=true;
		nexti++;
		if(nexti==iMax)
			nexti=0;
	
		prepareNextImg();
	}
}

function prevImage(){
	if(!changing){
		changing=true;
		nexti--;
		if(nexti==-1)
			nexti=iMax-1;
	
		prepareNextImg();
	}
}

//Startup
function init(){
	//Get maximum number of images
	while(document.getElementById('MIS-image'+iMax)){
		iMax++;
	}

	//Set first image as opaque and the rest as transparent
	document.getElementById('MIS-image0').style.zIndex=501;
	for(i=1; i<iMax; i++){
		document.getElementById('MIS-image'+i).style.opacity=0;
		document.getElementById('MIS-image'+i).style.zIndex=500;
	}
	i=0;

	resetTimer();
}
window.onload=init;
