$(document).ready(function()
{
	$('.init-hide').hide();
	
	$('#aanvraag-wrapper').click(function()
	{
		if( $(this).children('p').children('a').length )
		{
			window.location.href = $(this).children('p').children('a').attr('href');
		}
	});
	
	$('#content-select').addClass('no-scroll');
		
	$('input[type=radio]').focus(function()
	{
		if( $(this).next().next().is('.form-el') )
		{
			$(this).next().next().children('textarea').focus();
		}
	});
	
	$('.form-el textarea').focus(function()
	{
		if( !$(this).parent().siblings('input[type=radio]').last().attr('checked') )
		{
			$(this).parent().siblings('input[type=radio]').last().attr('checked', true);
		}
	});
	
	//==
	
	var el = $('#subnav li');
	if(el.length > 5)
	{
		el.hide();
		var index = el.index( $('#subnav li.active') );
		if(index>=5)
		{
			//als active niet op "page 1" is
			
			//als active+5 verder gaat dan de laatste pagina beschikbaar
			if( index+5 >= el.length )
			{
				el.slice(-5).show();
			}
			else
			{
				el.slice(index, index+5).show();
			}
			
			$('#paginate li:first-child').show();
			if( index+5 < el.length )
			{
				$('#paginate li:last-child').show();
			}
		}
		else
		{
			//als active wel op "page 1" is
			el.slice(0,5).show();
			$('#paginate li:last-child').show();
		}
		
		$('#subnav li:visible').first().addClass('first-child');
		$('#subnav li:visible').last().addClass('last-child');
			
		$('#paginate a').click(function()
		{
			var index = el.index( $('#subnav li:visible').first() );
			el.hide();
			
			if($(this).parent().is(':first-child'))
			{
				//PREV
				
				var from = index-1;
				if(from>=0)
				{
					var to = from + 5;
					el.removeClass('first-child last-child').slice(from, to).first().addClass('first-child');
					el.slice(from, to).show().last().addClass('last-child');
				}
				
				if(from===0)
				{
					$(this).parent().hide();
				}
				else if(index+5===el.size())
				{
					$(this).parent().siblings().show();
				}
			}
			else
			{
				//NEXT
				
				var from = index+1;
				if(from<=el.size()-5)
				{
					var to = from+5;
					el.removeClass('first-child last-child').slice(from, to).first().addClass('first-child');
					el.slice(from, to).show().last().addClass('last-child');
				}
				
				if(from===el.size()-5)
				{
					$(this).parent().hide();
				}
				else if(index===0)
				{
					$(this).parent().siblings().show();
				}
			}
			
			return false;
		});
	}
	
	if( $('.home #subnav .active, .producten #subnav .active').length )
	{
		setInterval(function()
		{
			var page = $('#site-wrapper').attr('class');
			var el = $('.'+page+' #subnav li.active').next().not('.empty');
			if( !el.length )
			{
				el = $('.'+page+' #subnav li:first-child');
			}

			el.addClass('active').siblings().removeClass('active');
			el = el.attr('class').split(' ')[0];
			$('#sub-content .'+el).show().siblings().hide();	
		}, 6500);
	}
	
	//als pics geladen zijn
	$(window).load(function()
	{
		$('.cycle').each(function()
		{
			$(this).cycle(
			{ 
				fx:			'fade',
				timeout:	0, 
				next:		$('.next', this.parentNode),
				prev:		$('.prev', this.parentNode),
			});
		});
	});
});
