//Duration between each slide changing
var interval_duration = 4000;

//All the different slides
//Warning:  slides in this object _will_ be scrolled through.  even if there isn't a link to them in the HTML
var slides = {
	"stocktaking": 	{
		"img": "./images/slideshow-stocktaking.jpg", 
		"txt": "Improve stock procedures and make efficiency & service gains", 
		"lnk": "stocktaking.html", 
		"cur": "true",
		"id" : "stocktaking"},
	"mystery": 		{
		"img": "./images/slideshow-mystery.jpg",
		"txt": "Understand your customers and find sales opportunities", 
		"lnk": "mystery-guests.html", 
		"cur": "false",
		"id" : "mystery"},
	"audit": 		{
		"img": "./images/slideshow-audit.jpg", 
		"txt": "Assess, prioritise and address<br />business risks", 
		"lnk": "audit.html", 
		"cur": "false",
		"id" : "audit"},
	"training": 	{
		"img": "./images/slideshow-training.jpg",
		"txt": "Enhance management skills with specialist techniques",
		"lnk": "training.html", 
		"cur": "false",
		"id" : "training"}
};

var busy = false;

var interval_id = false;

$(document).ready(function() {
	//If slides exists, do all this, otherwise don't bother
	if (slides) {
		//Image Preloader
		var preloader = [];
		for (var i in slides) {
			preloader[i] 		= new Image();
			preloader[i].src 	= slides[i].img;
		}
		
		//Slideshow

		interval_id = setInterval("change_slides()", interval_duration);
		
		//Button Click changes everything
		$('#image_menu a').click(function() {
			if (busy == false) {
				$('#image_menu a').each(function() {
					if ($(this).hasClass('active')) {
						slides[$(this).attr('rel')].cur = 'false';
					}
				});
				busy = true;
				var target 	= $(this).attr('rel');
				var bg_img 	= slides[target].img;
				var txt		= slides[target].txt;
				var link	= slides[target].lnk;
				slides[target].cur = 'true';
				$(this).addClass('thisone');
				
				do_change(bg_img, txt, link, 1);
			}
			return false;
		});
	}
	
});

function do_change(bg_img, txt, link, clic) {
	$('.slideshow .content').animate({opacity: '0'}, '300', function() {
		$('#image_menu a').removeClass('active');
		$('#image_menu a.thisone')
			.addClass('active')
			.removeClass('thisone');
			
		$(this).css('background','url("' + bg_img + '") no-repeat scroll left top transparent');
		$(this).children('.replace').html(txt);
		$(this).children('a').attr('href',link);
		
		$(this).animate({opacity: '1'}, '700', function() {
			busy = false;
			clearInterval(interval_id);
			if (clic != 1) {
				interval_id = setInterval("change_slides()", interval_duration);
			}
		});
	});
}

function change_slides() {
	busy = true;
	var set_next = false;
	for (var i in slides) {
		
		l = i;
//			$('#content-left').html(l);
		if (set_next == true) {
			$('#image_menu a').each(function() {
				if ($(this).attr('rel') == l) {
					$(this).addClass('thisone');
				}
			});
			var bg_img 	= slides[l].img;
			var txt		= slides[l].txt;
			var link	= slides[l].lnk;
			slides[l].cur = 'true';
			do_change(bg_img, txt, link, 0);
			break;
//			set_next = false;
		}
		
		if (slides[l].cur == 'true') {
			slides[l].cur = 'false';
			set_next = true;
		} else {
			set_next = false;
		}
		if (set_next == true && l == 'training') {
			l = 'stocktaking';
			$('#image_menu a').each(function() {
				if ($(this).attr('rel') == l) {
					$(this).addClass('thisone');
				}
			});
			var bg_img 	= slides[l].img;
			var txt		= slides[l].txt;
			var link	= slides[l].lnk;
			slides[l].cur = 'true';
			do_change(bg_img, txt, link, 0);
			break;

		}
		
	}
}
