jQuery(function( $ ){	
	var urls = [ 'carrousel_01_act', 'carrousel_02_act', 'carrousel_03_act' ];
	$.preload( urls, {
		base:'../img/',
		ext:'.jpg'
	});
});

$(document).ready(function() {	
	
	var huidiggal = 1;
	
	$("#forminloggen").each( function(){

		$(this).validate({
			rules: {
				inlognaam: {
					required: true,
					minlength: 4,
					maxlength: 20
				},
				ww3: {
					required: true,
					minlength: 5,
					maxlength: 15
				}
			},
			messages: {
				inlognaam: {
					required: "Vul je gebruikersnaam in.",
					minlength: "De gebruikersnaam moet bestaan uit minimaal 4 tekens.",
					maxlength: "De gebruikersnaam mag bestaan uit maximaal 20 tekens."
				},
				ww3:  {
					required: "Vul een geldig wachtwoord in.",
					minlength: "Het wachtwoord moet bestaan uit minimaal 5 tekens.",
					maxlength: "Het wachtwoord mag bestaan uit maximaal 15 tekens."
				}
			},
			errorPlacement: function(error, element) {
				element.parent().next().next().html(error);
			}
		});
	});
	
	$("#car").everyTime(7000,'controlled',function(i) {
		if(huidiggal == 3){
			huidiggal = 1;
		} else {
			huidiggal++;
		}
		$(".caritem.act").removeClass("act");
		$(".caritem#caritem"+huidiggal).addClass("act");
		$(".car_main_content").hide();
		$("#carinhoud"+huidiggal).fadeIn(300);
	});
	
	
	
	$(".caritem").click(function() {
		$("#car").stopTime('controlled');
		$(this).addClass("act");
		$(".caritem.act").removeClass("act");
	});
	
	$("#caritem1").click(function() {
		$(this).addClass("act");
		$(".car_main_content").hide();
		$("#carinhoud1").fadeIn(300);
	});
	
	$("#caritem2").click(function() {
		$(this).addClass("act");
		$(".car_main_content").hide();
		$("#carinhoud2").fadeIn(300);
	});
	
	$("#caritem3").click(function() {
		$(this).addClass("act");
		$(".car_main_content").hide();
		$("#carinhoud3").fadeIn(300);
	});
	
	$("#magazine_menuoverzicht tr").hover(function() {
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
	
	$("#bekijkallereacties").click(function() {
		$(this).hide();
		$("#klapin").show();
		$(".react").slideDown(500, "easeOutElastic");
	});
	
	$("#klapin").click(function() {
		$(this).hide();
		$("#bekijkallereacties").show();
		$(".inklap").slideUp(500, "easeOutCirc");
	});
	
	// reacties ================================================
	$('textarea#beschrijving').each(function(){ 
		$(this).val("");
		var tekens = 500;
		var length = $(this).val().length;  
		$(this).keyup(function(){  
			var new_length = $(this).val().length;  
			$('#aantaltekens').html(new_length);  
			var teller = tekens - $(this).val().length; 
			$(this).next('span').html(teller);						
				if(teller < 0){
					$(this).blur();
					$('#aantaltekens').css("color","#000000");
					$('#plaatsreactie').hide();
					teller = 0;
				} else {
					$('#aantaltekens').css("color","#476a00");
					$('#plaatsreactie').show();
				}
		});
		$(this).focus(function(){  
			$("#plaatsreactie").show();
			$('#aantaltekens').html("&nbsp;");
			var new_length = $(this).val().length;  
			$('#aantaltekens').html(new_length);  
		});
	});
	
	$("#plaatsreactie").click(function() {
		window.clearTimeout();
		var contents = $("#beschrijving").val();
		var pagina = $("#pag").val();
		var rand = $("#rand").val();
		var fout = false;
		if(contents == '' || contents.length < 9) {
			var fout = true;
			$('#aantaltekens').hide();
			$("#aantaltekens").html("Je hebt geen of te weinig tekst ingevuld.");
			$('#aantaltekens').css("color","#000000");
			$('#aantaltekens').fadeIn(200, function(){
				$('#aantaltekens').fadeOut(200, function(){
					$('#aantaltekens').fadeIn(200, function(){
						window.setTimeout(function() {
							$('#aantaltekens').fadeOut(300);
						}, 3000);
					});
				});
			});
		} else {
			$(this).hide();
			$('#aantaltekens').show();
			$("#aantaltekens").html("<img src='../img/laden2.gif' alt='' title='' style='vertical-align: center;'> Een moment geduld..");
			$.post("_plaatsreactie.php",
			   { pag: pagina, beschrijving: contents, r: rand},
				function(data){
					$("#reageerblok").fadeOut(100, function(){
						$("#feedbackblok").fadeIn(400);
					});
				}
			);
		}
		return false;
	});
	
	// zoeken ============================================================
	
	$("input#zoek").focus(function(){  
		$(this).val("");
	});
	
	// scroll producten ==================================================
	
	$('#productscroll_content').serialScroll({
		items:'img',
		prev:'#productscrollpijl1',
		next:'#productscrollpijl2',
		//offset: 50, //when scrolling to photo, stop 230 before reaching it (from the left)
		//start:1, //as we are centering it, start at the 2nd
		duration:1200,
		axis: 'x',
		force:true,
		stop:true,
		lock:false,
		cycle:false, 
		lazy:true,
		constant: false,
		easing:'easeOutQuad' //use this easing equation for a funny effect
		//jump: true //click on the images to scroll to them
	});

	$(".productlijstimg").click(function() {
		$('#productinfo .margin20').html('<p style="text-align: center;"><img src="../img/laden2.gif" /></p>');
		$(".productlijstimg.aan").removeClass("aan");
		$(this).addClass("aan");
		var keuzeprod = $(this).attr('id');
		$("#productinfo .margin20").load("producten_info.php?prodid="+keuzeprod+"&ms=" + new Date().getTime(), function() {
			Cufon.refresh('h1');
			Cufon.refresh('h2');
		});

	});
	
	
	
	
	
	
	
});