var	imgWidth;
var imgFadeWidth;
var slideSpeed = 500;
var slideshowInterval;




$(document).ready(function() {
	
	$("a.fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic'	
	});
	
	setSlideshowInterval();
	startSlideshow(".slideshow-logo",7000,13000);
	startSlideshow(".slideshow-ourpeople",1000,7000);

	$("#splash #slider ul li.1 .text").show();
	//Splash image width
	imgWidth = $("#splash #slider ul li img").width();
	
	//Set splash variables and output two prev splashes
	var splashCnt = $("#splash #slider ul li").size();
	var splashCurr = 1;
	
	$("#splash #slider ul li." + splashCnt).clone().prependTo("#splash #slider ul");
	$("#splash #slider ul li." + (splashCnt - 1)).clone().prependTo("#splash #slider ul");
	var splashCntTotal = $("#splash #slider ul li").size();
	
	// Inital splash resize when page loads
	resizeSplash(imgWidth);
	
	// Set interval to flick through splashes
	var nextSplashInterval = 0;
	//nextSplashInterval = setInterval ( "nextSplash()", 5000 );
	

	// Next/Previous splash buttons
	$("#prevBtn").click(function(){ clearInterval(slideshowInterval); prevSplash(); });
	$("#nextBtn").click(function(){	clearInterval(slideshowInterval); nextSplash(); });
	
	// Next/Previous splash functions
	function prevSplash() {
		setSlideshowInterval();
		$("#splash #slider ul li." + splashCurr + " .text").slideUp('slow', function() { 
			// Delete last splash
			$("#splash #slider ul li:last").remove();
			
			// Set new current splash no
			splashCurr--;
			splashCurr = (!splashCurr ? splashCnt : splashCurr);
			
			// Get next splash ready
			loadSplash = splashCurr - 2;
			loadSplash = (loadSplash <= 0 ? loadSplash + splashCnt : loadSplash);
			$("#splash #slider ul li." + loadSplash).clone().prependTo("#splash #slider ul");
	
			// Animate splash slide
			$("#splash #slider ul li").animate({
			  "left": "+=" + imgWidth + "px"
			}, slideSpeed, function() {
				$("#splash #slider ul li:first-child").css("left",imgFadeWidth + "px");
				$("#splash #slider ul li." + splashCurr + " .text").slideDown('slow');
			});	
		});
	}
	function nextSplash() {
		setSlideshowInterval();
		$("#splash #slider ul li." + splashCurr + " .text").slideUp('slow', function() { 
			// Delete first splash
			$("#splash #slider ul li:first").remove();
			
			// Set new current splash no
			splashCurr++;
			splashCurr = (splashCurr > splashCnt ? 1 : splashCurr);
			
			// Get next splash ready
			loadSplash = splashCurr;
			loadSplash--;
			loadSplash = (loadSplash == 0 ? splashCnt : loadSplash);
			var splashLoc = ((splashCnt + 1) * imgWidth) + imgFadeWidth + imgWidth;
			$("#splash #slider ul li." + loadSplash).clone().appendTo("#splash #slider ul").css("left",splashLoc);
	
			// Animate splash slide
			$("#splash #slider ul li").animate({
			  "left": "-=" + imgWidth + "px"
			}, slideSpeed, function () {
				$("#splash #slider ul li." + splashCurr + " .text").slideDown('slow');
			});	
		});	
	}
	
	function setSlideshowInterval() {
		clearInterval(slideshowInterval);
		slideshowInterval = setInterval ( nextSplash, 7000 );
	}
	
});

$(window).resize(function() {
	//Resize splash if change browser window size
	imgWidth = $("#splash #slider ul li img").width();	
	resizeSplash(imgWidth);
});

/*******************************************************************************************************************************************************************************************/

function startSlideshow(e,speed,interval) {
    //$(e + ' img:gt(0)').hide();
	$(e + ' img').hide();
	$(e + ' img.active').show();
    setInterval(function(){			
      $(e + ' :first-child').next('img').show();
	  $(e + ' :first-child').fadeOut(speed)
         .next('img').fadeIn(speed)
         .end().appendTo(e);}, 
      interval);
}

//Resize splash according to browser window size
function resizeSplash(imgWidth) {
	var windowWidth;
	var fadeWidth;
	var cssLeft;
	
	windowWidth = $(window).width();
	$("#splash #slider").width(windowWidth);
	
	imgFadeWidth = (windowWidth - imgWidth) / 2 - (imgWidth * 2);
	
	$("#splash #slider").center();
	
	$("#splash #slider ul li").each(function(index) {	
		cssLeft = (index * imgWidth) + imgFadeWidth;
		$(this).css("left",cssLeft);
	});
	
	fadeWidth = (windowWidth - imgWidth) / 2 - $("#splash .btns li a").width();
	$("#splash .overlay").width(fadeWidth);
}

//Center jquery function
jQuery.fn.center = function () {
    this.css("position","absolute");
    //this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}
