document.onclick = cleanupPhoto;

function viewPhoto(path, width, height) {
	// get element
	var galleryPhotoContainer = document.getElementById("galleryPhotoContainer");
	
	// not sure why this is needed...
	var extra_width = 10;
	var extra_height = 0;
	if (!is_ie6) {
		//extra_width += 10;
		extra_height += 30;
	}
	
	// set dimensions
	galleryPhotoContainer.style.width = (width + extra_width) + "px";
	galleryPhotoContainer.style.height = (height + extra_height) + "px";
	
	// set image dimensions
	var galleryPhoto = document.getElementById("galleryPhoto");
	galleryPhoto.src = path;
	galleryPhoto.style.width = width + "px";
	galleryPhoto.style.height = height + "px";
	
	// position
	galleryPhotoContainer.style.position = "absolute";
	galleryPhotoContainer.style.left = Math.round((getViewportWidth() / 2) - ((width + extra_width) / 2)) + "px";
	galleryPhotoContainer.style.top = Math.round(((getViewportHeight() / 2) - ((height + extra_height) / 2) - 50)) + "px";
	
	galleryPhoto.onload = showPhoto;
}

function showPhoto() {
	// get elements
	var galleryPhotoContainer = document.getElementById("galleryPhotoContainer");
	var galleryPhoto = document.getElementById("galleryPhoto");
	
	if (galleryPhoto.complete) {
		// show
		galleryPhotoContainer.style.display = "block";
		galleryPhotoContainer.style.visibility = "visible";
	} else {
		setTimeout("showPhoto()", 100);
	}
}

function cleanupPhoto(e) {
	if(is_ie) {
		e = window.event;
	}

	if (!onPhoto(e))
		closePhoto();
}

function onPhoto(e) {
	// get element
	var galleryPhotoContainer = document.getElementById("galleryPhotoContainer");

	var h_topLeft = galleryPhotoContainer.offsetLeft;
	var v_topLeft = galleryPhotoContainer.offsetTop;
	var h_bottomRight = galleryPhotoContainer.offsetWidth + galleryPhotoContainer.offsetLeft;
	var v_bottomRight = galleryPhotoContainer.offsetHeight + galleryPhotoContainer.offsetTop;
	
	if ( (e.clientX >= h_topLeft) && (e.clientX <= h_bottomRight)
		&& (e.clientY >= v_topLeft) && (e.clientY <= v_bottomRight) ) {
		return true;
	} else {
		return false;
	}
}

function closePhoto() {
	// get elements
	var galleryPhotoContainer = document.getElementById("galleryPhotoContainer");
	var galleryPhoto = document.getElementById("galleryPhoto");
	
	// hide
	if (galleryPhotoContainer != null) {
		galleryPhoto.src = "";
		galleryPhoto.width = "0px";
		galleryPhoto.height = "0px";
		
		galleryPhotoContainer.style.width = "0px";
		galleryPhotoContainer.style.height = "0px";
		galleryPhotoContainer.style.display = "none";
		galleryPhotoContainer.style.visibility = "hidden";
	}
}

function getViewportWidth() {
	var returnValue = 0;
	if(is_ie4up && !is_ie6) {
		returnValue = document.getElementsByTagName('body')[0].clientWidth;
	} else if(is_ie6) {
		returnValue = document.documentElement.clientWidth;
	} else {
		returnValue = window.innerWidth;
	}
	return returnValue;
}

function getViewportHeight() {
	var returnValue = 0;
	if(is_ie4up && !is_ie6up) {
		returnValue = document.getElementsByTagName('body')[0].clientHeight;
	} else if(is_ie6up) {
		returnValue = document.documentElement.clientHeight;
	} else {
		returnValue = window.innerHeight;
	}
	return returnValue;
}

function getWidth(inputObj) {
	var returnValue = inputObj.offsetWidth;
	return returnValue;
}

function getHeight(inputObj) {
	var returnValue = inputObj.offsetHeight;
	return returnValue;
}