function doslide(thisitem){
	
	// Set new background for the static div below
	$('#content-visible').removeClass();
	$('#content-visible').addClass(thisitem);
	
	// Copy content from animated div to the static div below
	$('#content-visible div').remove();
	$('#content-' + thisitem + ' div').clone().appendTo('#content-visible');
		
	// Remove inline style added to element by animate
	$('#content-' + thisitem).removeAttr('style');

}

function slideonclick(thisitem,selectedid,theSlides){
	
	// Move border to relevant colour key in tab
	$('#tab div.item-border').removeClass('on');
	$('#tab div#tab-' + thisitem).addClass('on');
	
	// Highlight selected option in side panel
	$('#panel li').removeClass();
	$('#link-' + thisitem).addClass('on');

	var conpos = $('#slider-container').offset(); // Get the position of the slider container
	var lipos = $('#link-' + thisitem + ' a').offset(); // Get the position of the clicked panel link
	var bgpos = parseInt((lipos.top - conpos.top) - 242); // Calculate required position of panel background in relation to clicked panel link
	$('#panel').css('background-position','0 ' + bgpos + 'px');
	
	// Move all divs to bottom and bring newest animating div to top
	// This prevents links above the current animating div to begin animation 'under' the other div. 
	$('.content-mover').css('z-index','0');
	$('#content-' + thisitem).css('z-index','90');
	
	// Animate hidden div with content into position
	$('#content-' + thisitem).animate({
		left: '0px'
		}, 1200, 'swing', function(){
			doslide(thisitem)
		});
	
}

function getcurrentslide(thisitemid,theSlides){
	for (var x=0;x<theSlides.length;x++){
		if (thisitemid == theSlides[x]){
			var slideid = x;
		}
	}
	return slideid;
}

function triggerslide(){
	selectedid++;
	if (selectedid>9){
		selectedid=0;
	}
	slideonclick(theSlides[selectedid],selectedid,theSlides);
}

function starttimer(){
	tid = setInterval(triggerslide, 9000);
}


var theSlides = new Array("welcome","training","cdm","im","roofSafety","fire","asbestos","environmental","firstAid");
var selectedid = 0;
var thisitem = "";
var tid = false;

$(document).ready(function(){

	selectedid = 0;
	thisitem = theSlides[selectedid];
	
	starttimer();
	
	// Remove links from panel if javascript is on
	$('.panel-link').removeAttr('href');
		
	$('.panel-link').click(function(){
		// Set selected section variable
		thisitem = this.title;
		
		selectedid = getcurrentslide(thisitem,theSlides);
		
		clearInterval(tid);
		
		slideonclick(thisitem,selectedid,theSlides);
		starttimer();
	});

});

