var carousellocations = {
	totalItems: null,
	itemWidth: null,
	itemMargin: null,
	totalWidth: null,
	viewportWidth: null,
	easeType: "linear",
	tweenDuration: null,
	durationPerItem: 300,

	init: function (container) {
		if ($(container).length == 0) return;

		// calculate and set variables
		carousellocations.totalItems = $(".location").length;
		carousellocations.itemWidth = $(".location").width();
		carousellocations.itemMargin = $(".location").css('margin-right').split("px");
		carousellocations.itemMargin = Number(carousellocations.itemMargin[0]);
		carousellocations.totalWidth = ((carousellocations.itemWidth + carousellocations.itemMargin) * carousellocations.totalItems);
		carousellocations.viewportWidth = $("#locations .viewport").width();
		carousellocations.tweenDuration = (carousellocations.durationPerItem * carousellocations.totalItems);

		// adjust viewport width
		$(container + " .viewport").css('width', (carousellocations.totalWidth));

		// adds script class to container
		$(container).addClass("script");

		// adds prev&next buttons
		$(container).before("<a href=\"#\" class=\"next_location\">volgende</a>");
		$(container).before("<a href=\"#\" class=\"prev_location\">vorige</a>");

		// events
		$("#locations .viewport").css('left', '0px');
		$("#locations .viewport").animate({ 'left': '-810px' }, { "duration": 3000, easing: 'easeInOutQuart', complete: function() { $(".next_location").addClass('next_location_dis'); }});
		
		
		//$("#locations .viewport").animate({'left' : '0' }, {duration: 3000 });
		$(".next_location").mouseover(function () {
			carousellocations.forward();
		});
		$(".prev_location").mouseover(function () {
			carousellocations.backward();
		});
		$(".next_location, .prev_location").mouseout(function () {
			carousellocations.stop();
		});
	},
	forward: function () {
		$(".prev_location").removeClass('prev_location_dis');
		var xPos = ((Number(carousellocations.totalWidth) - Number(carousellocations.viewportWidth)) - Number(carousellocations.itemMargin));
		$("#locations .viewport").animate({
			'left': '-' + xPos + 'px'
		}, { duration: carousellocations.tweenDuration, easing: carousellocations.easeType, complete: function() { $(".next_location").addClass('next_location_dis'); } });
	},
	backward: function () {
		$(".next_location").removeClass('next_location_dis');
		$("#locations .viewport").animate({
			'left': '0'
		}, { duration: carousellocations.tweenDuration, easing: carousellocations.easeType, complete: function() { $(".prev_location").addClass('prev_location_dis'); } });
	},
	stop: function () {
		$("#locations .viewport").stop();
	}
}

var carouselcases = {
	totalItems: null,
	itemWidth: null,
	itemMargin: null,
	totalWidth: null,
	viewportWidth: null,
	easeType: "linear",
	tweenDuration: null,
	durationPerItem: 300,

	init: function (container) {
		if ($(container).length == 0) return;

		// calculate and set variables
		carouselcases.totalItems = $(".case").length;
		carouselcases.itemWidth = $(".case").width();
		carouselcases.itemMargin = $(".case").css('margin-right').split("px");
		carouselcases.itemMargin = Number(carouselcases.itemMargin[0]);
		carouselcases.totalWidth = ((carouselcases.itemWidth + carouselcases.itemMargin) * carouselcases.totalItems);
		carouselcases.viewportWidth = $("#cases .viewport").width();
		carouselcases.tweenDuration = (carouselcases.durationPerItem * carouselcases.totalItems);

		// adjust viewport width
		$(container + " .viewport").css('width', (carouselcases.totalWidth));

		// adds script class to container
		$(container).addClass("script");

		// adds prev&next buttons
		$(container).before("<a href=\"#\" class=\"next_case\">volgende</a>");
		$(container).before("<a href=\"#\" class=\"prev_case\">vorige</a>");

		// events
		$("#cases .viewport").css('left', '0');
		$("#cases .viewport").animate({
			'left': '-540px'
		}, { "duration": 2000, easing: 'easeInOutQuart', complete: function() { $(".next_case").addClass('next_case_dis'); }});
		
		//$("#locations .viewport").animate({'left' : '0' }, {duration: 3000 });
		$(".next_case").mouseover(function () {
			carouselcases.forward();
		});
		$(".prev_case").mouseover(function () {
			carouselcases.backward();
		});
		$(".next_case, .prev_case").mouseout(function () {
			carouselcases.stop();
		});
	},
	forward: function () {
		$(".prev_case").removeClass('prev_case_dis');
		var xPos = ((Number(carouselcases.totalWidth) - Number(carouselcases.viewportWidth)) - Number(carouselcases.itemMargin));
		$("#cases .viewport").animate({
			'left': '-' + xPos + 'px'
		}, { duration: carouselcases.tweenDuration, easing: carouselcases.easeType, complete: function() { $(".next_case").addClass('next_case_dis');} });
	},
	backward: function () {
		$(".next_case").removeClass('next_case_dis');
		$("#cases .viewport").animate({
			'left': '0'
		}, { duration: carouselcases.tweenDuration, easeing: carouselcases.easeType, complete: function() { $(".prev_case").addClass('prev_case_dis'); }});
	},
	stop: function () {
		$("#cases .viewport").stop();
	}
}

var services = {
	totalItems: null,
	itemWidth: null,
	itemMargin: null,
	totalWidth: null,
	easeType: "easeOutExpo",
	tweenDuration: null,
	durationPerItem: 400,
	direction: null,

	init: function () {
		if ($("#services").length == 0) return;

		// calculate and set variables
		services.totalItems = $("#services div.txt").length;
		services.itemWidth = $("#services div.txt").width();
		services.itemMargin = $("#services div.txt").css('margin-right').split("px");
		services.itemMargin = Number(services.itemMargin[0]);

		var paddingLeft = $("#services div.txt").css('padding-left').split("px");
		var paddingRight = $("#services div.txt").css('padding-right').split("px");
		services.totalWidth = ((services.itemWidth + services.itemMargin) + (paddingLeft[0] + paddingRight[0]) * services.totalItems);
		services.viewportWidth = $("#services").css('width').split("px");
		services.viewportWidth = Number(services.viewportWidth[0]);
		services.tweenDuration = (services.durationPerItem * services.totalItems);

		// adjust viewport width
		$("#services").css('width', (services.totalWidth));

		// adds script class to container
		$("#services").addClass("script");

		// removes special class 'last' (added for styling when javascript is disbled)
		$("#services div.txt.last").removeClass("last");

		// place button
		$("#services").before("<a href=\"#\" class=\"readmore service\">Lees meer</a>");

		// init button
		$(".readmore").click(function () { /* LETOP - de positie staat hier hard in */
			if (services.direction == null || services.direction == "forward") {
				services.direction = "backward";
				$("#services").animate({
					'left': '-855px'
				}, services.tweenDuration, services.easeType);
				$(this).html('terug');
			} else {
				services.direction = "forward";
				$("#services").animate({
					'left': '0px'
				}, services.tweenDuration, services.easeType);
				$(this).html('lees meer');
			}
		});
	}
}

var profiles = {
	totalItems: null,
	itemWidth: null,
	itemMargin: null,
	totalWidth: null,
	easeType: "easeOutExpo",
	tweenDuration: null,
	durationPerItem: 200,
	direction: null,

	init: function () {
		if ($("#profiles").length == 0) return;

		// calculate and set variables
		profiles.totalItems = $("div.profile").length;
		profiles.itemWidth = $("div.profile").width();
		profiles.itemMargin = $("div.profile").css('margin-right').split("px");
		profiles.itemMargin = Number(profiles.itemMargin[0]); /* LET OP - de extra padding (30) staat er hard in */
		profiles.totalWidth = ((profiles.itemWidth + profiles.itemMargin) + 30) * profiles.totalItems;
		profiles.viewportWidth = $(".viewport").css('width').split("px");
		profiles.viewportWidth = Number(profiles.viewportWidth[0]);
		profiles.tweenDuration = (profiles.durationPerItem * profiles.totalItems);

		// adjust viewport width
		$(".viewport").css('width', (profiles.totalWidth));

		// adds script class to container
		$("#profiles").addClass("script");

		($(".facilitators li")).each(function () {
			$(this).click(function () {
				var surname = $(this).attr('class');
				var offset = $('.profile.' + surname).position();
				$(".viewport").stop();
				$(".viewport").animate({
					'left': '-' + offset.left + 'px'
				}, profiles.tweenDuration, profiles.easeType);

				$(".facilitators li").removeClass('active');
				$(this).addClass('active');
			});
		});
	}
}

var pressitems = {
	totalItems: null,
	itemWidth: null,
	itemMargin: null,
	totalWidth: null,
	viewportWidth: null,
	easeType: "linear",
	tweenDuration: null,
	durationPerItem: 300,

	init: function (container) {
		
		$("#press .readmore, #press .thumb").click(function () {
			window.open($(this).attr("href"), 'the playing circle', "resizable=no,toolbar=no,scrollbars=yes,menubar=no,status=no,directories=no,width=1000,height=550,left=100,top=100");
			return false;
		});
		
		if ($(container).length == 0) return;

		// calculate and set variables
		pressitems.totalItems = $(".press-item").length;
		pressitems.itemWidth = $(".press-item").width();
		pressitems.itemMargin = $(".press-item").css('margin-right').split("px");
		pressitems.itemMargin = Number(pressitems.itemMargin[0]);
		pressitems.totalWidth = ((pressitems.itemWidth + pressitems.itemMargin) * pressitems.totalItems);
		pressitems.viewportWidth = $("#press .viewport").width();
		pressitems.tweenDuration = (pressitems.durationPerItem * pressitems.totalItems);

		// adjust viewport width
		$(container + " .viewport").css('width', (pressitems.totalWidth));

		// adds script class to container
		$(container).addClass("script");

		// adds prev&next buttons
		$(container).before("<a href=\"#\" class=\"next_case\">volgende</a>");
		$(container).before("<a href=\"#\" class=\"prev_case\">vorige</a>");

		// events
		$("#press .viewport").css('left', '0');
		$("#press .viewport").animate({
			'left': '-572px'
		}, { "duration": 2000, easing: 'easeInOutQuart', complete: function() { $(".next_case").addClass('next_case_dis'); }});
		
		//$("#locations .viewport").animate({'left' : '0' }, {duration: 3000 });
		$(".next_case").mouseover(function () {
			pressitems.forward();
		});
		$(".prev_case").mouseover(function () {
			pressitems.backward();
		});
		$(".next_case, .prev_case").mouseout(function () {
			pressitems.stop();
		});
	},
	forward: function () {
		$(".prev_case").removeClass('prev_case_dis');
		var xPos = ((Number(pressitems.totalWidth) - Number(pressitems.viewportWidth)) - Number(pressitems.itemMargin));
		$("#press .viewport").animate({
			'left': '-' + xPos + 'px'
		}, { duration: pressitems.tweenDuration, easing: pressitems.easeType, complete: function() { $(".next_case").addClass('next_case_dis');} });
	},
	backward: function () {
		$(".next_case").removeClass('next_case_dis');
		$("#press .viewport").animate({
			'left': '0'
		}, { duration: pressitems.tweenDuration, easeing: pressitems.easeType, complete: function() { $(".prev_case").addClass('prev_case_dis'); }});
	},
	stop: function () {
		$("#press .viewport").stop();
	}
}

var slidesArray = new Array();

function Slideshow() {
	this.childs = null;
	this.total = null;
	this.counter = 1;
	this.fadeInterval = null;
	this.intervalTime = 3500;
	this.transitionTime = 600;
	this.randomize = true;
}

Slideshow.prototype.crossFade = function () {
	if (this.counter == 0) this.counter = this.total;

	$(".slideshow img").removeClass('active');
				
	this.counter--;
	$("img", this.childs[this.counter]).animate({
		opacity: 0
	}, this.transitionTime, "linear");

	if ((this.counter - 1) == -1) {
		// startover
		$("img", this.childs[this.total - 1]).animate({
			opacity: 1
		}, this.transitionTime, "linear").addClass('active');

	} else {
		// continue
		$("img", this.childs[this.counter - 1]).animate({
			opacity: 1
		}, this.transitionTime, "linear").addClass('active');
	}
	$("#tooltip").html(''+$('img.active').attr('alt'));
	$('.slideshow li:last a').attr('href',$('img.active').parent().attr('href'));
};

var sldeshows = {
	init: function () {
		var lastHref = $(".slideshow li:last a").attr('href');
		//var loc_name = $('img.active').attr('alt');
		$("#tooltip").html(''+$(".slideshow li:last img").attr('alt'));
		
		$(".slideshow li:last a").click(function(e){						
			if($(".slideshow li:last img.active").length > 0) {
				e.preventDefault();
				$('.slideshow li:last a').attr('href',lastHref);
			}	
		});
		
		
		if ($(".slideshow").length == 0) return;

		if ($(".slideshow").length > 1) {
			$('.slideshow li img').css("opacity", 1);
		} else {
			$('.slideshow li img').css("opacity", 0);
		}

		$('.slideshow').each(function () {
			var slideshow = new Slideshow;
			slideshow.childs = $(this).children();
			slideshow.total = slideshow.childs.length;
			slideshow.counter = slideshow.total;

			if (slideshow.childs.length > 1) {
				if ($(this).hasClass('wait')) {
					function wait() {
						slideshow.fadeInterval = setInterval(function () {
							slideshow.crossFade();
						}, slideshow.intervalTime);
						clearInterval(waitInterval);
					}
					var waitInterval = setInterval(function () {
						wait();
					}, (slideshow.intervalTime / 2));
				} else {
					slideshow.fadeInterval = setInterval(function () {
						slideshow.crossFade();
					}, slideshow.intervalTime);
				}
			}

			$("img", slideshow.childs[slideshow.total - 1]).animate({
				opacity: 1
			}, 0, "linear");
			
			slidesArray.push(slideshow);
		});
	}
};

var menu = {
	init: function () {

		$("#main-nav li:not(.active) a").hover(

		function () {
			var arrBg = $(this).stop().css('backgroundPosition').split(" ");
			$(this).animate({
				backgroundPosition: '(' + arrBg[0] + ' 0px)'
			}, 150);

		}, function () {
			var arrBg = $(this).stop().css('backgroundPosition').split(" ");
			$(this).animate({
				backgroundPosition: '(' + arrBg[0] + ' -12px)'
			}, 150);
		});

	}
}

/* E-mail validation */

function validateEmail(email) {
  var reg = /^([A-Za-z0-9\'_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email) == false) {
	  return false;
  } else {
	  return true;
  }
}

var formsend = {
	init: function () {
		$('#contact_form input.submit_btn').click(function(e){
			// Loop alle velden met class required en check of ze zijn ingevuld
			if($('input#name').val() == '' || $('input#company').val() == '' || $('input#subject').val() == '' || $('input#phone').val() == '' || $('input#email').val() == '' || $('#message').val() == '') {
				$('span.error').css('display', 'block').html('Niet alle verplichte velden zijn ingevuld.');
				e.preventDefault();
			}
			
			if(!validateEmail($('input#email').val())){
				$('span.error').css('display', 'block').html('Dit is geen geldig e-mailadres.');
				e.preventDefault();
			}
		});
		
		$('#loc_offerte input.submit_btn').click(function(e){
			var d_height = $('#loc_offerte').css('height');
			if($('#loc_offerte input#name').val() == '' || $('#loc_offerte input#company').val() == '' || $('#loc_offerte input#email').val() == '' || $('#message').val() == '') {
				$('#loc_offerte span.error').css('display', 'block').html('Niet alle verplichte velden zijn ingevuld.');
				e.preventDefault();
			} else if(!validateEmail($('#loc_offerte input#email').val())){
				$('#loc_offerte span.error').css('display', 'block').html('Dit is geen geldig e-mailadres.');
				e.preventDefault();
			}
		});
	}
}

$(document).ready(function () {
	sldeshows.init();
	carousellocations.init("#locations");
	carouselcases.init("#cases");
	services.init();
	profiles.init();
	pressitems.init("#press");
	menu.init();
	formsend.init();	
	
	$(".back").click(function (ev) {
		history.back();
		ev.preventDefault();
	})

	$('.locationslider').cycle({
		prev: '#prev',
		next: '#next'
	});
});
