// Speed of the automatic slideshow
var slideshowSpeed = 7000;
var learnMoreImg = "<img src='./?a=73279' alt='Learn More' height='24' width='92'/> "
var learnMoreImgHover = "<img src='./?a=73280' alt='Learn More' height='24' width='92'/>"

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "About Us",
		"image" : './?a=73273',
		"url" : "http://new.dpi.vic.gov.au",
		"firstline" : "Welcome to the <br /> Department of Primary Industries",
		"secondline" : "Working with Victoria’s primary and energy industries to face the challenges and opportunities of the future",
		"more": learnMoreImg
	}, {
		"title" : "Agriculture",
		"image" : './?a=73274',
		"url" : "http://new.dpi.vic.gov.au/agriculture",
		"firstline" : "Agriculture",
		"secondline" : "Maintaining competitive, productive and sustainable farm businesses",
		"more": learnMoreImg
	}, {
		"title" : "Earth Resources",
		"image" : './?a=73275',
		"url" : "http://new.dpi.vic.gov.au/earth-resources",
		"firstline" : "Earth Resources",
		"secondline" : "Delivering a thriving, innovative, responsible earth resources sector",
		"more": learnMoreImg
	}, {
		"title" : "Energy",
		"image" : './?a=73276',
		"url" : "http://new.dpi.vic.gov.au/energy",
		"firstline" : "Energy",
		"secondline" : "Working with industry to develop a sustainable, reliable energy future",
		"more": learnMoreImg
	}, {
		"title" : "Fisheries",
		"image" : './?a=73277',
		"url" : "http://new.dpi.vic.gov.au/fisheries",
		"firstline" : "Fisheries",
		"secondline" : "Securing, sharing and growing our fisheries",
		"more": learnMoreImg
	}
	, {
		"title" : "Forestry",
		"image" : './?a=73278',
		"url" : "http://new.dpi.vic.gov.au/forestry",
		"firstline" : "Forestry",
		"secondline" : "Facilitating productive, competitive and sustainable forest industries",
		"more": learnMoreImg
	}
];


$(document).ready(function() {

	//Click on Menu to view slide
		$("#banner-menu").click(function(evt) {
			currentImg = (evt.target.name);
			//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;
			}
		}
		
		// 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;
		if(currentZindex == 0) {
			currentZindex = 1;
		}
		// 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(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		//$("#headertxt").fadeOut();
		
		// Set the new header text
		$(".firstline").html(photoObject.firstline);
		$(".secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$(".learn-more-button")
			.attr("href", photoObject.url)
			.html(photoObject.more);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				//$("#headertxt").css({"display" : "block"});
				$("#headertxt").fadeIn();
				animating = false;
			}, 200);
		});
	};
	
	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);
	
});
