	$(function()
	{
		// Initialise scrollers
		//#detail-copy .inner, 
		$("#contents-copy .inner, .artist-list .copy p, .artist-details .copy .inner, #contact .inner, #submit .inner").jScrollPane({scrollbarWidth:30, showArrows:true, animateTo:true});
		
		// Preload images
		var preload = ["nav_home_over.png", "nav_theartists_over.png", "nav_onlineexhibit_over.png", "nav_stories_over.png", "nav_yourstories_over.png", "nav_contact_over.png", "scroll_up_over.png", "scroll_down_over.png"];
		
		// Additional preload images
		if($("#exhibit-nav").length)
		{
			preload[preload.length] = "exhibit_birth_over.png";
			preload[preload.length] = "exhibit_marriage_over.png";
			preload[preload.length] = "exhibit_death_over.png";
			preload[preload.length] = "exhibit_installations_over.png";
		}
		
		for(var i = 0; i < preload.length; i++)
			$("<img/>").attr("src", "images/" + preload[i]);
		
		// Set home page slideshow running
		if($("#home-slideshow").length)
		{
			var homeLoadCount = 0;
			var homeLoadTotal = $("#home-slideshow img").length;
			$("#home-slideshow img").load(function()
			{
				homeLoadCount++;

				if(homeLoadCount == 1)
				{
					$(this).css("z-index", "2");
				}
				
				if(homeLoadCount == Math.ceil(homeLoadTotal/2))
				{
					$("#home-slideshow img").css("z-index", "1");
				}
			});
			
			setTimeout(homeSlideshowAnimate, 2000);
		}
			
		// Set gallery slideshow running
		if($(".slideshow").length)
		{
			//var slideshowLoadCount = 0;
			//var slideshowLoadTotal = $(".slideshow img").length;

			$(".slideshow img").css("z-index", "1");
			$(".slideshow img:first").css("z-index", "2");
			setTimeout(gallerySlideshowAnimate, 1000);
		}
			
		// Trigger gallery text rollover on image rollover
		$("#contents-gallery a")
			.mouseover(function()
			{
				$("#contents-copy a[href="+$(this).attr("href")+"]").addClass("hover");
			})
			.mouseout(function()
			{
				$("#contents-copy a[href="+$(this).attr("href")+"]").removeClass("hover");
			});
		
		// Load external links in a new window
		$("a[rel=external]").each(function()
		{
			this.target = "_blank";
		});
		
		// Mouseover thumbnails
		var originalImage = null;
		$("#detail-gallery ul a")
			.each(function()
			{
				// Preload large version of each thumbnail
				var src = $("img", this).attr("src").replace(/thumbs/, "framed");
				$("<img/>").attr("src", src);
			})
			.mouseover(function()
			{
				if($("#detail-gallery-image").length)
				{
					originalImage = $("#detail-gallery-image").attr("src");
					$("#detail-gallery-image").attr("src", $("img", this).attr("src").replace(/thumbs/, "framed"));
				}
				else
				{
					gallerySlideshowPause = true;
					originalImage = $(".slideshow img:first").attr("src");
					$(".slideshow img:first").attr("src", $("img", this).attr("src").replace(/thumbs/, "framed"));
				}
			})
			.mouseout(function()
			{
				if(originalImage)
				{
					if($("#detail-gallery-image").length)
						$("#detail-gallery-image").attr("src", originalImage);
					else
						$(".slideshow img:first").attr("src", originalImage);
				}
				
				gallerySlideshowPause = false;
			});

	});
	
	function homeSlideshowAnimate()
	{
		$("#home-slideshow img:last")
			.remove()
			.prependTo("#home-slideshow");
			
		setTimeout(homeSlideshowAnimate, 2000);
	}

	var gallerySlideshowPause = false;
	function gallerySlideshowAnimate()
	{
		if(!gallerySlideshowPause)
		{
			$(".slideshow img:first")
				.remove()
				.css("z-index", "1")
				.appendTo(".slideshow");
				
			$(".slideshow img:first")
				.css("z-index", "2");
		}
			
		setTimeout(gallerySlideshowAnimate, 500);
	}