function getWindowInnerHeight() {
	if (typeof(window.innerHeight) == "number") {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	} else {
		return 768;
	}
}

function getWindowInnerWidth() {
	if (typeof(window.innerWidth) == "number") {
		return window.innerWidth;
	} else if (document.documentElement && typeof(document.documentElement.clientWidth) == "number") {
		return document.documentElement.clientWidth;
	} else if (document.body && typeof(document.body.clientWidth) == "number") {
		return document.body.clientWidth;
	} else {
		return 1024;
	}
}

function getWindowPageXOffset() {
	if (typeof(window.pageXOffset) == "number") {
		return window.pageXOffset;
	} else if (document.documentElement && typeof(document.documentElement.scrollLeft) == "number") {
		return document.documentElement.scrollLeft;
	} else if (document.body && typeof(document.body.scrollLeft) == "number") {
		return document.body.scrollLeft;
	} else {
		return 0;
	}
}

function getWindowPageYOffset() {
	if (typeof(window.pageYOffset) == "number") {
		return window.pageYOffset;
	} else if (document.documentElement && typeof(document.documentElement.scrollTop) == "number") {
		return document.documentElement.scrollTop;
	} else if (document.body && typeof(document.body.scrollTop) == "number") {
		return document.body.scrollTop;
	} else {
		return 0;
	}
}
function greyOut(vis, options) {
	// Pass true to gray out screen, false to ungray
	// options are optional.  This is a JSON object with the following (optional) properties
	// opacity:0-100         // Lower number = less grayout higher = more of a blackout 
	// zindex: #             // HTML elements with a higher zindex appear on top of the gray out
	// bgcolor: (#xxxxxx)    // Standard RGB Hex color code
	// greyOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
	// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
	// in any order.  Pass only the properties you need to set.
	var options = options || {}; 
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = (opacity / 100);
	var bgcolor = options.bgcolor || '#000000';
	var dark=document.getElementById('darkenScreenObject');
	if (!dark) {
		var tbody = document.getElementsByTagName("body")[0];
		var tnode = document.createElement('div');           // Create the layer.
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
	}
	if (vis) {
		// Calculate the page width and height
		var pageWidth = getWindowInnerWidth() + "px";
		var pageHeight = getWindowInnerHeight() + "px";

		//set the shader to cover the entire page and make it visible.
		dark.style.opacity=opaque;                      
		dark.style.MozOpacity=opaque;                   
		dark.style.filter = "alpha(opacity=" + opacity + ")"; 
        	dark.style.position="absolute";
		dark.style.left = getWindowPageXOffset() + "px";
		dark.style.top = getWindowPageYOffset() + "px";
		dark.style.zIndex = zindex;        
		dark.style.backgroundColor = bgcolor;  
		dark.style.width = pageWidth;
		dark.style.height = pageHeight;
		dark.style.display='block';                          
	} else {
		dark.style.display='none';
	}
}

function showGallery(prefix, suffix, maxIndex, currentIndex) {
	greyOut(true);

	var previousLink = document.getElementById("galleryPreviousLink");
	if (previousLink) {
		var previousIndex = currentIndex - 1;
		if (previousIndex < 1) {
			previousIndex = maxIndex;
		}
		previousLink.href = "javascript:showGallery('" + prefix + "', '" + suffix + "', " + maxIndex + ", " + previousIndex + ");";
	}

	var nextLink = document.getElementById("galleryNextLink");
	if (nextLink) {
		var nextIndex = currentIndex + 1;
		if (nextIndex > maxIndex) {
			nextIndex = 1;
		}
		nextLink.href = "javascript:showGallery('" + prefix + "', '" + suffix + "', " + maxIndex + ", " + nextIndex + ");";
	}

	var image = document.getElementById("galleryImageObject");
	if (image) {
		image.src = prefix + currentIndex + suffix;
	}

	var gallery = document.getElementById("galleryObject");
	if (gallery) {
		var galleryHeight = 700;
		var galleryWidth = 700;
		if (gallery.style.display != "block") {
			var galleryX = (getWindowInnerWidth() - galleryWidth)/2;
			var galleryY = getWindowInnerHeight()/3 - galleryHeight/2;
			galleryX = galleryX + getWindowPageXOffset();
			if (galleryX < 0) {
				galleryX = 0;
			}
			galleryY = galleryY + getWindowPageYOffset();
			if (galleryY < 0) {
				galleryY = 0;
			}
			gallery.style.top = galleryY + "px";
			gallery.style.left = galleryX + "px";
		}
		gallery.style.position="absolute";
		gallery.style.zIndex = 100;
		gallery.style.display = "block";
	} else {
		alert("Gallery could not be found.");
	}
}

function hideGallery() {
	var gallery=document.getElementById("galleryObject");
	if (gallery) {
		gallery.style.display = "none";
	}
	greyOut(false, null);
}
