//DONT CHANGE ANY JAVASCRIPT BELOW THIS LINE!
//----------------------------------------------------

images = 'assets/images/';
current_image = 1; //start at the first image
total_images = (sh.length-1);
next_img = 'photo1';
fo_locked = 0;	//used for locking
fi_locked = 0;	//used for locking
auto_resume = auto_resume - auto_time_per_image;
auto = false;
restart_show = false;
transition_time = transition_time * 1000;



window.onload = function() {
	
	// The page is setup for IE, so we need to fix the photo
	// slideshow to play nice in every other browser by adjusting
	//the position of the images being displayed
	browserName=navigator.appName; 
	if (browserName != "Microsoft Internet Explorer") {
		document.getElementById('photo1').style.top = '50px';
		document.getElementById('photo2').style.top = '50px';
	}
	loadPhoto();
	startAutomatedShow();
}

function startAutomatedShow() {
	auto = setInterval("nextImage()",(transition_time*auto_time_per_image));
}
function fi_unlock() {
	fi_locked = 0;
}
function fo_unlock() {
	fo_locked = 0;
}
function fadeIn(id, millisec) {
	fi_locked = 1;
	var speed = Math.round(millisec / 100);
	var timer = 1;
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
		timer++;
	}
	setTimeout("fi_unlock()", millisec);
}

function fadeOut(id, millisec) {
	fo_locked = 1;
	var speed = Math.round(millisec / 100);
	var timer = 1;
	for(i = 100; i >= 0; i--) {
		setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
		timer++;
	}
	setTimeout("fo_unlock()", millisec);
}

function loadPhoto() {
	setDivBG(sh[current_image],next_img);
	fadeIn(next_img,transition_time);
	updateNextImage();
	getNextImage();
	setDivBG(sh[current_image],next_img);
	
}
function preloadNextImage() {
	getNextImage();
	setDivBG(sh[current_image],next_img);
}
function preloadPrevImage() {
	getPrevImage();
	setDivBG(sh[current_image],next_img);
}
function setDivBG(image, id){
	document.getElementById(id).style.backgroundImage = "url(" + images +  image + ")";
}

function getNextImage() {
	current_image = current_image + 1;
	if(current_image > total_images) {
		current_image = 0;
	}
}

function getPrevImage() {
	current_image = current_image - 1;
	if(current_image < 0) {
		current_image = total_images;
	}
}

function updateNextImage() {
	//alert(next_img.substring(0,5));
	if (next_img.substring(5) == 1) {
		next = '2';
	}
	else {
		next = '1';
	}
	next_img = next_img.substring(0,5) + next;
}

function nextImage() {
	if(fo_locked == 0 && fi_locked == 0) {
		fadeIn(next_img,transition_time);
		updateNextImage();
		fadeOut(next_img,transition_time);
		setTimeout("preloadNextImage()",transition_time);
	}
}
function prevImage() {
	if(fo_locked == 0 && fi_locked == 0) {
		fadeIn(next_img,transition_time);
		updateNextImage();
		fadeOut(next_img,transition_time);
		setTimeout("preloadPrevImage()",transition_time);
	}
}
function pauseImage() {
	clearInterval(auto);
}
function clickNextImage() {
	pauseImage();
	nextImage();
	clearInterval(restart_show);
	restart_show = setInterval("startAutomatedShow()",(transition_time*auto_resume));
}
function clickPrevImage() {
	pauseImage();
	prevImage();
	clearInterval(restart_show);
	restart_show = setInterval("startAutomatedShow()",(transition_time*auto_resume));
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.display = "block";
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
function imageSwapOn(element,src) {
	element.src = images+src;
}