if (typeof Prototype != "undefined") {
	//document.observe("dom:loaded", function()
	Event.observe(window, "load", function() {
		var currentClassName = "current";
		var elemGallery = $("home_gallery");
		var galleryLinks;
		var handleGalleryLinkClick;
		var i;
		
		try {
			if (elemGallery) {
				galleryLinks = elemGallery.select("li a");
				
				// Start the gallery at "idle"
				elemGallery.currentImage = false;
				elemGallery.fade = function() {
					var effectDuration = 0.5;
					var timeNow = (new Date()).getTime() / 1000;
					var progress = (timeNow - this.imageFadeStartTime) / effectDuration;
					
					// Cap progress at 100%
					progress = Math.min(progress, 1.0);
					
					// Update the animation
					this.currentImage.setOpacity(progress);
					
					if (progress >= 1.0) {
						if (this.lastImage) {
							//this.lastImage.style.visibility = "hidden";
							this.lastImage.style.display = "none";
						}
						
						this.state = 0; // "Done"
						this.removeClassName("busy");
						
					} else {
						// Keep looping
						setTimeout($(this.fade).bind(this), 50);
					}
				};
				elemGallery.imageCache = {};
				elemGallery.lastImage = false;
				elemGallery.show = function(url, linkURL) {
					// Gallery must be idle
					if (this.state != 0) {
						//alert("Sorry, can't show the image; gallery isn't idle yet.");
						return;
					}
					
					if (this.imageCache[url]) {
						//alert("show: url found in image cache");
						//alert("Found the image: " + Object.toJSON(this.imageCache[url]));
						this.imageClickURL = linkURL ? linkURL : false;
						this.startFadingTo(url);
					} else {
						// Queue up the image
						//alert("show: url not found in cache, queueing image");
						//alert("Not found in image cache: " + Object.toJSON(this.imageCache));
						
						var objImage = new Image();
						var thisCopy = this;
						
						this.state++; // Downloading
						this.addClassName("downloading");
						this.addClassName("busy");
						
						$(objImage).observe("load", function() {
							//alert("finished downloading " + url);
							thisCopy.removeClassName("downloading");
							thisCopy.imageClickURL = linkURL ? linkURL : false;
							thisCopy.startFadingTo(url);
						});
						
						this.imageCache[url] = {};
						this.imageCache[url].obj = objImage;
						this.imageCache[url].elem = false;
						this.imageCache[url].foo = url;
						
						// Has to go after everything else, seems to fire immediately (and asynchronously) in IE
						objImage.src = url;
						
						//alert("Not found in image cache: " + Object.toJSON(this.imageCache));
					}
				};
				elemGallery.startFadingTo = function(url) {
					this.lastImage = this.currentImage;
					
					if (!this.imageCache[url].elem) {
						// This image doesn't have an element, fix that
						this.imageCache[url].elem = $(document.createElement("img"));
						this.imageCache[url].elem.setAttribute("src", url);
						this.down(".fade").appendChild(this.imageCache[url].elem);
						$(this.imageCache[url].elem).observe("click", function() {
							if (elemGallery.imageClickURL) {
								window.location = elemGallery.imageClickURL;
							}
						});
						if (elemGallery.imageClickURL) {
							$(this.imageCache[url].elem).addClassName("linked");
						}
					}
					
					this.currentImage = this.imageCache[url].elem;
					
					this.state++; // Fading
					this.addClassName("busy");
					
					if (this.lastImage) {
						this.lastImage.style.zIndex = this.style.zIndex + 1;
					}
					
					this.currentImage.setOpacity(0.0);
					//this.currentImage.style.visibility = "visible";
					this.currentImage.style.display = "inline";
					this.currentImage.style.zIndex = this.style.zIndex + 2;
					
					this.imageFadeStartTime = (new Date()).getTime() / 1000;
					
					// Begin the fade loop
					setTimeout($(this.fade).bind(this), 50);
				};
				elemGallery.state = 0;
				
				var linkTextToURL = function(strText) {
					var ret = strText.toLowerCase().replace(/[^a-z\- ]+/, "").replace(/ /g, "-").replace(/-+/g, "-");
					//alert("images/home_gallery/" + ret + ".jpg");
					return "images/home_gallery/" + ret + ".jpg";
				};
				
				var linkToURL = function(elemLink) {
					//return elemLink.firstChild.nodeValue;
					
					var parentLI = $(elemLink).up("li");
					
					if (parentLI) {
						var matches = parentLI.id.match(/\d+/gi);
						if (matches.length > 0) {
							return "binary/home_gallery/thumbnail/" + matches[0] + ".jpg";
						}
					}
					
					return "";
				};
				
				handleGalleryLinkClick = function(e) {
					var i;
					var imageFound;
					var imageURL;
					
					$(this);
					
					// Cancel default link behavior
					if (e) {
						$(e).stop();
					}
					
					// Only allow clicks when the gallery is idle and when not clicking an already-selected link
					if (elemGallery.state == 0 && !$(this.parentNode).hasClassName(currentClassName)) {
						// Generate the URL for the image to load, based on the link text
						//imageURL = this.firstChild.nodeValue.toLowerCase().replace(/[^a-z\- ]+/, "").replace(" ", "-").replace(/-+/, "-");
						//imageURL = "images/home_gallery/" + imageURL + ".jpg";
						//imageURL = linkTextToURL(this.firstChild.nodeValue);
						imageURL = linkToURL(this);
						//alert("imageURL = " + imageURL);
						
						// (Load if needed, and) Change to the given image
						elemGallery.show(imageURL, this.hasClassName("disabled") ? false : this.href);
						
						// Update class names
						for (i = 0; i < galleryLinks.length; i++) {
							if (galleryLinks[i] == this) {
								$(galleryLinks[i].parentNode).addClassName(currentClassName);
							} else {
								$(galleryLinks[i].parentNode).removeClassName(currentClassName);
							}
						}
					} else {
						//alert("Sorry, can't do that. I'm already active.");
					}
				};
				
				if (galleryLinks.length > 0) {
					// Activate each link
					for (i = 0; i < galleryLinks.length; i++) {
						galleryLinks[i].observe("click", handleGalleryLinkClick);
					}
					
					// Add a loading throbber
					var elemThrobberContainer = $(document.createElement("div"));
					elemThrobberContainer.addClassName("throbber");
					
					var elemThrobber = $(document.createElement("img"));
					elemThrobber.setAttribute("src", "images/throbber.gif");
					elemThrobberContainer.appendChild(elemThrobber);
					
					elemGallery.appendChild(elemThrobberContainer);
					
					// Delete the first image, and auto load the first link
					var firstImage = elemGallery.down(".fade img");
					firstImage.parentNode.removeChild(firstImage);
					//elemGallery.show(linkTextToURL(galleryLinks[0].firstChild.nodeValue));
					//elemGallery.show(linkToURL(galleryLinks[0]));
					handleGalleryLinkClick.call(galleryLinks[0]);
					
					// Auto-change timer
					var autoChangeFunc = (function() {
						//alert(this + "\n" + this.state);
						//return;
						
						// Don't auto-change if downloading or if the user is interacting with it.
						if (this.state == 0 && !this.hasClassName("hover")) {
							// Get the "current" link
							var currentLink = this.down("ul li.current a");
							//alert("currentLink = " + currentLink);
							if (currentLink) {
								// Get the next link
								var nextLI = currentLink.up("li").next("li");
								var nextLink;
								if (!nextLI) {
									nextLink = galleryLinks[0];
								} else {
									nextLink = nextLI.down("a");
								}
								
								// Show that link
								//this.show(linkTextToURL(nextLink.firstChild.nodeValue));
								handleGalleryLinkClick.call(nextLink);
							} else {
								//alert("Can't find current link");
							}
						} else {
							//alert("Gallery is busy.");
						}
					}).bind(elemGallery);
					
					// Mouseover and mouseout hover state
					elemGallery.observe("mouseover", function() {
						$(this).addClassName("hover");
						
						if (this.autoChangeHandle) {
							clearInterval(this.autoChangeHandle);
							this.autoChangeHandle = false;
						}
					});
					elemGallery.observe("mouseout", function() {
						$(this).removeClassName("hover");
						
						if (!this.autoChangeHandle) {
							this.autoChangeHandle = setInterval(autoChangeFunc, 4000);
						}
					});
					
					elemGallery.autoChangeHandle = setInterval(autoChangeFunc, 4000);
				}
			}
		} catch (e) {
			alert("Unhandled Exception: " + Object.toJSON(e));
		}
	});
}
