// resize functie (deel code van supersized.js)
	$.fn.resizenow = function() {
		return this.each(function() {
			var imgH = $(this).height(),
				imgW = $(this).width(),
				biwH = $(window).height(),
				biwW = $(window).width(),
				ratio = imgH/imgW;
	
			if ((biwH/biwW) > ratio){
				$(this).height(biwH);
				$(this).width(biwH / ratio);
				$(this).height(biwH);
				$(this).width(biwH / ratio);
			} else {
				$(this).width(biwW);
				$(this).height(biwW * ratio);
				$(this).width(biwW);
				$(this).height(biwW * ratio);
			}
			$(this).css('left', (biwW - $(this).width())/2);
			$(this).css('top', (biwH - $(this).height())/2);
	
			return false;
		});
	};

// preload
	$.fn.preloader = function(options){
		var defaults = {
			delay:0,
			preload_parent:this,
			check_timer:300,
			ondone:function(){ },
			oneachload:function(image){  },
			fadein:300 
		};

		var options = $.extend(defaults, options),
			root = $(this),
			images = root.find("img").css({"visibility":"hidden",opacity:0}),
			timer,
			counter = 0,
			i=0,
			checkFlag = [],
			delaySum = options.delay ,

		init = function(){
			timer = setInterval(function(){
				if(counter>=checkFlag.length){
					clearInterval(timer);
					options.ondone();
					return;
				}
				for(i=0;i<images.length;i++){
					if(images[i].complete==true){
						if(checkFlag[i]==false){
							checkFlag[i] = true;
							options.oneachload(images[i]);
							counter++;
							
							delaySum = delaySum + options.delay;
						}
						$(images[i]).css("visibility","visible").delay(delaySum).animate({opacity:1},
						options.fadein,
						function(){ $(this).parent().removeClass("preloader");});
					}
				}
			},options.check_timer)
		};

		images.each(function(){
			if($(this).parent(options.preload_parent).length==0)
				$(this).wrap("<div class='preloader' />");
			else
				$(this).parent().addClass("preloader");
			checkFlag[i++] = false;
		}); 
		images = $.makeArray(images); 

		var icon = jQuery("<img />",{
			id : 'loadingicon' ,
			src : 'images/load.gif'
		}).hide().appendTo("body");

		timer = setInterval(function(){
			if(icon[0].complete==true){
				clearInterval(timer);
				init();
				icon.remove();
				return;
			}
		},100);
	}
	$(document).ready(function(){
// javascript is on...
// ...then we can call the resize function for the .bg-image
		$('.bg-image-wrap').find('img').slice(0,1).resizenow();

// alleen eerste img in '.bg-image-wrap' laten zien
		$('.bg-image-wrap').find('img').css({'opacity':0, 'z-index':2});
		$('.bg-image-wrap').find('img').slice(0,1).css({'opacity':1, 'z-index':3}).addClass('activeslide');
		$('.bg-image-wrap').find('img').preloader();
// #nextslide function
		var speed = 500;
		var totals = parseInt($('.bg-image-wrap').find('img').size());
		var rclicks = 1;
		var lclicks = totals;
		$('#nextslide').live('click', function(){
			$('.bg-image-wrap').find('img.activeslide').css('z-index', 2).stop().animate({'opacity':0}, speed);
			if(rclicks < totals){
				rclicks = rclicks+1;
				lclicks = lclicks-1;
				$('.bg-image-wrap').find('img.activeslide').parent().next().find('img').css('z-index', 3).resizenow().stop().animate({'opacity':1}, speed);
				$('.bg-image-wrap').find('img.activeslide').removeClass('activeslide').parent().next().find('img').addClass('activeslide');
			} else {
				rclicks = 1;
				lclicks = totals;
				$('.bg-image-wrap').find('img').slice(0,1).css('z-index', 3).resizenow().stop().animate({'opacity':1}, speed);
				$('.bg-image-wrap').find('img.activeslide').removeClass('activeslide');
				$('.bg-image-wrap').find('img').slice(0,1).addClass('activeslide');
			}
			//alert(rclicks+' + '+lclicks);
			return false;
		});
// #prevslide function
		$('#prevslide').live('click', function(){
			$('.bg-image-wrap').find('img.activeslide').parent().css('z-index', 2).stop().animate({'opacity':0}, speed);
			if(lclicks < totals){
				rclicks = rclicks-1;
				lclicks = lclicks+1;
				$('.bg-image-wrap').find('img.activeslide').parent().prev().find('img').css('z-index', 3).resizenow().stop().animate({'opacity':1}, speed);
				$('.bg-image-wrap').find('img.activeslide').removeClass('activeslide').parent().prev().find('img').addClass('activeslide');
			} else {
				rclicks = totals;
				lclicks = 1;
				$('.bg-image-wrap').find('img').last().css('z-index', 3).resizenow().stop().animate({'opacity':1}, speed);
				$('.bg-image-wrap').find('img.activeslide').removeClass('activeslide');
				$('.bg-image-wrap').find('img').last().addClass('activeslide');
			}
			//alert(rclicks+' + '+lclicks);
			return false;
		});
	});
// na browser resize, afbeelding ook resizen
	$(window).bind("resize", function(){
		$('.bg-image-wrap').find('img.activeslide').resizenow();
	});
