var speed = .75;				//speed of transition in seconds
var inter = 2;				//inerval between transitions in seconds
var fps = 12;				//sudo frames per second, mainly how many intervals you want per second
var slideNum = 1;			//slide number
var slideAmount = 3;		//Total number of slides
var idPre = "r";			//Prefix of the id of the div you want to transtion, everything before the incremented number
var idSuff = "";			//Suffix of the id of the div you want to trasition, everything after the incremented number
var minOpac = 0;			// minimum opacity on a 0-1 scale, defaults to 0
var maxOpac = 1;			// maximum opacity on a 0-1 scale, defaults to 1
var transInterval;			// the setInterval

//check and make sure min and max opacity are valid numbers, if not fix. focus on max opacity

//min
//check that it is 0-1
var opacOK = false;

while(opacOK == false)
{	
	if(minOpac <= 1 && minOpac >= 0)
	{
		//max
		//check that it is 0-1
		if(maxOpac <= 1 && maxOpac >= 0)
		{
			//chack that max >= min
			if(maxOpac >= minOpac)
			{
				opacOK = true;
			}else{
				minOpac = maxOpac;
				opacOK = true;
			}
		}else{
			maxOpac = 1;
		}
	}else{
		minOpac = 0;
	}
}

//change opacity of an element
//  id is the id of the element
//  opac is the opacty on a 0-1 scale
function changeOpac(id, opac){
	var item = document.getElementById(id);
	
	item.style.opacity = opac;

	var opacAsInt = opac * 100;
	item.style.filter = "alpha(opacity = "+opacAsInt+")";
}


// function used to start transition
function goTrans(){
	//call external hook function
	if(typeof hook_goTrans == 'function') {
		hook_goTrans();
	}
	
	//increment slideNum
	slideNum++;
	
	var cSlide = ((slideNum-1)%slideAmount)+1;
	var pSlide = ((slideNum-2)%slideAmount)+1;
		
	var opacRange = maxOpac - minOpac;
	var opacInterval = opacRange/(fps*speed);
	
	//setup rotation
		//fadeout pSlide
		//fadein cSlide
	for(var i = 0; i<(fps*speed);i++)
	{
		setTimeout('changeOpac("'+idPre+pSlide+idSuff+'", '+(maxOpac-opacInterval*(i+1))+')', speed/(fps*speed)*(i+1)*1000);
		setTimeout('changeOpac("'+idPre+cSlide+idSuff+'", '+(minOpac+opacInterval*(i+1))+')', speed/(fps*speed)*(i+1)*1000);
		
		if(((fps*speed) - i) < 1) {
			console.log('go');
		}
	}
}

function go() {
	//initialize elements
	for(var i=1; i<=slideAmount; i++)
	{
		if(i==slideNum)
		{
			changeOpac(idPre+i+idSuff, maxOpac);
		}else{
			changeOpac(idPre+i+idSuff, minOpac);
		}
		document.getElementById(idPre+i+idSuff).style.display = "block";
	}
	
	//set timeout to start transition
	transInterval = setInterval('goTrans()', (inter*1+speed*1)*1000);

}

function stop() {
	clearInterval(transInterval);
}

