/*************************************************/
/** Background slideshow - jQuery Plugin		**/
/** Author : Marco Kuiper						**/
/** http://www.marcofolio.net/					**/
/*************************************************/
var slideshowSpeed = 6000;
var imagesFolder = 'images/header/';
$(document).ready(function(){
	$('#back_header_nav').click(function(){
		stopAnimation();
		navigate('back');
	});
	$('#next_header_nav').click(function(){
		stopAnimation();
		navigate('next');
	});
	var interval;
	$('#control_header_nav').toggle(function(){
		stopAnimation();
	},
	function(){
		$(this).css({'background':'url(images/bg-rotator/buttons.png) no-repeat -64px 0px'}).attr('title','Pause');
		navigate('next');
		interval = setInterval(function(){
			navigate('next');
		},slideshowSpeed);
	});
	var activeContainer = 1;
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction){
		if(animating){
			return;
		}
		if(direction == 'next'){
			currentImg++;
			if(currentImg == photos.length + 1){
				currentImg = 1;
			}
		}
		else{
			currentImg--;
			if(currentImg == 0){
				currentImg = photos.length;
			}
		}
		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;
		currentZindex--;
		$('#header_image_'+activeContainer).css({
			'display':'block',
			'background':'url('+imagesFolder+photoObject.image+') no-repeat center top',
			'z-index':currentZindex
		});
		$('#header_text').css({'display':'none'});
		$('#header_first_line').html(photoObject.firstline);
		$('#header_second_line').attr('href',photoObject.url).attr('title',photoObject.secondline).html(photoObject.secondline);
		$('#pictured_url').attr('href',photoObject.url_cat).attr('title',photoObject.title).html(photoObject.title);
		$('#header_image_'+currentContainer).fadeOut(function(){
			setTimeout(function(){
				$('#header_text').css({'display':'block'});
				animating = false;
			},500);
		});
	};
	var stopAnimation = function(){
		$('#control_header_nav').css({'background':'url(images/bg-rotator/buttons.png) no-repeat -32px 0px'}).attr('title','Lecture');
		clearInterval(interval);
	};
	navigate('next');
	interval = setInterval(function(){
		navigate('next');
	},slideshowSpeed);
});
