/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

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

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [
    // FALL
   /*{
        "title": "Fall Hikes",
        "image": "Fall/fallhikes.jpg",
        "url": "/?page_id=26101",
        "firstline": "Fall Hikes",
        "secondline": "Fall is a great time to discover the beauty and variety of California State Parks with fewer visitors and cooler weather."
    },
    {
        "title": "Henry Cowell Redwoods SP",
        "image": "Fall/HenryCowellRedwoodsSP_P0067981.jpg",
        "url": "/?page_id=546",
        "firstline": "Henry Cowell Redwoods SP",
        "secondline": "The main park area contains the large, old-growth redwoods, while the northern area (Fall Creek) has about 20 miles of hiking trails. The tallest tree in the park is about 285 feet tall, and about 16 feet wide."
     },
    {
        "title": "Hendy Woods SP",
        "image": "Fall/HendyWoodsSP_P0074182.jpg",
        "url": "/?page_id=438",
        "firstline": "Hendy Woods SP",
        "secondline": "Located in the middle of the Anderson Valley wine district, this park is warmer and less foggy than most redwood parks along the coast."
    },
    {
        "title": "California State Parks Museum Collections",
        "image": "Fall/emuseum.jpg",
        "url": "http://www.museumcollections.parks.ca.gov",
        "firstline": "California Landscapes from the Boggs Collection",
        "secondline": "We invite you to explore these outstanding examples of historical objects, archaeological specimens, photography, and art."
    },
        {
        "title": "Millerton Lake SRA",
        "image": "Fall/MillertonLakeSRA_P0072150.jpg",
        "url": "/?page_id=587",
        "firstline": "Millerton Lake SRA",
        "secondline": "With over 40 miles of shore land for water sports, this SRA offers visitors swimming, fishing, and boating. The hills surrounding the lake provide good hiking opportunities."
    },    
    {
		"title" : "Bodie State Historic Park",
		"image": "Fall/bodie.jpg",
		"url": "/?page_id=509",
		"firstline": "Bodie State Historic Park",
		"secondline": "A genuine California gold-mining ghost town"
    }, 
    {
        "title": "Children In Nature",
        "image": "Fall/ChildrenInNature.jpg",
        "url": "/?page_id=24914",
        "firstline": "Children In Nature",
        "secondline": "All California children will be inspired to actively and creatively engage with and appreciate the natural environment."
    },
    {
        "title": "Pacheco SP",
        "image": "Fall/Pacheco-SP.jpg",
        "url": "/?page_id=560",
        "firstline": "Pacheco SP",
        "secondline": "The park has beautiful displays of spring wildflowers, scenic vistas, and outstanding trails for horse-back riding, hiking, and mountain biking."
    },
   {
        "title": "Hungry Valley SVRA",
        "image": "Fall/HungryValley.jpg",
        "url": "/?page_id=405",
        "firstline": "Hungry Valley SVRA",
        "secondline": "This off-highway area is California's second largest off-highway vehicular recreation area, has hills and valleys, grassland, coastal sage scrub and oak woodland."
    }*/

// WINTER

    {
        "title": "Winter Hikes",
        "image": "Winter/LakeValley.jpg",
        "url": "/?page_id=26101",
        "firstline": "Winter Hikes",
        "secondline": "Winter is a great time to take a hike in a California State Park. The air is brisk and clear, making it ideal to discover the variety of landscapes state parks have to offer."
    },
    {
        "title": "Millerton Lake SRA",
        "image": "Winter/MillertonLakeSRA_P0072154.jpg",
        "url": "/?page_id=587",
        "firstline": "Millerton Lake SRA",
        "secondline": "With over 40 miles of shore land for water sports, this SRA offers visitors swimming, fishing, and boating."
     },
    {
        "title": "Mono Lake Tufa SNR",
        "image": "Winter/MonoLake.jpg",
        "url": "/?page_id=514",
        "firstline": "Mono Lake Tufa SNR",
        "secondline": "Mono Lake is a majestic body of water covering about 65 square miles. It is an ancient lake, over 1 million years old -- one of the oldest lakes in North America. It has no outlet."
    },
    {
        "title": "Winter Recreation",
        "image": "Winter/OHMVR-Winter.jpg",
        "url": "/?page_id=1233",
        "firstline": "Winter Recreation",
        "secondline": "At each SNO-PARK, visitors have access to snowcleared parking lots and sanitation facilities. The sites can accommodate self-guided activities such as cross-country skiing, dog sledding, general snow play, and snowmobiling."
    },
    {
        "title": "California State Parks Museum Collections",
        "image": "Fall/emuseum.jpg",
        "url": "http://www.museumcollections.parks.ca.gov",
        "firstline": "California Landscapes from the Boggs Collection",
        "secondline": "We invite you to explore these outstanding examples of historical objects, archaeological specimens, photography, and art."
    },
    {
        "title": "Emerald Bay SP",
        "image": "Winter/Emerald-Bay-SP2.jpg",
        "url": "/?page_id=506",
        "firstline": "Emerald Bay SP",
        "secondline": "The park features Vikingsholm, one of the finest examples of Scandinavian architecture in the western hemisphere and the Tea House on Fannette Island, the only island to be found in all of Lake Tahoe."
    },
    {
        "title": "Morro Strand SB",
        "image": "Winter/MorroStrandSB_P061117b.jpg",
        "url": "/?page_id=593",
        "firstline": "Morro Strand SB",
        "secondline": "This beach is a coastal frontage park featuring outstanding picnic sites. A three-mile stretch of beach connects the southern and northern entrances to the beach."
    },
    {
        "title": "Limekiln SP",
        "image": "Winter/LimekilnSP_P0070647.jpg",
        "url": "/?page_id=577",
        "firstline": "Limekiln SP",
        "secondline": "The park features breathtaking views of the Big Sur Coast, the beauty of the redwoods, the rugged coast and the cultural history of limekilns."
    },
    {
        "title": "Salt Point SP",
        "image": "Winter/SaltPoint.jpg",
        "url": "/?page_id=453",
        "firstline": "Salt Point SP",
        "secondline": "Rocky promontories, panoramic views, kelp-dotted coves, and the dramatic sounds of pounding surf, open grasslands, forested hills, pristine prairies, and pygmy forests- you can experience all of these coastal wonders within."
    }
];



$(document).ready(function() {
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/banners/btn_pause.png)" });
		
		// Show the next image
		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;
			}
		} 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 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
		$("#bannerimg" + activeContainer).css({
			"background-image" : "url(images/banners/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#bannertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#bannerimg" + currentContainer).fadeOut(function () {
			setTimeout(function() {
			    $("#bannertxt").css({ "display": "block" });
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/banners/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
