Cufon.replace('#content .topimage h3');
Cufon.replace('#content .nieuw-sub .right .option .text');
Cufon.replace('#content .onder .image .text');
Cufon.replace('#links h3 a');
Cufon.replace('#content .prod_one h2');

function imgpreload(imgs,settings)
{
	// settings = { each:Function, all:Function }
	if (settings instanceof Function) { settings = {all:settings}; }

	// use of typeof required
	// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator#Description
	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.src = imgs[i];
	}
}

if (typeof jQuery != "undefined")
{
	(function($){

		// extend jquery (because i love jQuery)
		$.imgpreload = imgpreload;

		// public
		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			// declare urls and loop here (loop a second time) to prevent
			// pollution of above closure with unnecessary variables

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		// public
		$.fn.imgpreload.defaults =
		{
			each: null // callback invoked when each image in a group loads
			, all: null // callback invoked when when the entire group of images has loaded
		};

	})(jQuery);
}

jQuery(document).ready(function()
	{
		$("#content .prod_one .images a").click(change_photo2)		
		$(".ref .right .photos .photo a").click(change_photo)
		$('.back').click(function() { history.back(); return false;});
		$(".contactform input[name=mail]").blur(function(){
			if ($(this).attr('value')=='') $(this).attr('value',$(this).attr('rel'));
		}).focus(function() {
			if ($(this).attr('value')==$(this).attr('rel')) $(this).attr('value','')
		});
		$(".contactform textarea[name=text]").blur(function(){
			if ($(this).attr('value')=='') $(this).attr('value',$(this).attr('rel'));
		}).focus(function() {
			if ($(this).attr('value')==$(this).attr('rel')) $(this).attr('value','')
		});
		
		$(".contactform").submit(function(){
			if (!IsEmailCorrect($(this).find('input[name=mail]').attr('value')))
				{
				alert('Graag uw e-mailadres invullen');
				return false;
				}
			if ($(this).find('textarea[name=text]').attr('value')=='' || $(this).find('textarea[name=text]').attr('value')==$(this).find('textarea[name=text]').attr('rel'))
				{
				alert('Graag het berichtveld invullen');
				return false;
				}
		});	
		
		$("a.stuur_door").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true, 'overlayOpacity': 0.8, 'hideOnContentClick': false, 'frameWidth': 480, 'frameHeight': 390 });
		$(".jqzoom").jqzoom({title: false});
		$(".print").click(function(){window.print();});
		$(".checkboxes a").click(checkboxes);
		$(".formulier form").submit(function(){ return check_form(this)});
	});

function checkboxes()
{
	$(this).parent().find('a').removeClass('selected');
	$(this).addClass('selected');
	$(this).parent().find('input').val($(this).html());
	return false;
}

function friends(address,where)
{
	if ($("#friends form input[name=your_name]").attr('value')=='')
		{
			alert('Vul uw naam in');
			return false;
		}
	if (!IsEmailCorrect($("#friends form input[name=your_mail]").attr('value')))
		{
			alert('Vul uw e-mailadres in');
			return false;
		}
	if ($("#friends form input[name=friend_name]").attr('value')=='')
		{
			alert('Vul de naam van uw kennis in');
			return false;
		}
	if (!IsEmailCorrect($("#friends form input[name=friend_mail]").attr('value')))
		{
			alert('Vul het e-mailadres van uw kennis in');
			return false;
		}
	your_name=$("#friends form input[name=your_name]").attr('value');
	your_mail=$("#friends form input[name=your_mail]").attr('value');
	friend_name=$("#friends form input[name=friend_name]").attr('value');
	friend_mail=$("#friends form input[name=friend_mail]").attr('value');
	text=$("#friends form textarea[name=text]").attr('value');
	$("#friends").parent().load( where , { 'your_name': your_name, 'your_mail': your_mail, 'friend_name': friend_name, 'friend_mail': friend_mail, 'text': text, 'sent': 1, 'address': address } );
	return false;

}

function change_photo2()
{
	$("#content .prod_one .images a").removeClass('active');
	$(this).addClass('active');
	var im=new Image();
	im.src=$(this).attr('href');
	image = $(this).attr('href');
	$.imgpreload($(this).attr('href'),function() {
		$("#content .prod_one .image img").attr('src',image).attr('width',im.width).attr('height',im.height);
	});
	/*$(im).load(
		function() {
			$("#content .prod_one .image img").attr('src',$(this).attr('href')).attr('width',im.width).attr('height',im.height);
		}
	);*/
	return false;
}

function change_photo()
{	
	$(".ref .right .photos .photo a").removeClass('active');
	$(this).addClass('active');
	var im=new Image();
	im.src=$(this).attr('href');
	image = $(this).attr('href');
	$.imgpreload($(this).attr('href'),function() {
		$(".refgallery img").attr('src',image).attr('width',im.width).attr('height',im.height);
	});
	//$(".refgallery img").attr('src',$(this).attr('href'));
	return false;
}

function IsEmailCorrect(email) {
  return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/)!=null;
}

function check_form(element)
{
var and_what = 0;

jQuery(element).find(".check").each(function () {
	if( jQuery(this).val() == '' || ( jQuery(this).hasClass("email") && !IsEmailCorrect( jQuery(this).val() ) ) )
		{
			jQuery(this).addClass("error");
			and_what = 1;
		}
		else
		{
			jQuery(this).removeClass("error");
		}
});

if(and_what == 1)
	{
		alert("Vul de verplichte velden.");
		return false;
	}
	else
	{
	return true;
	}
	return false;
}


