(function($) {

		/* custom selector --> :parents */
		jQuery.expr[':'].parents = function(a,i,m){
			return jQuery(a).parents(m[3]).length < 1;
		};

		$.fn.showDelay = function(duration, delay, callback, finish) {
			var wait = 0;
			var ln = this.length-1;

			return this.each(function(index) {
				$(this).delay(wait).css({ visibility: 'visible' }).hide().fadeIn(duration, function() {
					if (typeof callback == 'function') callback.call(this);
					if (index == ln && typeof finish == 'function') finish.call(this);
				});
				wait += delay;
			});
		};


		$.fn.hideDelay = function(duration, delay, callback, finish) {
			var wait = 0;
			var ln = this.length-1;

			return this.each(function(index) {
				$(this).delay(wait).fadeOut(duration, function() {
					$(this).show().css({ visibility: 'hidden' });
					if (typeof callback == 'function') callback.call(this);
					if (index == ln && typeof finish == 'function') finish.call(this);
				});
				wait += delay;
			});
		};


		$.wait = function(time) {
		  return $.Deferred(function(dfd) {
		    setTimeout(dfd.resolve, time);
		  });
		}

	})(jQuery);

	var SmallCarousel = function (header) {
		var self = this;
        this._header = header;
        this._sub = this._header.hasClass('sub');
        this._wrapper = this._header.find('.wrapper');
        this._list = this._wrapper.find('ul');
        this.autoplay = 0;
        this.loaded = false;
        this.items = this._header.find('li').length;
        if (this.items > 1) {
            
            this._header.find('.next').bind('click', function () {
                self.next();
            });
            this._header.find('.previous').bind('click', function () {
                self.previous();
            });
            $(document).bind('keyup', function (e) {
                if(self.loaded) {
                    if (e.which == 39) {
                        self.next();
                    } else if (e.which == 37) {
                        self.previous();
                    }
                }
            });
            this.positionWrapper();
            $(window).resize(function () {
                self.positionWrapper();
            });
            this._list.delegate('.img_placeholder', 'mouseenter mouseleave', function (e) {
                if ($(this).closest('li').prev().hasClass('current-item')) {
                    if (e.type == 'mouseenter') {
                        self._header.find('.arrow.next').addClass('hover');				
                    } else {
                        self._header.find('.arrow').removeClass('hover');				
                    }
                } else if ($(this).closest('li').next().hasClass('current-item')) {
                    if (e.type == 'mouseenter') {
                        self._header.find('.arrow.previous').addClass('hover');				
                    } else {
                        self._header.find('.arrow').removeClass('hover');				
                    }
                }
            });
        } else {
            this._header.find('li').filter('.current-item').css('width', this._header.find('li').filter('.current-item').width());
            self._list.addClass('single');
        }
        this._list.delegate('.img_placeholder', 'click', function(e) {
            var listItem = $(this).parent().parent()[0],
                $currentItem = self._list.children('.current-item');
            if (self.loaded) {
                if (self._sub) {
                    if (listItem == $currentItem[0]){
                        self.enlargeImage();
                    } else if (self.items > 1) {
                        if (listItem == $currentItem.next()[0]) {
                            self.next();
                        } else if(listItem == $currentItem.prev()[0]) {
                            self.previous();					
                        }
                    }
                } else {
                    if (listItem == $currentItem[0]){
                        window.location.href = $(this).find('a').attr('href');
                    } else if (self.items > 1) {
                        if (listItem == $currentItem.next()[0]) {
                            self.next();
                        } else if(listItem == $currentItem.prev()[0]) {
                            self.previous();					
                        }
                    }
                }
            }
            e.stopPropagation();
        })
        
	};
	SmallCarousel.prototype = {
		next : function () {
			this.go(true);	
		},
		previous : function () {
			this.go(false);
		},
		go : function (next) {
			var current = this._list.find('.current-item'),
			    cloner = next ? this._list.children(':first') : this._list.children(':last'),
			    changeTo = next ? current.next() : current.prev(),
			    moveLength = (current.width() / 2) + (changeTo.width() / 2),
			    moveFrom = this._list.offset().left,
			    fadeOut = next ? current.prev() : current.next(),
			    fadeIn,
			    self = this;
			if (!fadeOut.filter(':animated').length) {
				clearTimeout(this.autoplay);
				current.removeClass('current-item');
				this._header.find('.next, .previous').unbind();
				$(document).unbind('keyup');
				
				if (next) {
					moveLength = moveLength + 20;
					this._list.append(cloner.clone());
					fadeIn = changeTo.next();
					this._list.animate({left: moveFrom - moveLength}, 1000);
					current.find('h2, h3').fadeOut(600).children('a').animate({left:200}, 700, function(){$(this).css('left',0)});
				} else {
					this._list.prepend(cloner.clone());
					fadeIn = changeTo.prev();
					this._list.css({left: moveFrom - fadeIn.width()});
					moveFrom = moveFrom - fadeIn.width();
					this._list.animate({left: moveFrom + moveLength}, 1000);
					current.find('h2, h3').fadeOut(600).children('a').animate({left:-200}, 700, function(){$(this).css('left',0)});
				}
				if (!this._sub) {
					this._header.removeClass('portal0 portal1 portal2').addClass(changeTo.attr('data-portal'));
				}
				changeTo.find('h2, h3').delay(200).fadeIn(1200);
				fadeIn.fadeIn(1000);
				fadeOut.fadeOut(1001, function () { //1001 to make sure it is last
					cloner.remove();
					changeTo.addClass('current-item');
					self.positionWrapper(self);
					self._header.find('.next').bind('click', function () {
						self.next();
					});
					self._header.find('.previous').bind('click', function () {
						self.previous();
					});
					$(document).bind('keyup', function (e) {
						if (e.which == 39) {
							self.next();
						} else if (e.which == 37) {
							self.previous();
						}
					});
					self.start();
				});	
			}
		},
		loadImages : function () {
			var tempImg = new Image(),
					$items = this._list.find('li'),
					$placeholder,
					$arrows = this._header.children('.arrows'),
					self = this;
			this._header.addClass($items.filter('[data-load-order=1]').attr('data-portal'));
			
			function loadSingle(i){
				tempImg = new Image();
				$placeholder = $items.filter('[data-load-order=' + i + ']').find('.img_placeholder')
				tempImg.src = $placeholder.attr('data-img');
				$(tempImg).load(function() {
					$placeholder.html(tempImg);
					if (self._sub) {
						$placeholder.append(
							$('<h3 />').append(
								$('<a href="#" />').append(
									$('<span />').html($placeholder.attr('data-title') || ''))
								)
							).append($('<span class="zoom" />'));	
					} else {
						$placeholder.append(
							$('<h2 />').append(
								$('<a href="' + $placeholder.attr('data-url') + '" />').append(
									$('<span />').html($placeholder.attr('data-title') || '')
								)
							)
						);
					}
					$placeholder.children('img').fadeIn(800);
					self.positionWrapper();
					if ($items.filter('[data-load-order=' + (i + 1) + ']').length) {
						loadSingle(i + 1)
					} else {
						$items.filter('.current-item').find('h2, h3').fadeIn(800);
                        self.loaded = true;
                        showBackground();
                        self._header.addClass('ready');
                        if (self.items > 1) {
                            $arrows.fadeIn(800);
                            loadOthers();
                            self.start();
                        } else {
                            $items.filter('.current-item').css('width', $items.filter('.current-item').find('img').width());
                            self._list.addClass('single');
                        }
					}
				});
			}
			function loadOthers() {
				//select all items that do not have the attribute data-load-order
				$items.filter(function() { return $(this).filter('[data-load-order]').length == 0 }).each(function () {
					tempImg = new Image();
					$placeholder = $(this).find('.img_placeholder')
					tempImg.src = $placeholder.attr('data-img');
					$placeholder.html(tempImg);
					$placeholder.children('img').show();
					if (self._sub) {
						$placeholder.append(
							$('<h3 />').append(
								$('<a href="#" />').append(
									$('<span />').html($placeholder.attr('data-title') || ''))
								)
							).append($('<span class="zoom" />'));		
					} else {
						$placeholder.append(
							$('<h2 />').append(
								$('<a href="' + $placeholder.attr('data-url') + '" />').append(
									$('<span />').html($placeholder.attr('data-title') || '')
								)
							)
						);
					}
				});
			}
			loadSingle(1);
		},
		positionWrapper: function (scope) {
			var scope = scope || this,
			    center = $(window).width() / 2,
				currentWidth = scope._list.children('.current-item').width(),
                margin = 20,
				firstWidth = scope._list.children('li:visible:first').width(); 
				scope._list.css('left', center - firstWidth - margin - (currentWidth / 2));
		},
		start: function () {
			var self = this;
			this.autoplay = setTimeout(function() { self.next() }, 5000);
		},
		enlargeImage: function () {
			var current = this._list.find('.current-item'),
			    currentImg = current.find('img'),
					bigImage = new Image(),
					self = this,
					arrows = this._header.children('.arrows'),
					overlay = this._header.children('.overlay');
					
			clearTimeout(this.autoplay);
			bigImage.src = current.find('.img_placeholder').attr('data-img-large');
			arrows.fadeOut(500);
			
			$(bigImage).load(function() {
				if(self._header.children('.bigImage').length == 0) {
					var newImage = $('<div class="bigImage" />'),
							bigLeft = currentImg.offset().left,
							animateLeft = bigLeft + (currentImg.width() /2 ) - (bigImage.width / 2) ,
							bigWidth = bigImage.width,
							bigHeight = bigImage.height;
					newImage.append('<span class="close"></span>');
					bigImage.width = currentImg.width();
					bigImage.height = currentImg.height();
					newImage.append(bigImage);
					newImage.css('left', bigLeft);
					self._header.append(newImage);
					overlay.fadeIn(500);
					newImage.animate({
						left: animateLeft,
						width: bigWidth,
						height: bigHeight
					});
					newImage.find('img').animate({
						width: bigWidth,
						height: bigHeight
					});
					newImage.click(function() { self.closeImage() });
					overlay.bind('click', function() { self.closeImage() });
				}
			}).each(function() {
				if (this.complete) { 
					$(this).trigger("load");
				}
			});
		},
		closeImage: function () {
			var self = this;
			this._header.children('.overlay').unbind().fadeOut(500);
            if(this.items > 1){
                this._header.children('.arrows').fadeIn(500);
            }
			this._header.children('.bigImage').fadeOut(500, function() {
					$(this).remove();
                    if(self.items > 1){
                        self.start();
                    }
			});
		}
	}
	
	var Calendar = {};
	
	(function(context) {
	
		var loading = false;
		var loadingTimer = [];
		var loadingInterval = 4000;
		var loadingFrames = 81;
		var loadingFrameHeight = 26;
		var loadingFrame = [];
	
		context._startLoading = function(obj, hash) {
		
			loadingFrame[hash] = 0;
			loadingTimer[hash] = setInterval(function() { context._animateLoading(obj, hash) }, (loadingInterval/loadingFrames));
		
		};
		
		context._stopLoading = function(obj, hash) {

			clearInterval( loadingTimer[hash] );
			$(obj).find('.loader').hide();
		
		};
		
		context._animateLoading = function(obj, hash) {
		
			$(obj).find('.loader').css({ backgroundPosition: '0 ' + (loadingFrame[hash] * (loadingFrameHeight*-1) ) + 'px' }).show();
			loadingFrame[hash] = (loadingFrame[hash] + 1) % loadingFrames;	
		
		};
	
		context.loadItems = function(day, mag) {
		
			loading = true;
		
			$loader = $('#agenda-detail-loading');
			$content = $('#agenda-detail-content');
			
			$content.hide();
			$loader.show();
			$loader.find('.msg').hide();
			
			context._stopLoading($loader, 'agenda');
			context._startLoading($loader, 'agenda');
			
			$.ajax({
				url: 'ajax/agenda.ajax.php',
				type: 'POST',
				data: ({ day: day, mag: mag }),
				dataType: 'html',
				success: function(data) {
					$.wait(1000).then(function() {
						$loader.hide();
						$content.html(data).show();
						context._stopLoading($loader, 'agenda');
						loading = false;
					});
				}
			});
		
		};
	
		context.init = function() {
			
			if ( $('#agenda .calendar ol li.today a').length ) {
				$('#agenda-detail-loading').hide();
				$('#agenda-detail-content').show();			
			}
			
			$('#agenda .calendar ol li a').click(function(e) {			
				if (loading == true || $(this).parent().hasClass('active') ) return false;
				mag = $(this).data('mag');
				day = $(this).data('day');
				context.loadItems(day, mag);				
				$(this).parent().addClass('active').siblings().removeClass('active');				
				e.preventDefault();
			});
		
		};
	
	})(Calendar);
	
	var Subscriptions = {};
	
	(function(context) {
	
		context.init = function() {

			/**
			 * Inklappen van de subscriptions per magazine
			 */
		
			$('#subscriptions .subscription .startissue').hide();		
			$('#subscriptions .magazines li:not(.active) .group').hide();		

			/**
			 * Openklappen van de subscription per magazine
			 */

			$('#subscriptions .magazines li .mag').click(function(e) {			
				$obj = $(this).parent();
				$obj.siblings().removeClass('active').find('.group').hide();
				$obj.toggleClass('active').find('.group').toggle();
				
				// scroll naar uitgeklapte groep
				offset = $obj.offset();
				// 110 is de offset top van wanneer het hoofdmenu sticky wordt
				offset.top = offset.top - 110;
				$('html, body').animate({ scrollTop: offset.top-100 }, 'slow');
				e.preventDefault();
			});
			
			/**
			 * Toevoegen van subscription door 'add to cart'
			 */			

			$('#subscriptions .subscription .btn').click(function(e)
			{
				if (!$(this).hasClass('follow')) {
					// als renew is aangevinkt de link volgen, het startissue blok wordt niet uitgeklapt
					if ($(this).parent().find('.renew input').is(':checked') ) {
						$(this).closest('form').submit();
					} else {
					
						// voorzie alle siblings van de geklikt subscription met een transparante laag
						$obj =  $(this).closest('.group');
						$obj.add( $obj.siblings() ).find('.cover').css({ opacity: .9 }).show();
						$(this).closest('.subscription').find('.cover').hide().end()
		
						// toon het startissue blok
						$(this).closest('.subscription').find('.startissue').slideDown();
					}
					// volg de link niet
					e.preventDefault();
				}
			});
			
			/**
			 * Sluiten van het startissue blok
			 */
			
			$('#subscriptions .subscription .startissue .close').click(function(e)
			{
				//  verwijder de transparante lagen over de subscriptions
				$obj = $(this).closest('.group');
				$obj.add( $obj.siblings() ).find('.cover').hide();			
				
				// verberg het startissue blok
				$(this).parent().slideUp();
				
				// volg de link niet
				e.preventDefault();
			});
			
			/**
			 * Renew de subscriptions
			 */
			
			$('#subscriptions .subscription .renew input').click(function(e) {
				if ( $(this).is(':checked') ) {
					$obj = $(this).closest('.group');
					$obj.add( $obj.siblings() ).find('.cover').hide();
					$(this).closest('.subscription').find('.startissue').slideUp();
				}
			});
		}
	
	})(Subscriptions);

	function photoHeader() {
		if ($('#ph').length) {
			var $header = $('#ph'),
					loaderInterval = 0,
					autoPlay = 0,
					positionY = 0;
					
			function changeLoader() {
				if ($header.find('.loader').length) {
					$header.find('.loader').each(function () {
						positionY = $(this).css('background-position-y');
						if (!positionY){ 
							positionY = $(this).css('background-position') || '0px -2080px';
							positionY = positionY.split(' ')[1]
						}
						positionY = parseInt(positionY);
						$(this).css('background-position', '0px ' + ((positionY + 26) || -2080) + 'px');
						
					});
				} else {
					clearInterval(loaderInterval);
				}
			}

			if($header.find('.loader').length)  {
				loaderInterval = setInterval(changeLoader, 25);
			}							
			$header.addClass(
				$header.find('.wrapper li[data-load-order=1]')
					.addClass('current-item')
					.attr('data-portal'))
				.find('.wrapper li:not([data-show-on-start])').hide();
			
			var carousel = new SmallCarousel($header);	
			
			carousel.loadImages();
		} else {
            showBackground();
        }
	};
	
	var alignMenu = {};
	
	(function(context) {
	
		var browserclass = ' ';
		var ua = $.browser;
	
		context.init = function() {

			if (ua.mozilla) {
				browserclass = ' moz';
			} else if (ua.msie && ua.version.slice(0,3) == '9.0') {
				browserclass = ' ie9';
			}

			/* __TIJDELIJK (info@SvO||Koen): Quickfix om subnavs uit te lijnen met de hoofdnavs */		
			$('#nav-main ol > li:eq(4) ol').addClass('temp_books'+browserclass);
			$('#nav-main ol > li:eq(5) ol').addClass('temp_store'+browserclass);
			$('#nav-main ol > li:eq(6) ol').addClass('temp_service'+browserclass);
		
		};
	
	})(alignMenu);
	
	/*
	 * Related items
	 */
	
	var relatedItems = {};
	(function(context) {
		
		var loading = false;
		var loadingTimer = [];
		var loadingInterval = 4000;
		var loadingFrames = 81;
		var loadingFrameHeight = 26;
		var loadingFrame = [];
		var $container = $('#related-items');
		var id = $container.data('id');
		var page = 1;
		
		context.init = function() {
		
			context.loadItems(page);

			$container.find('a.prev').click(function(e) {
				context.loadItems(page-=1);
				e.preventDefault();
			});
		
			$container.find('a.next').click(function(e) {
				context.loadItems(page+=1);
				e.preventDefault();
			});
			
		};
		
		context.loadItems = function(page) {
			
			$container.find('figure').remove();
			context._startLoading($container,'related');			
			$container.find('a.prev,a.next').hide();
			$container.find('span.prev,span.next').show();

			$.ajax({
				url: 'ajax/related.ajax.php',
				type: 'GET',
				data: ({ id: id, page : page }),
				dataType: 'json',
				success: function(data) {
					if (data.items.length) {					
						page = data.page;
						$container.show();										
						
						$.each(data.items, function() {
							$(this.item.html).appendTo($container.find('.related-items-wrapper')).hide();
						});						
						
						context._stopLoading($container, 'related');
						$container.find('figure').showDelay(400, 400, false, function() {
							if (data.next == 'true') {
								$container.find('a.next').show();
								$container.find('span.next').hide();
							} else {
								$container.find('a.next').hide();
								$container.find('span.next').show();
							}
							
							if (data.prev == 'true') {
								$container.find('a.prev').show();
								$container.find('span.prev').hide();
							} else {
								$container.find('a.prev').hide();
								$container.find('span.prev').show();
							}						
						});
					}
				}
			});
			
		};
	
		context._startLoading = function(obj, hash) {
		
			loadingFrame[hash] = 0;
			loadingTimer[hash] = setInterval(function() { context._animateLoading(obj, hash) }, (loadingInterval/loadingFrames));
		
		};
		
		context._stopLoading = function(obj, hash) {

			clearInterval( loadingTimer[hash] );
			$(obj).find('.loader').hide();
		
		};
		
		context._animateLoading = function(obj, hash) {
		
			$(obj).find('.loader').css({ backgroundPosition: '0 ' + (loadingFrame[hash] * (loadingFrameHeight*-1) ) + 'px' }).show();
			loadingFrame[hash] = (loadingFrame[hash] + 1) % loadingFrames;	
		
		};
		
	})(relatedItems);
	
	/*
	 * Ready, set, let's go!
	 */
	
	$(document).ready(function(){
	
		// Externe links loggen
	       $('a').click(function() {
	               if( window.location.hostname != this.hostname ){
	                       _gaq.push(['GaTracker1._trackPageview', '/uitgaand/' + this.href]);
	               }
	               return true;
	       });
		
		Calendar.init();
		Subscriptions.init();
		photoHeader();
		alignMenu.init();
		
		if ($('#related-items').length) {
			relatedItems.init();
		}	

		/* login blokje pas verwijderen als er naast wordt geklikt */		
		$('#login .login').hover(function(e) {
			$(this).addClass('show');
			e.stopPropagation();
		});
		
		/* enter submit het formulier */
		$('#login input, #shopping-cart .add-coupon input').keypress(function(e) {
			if (e.keyCode == 13) {
				$(this).closest('form').submit();
				e.preventDefault();
			}
		});
		
		$(document).click(function() {
			$('#login .login').removeClass('show');
			$('#home-magazines .xitem').removeClass('closed open').addClass('clickme');
		});
		
		/* open links met rel=external in een nieuw venster */
		
		$('a.external').attr('target', '_blank');
		
		/* only open social links, do not propagate */
		$('.social a').click(function(e) {
			e.stopPropagation();
		});
		
		/* Vervang de default submit buttons met een fancy button */	
			
		$('.submit').find('input[type=submit]').hide().parent().find('.btn, a').filter(':parents(.btn)').show().click(function(e) {
			
			// hidden submit field toevoegen zodat bij meerdere submit buttons gekeken kan worden waar op geklikt is
			name = $(this).prev().attr('name');
			val = $(this).prev().val();
			$('<input />', {
				type: 'hidden',
				name: name,
				value: val
			}).appendTo( $(this).closest('form') );
		
			$(this).closest('form').submit();
			e.preventDefault();
		});
		
		/* vergroot de clickearea's */
		
		$('.xitem').filter(':parents(#detail, .cta, #home-magazines, #magazines)').add('#related-items article, #search-results article').click(function(e) {
			url = $(this).find('a:eq(0)').attr('href');
			document.location = url;
		}).addClass('clickme');
		
		/* toon/verberg afleveradres in bestelproces */
		
		$('#account-delivery-address:not(.show)').hide();
		$('#account-delivery-address-toggle').click(function() {
			$('#account-delivery-address').toggle();
		});
		
		/* toon infinite scroll bij nieuws overzicht */		

/*
		if($('#news').length) {
			$('#news').addClass('infinite_scroll');
			//$('#news .nav-paging ol').empty();
			init_infinite_scroll();
		}
*/

		/* magazine blokken op homepage in/uitklappen */

		if ($('#home-magazines .xitem').length) {
			$('#home-magazines .xitem').addClass('clickme').click(function(e) {
				$(this).removeClass('closed clickme').addClass('open ');
				$(this).siblings().addClass('closed clickme')
				e.stopPropagation();
			});
		}
		
		/* magazines blokken uitrekken adhv hoogte excerpt tekst */
		
		if ($('#magazines').length) {
			var h = 0;
			$('#magazines .xitem .excerpt').each(function() {
				t = $(this).outerHeight();
				if (t > h) h = t;
			});
			$('#magazines .xitem .excerpt').css({ height: h });
		}
		
		/* store blokken per regel dezelfde hoogte maken */
		
		if ($('#store .xitem').length) {
					
			var h = 0;
			var t = 0;
			var x = 0;
			var y = 0;
			var n = 0;
			
			$elems = $('#store > .xitem');

			$elems.each(function(i) {
			
				// header hoogte van hoogste blok berekenen
				t = $(this).find('.main-header').height();
				if (t > h) h = t;
			
				// tekst hoogte van hoogste blok berekenen
				x = $(this).find('.excerpt').height();
				if (x > y) y = x;
						
				// bij elke 3e de hoogte van alle blokken op dezelfde regel aanpassen
				if ((i++ % 3) == 2) {				
					$(this).add( $(this).prev() ).add( $(this).prev().prev() ).find('.main-header').css({ height: h });
					$(this).add( $(this).prev() ).add( $(this).prev().prev() ).find('.excerpt').css({ height: y });
					h = 0;
					y = 0;
				}

				n = i;
				
			});
			
			// wanneer op de laatste regel 2 blokken staan
			if (n % 3 == 2) {
				$elems.eq(n-1).add( $elems.eq(n-2) ).find('.main-header').css({ height: h });
				$elems.eq(n-1).add( $elems.eq(n-2) ).find('.excerpt').css({ height: y });
				h = 0;
				y = 0;
			}
		}	
		
		/* magazine blokken per regel dezelfde hoogte maken */
		
		if ($('#magazines .xitem').length) {
					
			var h = 0;
			var t = 0;
			var x = 0;
			var y = 0;
			var n = 0;
			
			$elems = $('#magazines > .xitem');

			$elems.each(function(i) {
			
				// header hoogte van hoogste blok berekenen
				t = $(this).find('.main-header').height();
				if (t > h) h = t;
			
				// tekst hoogte van hoogste blok berekenen
				x = $(this).find('.buy .ext').height();
				if (x > y) y = x;
						
				// bij elke 3e de hoogte van alle blokken op dezelfde regel aanpassen
				if ((i++ % 3) == 2) {				
					$(this).add( $(this).prev() ).add( $(this).prev().prev() ).find('.main-header').css({ height: h });
					$(this).add( $(this).prev() ).add( $(this).prev().prev() ).find('.buy .ext').css({ height: y });
					h = 0;
					y = 0;
				}

				n = i;
				
			});
			
			// wanneer op de laatste regel 2 blokken staan
			if (n % 3 == 2) {
				$elems.eq(n-1).add( $elems.eq(n-2) ).find('.main-header').css({ height: h });
				$elems.eq(n-1).add( $elems.eq(n-2) ).find('.buy .ext').css({ height: y });
				h = 0;
				y = 0;
			}
		}
		
		/* toon juiste formulier bij nieuwe of bestaande klant in stap 2 van de winkelwagen */
		
		if($('input[name="shop-account-type"]').length > 0) {
		
			$('#clients form').hide();
			frmId = "#shop-frm-"+$('input[name="shop-account-type"]:checked').val()+"-client";
			$(frmId).show();
				
			$('input[name="shop-account-type"]').click( function() { 
				frmId = "#shop-frm-"+$('input[name="shop-account-type"]:checked').val()+"-client";
				$('#clients form').hide();
				$(frmId).show();
			})
		}
		
		/*
		 * Maak de navigatie sticky
		 */
		 
		function stickyNav() {		
			pos = $(window).scrollTop();
			if (pos >= 110) {
				$('body').css({ paddingTop: $('#nav-main').outerHeight() });
				$('#nav-main').css({ position: 'fixed', top: 0 });
			} else {
				$('body').css({ paddingTop: 0 });
				$('#nav-main').css({  position: 'relative', top: 0 });
			}			
		}
		
		var didScroll = false;		
		$(window).scroll(function() {
			if (!didScroll) {
				timer = setInterval(function() {
					if (didScroll) {
						didScroll = false;
						clearTimeout(timer);						
						stickyNav();
					}
				}, 100);			
			}			
			didScroll = true;
		});


		
		if($('form[action*="ogone"]').length) {
			setTimeout(function () {$('form[action*="ogone"]').submit()}, 5000);
			setTimeout(countdownTimer, 1000);
		}
		function countdownTimer() {
			var time = $('#timer .time')
			current = parseInt(time.html());
			if (current > 0) {
				time.html(current - 1);
				current = parseInt(time.html())
				if (current == 1) {
					$('#timer .seconds').html('second');
				}
				else if (current == 0) {
					$('#timer .seconds').html('seconds');
				}
				setTimeout(countdownTimer, 1000);
			}
		}
		
	});
    
    function showBackground() {
        if ($('body').attr('data-background')) {
            $('body').css('background-image', 'url("' + $('body').attr('data-background') + '")');
        }
    }



