/* PHOTO GALLERY JAVASCRIPT LIBRARY */

window.onload = function(){
	// if we're showing a gallery, let's show the current photo!
	if(document.getElementById('photoLoader')){
		var photoLoader = document.getElementById('photoLoader');
		photoLoader.style.width = photos[currentPhoto]['width'];
		photoLoader.style.height = photos[currentPhoto]['height'];
		photoLoader.style.visibility = "visible";
		var photo = document.getElementById('photo');
		photo.style.backgroundImage = "url(/photo_galleries/"+gallery+"/"+photos[currentPhoto]['filename']+")";

		
		// activate buttons
		if(currentPhoto > 0){ previousOn(); }
		if(currentPhoto < photos.length){ nextOn(); }
	}
	init();
}

// turn on previous button
function previousOn(){
	var prev = document.getElementById("previous");
	prev.className = "buttonOn";
	prev.onclick = function(){
		previousPhoto();
	}
}

// turn off previous button
function previousOff(){
	var prev = document.getElementById("previous");
	prev.className = "";
	prev.onclick = function(){}
}

// turn on next button 
function nextOn(){
	var next = document.getElementById("next");
	next.className = "buttonOn";
	next.onclick = function(){
		nextPhoto();
	}
}

// turn off next button
function nextOff(){
	var next = document.getElementById("next");
	next.className = "";
	next.onclick = function(){	}
}


// step backwards through photo array
function previousPhoto(){
	var prev = document.getElementById("previous");
	prev.blur();
	if(currentPhoto > 0){
		currentPhoto--;
		if(document.getElementById('photoLoader')){
			var photoLoader = document.getElementById('photoLoader');
			photoLoader.style.visibility = "hidden";
			photoLoader.style.width = photos[currentPhoto]['width'];
			photoLoader.style.height = photos[currentPhoto]['height'];
			photoLoader.style.visibility = "visible";
			var photo = document.getElementById('photo');
			photo.style.backgroundImage = "url(/photo_galleries/"+gallery+"/"+photos[currentPhoto]['filename']+")";
		}
		if(currentPhoto==0){
			previousOff();
		} else if (currentPhoto < photos.length){
			nextOn();
		}
	}
}

// step forwards through photo array
function nextPhoto(){
	var next = document.getElementById("next");
	next.blur();
	if(currentPhoto < photos.length){
		currentPhoto++;
		if(document.getElementById('photoLoader')){
			var photoLoader = document.getElementById('photoLoader');
			photoLoader.style.visibility = "hidden";
			photoLoader.style.width = photos[currentPhoto]['width'];
			photoLoader.style.height = photos[currentPhoto]['height'];
			photoLoader.style.visibility = "visible";
			var photo = document.getElementById('photo');
			photo.style.backgroundImage = "url(/photo_galleries/"+gallery+"/"+photos[currentPhoto]['filename']+")";
		}
		if(currentPhoto+1 == photos.length){
			nextOff();
		} else if (currentPhoto > 0){
			previousOn();
		}
	}
}