(function () {

	var preloader = [];
	preloader[0] = new Image();
	preloader[0].src = "/images/throbber.gif";

	function isUndefined(possiblyUndefined) {
		return (typeof possiblyUndefined === "undefined");
	}

	function isUndefinedOrEmpty(possiblyUndefined) {
		return (isUndefined(possiblyUndefined) || ("" + possiblyUndefined).length === 0);
	}

	function pre(imgSrc) {
		if (!isUndefinedOrEmpty(imgSrc)) {
			var img = new Image();
			img.src = imgSrc;
			return img;
		}
	}

	function changeHash(newSrc) {
		if (!isUndefinedOrEmpty(newSrc)) {
			window.location.hash = "#!img=" + newSrc.replace(/\.jpg$/, "");
		} else {
			window.location.hash = "#";
		}
	}

	function showLargeImage(newSrc, newAlt) {
		var $largeImage = $("#largeImage");
		$largeImage.hide();
		if (!isUndefinedOrEmpty(newSrc)) {
			$largeImage.bind("load", function ()  {
				var $this = $(this);
				$this.hide();
				$this.show();
			});
			$largeImage.attr("src", newSrc);
			$largeImage.attr("alt", newAlt);
			$largeImage.show();
		}
	}

	function getPreviousListItem(listItem) {
		var $listItem = $(listItem);
		if ($listItem.prev("li").length === 0) {
			// no more links, go to the end
			if ($listItem.parent("ul").children(":last").children("a").length > 0) {
				// this list item has a link in its childrem
				return $listItem.parent("ul").children(":last")[0];
			} else {
				// no link in this item, go to the previous
				return getPreviousListItem($listItem.parent("ul").children(":last")[0]);
			}
		} else {
			if ($listItem.prev("li").children("a").length > 0) {
				// this list item has a link in its childrem
				return $listItem.prev("li")[0];
			} else {
				// no link in this item, go to the previous
				return getPreviousListItem($listItem.prev("li")[0]);
			}
		}
	}

	function getNextListItem(listItem)
	{
		var $listItem = $(listItem);
		if ($listItem.next("li").length === 0) {
			// no more links, go to the end
			if ($listItem.parent("ul").children(":first").children("a").length > 0) {
				// this list item has a link in its childrem
				return $listItem.parent("ul").children(":first")[0];
			} else {
				// no link in this item, go to the previous
				return getNextListItem($listItem.parent("ul").children(":first")[0]);
			}
		} else {
			if ($listItem.next("li").children("a").length > 0) {
				// this list item has a link in its childrem
				return $listItem.next("li")[0];
			} else {
				// no link in this item, go to the previous
				return getNextListItem($listItem.next("li")[0]);
			}
		}
	}

	$(function() {
		$(".no-js").removeClass("no-js").addClass("js");

		// Home 2011 random images
		$("html.home.ie6 img.random").each(function() {
			var $img = $(this);
			$img.replaceWith("<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='scale');\" class=\"random\"></div>");
		});

		var $homeImgs = $(".home img.random, .home div.random");
		if ($homeImgs.length > 1) {
			$homeImgs.hide().eq(Math.floor(Math.random() * $homeImgs.length)).show().addClass("selected");
			window.setInterval(function () {
				var $imgFrom, $imgTo, doNext = false, imgCount = $homeImgs.length;
				$homeImgs.each(function (index) {
					var $this = $(this);
					if (doNext) {
						doNext = false;
						$imgTo = $this;
					} else if ($this.hasClass("selected")) {
						doNext = true;
						$imgFrom = $this;
						if (index >= (imgCount - 1)) {
							$imgTo = $homeImgs.eq(0);
						}
					}
				});
				$imgFrom.removeClass("selected").fadeOut("slow");
				$imgTo.addClass("selected").fadeIn("slow");
			}, 4000);	// 4 seconds before it fades to next image
		}

		// add hover to main header images
		$("img.hover").hover(function () {
			this.src = this.src.replace(/_on\./, "_over.");
		}, function () {
			this.src = this.src.replace(/_over\./, "_on.");
		}).each(function () {
			// preload hovers
			pre(this.src.replace(/_on\./, "_over."));
		});

		$("area.hover").hover(function () {
			var img = $("img[usemap='#" + $(this).parent().attr("id") + "']")[0];
			//img.src = img.src.replace(/_on\./, "_over.");
			var imgSwitch = "";
			if (this.className.indexOf("hover-l") >= 0) {
				imgSwitch = "_l";
			} else if (this.className.indexOf("hover-r") >= 0) {
				imgSwitch = "_r";
			} else if (this.className.indexOf("hover-top") >= 0) {
				imgSwitch = "_top";
			}
			img.src = img.src.replace(/_on\./, imgSwitch + "_over.");
		}, function () {
			var img = $("img[usemap='#" + $(this).parent().attr("id") + "']")[0];
			img.src = img.src.replace(/(_(l|r|top))?_over\./, "_on.");
		}).each(function () {
			// preload hovers
			var img = $("img[usemap='#" + $(this).parent().attr("id") + "']")[0];
			pre(img.src.replace(/_on\./, "_l_over."));
			pre(img.src.replace(/_on\./, "_r_over."));
			pre(img.src.replace(/_on\./, "_top_over."));
		});
		
		// add click event to thumb links
		var $index = $("#index");
		var $large = $("#large");
		var $largeImage = $("#largeImage", $large);
		$("#thumbs a").bind("click", function() {
			var $this = $(this);
			var newSrc = $this.attr("href");
			var newAlt = $this.attr("title");
			//showLargeImage(newSrc, newAlt);
			//$index.hide();
			//$large.show();
			changeHash(newSrc);
			return false;
		});
		
		// add click events to image navigation
		$("#upmap, #uplink").bind("click", function() {
			//$index.show();
			//$large.hide();
			//showLargeImage("", "");
			changeHash("");
			return false;
		});
		// prev & next
		$("#previousmap, #previouslink").bind("click", function() {
			var oldSrc = $largeImage.attr("src");
			var $prevLink = $(getPreviousListItem($("a[href$='" + oldSrc + "']").parent("li")[0])).children("a");
			var newSrc = $prevLink.attr("href");
			var newAlt = $prevLink.attr("title");
			//showLargeImage(newSrc, newAlt);
			changeHash(newSrc);
			$(this).blur();
			return false;
		});
		$("#nextmap, #nextlink").bind("click", function() {
			var oldSrc = $largeImage.attr("src");
			var $nextLink = $(getNextListItem($("a[href$='" + oldSrc + "']").parent("li")[0])).children("a");
			var newSrc = $nextLink.attr("href");
			var newAlt = $nextLink.attr("title");
			//showLargeImage(newSrc, newAlt);
			changeHash(newSrc);
			$(this).blur();
			return false;
		});
		
		var showLarge = false;
		var loadImgFromParams = function (paramString) {
			showLarge = false;
			if (!isUndefinedOrEmpty(paramString) && paramString.indexOf("img=") >= 0) {
				var imgSrc = paramString.split("img=")[1] + ".jpg";
				var $link = $("a[href$='" + imgSrc + "']");
				var newSrc = $link.attr("href");
				var newAlt = $link.attr("title");
				showLargeImage(newSrc, newAlt);
				showLarge = true;
			}
			if (showLarge) {
				$index.hide();
				$large.show();
				window.scrollTo(window.scrollX, 1);
			} else {
				$large.hide();
				$index.show();
			}
		};

		if ($(".artwork").length > 0) {
			// load image from querystring
			loadImgFromParams(window.location.search);

			// load image from hash
			$(window).hashchange(function() {
				loadImgFromParams(window.location.hash);
			});
			$(window).hashchange();
		}
		
		// mobile fix to scroll past toolbar
		if (navigator.userAgent.indexOf("Mobile") > -1) {
			$(window).load(function() {
				if (!this.navigator.standalone) {
					this.setTimeout(function() { this.scrollTo(this.scrollX, this.scrollY + 1); }, 500);
				}
			});
		} else {
			// preload large images
			$("a[href$='.jpg']").each(function (i) {
				pre(this.href);
			});
		}
	});

}());
