window.imageList = Array();

$(window).load(function() {
	if (($(".mainrow .text").attr("scrollHeight") - 23) <= $(".mainrow .text").height()) {
		$(".mainrow .text").css("overflowY","hidden");
	}
	
	// Get image manifest
	if (ImageCategory != "gallery") {
		$.ajax({
			url: "/images/viewerbox/imagemanifest.xml",
			success: function(returnedData, textStatus) {
				var imagesArray = returnedData.getElementsByTagName("image");
				
				for (var imageCount = 0; imageCount < imagesArray.length; imageCount ++) {
					if (imagesArray[imageCount].textContent != undefined) {
						window.imageList.push(imagesArray[imageCount].textContent);
					} else if (imagesArray[imageCount].firstChild.nodeValue != undefined) {
						window.imageList.push(imagesArray[imageCount].firstChild.nodeValue);
					}
				}
				
				beginAnimating();
			}
		});
	}
});

$(document).ready(function() {
	$(".gallery-item a").click(function(eventData) {
		eventData.preventDefault();
		
		var CImageLoader = new Image();
		CImageLoader.src = $(this).attr("href");
		
		CImageLoader.onload = function(eventData) {
			$(".mainrow .image").append("<img src='/interfaces/image.php?src=" + this.src + "' width='363' height='335' alt='Gallery Image' />");
			$(".mainrow .image").scrollTo($(".mainrow .image img:last-child"),"slow",function(eventData) {
				$(".mainrow .image img").not(".mainrow .image img:last-child").remove();
				$(".mainrow .image").attr("scrollTop",0);
			});
		};
	});
	
	if (window.navigator.userAgent.indexOf("IE 6") != -1) {
		DD_belatedPNG.fix('.toplevellist, .footer');
	}
});

function beginAnimating() {
	var ImageInsertCount = 0;
	
	for (var imageNode = 0; imageNode < window.imageList.length; imageNode++) {
		// does this image already exist as the non-JS image in the HTML itself?
		
		if ($("img[src*=" + window.imageList[imageNode] + "]").length == 0) {
			if ((ImageCategory != "home" && ImageCategory == window.imageList[imageNode].split("-")[0]) || ImageCategory == "home") {
				$(".image").append("<img src=\"/images/viewerbox/" + window.imageList[imageNode] + "\"  alt=\"Illustrative Water Sports Image\" width=\"363\" height=\"335\" />");
				$(".image img").css("float","left");
				ImageInsertCount ++;
			}
		}
	}
	
	if (ImageInsertCount) {
		window.currentImage = 0;
		window.imageCount = ImageInsertCount;
		window.animateTimer = window.setInterval(function() {
			if (window.currentImage < imageCount) {
				window.currentImage ++;
			} else {
				window.currentImage = 0;
			}
			
			$(".image").scrollTo({top: (window.currentImage*335) + "px"}, 1000, {easing: "swing"});
		}, 5000);
	}
}