// JavaScript Document

//var imgNameArray = new Array("00", "01", "02", "03", "04","05", "06", "07","08","09","10","12","13","14","15","16","17","18","19","20","21");
var timeDelay = 15000;
var transitionDelay = 1700;

//Get number of images
var imgNameLength = imgNameArray.length;

function preLoad (nxt,prv){
	//Preload next and previous images
	nextImageObject = new Image();
	prevImageObject = new Image();	
	nextImageObject.src = 'slideshow/'+imgNameArray[nxt]+'.jpg'; 
	prevImageObject.src = 'slideshow/'+imgNameArray[prv]+'.jpg'; 
	//alert(nxt+', '+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('hp_photo_block', 100, 0, transitionDelay);
}

//Load the next or previous photo
function loadImage(whereTo){
	if(whereTo == "prev"){ var goToImg = prevImg; } else { var goToImg = nextImg; }
	var imgDiv = document.getElementById('hp_photo_block');
	var imgContainer = document.getElementById('');
	imgDiv.style.display = "none";
	imgDiv.innerHTML = '<img src="slideshow/'+imgNameArray[goToImg]+'.jpg" />';
	opacity('hp_photo_block', 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; 

    //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";
}