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

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

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "",
		"image" : "Caton-Slide-1.jpg",
		"url" : "",
		"firstline" : "Going on",
		"secondline" : "vacation?"
	}, {
		"title" : "Office ",
		"image" : "Caton-Slide-2.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}, {
		"title" : "",
		"image" : "Caton-Slide-3.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}, {
		"title" : "",
		"image" : "Caton-Slide-4.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}
];



jQuery(document).ready(function() {
		
	// Backwards navigation
	jQuery("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	jQuery("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	
	
	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;
			}
		}
	var navigateByID = function(ID){
		currentImg = ID;
	}
		// 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
		jQuery("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		jQuery("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		jQuery("#firstline").html(photoObject.firstline);
		jQuery("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		jQuery("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		jQuery("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				jQuery("#headertxt").css({"display" : "block"});
				animating = false;
			}, 1);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		jQuery("#control").css({ "background-image" : "url(/slider/images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
		
		jQuery('#headernav').find('ul').children().each(function(){	
			jQuery(this).removeClass('active_button').addClass('inactive_button');
		});
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		jQuery('#headernav').find('ul').children().each(function(){	
			jQuery(this).removeClass('active_button').addClass('inactive_button');
		});
		navigate("next");
		jQuery('#headernav').children().find('#'+currentImg).removeClass('inactive_button').addClass('active_button');
	}, slideshowSpeed);
	
	//jQuery('#headernav').children().remove();
	counter = 0;
	ul = jQuery('#headernav').children();
	//ul = jQuery('<ul>');
	for(photo in photos){
		if(currentImg == (counter + 1)){			
			li = jQuery('<li>').attr('id', (counter+1)).addClass('active_button').appendTo(ul);
		}
		else{
			li = jQuery('<li>').attr('id', (counter+1)).addClass('inactive_button').appendTo(ul);
		}
		counter++ ;
	}
	//ul.appendT(jQuery('#headernav'));
	
	jQuery('#headernav').children().children().click(function(){

		clearInterval(interval);
		jQuery('#headernav').find('ul').children().each(function(){	
			jQuery(this).removeClass('active_button').addClass('inactive_button');
		});
		currentImg = jQuery(this).attr("id");
		currentImg --;
		
		navigate("next");
		
		jQuery(this).removeClass('inactive_button').addClass('active_button');
		
		
		interval = setInterval(function() {
			jQuery('#headernav').find('ul').children().each(function(){	
				jQuery(this).removeClass('active_button').addClass('inactive_button');
			});
			navigate("next");
			jQuery('#headernav').children().find('#'+currentImg).removeClass('inactive_button').addClass('active_button');
		}, slideshowSpeed);
		
	});
});
