$(document).ready(function() {
	var preload = new Array();
    $('.hover').each(function() {
        if (!$(this).hasClass('active')) preload.push($(this).attr('src').replace(/\.(.+)$/i, '-over.$1'))
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if (preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
	$('.hover').hover(function() {
		if (!$(this).hasClass('active')) $(this).attr('src', $(this).attr('src').replace(/\.(.+)$/i, '-over.$1'));
	}, function() {
		if (!$(this).hasClass('active')) $(this).attr('src', $(this).attr('src').replace(/-over\.(.+)$/i, '.$1'));
	});
	
	var topspace = ($('#marketing').height() - $('#marketing_content').height())/2;
	$('#marketing_content').css('margin-top',topspace+'px');
	topspace = ($('body').height() - $('#sitecontainer').height())/2;
	if (topspace < 0) topspace = 0;
	$('#sitecontainer').css('top',topspace+'px');
	topspace = ($('body').height() - ($('#afleveropties').height()+40))/2;
	if (topspace < 0) topspace = 0;
	$('#afleveropties').css('top',topspace+'px');
	
	$(window).bind('resize', function() {
		var topspace = ($('body').height() - $('#sitecontainer').height())/2;
		if (topspace < 0) topspace = 0;
		$('#sitecontainer').css('top',topspace+'px');
		topspace = ($('body').height() - ($('#afleveropties').height()+40))/2;
		if (topspace < 0) topspace = 0;
		$('#afleveropties').css('top',topspace+'px');
	});
	
	if ($('#wrapper').height() > 260) {
		$('#wrapper').css('height','260px').jScrollPane({showArrows:true,scrollbarWidth:22,scrollbarMargin:20,scrollbarOnLeft:false,reinitialiseOnImageLoad:true});
	}
	
	$('#bt_afleveropties').click(function() {
		$('#sitecontainer').fadeOut(600, function() {
			$('#afleveropties').fadeIn(600);
		});
	});
	$('#close_afleveropties').click(function() {
		$('#afleveropties').fadeOut(600, function() {
			$('#sitecontainer').fadeIn(600);
		});
	});

	if ($('#contactform').length > 0) {
		$('#contactform').validate({
			rules: {
				email : {
					required: true,
					email: true
				},
				subject : {
					required: true,
					minlength: 1,
					maxlength: 255
				},
				message : {
					required: true,
					minlength: 1,
					maxlength: 2000
				}
			},
			highlight : function(element, errorClass) {
				$('#'+element.id).css('border', '1px solid red');
			},
			unhighlight : function(element, errorClass) {
				$('#'+element.id).css('border', '1px solid grey');
			},
			messages : {
				email : '',
				subject : '',
				message: ''
			},
			submitHandler : function(form) {
				$(form).ajaxSubmit({
					type : 'POST',
					url : 'handleform.php',
					async : true,
					success : showContactFormResponse
				});
			}
		});
	}
});

function showContactFormResponse(responseText, statusText) {
	if (responseText == 'succes') {
		$('#wrapper').fadeOut(600, function() {
			$('#wrapper').html('<p>Uw bericht is verstuurd.</p>').fadeIn(600);
		});
	}
}
