$(document).ready(function(){
	$('ul.logos-list').fadeGallery({
		IE:false,
		prev : false,
		next : false,
		play : false,
		pause : false,
		thumbnails : false,
		autoplay : 4000,   //autoplay duration for logos
		autoheight:false
	});
	initStepsForm();
	initVideoOverlay();
	initFaq();
	if ($("#contactform").length > 0)
		initValidation("#contactform");
		
	if ($("#incompanyform").length > 0)
		initValidation("#incompanyform");
		
	initProefExamenResult();
	initStudieBepalingenOverlay();
});

jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(this),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('a'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && $.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		// thumbnails
		thumbs.each(function(i){
			$(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}

// initStepsForm
function initStepsForm(){
	var _holder = $('.steps-holder .step'),
		_next = $('a.next', _holder),
		stepsProgress = $('.steps-progress li');
	
	stepsProgress.eq(0).addClass('active');
	_holder.not(':first').hide();
	
}

function initVideoOverlay(){
	$(".video-thumb").click(function(e){
		e.preventDefault();
		var moviecode = $(this).attr("rel");
		
		$("body").overlay({
			window_width: 480,
			window_height: 385,
			close_img: '/images/btn-close.png',
			src: '<div id="widgetVideoBox" style="width: 480px; height: 385px;"></div>',
			callback_ajax: 'setVideo("'+moviecode+'")'
		});
	});
}

function initStudieBepalingenOverlay(){
	$(".voorwaarden-link").click(function(e){
		e.preventDefault();
		var url = "/algemene-voorwaarden/popup-(laten-staan)";
		$("body").overlay({
			window_width: 650,
			window_height: 480,
			close_img: '/images/btn-close.gif',
			src: '<iframe src="'+url+'" width="650" height="480" frameborder="0"></iframe>',
			//callback_ajax: 'setVideo("'+moviecode+'")'
		});
	});
}

function setVideo(code){
	var output = '<object width="480" height="385">'+
		'<param name="movie" value="http://www.youtube.com/v/'+code+'"></param>'+
		'<param name="allowFullScreen" value="true"></param>'+
		'<param name="allowscriptaccess" value="always"></param>'+
		'<embed src="http://www.youtube.com/v/'+code+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed>'+
	'</object>';
	
	$("#widgetVideoBox").html(output);
}

function initFaq(){
	$(".faq-box").find(".faqVraag").click(function(e){
		e.preventDefault();
		$(this).parent().parent().find(".antwoord").slideUp();
		if ($(this).siblings(".antwoord").is(":hidden"))
			$(this).siblings(".antwoord").slideDown();
	});
}

function initProefExamenResult(){
	if ($("#proefexamenAnswerBox").length > 0)
	{
		var totalQuestions = $(".questions-form").find(".row").length;
		var correctAnswers = $(".questions-form").find(".correct-answer").length;
		var errorCount	   = totalQuestions - correctAnswers;
		if (correctAnswers < totalQuestions)
		{
			if (errorCount == 1)
				var resultText = "U had "+ errorCount +" fout gemaakt op "+ totalQuestions +" vragen. Dat is "+ errorCount +" fout teveel om te kunnen slagen";
			else
				var resultText = "U had "+ errorCount +" fouten gemaakt op "+ totalQuestions +" vragen. Dat zijn "+ errorCount +" fouten teveel om te kunnen slagen";
		}
		else
			var resultText = "U heeft 0 fouten gemaakt. Met dit fouten aantal bent u geslaagd.";
			
		$("#proefexamenAnswerBox").html(resultText);
	}
}
