
	
	function startAll(){
		showRandomFoto();
		startsponsoren();
		setInterval(checksponsorenPosition,200);
		checksponsorenPosition();
			
	}
	
	
	function checksponsorenPosition(){
		var newpos = 0;
		var widthPerLI = 0;
		$("#sponsoren ul li").each(function(){	
			widthPerLI += $(this).innerHeight(); 
			if(widthPerLI < -1 * parseInt($("#sponsoren ul").css('top'))){
				$("#sponsoren ul").stop();
				newpos = parseInt($("#sponsoren ul").css('top')) + $(this).innerHeight();
				$(this).appendTo("#sponsoren ul");
				$("#sponsoren ul").css('top', newpos+'px');
				startsponsoren();
			}
		})
	}
	
	function startsponsoren(){
		$("#sponsoren ul").stop(); 
		var totalheight = 0;
		$("#sponsoren ul li").each(function(){	totalheight += $(this).innerHeight(); })
		$("#sponsoren ul").stop().animate({ 
			top: "-"+totalheight+"px"
		}, totalheight * 40, "linear");
	}
	
	function showRandomFoto(){
		 $("#fotoload").fadeOut(200, function(){
			 $.ajax({
			   type: 	"GET",
			   url: 	"randompic.php",
			   success: function(msg){
					$("#fotoload div").html(msg);
					newpos = $("#newpic").attr('rel') - -1;
					if($("#foto_container").css('height') != newpos+'px'){
						
						var newheight = $("#content").height() - newpos - 45;
//						$("#sponsoren").animate({'height':(newheight-2)+'px'},500,false);
						//$("#sponsoren_container").animate({'height':newheight+'px'},500,false);
																									   
						$("#foto_container").animate({'height':newpos+'px'},500,false,function(){
							$("#fotoload").fadeIn(200,function(){
								setTimeout(showRandomFoto,6000);
							})
						})	
					}else{
						$("#fotoload").fadeIn(200,function(){
							setTimeout(showRandomFoto,6000);
						})
					}
				}
			})
		});
	}
	
	
	
$(document).ready(function(){
						   
	
	$('.route:not(.jQueried)').addClass('jQueried').maps();
	
	$('#left p:even').css('backgroundColor','#282828')
	$('#menu > li > a').wrap('<span>');
	
	var busyDD = false;
	$('#menu > li').hover(function(){
		busyDD = true;
		$('#menu').css('height','300px');
		if($(this).find('ul').outerWidth() < $(this).outerWidth()){
			$(this).find('ul').css('width', $(this).outerWidth() + 'px');   
		}
		$(this).find('ul').css('opacity',0.9).stop(true,true).slideDown(200);
	},function(){
		busyDD = false;
		if($(this).find('ul').length > 0){
			$(this).find('ul').stop(true,true).slideUp(200, function(){						 
				if(busyDD == false){
					$('#menu').css('height','130px');
				}
			});
		}else{
					$('#menu').css('height','130px');
		}
	});
	
	$('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);
	});


	
	$("#sponsoren ul li").hover(function(){ 
			$("#sponsoren ul").stop(); 
			$(this).addClass('activeLI');
			var currentItem = $(this);
			var stopped = false;
			var widthPerLI = 0;
			$("#sponsoren ul li").each(function(){	
				if($(this).hasClass('activeLI') && stopped == false){
					stopped = true;
					if(parseInt($("#sponsoren ul").css('top')) < (-widthPerLI)){
						var newpos = (parseInt(widthPerLI)-10);
						$("#sponsoren ul").animate({ 
							top: -newpos+"px"
						}, 200);
					}
				}
				widthPerLI += $(this).innerHeight(); 
			});
			}, 
			function(){ 
			$(".activeLI").removeClass('activeLI');
			startsponsoren(); 
		}
	)
	
	
	$(".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;
	});	
	
	
		
	if($.cookie('tickerposition')){
		while($.cookie('tickerfirst') != $("#sponsoren ul li:first").attr('id') && $("#sponsoren ul li#"+$.cookie('tickerfirst')).length > 0){
			$("#sponsoren ul li:first").appendTo("#sponsoren ul");
		}
		$("#sponsoren ul").css('top',$.cookie('tickerposition'));
	}
	
	$("a[rel]").fancybox({
			'zoomSpeedIn':		300, 
			'zoomSpeedOut':		300, 
			'overlayOpacity':	0.7,
			'hideOnContentClick': false
		});				 
	$("a[href^=https://v1.paylogic]").fancybox({
		'type':		'iframe', 
		'width':	780,
		'height':	500,
		'hideOnContentClick': false
	});
	
	
	window.onunload = function(){
			$.cookie('tickerposition', $("#sponsoren ul").css('top'), { path: '/', expires: 1 });
			$.cookie('tickerfirst', $("#sponsoren ul li:first").attr('id'), { path: '/', expires: 1 });	
	}
	
	window.onscroll = function(){
		$("#bg").css({'top' :($(window).scrollTop()) + 'px'});
	}
	
	startAll();
	
});
