

$(document).ready(function() {
	
	function gotopage(el) {
		$('#content-wrapper div.active').removeClass('active');
		var target = $(el.attr('href'));
		var x = target.data('pos').left;
		var y = target.data('pos').top;
		target.addClass('active');
		$('#content-wrapper').animate({
			left:0-x,
			top:0-y
			}, 1000);

		if(el.attr('rel') != ''){
			// search scrollable
			var target = $(el.attr('href'));
			var scroll = $('div.scrollable-vertical', target);
			var index = $('#' + el.attr('rel'), scroll).index();
			scroll.scrollable().setPage(index);
		}
	}
	
	/* ------------------------------------------------- */
	/* Slideshow init
	/* ------------------------------------------------- */
	$('div.slideshow').cycle();
	
	/* ------------------------------------------------- */
	/* SET ALL WRAPPERS TO VIEWPORT SIZE + RESIZE HANDLERS
	/* ------------------------------------------------- */
	function setWrappers() {
		var active = false;
		var columns = 4;
		var i = 0; // column count
		var k = 0; // row count
		$('div.wrapper').each(function() {
			var viewportWidth = $(window).width();
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
			if(i > (columns-1)) {
				i = 0;
				k++;
			}
			var w = viewportWidth;
			var h = viewportHeight;
			var x = i * w;
			var y = k * h;
			if($(this).hasClass('active')) {
				wrapperX = x;
				wrapperY = y;
				active = true;
				$(this).animate({
					width:w,
					height:h
				});
			}
			$(this).css({
				top:y,
				left:x,
				width:w,
				height:h
			});
			// SAVE NEW OFFSET
			$(this).data('pos', {
				left:x,
				top:y
			});
			i++;
		});
		if(active) $('#content-wrapper').css({
			left:0-wrapperX,
			top:0-wrapperY
			});
	}

	setWrappers();

	var resizeTimer = null;
	$(window).bind('smartresize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(setWrappers, 200);
	});
	/* ------------------------------------------------- */
	/* HOVER HANDLERS NAVIGATIONS
	/* ------------------------------------------------- */
	var closeTimer = null;
	var closeTimeOut = 800;
		
	$('#menu-wrapper #open-close').live('mouseover', function() {
		$('#menu-wrapper ul#menu').stop().animate({
			'left':0
		}, 200);
		$(this).stop().animate({
			'opacity':.5
		}, 200);
	});
		
	$('#menu-wrapper #menu li').live('mouseover', function() {
		if(closeTimer) clearTimeout(closeTimer)
	});
	$('#menu-wrapper #menu li').live('mouseout', function() {
		closeTimer = setTimeout(fadeMenu, closeTimeOut);
	});
		
	function fadeMenu () {
		$('#menu-wrapper ul#menu').stop().animate({
			'left':-200
		}, 50);
		$('#menu-wrapper #open-close').stop().animate({
			'opacity':1
		}, 50);
	}
		
	$('#menu li a').click(function(e) {
		e.preventDefault();
		gotopage($(this));
	});
		
	$('a.button').live("click", function(e) {
		e.preventDefault();
		gotopage($(this));
	});
		
	/* ------------------------------------------------- */
	/* AT START JUMP TO HOME
	/* ------------------------------------------------- */
	gotopage($('#btn-home'));
		
	/* ------------------------------------------------- */
	/* COLORBOX INIT
	/* ------------------------------------------------- */
	$('a.colorbox').colorbox({
		opacity:.8
	});
	
	/* ------------------------------------------------- */
	/* SCROLLABLE INIT
	/* ------------------------------------------------- */
	$('div.scrollable').each(function() {
		if($('div.item', $(this)).size() <= 1) $('a.scroll-right', $(this).parent()).addClass('disabled');
		$(this).scrollable({
			size:1,
			clickable:false,
			api:true
		});
	});
	$('a.scroll-nav').click(function(e) {
		e.preventDefault();
		return false;
	});
	// QUICK FIX TO MAKE BOTH ARROWS INVISIBLE
	$('div.scrollable-vertical').each(function() {
		$(this).scrollable({
			size:1,
			clickable:false,
			vertical:true,
			api:true
		});
	});

	$.easing.custom = function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	}
	$('div.tour-scrollable').scrollable({size:1,easing:'custom', speed:1000});
	/* --------------------------------------------------------- */
	/* VERTICAL ALIGN HANDLER
	/* --------------------------------------------------------- */
	$('.valign').vAlign();
	
	/* --------------------------------------------------------- */
	/* Scrollpane init
	/* --------------------------------------------------------- */
	$('div.scrollpane').jScrollPane({
		scrollbarWidth:14
	});
		
	/* --------------------------------------------------------- */
	/* Make clickable fully clickable
	/* --------------------------------------------------------- */
	function makeClickable() {
		$('.clickable').each(function() {
			$(this).css('cursor','pointer');
			$(this).unbind('click').click(function(e) {
				var a = $('a', $(this));
				if(a.size() > 0) {
					if($(this).hasClass('button')) {
						e.preventDefault();
						gotopage(a);
					} else {
						window.location = a.attr('href');
					}
				}
			});
		});
	}
	makeClickable();
	/* --------------------------------------------------------- */
	/* TOOL TIPS
	/* --------------------------------------------------------- */
	function kalenderHandler () {
		/* ------------------------------------------------- */
		/* KALENDER
	/* ------------------------------------------------- */
		$('ul.kalenderitems li a').each(function() {
			// CHECK FOR TOOLTIP
			if($(this).hasClass('tooltip')) {
				if($(this).hasClass('left-tt')) {
					$(this).tooltip({
						relative:true,
						position:'top right',
						offset:[71,0]
						});
				} else {
					$(this).tooltip({
						relative:true,
						position:'top left',
						offset:[71,0]
						});
				}
			}
			// HOVER CHANGES
			$(this).mouseover(function() {
				var txt = $('div.extra-content-holder' ,$(this).parent()).html();
				$('#extra-content-holder div.extra-content').html(txt);
			});
			
			$(this).mouseout(function() {
				$('#extra-content-holder div.extra-content').html('');
			})
			
		});
	/* ------------------------------------------------- */
	/* AJAX AGENDA
	/* ------------------------------------------------- */
		$('a.call_agenda').unbind('click').click(function(e) {
			e.preventDefault();
			var url = $(this).attr('href');
			$('#ajax-agenda').fadeOut(500,function(){
				$('#ajax-agenda').load(
					url,
					null,
					function(){
						$('#ajax-agenda').fadeIn();
						kalenderHandler();
					}
					);
			});
		});
		$('#kalender div.scrollable').scrollable({
			size:1,
			clickable:false,
			api:true
		});
		$('#kalender div.scrollable').scrollable().setPage($('#agenda-current').index());
	}
	kalenderHandler();
	/* ------------------------------------------------- */
	/* SLIDE LOGO
	/* ------------------------------------------------- */
	$('#logo').hover(function() {
		$(this).animate({
			'top':0
		});
	}, function() {
		$(this).animate({
			'top':-124
		});
	})
	
	$('div.click-handler').click(function() {
		if($(this).parent().hasClass('open')) {
			$(this).parent().animate({'bottom':-75});
			$(this).parent().removeClass('open');
		} else {
			$(this).parent().animate({'bottom':0});
			$(this).parent().addClass('open');
		}
	});
	/* ------------------------------------------------- */
	/* AJAX FAQ
	/* ------------------------------------------------- */
	$('a.call_faq').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		// add loading class
		$('#ajax-faq').parent().parent().addClass('loading');
		$('#ajax-faq').fadeOut(500,function(){
			$('#ajax-faq').load(
				url,
				null,
				function(){
					$('#ajax-faq').fadeIn();
					$('#ajax-faq').parent().parent().removeClass('loading');
					makeClickable();
				}
				);
		});
		$('#ajax-faq-details').load(url.replace('faq','faqdetails'), null, function() { $('a.colorbox', $('#ajax-faq-details')).colorbox(); });
	});
	/* ------------------------------------------------- */
	/* VALIDATION FORM
	/* ------------------------------------------------- */
	if($('#form-faq').size() > 0) {
		$("#form-faq").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#form-faq"
				});
			}
		});
	}
	/* ------------------------------------------------- */
	/* EMPTY FIELDS ON FOCUS
	/* ------------------------------------------------- */
	if($('#form-faq').size() > 0) {
		$('#form-faq input').each(function() {
			$(this).focus(function() {
				if( this.value == this.defaultValue ) {
					this.value = "";
				}
			}).blur(function() {
				if( !this.value.length ) {
					this.value = this.defaultValue;
				}
			});
		});
	}
	/* ------------------------------------------------- */
	/* VALIDATION FORM
	/* ------------------------------------------------- */
		if($('#form-newsletter').size() > 0) {
			$("#form-newsletter").validate({
				submitHandler: function(form) {
					jQuery(form).ajaxSubmit({
						target: "#feedback-newsletter"
					});
				}
			});
		}
	/* ------------------------------------------------- */
	/* EMPTY FIELDS ON FOCUS
	/* ------------------------------------------------- */
		if($('#field-newsletter').size() > 0) {
			$("#field-newsletter").focus(function() {
				if( this.value == this.defaultValue ) {
					this.value = "";
				}
			}).blur(function() {
				if( !this.value.length ) {
					this.value = this.defaultValue;
				}
			});
		}
	/* ------------------------------------------------- */
	/* INIT GMAPS
	/* ------------------------------------------------- */
	//initialize();
		
	/* ------------------------------------------------- */
	/* DATEPICKER
	/* ------------------------------------------------- */
	$.datepicker.setDefaults($.datepicker.regional['nl']);
	$('#datepicker').datepicker({
		minDate:new Date(),
		onSelect:function(dateText, inst) {
			//alert(dateText);
			var url = '/ajax/agenda/' + dateText;
			$('#ajax-agenda').fadeOut(500,function(){
				$('#ajax-agenda').load(
					url,
					null,
					function() {
						$('#ajax-agenda').fadeIn();
						kalenderHandler();
					}
					);
			});
		},
		dateFormat:'yy/mm/dd'
	});
	
	$('a.ui-state-default').click(function(e) { e.preventDefault(); });
	$('a.scroll-nav').click(function(e) { e.preventDefault(); });
	
});

$(window).load(function() {
      initialize();
  });

/* --------------------------------------------------------- */
/* PLUGIN CODE
/* --------------------------------------------------------- */
(function ($) {
	// VERTICALLY ALIGN FUNCTION
	$.fn.vAlign = function() {
		return this.each(function(i){
			var ah = $(this).height();
			var ph = $(this).parent().height();
			var mh = (ph - ah) / 2;
			$(this).css('margin-top', mh);
		});
	};
})(jQuery);

/* --------------------------------------------------------- */
/* GMAP SHIT
/* --------------------------------------------------------- */
function initialize() {
	var myLatlng = new google.maps.LatLng(51.073631210038116, 3.679996132850647);
	var myOptions = {
		zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.HYBRID
	}
	var map = new google.maps.Map(document.getElementById("gmap"), myOptions);
	var marker = new google.maps.Marker({
		position: myLatlng
	});
	// To add the marker to the map, call setMap();
	marker.setMap(map);
}