$(document).ready(function(){

	if ($('#paginanummer').html())
	{
		paginanummer = $('#paginanummer').val;
	}
	
	// flash inladen
	// als een div aangemaakt wordt met als class "flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.flash').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		//strip HTML comment from flash vars
		slideshowvars = slideshowvars.replace(/^<!--/, "");
		slideshowvars = slideshowvars.replace(/-->$/, "");
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent'
		});
	});	
	
	// inputvelden leegmaken en eventueel tekst herstellen
	$('input[name=zoek]').focus(function(){
		if($(this).val() == 'Zoek')
		{
			$(this).val('');
		}  
	});
	$('input[name=zoek]').blur(function(){
		if($(this).val() == '')
		{
			$(this).val('Zoek');
		}
	});
	
	// inputvelden leegmaken en eventueel tekst herstellen
	$('input[name=zoek2]').focus(function(){
		if($(this).val() == 'Zoekterm')
		{
			$(this).val('');
		}  
	});
	$('input[name=zoek2]').blur(function(){
		if($(this).val() == '')
		{
			$(this).val('Zoekterm');
		}
	});	
		
	$('.uitklaplink2').toggle(function() {
								 
  		$('.infocontent').slideDown('slow');
		
	}, function() {
		
  		$('.infocontent').slideUp('fast');
		
	});
	
	//## Klapblokken ##

	// subs in/uitklappen
	$('.uitklaplink').click(function () {
		var obj = $(this).parents('.uitklapblok:first').find('.uitklapdetail:first');

		if (!$(this).hasClass('keepothers')) //andere blokken sluiten
		{
			$(this).parents('.uitklapblokken').find('.uitklapdetail').each(function(){
				if (!$(this).hasClass('dontclose')) //dit blok niet altijd open houden
				{
					$(this).slideUp();
					$(this).find('.uitklaplink').removeClass('open');
				}
			})
		}

		//overal active verwijderen
		$(this).parents('.uitklapblokken').find('.uitklaplink').removeClass('active');

		if (obj.css('display')=='none') //geklikt item openen
		{
			obj.slideDown();
			$(this).addClass('active');
			$(this).addClass('open');
		}
		else //geklikt item sluiten
		{
			obj.slideUp();
			$(this).removeClass('open');
		}
	});
	//cookies opslaan bij verlaten van pagina
	$(window).unload(function(){
		$('.uitklapblokken').each(function(){
			if ($(this).hasClass('enablecookie'))
			{
				$(this).find('.uitklaplink').each(function()
				{
					var id = $(this).attr('id');
					if (id != undefined && id)
					{
						if ($(this).hasClass('open'))
							$.cookie(id, 1);
						else
							$.cookie(id, 0);
					}
				});
			}
		});
	});
	//cookies uitlezen
	$('.uitklapblokken').each(function(){
		if ($(this).hasClass('enablecookie'))
		{
			$(this).find('.uitklaplink').each(function()
			{
				var id = $(this).attr('id');
				if (id != undefined && id)
				{
					if ($.cookie(id) == 1) //was uitgeklapt
					{
						if (!$(this).hasClass('open'))
							$(this).trigger('click'); //uitklappen
					}
				}
			});
		}
	});

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');

	//field hints
	$(".hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).css("border-color", "");
				$(this).removeClass("hint");
			}
        });
    });

	//submit form link
	$('.js_submitform').click(function(){
		$(this).parents('form:first').submit();
		return false;
	})
	
	
	// menu dropdown
	$('.menu ul li a').mouseover(function () {
		if($(this).siblings('ul').hasClass('submenu')==true){
			$('.menu ul li ul').css('display','none');
			$(this).siblings('ul').css('display','block');
		}
		if($(this).parents('ul').hasClass('submenu')==false){
			$('.menu ul li ul').css('display','none');
			$(this).siblings('ul').css('display','block');	
		}
	});	
	
	$('.menu').mouseleave(function () {
		$('.menu ul li ul').css('display','none');																				
	});
	

	//AJAX FORMS automatisch
	if (typeof $().ajaxForm == 'function')
	{
		$('form.js_ajaxform').each(function(){
			var form = $(this);
			$(this).ajaxForm({
				dataType: 'json',
				beforeSerialize: function(){
					form.find('.hint').val('');
				},
				beforeSubmit: function(){
					form.parent().find('.errormelding').remove();

					//vereiste velden
					form.find('.required').each(function(){
						if ($(this).val().replace(' ', '') == '' || $(this).hasClass('hint')) //leeg of bevat nog de hint
						{
							return false;
							$(this).addClass('formerror');
						}
						else
							$(this).removeClass('formerror');
					});
					form.find('.required.email').each(function(){
						var regex = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
						if (!regex.test($(this).val()))
						{
							return false;
							$(this).addClass('formerror');
						}
					});
				},
				success: function(response){
					if (response.success)
					{
						form.html('<p>'+response.melding+'</p>');
					}
					else if (response.melding){
						form.before($('<p class="errormelding">'+response.melding+'</p>'));
					}
					else if (response.errors){
						form.before($('<p class="errormelding">'+response.errors+'</p>'));
					}
					form.find('.hint').trigger('blur');
				}
			})
		})
	}	
});

/*
 * slide an element up without causing blinking flash content
 */
function slideUp(object, speed, slideTime)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
	if (object.height() <= 0) return true;
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    var timeout = setInterval(function(){
        object.height(object.height() - ppf);
        if (object.height() <= 0){
            object.hide();
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}

/**
 * slide an element down without causing blinking flash content
 */
function slideDown(object, speed)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    object.height(0);
    object.show();
    var timeout = setInterval(function(){
        object.height(object.height() + ppf);
        if (object.height() >= orgh){
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}
