
var mh = 240; // minumum height

var bgs = new Array ();

	bgs[0] = 'kp_img/bg1.jpg';
	bgs[1] = 'kp_img/bg2.jpg';
	bgs[2] = 'kp_img/bg3.jpg';
	bgs[3] = 'kp_img/bg4.jpg';


$(window).load(function() {
	LoadBgImg();
	
	var ch = $('div#content').height();
	ch < mh ? ch=mh : '' ;

	$('div#midwrap').hide().css( { 'height' : ch } );
	$('div#footprint').hide();
});

//--------------------------------------------------

$(function () {

	$('h1').click(function(){
		location.href='index.php';
		return false;
	})


	function ShowWrap() {
		$('div#midwrap').slideDown(1200,function(){ $('div#footprint').fadeIn(1200); });
	}


	setTimeout(ShowWrap,600);


	$('ul#mainmenu li a').click(function () {
										  
	  var lnk = $(this).attr('href');
	  var mnu = $(this).attr('rel');
	  
	  //LoadBgImg();
	  
	  $('div#content').fadeTo(400,0,function(){
										   
		$('div#content').load(lnk, { 'mnu' : mnu , 'ajax' : 'yes' }, function(){

			SlideToHeight();
		
	 	 });
									   
	  }); 
	  
	  return false; 
    
	});


	$(window).resize(function() {
		$('#bgwrap img').ResizeBackground();
	});

	
});

//--------------------------------------------------

function SlideToHeight() {
	var ph = $('div#midwrap').height();
	var ch = $('div#content').height();
	
	ch < mh ? ch=mh : '' ;
	
	$("#midwrap img").load(function() {    
    	var count = 0;
    	var noImages = $("#midwrap img").size();
    	return function () {
			count++; 
			if(count === noImages) { 
				$('div#midwrap').animate({'height': ch }, 700, function(){ $('div#content').fadeTo(400,1) });    
			}
		};
	}());
	
	$('a.zoom').fancybox({
		'zoomSpeedIn' 	: 300,
		'zoomSpeedOut' 	: 200,
		'zoomOpacity' 	: true,
		'overlayOpacity': 0.5,
		'padding'		: 0
	});
	
	
}

//--------------------------------------------------

function LoadBgImg() {
	
	var rnd = Math.floor(Math.random() * bgs.length)
	
	$('#bgwrap img').remove();
	
	var img = new Image();
	$(img).load(function () {   
		$(this).hide();
		$('#bgwrap').append(this);
		$(this).fadeIn(1200);
		$('#bgwrap img').ResizeBackground();
	})
	.error(function () {
		alert('Image could nog be loaded.');
	})
	.attr('src', bgs[rnd]);
 }


//--------------------------------------------------

(function($) {

	var srch = '';
	var srcw = '';

	$.fn.ResizeBackground = function(options){
	
		srch === '' ? srch = $(this).height() : '' ;
		srcw === '' ? srcw = $(this).width() : '' ;
		
		var ratio = srch / srcw; 
		var scl = srcw / srch;
		
		var winh = $(window).height();
		var winw = $(window).width();
		
		// hoger dan breed
		if(winh>winw) {
			
			newh = winh;
			neww = Math.floor(newh * scl);
		
		}
		
		// breder dan hoog
		if(winw>winh) {
			
			neww = winw;
			newh = Math.floor(neww * ratio);
			
			if(newh<winh) {
				newh = winh;
				neww = Math.floor(newh * scl);
			}
		
		}		
		
		if(neww > winw ) {
			var posx = Math.floor(-1 * (neww - winw));
		}else{
			var posx = 0;
		}
		
		if(winw<921) {
			var posx = $(this).css('margin-left');
		}		
		
		$(this).css({
			'height' : newh,
			'width' : neww,
			'margin-left' : posx
		});
		
		var rationew = $(window).height()/ $(window).width()
		
	}
	
	
	$.fn.clearForm = function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		  return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
		  this.value = '';
		else if (type == 'checkbox' || type == 'radio')
		  this.checked = false;
		else if (tag == 'select')
		  this.selectedIndex = -1;
	  });
	};	
	
})(jQuery);

