$(document).ready(function() {
	if ($('#Slides').length > 0) {
		$('#Slides').cycle({ 
		fx: 'fade',
		speed: 1250,
		timeout: 6000, 
		randomizeEffects: false, 
		easing: 'easeInSine',
		next:   '.slideNext', 
		prev:   '.slidePrev',
		pager:  '#slidePager',
		cleartypeNoBg: true
		});
	}
	
	var Revealer = function(relativeParent,absoluteChild,speed) {
		var revealHeight = absoluteChild.height();
		relativeParent.height(revealHeight);
		relativeParent.addClass('hidden');
		this.toggleReveal = function() {
			if (!relativeParent.parent().find(':animated').length) {
				if (relativeParent.css('display')!='none' ) {
	        		relativeParent.animate({height:0}, speed, function() {
						relativeParent.hide();
					});
				} else {
	        		relativeParent.height(0).show().animate({height: revealHeight}, speed);
				}
			}
		};
	};
	
	$('#contact').after($('#contact-form'));
	
	var contactForm = new Revealer(
		$('#contact-form .flow'),				// the relatively positioned container
		$('#contact-form .flow .contents'),		// the absolutely positioned child
		300										// the speed of the animation
	);
	
	$('.contact-link').live('click',function(){
	    $('html, body').animate({scrollTop:0}, 'fast');
        contactForm.toggleReveal();
		return false;
	});
	$('#contact-form button[type="reset"]').live('click',function(){
        contactForm.toggleReveal();
		return false;
    });

	$('#contact-form form').live('submit',function(){
		var values = $(this).serialize();
		$.post('/includes/contact/index.php',values,function(markup) {
			$('#contact-form .contents').html(markup);
		},'html');
		return false;
	});
	
	$('body').addClass('hasJS');
	
	// validate the comment form when it is submitted
	$("#commentForm").validate();
	
	// validate signup form on keyup and submit
	$("#signupForm").validate({
		rules: {
			voorletters: "required",
			achternaam: "required",
			straat: "required",
			nummer: "required",
			postcode: "required",
			plaats: "required",
			functie: "required",
			telefoon: "required",
			extrainfoworkshop: "required",
			organisatie: {
				required: true,
				minlength: 1
			},
			password: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true
			},
			topic: {
				required: "#newsletter:checked",
				minlength: 2
			},
			agree: "required"
		},
		messages: {
			voorletters: "Vul aub uw voorletters in.",
			achternaam: "Vul aub uw achternaam in.",
			functie: "Vul aub uw functie in.",
			straat: "Vul aub uw straat in.",
			nummer: "Vul aub uw straatnummer in.",
			postcode: "Vul aub uw postcode in.",
			plaats: "Vul aub uw plaats in.",
			telefoon: "Vul aub uw telefoonnummer in.",
			extrainfoworkshop: "Beschrijf uw workshop aub",
			organisatie: {
				required: "Vul aub uw organisatie naam in.",
				minlength: "Uw invoer dient minimaal 1 karakter te bevatten."
			},
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long"
			},
			confirm_password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long",
				equalTo: "Please enter the same password as above"
			},
			email: "Vul aub uw emailadres in.",
			agree: "Please accept our policy"
		}
	});
	
	// propose username by combining first- and lastname
	$("#username").focus(function() {
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#password").blur(function() {
		$("#confirm_password").valid();
	});
	
	//code to hide topic selection, disable for demo
	var factuuradres = $("#factuuradres");
	// newsletter topics are optional, hide at first
	var inital = factuuradres.is(":checked");
	var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
	// show when newsletter is checked
	newsletter.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);
	});

	
});