var loader;
var catcher;
var cycle;
var cur 		= 1;
var maxi 		= 8;
var loading 	= false;
var speed 		= 800;
var timeout 	= 6000;
var activeClass = 'activeSlide'

$(document).ready(function(){
	catcher = $('#show');
	loader = $('#headerLoader');
	loader.hide();
	cycle = setTimeout(function(){loadHeader('next');},timeout);
	$('#nav li').click(function(e){
		clearTimeout(cycle);
		e.preventDefault();
		loadHeader($(this).find('a').html());
	});
	$('#show').hover(function(){
		clearTimeout(cycle);
	},function(){
		cycle = setTimeout(function(){loadHeader('next');},timeout);
	});
});

function loadHeader(elem){

	href = elem == 'next' ? cur+1 : elem;
	href = parseInt((href <= maxi ? href : 1));
	//if(console) console.log(href);
	if(href != cur && !loading){
		loader./*css('opacity',0).*/show();//.animate({opacity:0.4});
		loading = true;
		move();
		$.ajax({
			url: '/nl/loadheader/',
			data: 'itemNr=' + (parseInt(href)),
			success: function(data){
				loading = false;
				cur = href;
				catcher.find('.eyecatcherContent').remove();
				catcher.append(data);
				//return;
				move(false);
				loader.fadeOut();
				$('#nav li a').removeClass(activeClass);
				$('#nav li a:contains('+cur+')').addClass(activeClass);
				cycle = setTimeout(function(){loadHeader('next');},timeout);
			}
		});
	}
}

function move(out){
	if(out == undefined || out){
		//$('#show img').animate({left:'-100%'},{duration:speed});
		//$('#show img').fadeOut();
		$('#show .eyecatcherContent').animate({paddingLeft:'930px'},{duration:speed});
	}else{
		//$('#show img').animate({left:0},{duration:speed});
		$('#show div:first img').fadeOut(1000,function(){$('#show div:first').remove();});
		$('#show div img').fadeIn();
		$('#show .eyecatcherContent').animate({paddingLeft:'15px'},{duration:speed});
	}
}
