$(document).ready(function() {
	$.each(photos, function(key, value) {
		if (key == "image") jQuery.preLoadImages(value);
	});
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
	};
	
	var stopAnimation = function() {
	   // Clear the interval
	   clearInterval(interval);
	};	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Clear previous img and set the background image of the new active container
		$("#backgroundImg" + activeContainer).empty();
		$('<img src="' + photoObject.image + '" width="100%" />').appendTo("#backgroundImg" + activeContainer);
		$("#backgroundImg" + activeContainer).css({
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#backgroundText").css({"display" : "none"});
		
		// Set the new header text
		if (photoObject.firstline == "") { $("#backgroundText h1").css('display','none'); } 
		else { $("#backgroundText h1").html(photoObject.firstline); }
		
		// Set the new caption text
		if (photoObject.secondline == "") {
			$("#backgroundText #caption").css('display','none')
			$("#backgroundText > a.orange").css('display','none')
		} else {
			$("#backgroundText #caption")
				.css('display','none')
				.html(photoObject.secondline);
			$("#backgroundText > a.orange")
				.css('display','block')
				.html("More +");
		}
		
		// Fade out the current container and display the header text when animation is complete
		$("#backgroundImg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#backgroundText").fadeIn(200);
				animating = false;
			}, 500);
		});
	};
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	$('#backgroundText a#moretext').toggle(function(event){
			event.preventDefault;
			$('#backgroundText #caption').slideDown(200, function() { 
				$('#backgroundText a#moretext').html("Close");
			});
			if ($('#footer-extra').is(":hidden")) {
				// possible action
			} else {
				closeTray();
			}
		   	stopAnimation();
			return false;
		}, function(event) {
			event.preventDefault;
			$('#backgroundText #caption').slideUp(200, function() { 
				$('#backgroundText a#moretext').html("More +");
			});	     
			// Show the next image
			navigate("next");
			 
			// Start playing the animation
			interval = setInterval(function() {
			  navigate("next");
			}, slideshowSpeed);
			return false;
	});
	
	var windowWidth = $(window).width();
	var windowWidthRemainder = windowWidth-960;
	var windowWidthMargin = Math.floor(windowWidthRemainder/2);
	var windowHeight = $(window).height();
	var windowHeightStart = windowHeight-500;
	if (navigator.platform != "iPad") $('#backgroundText').css({'left' : windowWidthMargin, 'top' : windowHeightStart});
});

