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

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

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "T &amp; R Theakston Limited",
		"image" : "/banner/new1.jpg",
		"url" : "#",
		"firstline" : "&quot;We have often thought that in the years since we regained control of Theakstons in 2003, the best decision we ever made was to secure the services of Kirk Newsholme. They are an outstanding firm of Chartered Accountants staffed by outstanding individuals&quot;",
		"secondline" : "Simon Theakston, Executive Director, <br>T &amp; R Theakston Limited"
	}, {
		"title" : "Gent Visick",
		"image" : "/banner/new7.jpg",
		"url" : "#",
		"firstline" : "&quot;I'm sure I shouldn&rsquo;t admit this in public but I like my accountant! KN have taken the time to understand what we do and provide advice which is tailored to our needs.&quot;",
		"secondline" : "Rupert Visick, <br>Gent Visick"
	}, {
		"title" : "Thomson Limited",
		"image" : "/banner/new2.jpg",
		"url" : "#",
		"firstline" : "&quot;We have been with KN for over a decade now, having previously been with one of the big 4. Our experience with KN has been excellent, all the required expertise with a personal touch&quot;",
		"secondline" : "Richard Thomson, Managing Director, <br>Thomson Limited"
	}, {
		"title" : "The Winteringham Group Limited",
		"image" : "/banner/new3.jpg",
		"url" : "#",
		"firstline" : "&quot;Having previously been with a large national accountancy practice, we felt our company needed a more personal input. Kirk Newsholme has the unique ability to provide the professional expertise of a multinational alongside the close personal attention of an independent&quot;",
		"secondline" : "Joanne Martin, Director, <br>The Winteringham Group Limited"
	}, {
		"title" : "Group Rhodes",
		"image" : "/banner/new4.jpg",
		"url" : "#",
		"firstline" : "&quot;We moved to KN back in 1999 from a national firm. We have enjoyed a first class relationship with them and have benefitted from invariably high standards of service throughout. One of the most pleasing aspects about our work together has been the strong continuity of personnel &ndash; with each contact we build on previous progress, rather than needing to reiterate needs or explain historical issues.&quot;",
		"secondline" : "Mark Ridgway Managing Director, <br>Group Rhodes"
	}, {
		"title" : "T.F &amp; J.H. Braime (Holdings) P.L.C",
		"image" : "/banner/new5.jpg",
		"url" : "#",
		"firstline" : "&quot;Since moving to KN from an international firm in 2009 we have made significant, sustained cost savings. The quality of their staff&rsquo;s technical knowledge has impressed us which is very important as an AIM listed company. The continuity of staff combined with partner involvement, has made us feel more involved in the process and we are very happy with our decision in selecting KN.&quot;",
		"secondline" : "Nicholas Braime, Chairman and <br>Managing Director, T.F &amp; J.H. <br>Braime (Holdings) P.L.C"
	}, {
		"title" : "Pickersgill - Kaye Limited",
		"image" : "/banner/new6.jpg",
		"url" : "#",
		"firstline" : "&quot;We have been delighted with the quality of service from Kirk Newsholme after moving to them from a national firm in 2008. By working consistently with the same team members, we have been able to build highly productive relationships. The same outstanding value for money has been sustained throughout.&quot;",
		"secondline" : "Peter Murphy, Managing Director, <br>Pickersgill - Kaye Limited"
	}
];



$(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/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
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").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
		$("#headerimg" + currentContainer).fadeOut(2000, function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/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);
	
});
