var overlay = null;
var overlay_half = null;
var actualGallery = 0;
var actualPhoto = 0;
var galleryContentClass = "";
var overlayContentClass = "gallery_item";
var showGalleryNavigation = true;

function galleryInit() {
	setOverlayDimensions();

	return true;
}

function setOverlayDimensions() {
	if(document.getElementById("overlay")) {
		overlay = document.getElementById("overlay");

		overlay.style.height = (document.documentElement.scrollHeight + 80) + "px";
		overlay.style.width = (document.documentElement.scrollWidth) + "px";
		overlay.style.display = "none";
	}

	if(document.getElementById("overlay_half")) {
		overlay_half = document.getElementById("overlay_half");

		overlay_half.style.height = (document.documentElement.scrollHeight + 80) + "px";
		overlay_half.style.width = (document.documentElement.scrollWidth) + "px";
		overlay_half.style.display = "none";
	}

	return true;
}

function setGalleryContentClass(className) {
	galleryContentClass = className;
}

function openPhoto(id) {

	actualGallery = id;
	actualPhoto = 0;
	galleryContentClass = "gallery_" + id;

	overlay.style.display = "block";
	overlay_half.style.display = "block";

	var items = getElementsByClassName(document, "*", overlayContentClass);

	for(i = 0; i < items.length; i++) {
		items[i].style.display = "none";
	}

	var item = document.getElementById("gallery_item_" + id);

	item.style.display = "block";
	if(document.documentElement.scrollTop < 50) {
		item.style.marginTop = "50px";
	} else {
		item.style.marginTop = (document.documentElement.scrollTop + 10) + "px";
	}

	var images = item.getElementsByTagName("img");
	var maxWidth = images[0].width;

	for(i = 1; i < images.length; i++) {
		if(images[i].width > maxWidth) {
			maxWidth = images[i].width;
		}
	}
	item.style.width = maxWidth + "px";

	if(showGalleryNavigation) {
		document.getElementById("navigation_" + actualGallery).style.width = item.width + "px";
	}

	return true;
}

function nextPhoto() {
	var photos = document.getElementById("gallery_" + actualGallery).getElementsByTagName("img");

	if(actualPhoto < photos.length - 1) {
		changePhoto(actualPhoto + 1);
	} else {
		changePhoto(0);
	}

	return true;
}

function prevPhoto() {
	var photos = document.getElementById("gallery_" + actualGallery).getElementsByTagName("img");

	if(actualPhoto > 0) {
		changePhoto(actualPhoto - 1);
	} else {
		changePhoto(photos.length - 1);
	}

	return true;
}

function changePhoto(id) {
	var photos = document.getElementById("gallery_" + actualGallery).getElementsByTagName("img");

	actualPhoto = id;

	for(i = 0; i < photos.length; i++) {
		photos[i].style.display = "none";
	}

	var photo = document.getElementById("img_" + actualGallery + "_" + id);

	photo.style.display = "block";

	if(showGalleryNavigation) {
		document.getElementById("navigation_" + actualGallery).style.width = photo.width + "px";
	}

	return true;
}

function closePhoto() {

	overlay.style.display = "none";
	overlay_half.style.display = "none";

	return true;
}
