var overlay = null;
var overlay_half = null;
var actualPhoto = 0;
var galleryContentClass = "gallery_item";
var showGalleryNavigation = false;

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) {

	actualPhoto = id;

	overlay.style.display = "block";
	overlay_half.style.display = "block";

	var items = getElementsByClassName(document, "*", galleryContentClass);

	for(i = 0; i < items.length; i++) {
		items[i].style.display = "none";
	}

	var item = document.getElementById("gallery_item_" + id);

	item.style.display = "block";
	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 > max) {
			maxWidth = images[i].width;
		}
	}
	item.style.width = maxWidth + "px";

	if(showGalleryNavigation) {
		document.getElementById("gallery_navigation").style.width = item.width + "px";
	}

	return true;
}

function nextPhoto() {
	if(actualPhoto < getElementsByClassName(document, "*", galleryContentClass).length - 1) {
		openPhoto(actualPhoto + 1);
	} else {
		openPhoto(0);
	}

	return true;
}

function prevPhoto() {
	if(actualPhoto > 0) {
		openPhoto(actualPhoto - 1);
	} else {
		openPhoto(getElementsByClassName(document, "*", galleryContentClass).length - 1);
	}
}

function closePhoto() {

	overlay.style.display = "none";
	overlay_half.style.display = "none";

	return true;
}