
function nextBlock(){
	newblock = (activeBlock+1)
	if(newblock >= $("#homeslider li").length){
		newblock = 0;	
	}
	activateSmallBlock(newblock);
}

function activateSmallBlock(index){
	$("#homeslider li:eq("+activeBlock+")").removeClass('active').removeClass('hover').find('h2').stop(true).animate({ textIndent:'0px' }, 300);
	activeBlock = index;
	$("#homeslider li:eq("+activeBlock+")").addClass('hover').find('h2').animate({ textIndent:'250px' }, 300, function(){
		$(this).parent().addClass('active');
		$(this).animate({textIndent: '0px' } ,300);
	});
//	$("#smallblocks .first .slider").stop().animate({'top':(index*-133) + 'px'},300)
}

function showlb(){
	$("#lb").fadeIn('fast');	
}

var currentPicture = null;
var oldPicture = null;
function loadImage(){
	//website_images/bovenbalk_'.$rec['intID'].'.jpg
	imageIDS = $('#slideshow').attr('class').split(';');
	oldPicture = currentPicture;
	if(currentPicture == null){
		currentPicture = 0;
	}else{
		currentPicture++;	
		if(currentPicture == imageIDS.length){
			currentPicture = 0;
		}
	}
	
	if($('#image'+currentPicture).length == 0){
		$image = $('<img src="website_images/bovenbalk_'+imageIDS[currentPicture]+'.jpg" id="image'+currentPicture+'" alt="Auto Loaded Image '+currentPicture+'" />')
		$image.load(function(){
			if($('#image'+oldPicture).length == 1){
				$('#image'+oldPicture).fadeTo(400,0, function(){$(this).remove()});
			}
			$(this).fadeTo(400,1);
			setTimeout(loadImage,5000);
		});
		$('#slideshow').append($image);
	}
}

$(document).ready(function(){
	showlb();
	$("#closelb, #lbbg").css('cursor','pointer').click(function(){
		$("#lb").fadeOut('fast');	
		return false;
	})
	$('#contactmodule:not(.jQueried)').addClass('jQueried').maps();
	
	$('#menu > li > a').wrap('<span>');
	
	$('#auto-van-de-week').click(function(){
		window.location = $('base').attr('href') + $(this).find('a').attr('href');
	});		
	
	
	$('#menu > li').hover(function(){
		$(this).find('ul').css('opacity',0.9).stop(true,true).slideDown(200);
	},function(){
		$(this).find('ul').stop(true,true).slideUp(200);
	});
	
	$(".splash-left li").hover(
		function(){ 
			$(this).find('h2').stop().fadeTo(300, 1);
			$(this).find('a').stop().animate({backgroundPosition:'17px 0px'},300);
		},
		function(){ 
			$(this).find('h2').stop().fadeTo(300, 0.7); 
			$(this).find('a').stop().animate({backgroundPosition:'0px 0px'},300);
		}		  
	).click(function(){
		var linkNew = $('base').attr('href') + $(this).find('a').attr('href');
		if($(this).find('a').attr('target') == '_blank'){
			window.open($(this).find('a').attr('href'));
		}else{
			window.location = linkNew;
		}
	}).each(function(){
		$(this).find('h2').stop().fadeTo(0,0.7); 
	});   

	$("#homeslider li").hover(
			function(){ 
				if(!$(this).hasClass('hover')){
					nr = $(this).index();
					activateSmallBlock(nr);
				}
				clearInterval(blockInterval);
			},
			function(){
				if($(this).hasClass('hover')){
					clearInterval(blockInterval);
					blockInterval = setInterval(nextBlock,3000);
				}
			}
	);
	
	activeBlock = 0;
	blockInterval = null;
	activateSmallBlock(0);
	blockInterval = setInterval(nextBlock,3000);
	
	loadImage();
		
	
	$('a.meel').each(function(i) {
		var text = $(this).text();
		var address = text.replace(" op ", "@");
		var address = address.split(' punt ').join('.');
		$(this).attr('href', 'mailto:' + address);
		$(this).text(address);
	});

	$(".ajaxformulier").unbind('submit').submit(function(){
		if($(this).submitted != true){
		$(this).submitted = true;		
			var form = $(this);
			var data = $(this).serialize();
			$(this).find('input, textarea, checkbox, radio, select').each(function(){
				if($(this).attr('title')){
					ar = $(this).attr('name').split('[]');
					naam = ar[0];
					data += '&veldnamen['+encodeURIComponent(naam)+']=' + encodeURIComponent($(this).attr('title'))
				}
			});
			$.ajax({
				type: 	$(this).attr('method'),
				url: 	$(this).attr('action'),
				data: 	data,
				success: function(msg){
					form.submitted = false;
					if(form.attr('target')){
						$('#'+form.attr('target')).html(msg);
					}
				},
				error: function(xhr, ajaxOptions, thrownError){
					alert(xhr.status);
					alert(thrownError);
				}
			});
		}
		return false;
	});	
	
	$(".fotos a:not(.jQueried)").fancybox({
		'showCloseButton'	: true,
		'titlePosition' 	: 'inside'
	}).addClass('jQueried');
});
