// Speed of the automatic slideshow
var slideshowSpeed = 7500;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"image" : "slideshow-1.jpg",
		"url" : "http://gainesvillecountrydayschool.org/elementary",
		"linktext" : "More About Our Elementary Program",
		"firstline" : "Elementary - Investigative Science",
		"secondline" : "<p>We assist each student in scientific discovery by nurturing his/her understanding, interest, and enthusiasm for science while teaching the skills necessary for each child to grow successfully as a scientist.</p>"
	}, {
		"image" : "slideshow-2.jpg",
		"url" : "http://gainesvillecountrydayschool.org/elementary",
		"linktext" : "More About Our Elementary Program",
		"firstline" : "Confident and Competent with Technology",
		"secondline" : "<p>Our fully equipped Mac lab enables students kindergarten through fifth grade to work with Macintosh and Microsoft software. From creating charts and graphs for the science fair to making a video yearbook, we want students graduating fifth grade to be both competent and confident with technology.</p>"
	}, {
		"image" : "slideshow-3.jpg",
		"url" : "http://gainesvillecountrydayschool.org/after-school",
		"linktext" : "Learn More",
		"firstline" : "After School - Work, Play, Socialize.",
		"secondline" : "<p>Our After school program provides a place for students to work, play and socialize in a safe and nurturing environment. We offer a variety of specialized classes and clubs and encourage students to participate in as may activities as they would like (or have time for).</p>"
	}, {
		"image" : "slideshow-4.jpg",
		"url" : "http://www.gainesvillecountrydayschool.org/early-childhood/cirriculum",
		"linktext" : "More About our Early Childhood Program",
		"firstline" : "Early Childhood - Play is a child's work",
		"secondline" : "<p>Play has been found to contribute to children’s physical, mental, emotional, and social development. The dramatic play acted out every day in our play center leads to improvement in the verbal ability, creative thinking, group problem solving and acting out of feelings and ideas that are difficult for preschoolers to comprehend or talk about.</p>"
	}
];


$(document).ready(function() {
	
	$(".slide-thumb").click(function(){
		currentImg = $(this).html();
		navigate("specific");
		$(".play-trigger").show();
		$(".pause-trigger").hide();
		$('#secondline').slideUp(400, function(){$(".caption").removeClass("opened");});
		$(".close-trigger").hide();
		stopAnimation();
		return false;
	});
	
	$(".slide-thumb").mouseover(function(){
		$('#thumbs div').removeClass("active-thumb");
		$(this).next("div").addClass("active-thumb").show();
		$('#thumbs div:not(.active-thumb)').hide();
	});
	
	$("#thumbs").mouseout(function(){
		$('#thumbs div').hide();
	});
	
	$(".play-trigger").click(function(){
		$(this).hide();
		$(".pause-trigger").show();
		$('#secondline').slideUp('fast');
		$(".caption").removeClass("opened");
		$(".close-trigger").hide();
		return false;
	});
	
	$(".pause-trigger").click(function(){
		$(this).hide();
		$(".play-trigger").show();
		return false;
	});
	$(".secondline-trigger").click(function(){
		$("#secondline").slideToggle({duration:800, easing: 'easeOutBounce'});
		$(".play-trigger").show();
		$(".pause-trigger").hide();
		$(this).hide();
		$(".close-trigger").show();
		$(".caption").addClass("opened");
		stopAnimation();
		return false;
	});
	
	$(".close-trigger").click(function(){
		$("#secondline").slideToggle(400, function(){ $(".close-trigger").hide();
		$(".secondline-trigger").show(); $(".caption").removeClass("opened");});
		$(".play-trigger").hide();
		$(".pause-trigger").show();
		return false;
	});
	
	// Backwards navigation
	$("#back").click(function() {
		$("#off").removeClass('selected');
		$("#on").addClass('selected');
		$('#secondline').slideUp(400, function(){$(".caption").removeClass("opened");});

		$(".close-trigger").hide();
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		$("#off").removeClass('selected');
		$("#on").addClass('selected');
		$('#secondline').slideUp(400, function(){$(".caption").removeClass("opened");});
		$(".close-trigger").hide();
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	
	// Slidehow Off
	$("#off").click(function() {
		stopAnimation();
	});
	
	// Slidehow On
	$(".play-trigger, .close-trigger").click(function() {
		
		// Clear the interval
		clearInterval(interval);
		
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	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;
			}
		// Check if we have specified a specific image
		} else if(direction == "specific") {
			
		} 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);
		$(".slide-thumb").removeClass("currentThumb");
		$("#thumb" + currentImg).addClass("currentThumb");
		
	};
	var currentZindex = -1;
	
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/slideshow-images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#firstline > span").fadeOut(500,function() {
			$(".caption").removeClass("opened");
			$("#firstline > span").html(photoObject.firstline);
			Cufon.replace('#firstline > span', {fontFamily: 'Archer Semibold'});
			$("#secondline > p")
				.html(photoObject.secondline);
			$(".linkto")
				.attr("href", photoObject.url)
				.html(photoObject.linktext);
		});		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(1100 ,function() {
				$(".secondline-trigger").show();
				$("#firstline > span").fadeIn(150);
				animating = false;
		});
	};
	
	var stopAnimation = function() {
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
		
});
