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

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

$(function () {

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

	$(window).resize(function() {
		$('#bgwrap img').ResizeBackground();
	});
	
	//-------------- menu hovers
	
	$('ul#mainnav li span').hide();
		
	$("ul#mainnav li a").hover(function() {
	  $(this).next('span').fadeIn(200);
	}, function() {
	  $(this).next('span').fadeOut(200);
	});		
	
	//-------------- mixed stuff
	
	$("#accordion").tabs("#accordion div.pane", {
		tabs: 'h2', 
		effect: 'slide',
		initialIndex : 9
	});	
	
	$('#accordion h2:not(.current)').hover(
		function() { $(this)
			.addClass('hover');
			},
		function() { $(this)
			.removeClass('hover');
			}
	);	

	SetForm();
		
	//-------------- searchbox	
	
	$('#wdg-search').hide();
	
	$("a#btn-search").toggle(
	  function () {
		//$('#wdg-search').animate({opacity: "show", left: "0"}, 200);
		$('#wdg-search').show(200);
	  },
	  function () {
		//$('#wdg-search').animate({opacity: "hide", left: "-120"}, 300);
		$('#wdg-search').hide(200);
	  }
	);
	
	$('#subm-search').click(function () {
		$('#wdg-search').hide(200);
		return false;
	});
	//------------------------
	
	$('div.prodnav a:first').addClass('act');

	$('.imgwrap a').live("click", function() {													 
		
		var wrap = $(this).parent('div').parent('div');
		var img = $(this).attr('href');
		var omg = wrap.find('img:first').attr('src');
		
		if(img != omg) {
		
			wrap.find('a.act').removeClass('act');
			$(this).addClass('act');													 
			
			wrap.find('img:not(:first)').each(function(){
				 $(this).remove();
			});
		
			wrap.find('img:first').fadeOut(500,function() {
				loadimg(img,wrap);											  
			});	
		
		}
		return false;
	});	

	
});

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

function SetForm() {
	$('input:not(:hidden)').clearingInput();
	$('textarea').clearingInput();
}

function LoadBgImg() {
	
	var rnd = Math.floor(Math.random() * 21);
	
	if(rnd<1) {
	 rnd='01';
	}else if(rnd<10) {
	 rnd = '0' + rnd;	
	}
	
	$('#bgwrap img').remove();
	
	var img = new Image();
	$(img).load(function () {   
		$(this).hide();
		$('#bgwrap').append(this);
		$(this).fadeIn(1500);
		$('#bgwrap img').ResizeBackground();
	})
	.error(function () {
		alert('Image could nog be loaded.');
	})
	.attr('src', 'kp_img/bgs/0'+rnd+'.jpg');
 }

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

function loadimg(image,wrap) {		
	var img = new Image();
	$(img).load(function () {   
		$(this).hide();
		wrap.find('.imgholder img:first').remove();
		wrap.find('.imgholder').append(this);
		$(this).fadeIn(600);
	})
	.attr('src', image);
 }

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

(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<961) {
			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);
