
$(function(){

	// Home header slider
	if ($('#introslider').length > 0) {
		$(window).load(function(){
			var intro = $('#introslider');
			var items = intro.find('>ul>li');
			var currentIndex = 0;
			var timer;
			var nav = $('<div class="nav"></div>').appendTo(intro);
			var animating = false;

			for (var i=0; i<items.length; i++) {
				nav.append('<span></span>');
			}
			nav.find('span').each(function(i){
				$(this).data('idx',i);
				if (i == 0) $(this).addClass('active');
			}).click(function(){
						switchTo($(this).data('idx'));
					});

			function getNextIndex() {
				return (currentIndex == items.length-1) ? 0 : currentIndex+1;
			}

			function switchTo(idx) {
				if (animating || idx == currentIndex) return;
				animating = true;
				var itemOld = $(items[currentIndex]);
				var itemNew = $(items[idx]);

				itemNew.css('zIndex',5).show();
				itemOld.css('zIndex',6).animate({
					opacity: 0
				},function(){
					$(this).css({
						display: 'none',
						opacity: 1
					});
					animating = false;
					resetTimer();
				});

				currentIndex = idx;
				nav.find('span').removeClass('active').filter(':eq('+idx+')').addClass('active');
			}

			function resetTimer () {
				clearInterval(timer);
				timer = setInterval(function(){
					switchTo(getNextIndex());
				},5000);
			}

			function stopTimer () {
				clearInterval(timer);
			}

			resetTimer();

		});
	}

	$('#introslider li>a').append('<span></span>');

	$('#intro').append('<span class="__menudivider1"></span><span class="__menudivider2"></span>');


	// Header menu
	$('#mainmenu>li>a').hover(function(e){
		var li = $(this).parent('li');
		if (li.hasClass('active')) return this;
		if (e.type == 'mouseenter') {
			$(this).stop(true,false).animate({
				height: '48',
				paddingTop: '19',
				backgroundPosition: '32px 25px'
			},250,'swing');
			li.stop(true,false).animate({
				paddingTop: '0',
				height: '67',
				backgroundPosition: '0 0'
			},250,'swing');
		} else {
			$(this).stop(true,false).animate({
				height: '37',
				paddingTop: '10',
				backgroundPosition: '32px 16px'
			},250,'swing');
			li.stop(true,false).animate({
				paddingTop: '20',
				height: '47',
				backgroundPosition: '-16px 20px'
			},250,'swing');
		}
	});


	// Projectslider
	if ($('#projectslider').length > 0) {
		$(window).load(function(){
			var slider = $('#projectslider');
			var items = slider.find('>ul>li');
			var currentIndex = 0;
			var timer;
			var slideWidth = slider.hasClass('small') ? 280 : 410;
			var navPrev = $('<span class="__prev"></span>').appendTo(slider);
			var navNext = $('<span class="__next"></span>').appendTo(slider);
			var animating = false;

			navPrev.click(switchToPrev);
			navNext.click(switchToNext);

			function switchToNext() {
				var idx = (currentIndex == items.length-1) ? 0 : currentIndex+1;
				switchTo(idx,'next');
			}

			function switchToPrev() {
				var idx = (currentIndex == 0) ? items.length-1 : currentIndex-1;
				switchTo(idx,'prev');
			}

			function switchTo(idx,dir) {
				if (animating || idx == currentIndex) return;
				animating = true;
				var itemOld = $(items[currentIndex]);
				var itemNew = $(items[idx]);

				itemNew.show().css({
					left: dir=='next' ? slideWidth : -slideWidth
				}).animate({
							left: 0
						},500,'easeInOutExpo');
				itemOld.animate({
					left: dir=='next' ? -slideWidth : slideWidth
				},500,'easeInOutExpo',function(){
					animating = false;
					resetTimer();
				});

				currentIndex = idx;
			}

			function resetTimer () {
				clearInterval(timer);
				timer = setInterval(function(){
					switchToNext();
				},5000);
			}

			function stopTimer () {
				clearInterval(timer);
			}

			resetTimer();

		});
	}

	// Generic carroussel
	$(window).load(function(){
		$('div.carroussel').addClass('js').each(function(){
			var slider = $(this);
			var items = slider.find('>ul>li');
			var currentIndex = 0;
			var timer;
			var slideWidth = slider.width();
			var slideHeight = 0;

			items.each(function(){
				var h = $(this).height();
				if (h>slideHeight) slideHeight = h;
			});

			slider.height(slideHeight);

			if (items.length < 2) return this;

			var navPrev = $('<span class="__prev"></span>').appendTo(slider);
			var navNext = $('<span class="__next"></span>').appendTo(slider);
			var animating = false;

			navPrev.click(switchToPrev);
			navNext.click(switchToNext);

			function switchToNext() {
				var idx = (currentIndex == items.length-1) ? 0 : currentIndex+1;
				switchTo(idx,'next');
			}

			function switchToPrev() {
				var idx = (currentIndex == 0) ? items.length-1 : currentIndex-1;
				switchTo(idx,'prev');
			}

			function switchTo(idx,dir) {
				if (animating || idx == currentIndex) return;
				animating = true;
				var itemOld = $(items[currentIndex]);
				var itemNew = $(items[idx]);

				itemNew.show().css({
					left: dir=='next' ? slideWidth : -slideWidth
				}).animate({
							left: 0
						},500,'easeInOutExpo');
				itemOld.animate({
					left: dir=='next' ? -slideWidth : slideWidth
				},500,'easeInOutExpo',function(){
					animating = false;
					resetTimer();
				});

				currentIndex = idx;
			}

			function resetTimer () {
				clearInterval(timer);
				timer = setInterval(function(){
					switchToNext();
				},5000);
			}

			function stopTimer () {
				clearInterval(timer);
			}

			resetTimer();
		});
	});

	// More info labels
	$('.more-info,.more-info-english')
			.each(function(){
				$(this).html('<span class="__content">'+$(this).html()+'</span>');
				var s = $(this).find('>span.__content');
				s.data('height',s.height());
				$(this).append('<span class="__moreinfolabel"><span>'+($(this).hasClass('more-info-english')?'More info':'Meer info')+'</span></span>')
			})
			.addClass('js')
			.find('span.__moreinfolabel')
			.click(function(){
				var p = $(this).parent();
				var s = p.find('>span.__content');
				var active = p.hasClass('active');
				s.animate({
					height: active ? 0 : s.data('height') +15,
					marginTop: active ? 0 : 15
				},300,function(){
					p.toggleClass('active');
					if (p.hasClass('more-info-english')) {
						p.find('span.__moreinfolabel>span').text(p.hasClass('active')?'Close':'More info');
					} else {
						p.find('span.__moreinfolabel>span').text(p.hasClass('active')?'Sluiten':'Meer info');
					}
				});
			});


	// Timeline
	if ($('#timeline').length) {
		var timeline = $('#timeline');
		var pointsrc = timeline.find('>div');

		// fix overflow
		timeline.parent('div.contentblock').css('overflow','visible');

		var baseLeft = 41;
		var yearWidth = 102;
		var startYear = 2008;

		pointsrc.each(function(){
			var point = $(this).data('point');
			var pointData = point.split('-');
			var month = parseInt(pointData[0],10);
			var year = parseInt(pointData[1],10);
			var current = $(this).data('current') ? true : false;

			var pointLeft = baseLeft + ((year-startYear) * yearWidth) + (month/12 * yearWidth);

			var pointEle = $('<div class="point'+(current?' current':'')+'"/>');
			pointEle.css({
				left: pointLeft,
				top: 35
			}).data('html',$(this).html());

			if (current && $(this).data('now')) {
				var currentData = $(this).data('now').split('-');
				var currentMonth = parseInt(currentData[0],10);
				var currentYear = parseInt(currentData[1],10);

				var currentLeft = baseLeft + ((currentYear-startYear) * yearWidth) + (currentMonth/12 * yearWidth);

				var progressEle = $('<div class="progress"/>');
				progressEle.css({
					left: pointLeft,
					top: 38
				});

				// Wait a bit with animating progress to prevent choppiness
				$(window).load(function(){
					progressEle.appendTo(timeline).delay(500).animate({
						width: (currentLeft - pointLeft)
					},2000,'swing');
				});
			}

			pointEle.appendTo(timeline).hover(function(e){
				var popupEle = timeline.find('div.popup');
				if (popupEle.length == 0)
					popupEle = $('<div class="popup"><div></div><span></span></div>').appendTo(timeline);

				if (e.type == 'mouseenter') {
					popupEle.find('>div').html($(this).data('html'));
					popupEle.stop().css({
						left: $(this).css('left'),
						bottom: 50,
						opacity: 0,
						display: 'block'
					}).animate({
						bottom: 45,
						opacity: 1
					},300);
				} else {
					popupEle.stop().hide();
				}
			});
		});
	}

	// Add JS print button
	$('<span id="print" title="Pagina afdrukken"></span>').click(function(){
		window.print();
	}).prependTo('div.contentblock.main');

	// Video overlays
	if ($.fancybox) {
		$('a.video').fancybox({
			type: 'iframe',
			width: 560,
			height: 345,
			overlayColor: '#000',
			titleShow: false
		});
	}
	$('a.video').append('<span></span>');

	// Lazy load video's
	$(window).load(function(){
		$('div.video-load').each(function(){
			var ytid = $(this).data('youtubeId');
			var width = $(this).width();
			var height = $(this).height();
			$(this).html('<iframe width="'+width+'" height="'+height+'" src="http://www.youtube.com/embed/'+ytid+'" frameborder="0" allowfullscreen></iframe>');
		});
	});

	// Sync columns...
	function syncColumns(){
		$('div.columns>div.contentblock:first').each(function(){
			var c1 = $(this);
			var c2 = $(this).next('div.contentblock');
			var h1 = c1.height();
			var h2 = c2.height();

			if (h1 > h2) c2.css('minHeight',(c2.hasClass('with-menu') ? h1+30 : h1) +'px');
			if (h2 > h1) c1.css('minHeight',(c2.hasClass('with-menu') ? h2-30 : h2) +'px');
		});
	}

	$(window).load(syncColumns);
	//syncColumns();

});

// Faster easing effect
$.extend($.easing, {
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

// Background animation addon
(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];

			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
	};
})(jQuery);
