// JavaScript Document

// JavaScript Document

var timeDelay = 6500;
var transitionDelay = 600;

//Get number of images
var imgNameLength = imgNameArray.length;

function preLoad (nxt,prv){
	//Preload next and previous images
	nextImageObject = new Image();
	prevImageObject = new Image();	
	//Get path from computed div
	nextImageObject.src = 'images/'+imgNameArray[nxt];
	prevImageObject.src = 'images/'+imgNameArray[prv]; 
}

//Calculate the next image in the order
if(currentImg == (imgNameLength-1)){
	var nextImg = 0;
} else {
	var nextImg = currentImg + 1;
}

//Calculate the previous image in the order
if(currentImg == 0){
	var prevImg = imgNameLength-1;
} else {
	var prevImg = currentImg-1;
}

preLoad(nextImg, prevImg);

nextInterval = setInterval("nextPhoto(0)", timeDelay); 

//Events for moving to the next photo
function nextPhoto(button){
	//Dim the existing image
	dimPrevious();
	//If the next button has been clicked, stop automatically rotating images
	if(button == 1){ clearInterval(nextInterval); }
	//Wait for the existing image to dim, then load the next image
	setTimeout("loadImage('next')", transitionDelay);
}

//Events for moving to the previous photo
function prevPhoto(button){
	//Dim the existing image
	dimPrevious();
	//If the next button has been clicked, stop automatically rotating images
	if(button == 1){ clearInterval(nextInterval); }
	//Wait for the existing image to dim, then load the next image
	setTimeout("loadImage('prev')", transitionDelay);
}

//Dim out the current photo before loading the next one
function dimPrevious(){
	opacity('bg_photo', 100, 0, transitionDelay);
}

//Load the next or previous photo
function loadImage(whereTo){
	if(whereTo == "prev"){ 
		var goToImg = prevImg; 
	} else { 
		var goToImg = nextImg; 
	}

	//Get the alternate text and caption html (hidden divs)
	//imageAlt = document.getElementById('alt_'+imgUnidArray[goToImg]);
	//imageCap = document.getElementById('cap_'+imgUnidArray[goToImg]);
	
	//Get the html content from the hidden divs
	//imageAlt = imageAlt.innerHTML;
	//imageCap = imageCap.innerHTML;

	//var capDiv = document.getElementById('photo_caption');
	var imgDiv = document.getElementById('bg_photo');
	imgDiv.style.display = "none";
	imgDiv.innerHTML = '<img src="images/'+imgNameArray[goToImg]+'" alt="MAOL Student" title="MAOL Student"/>';
	//capDiv.innerHTML = imageCap;
	opacity('bg_photo', 0, 100, transitionDelay);
	
	//Redefine current photo
	currentImg = goToImg;

	//Calculate the next image in the order
	if(currentImg == (imgNameLength-1)){
		nextImg = 0;
	} else {
		nextImg = currentImg+1;
	}

	//Calculate the previous image in the order
	if(currentImg == 0){
		prevImg = imgNameLength-1;
	} else {
		prevImg = currentImg-1;
	}
	//Preload the new next and previous images
	preLoad(nextImg, prevImg);
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
	//alert(id);

    //determine the direction for the blending, if start and end are the same nothing happens 
	//More transparent
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
		if(i = opacEnd){
			//alert('faded out');
			return true;
		}
	//More opaque	
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
		if(i >= opacEnd){
			//alert('faded in');
		}		
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")";
	object.display = "block";
	//document.getElementById('photo_caption').innerHTML = opacity;	
}