$(document).ready(function(){
						   
	$(document).pngFix( );

						   
	if ($('#paginanummer').html())
	{
		paginanummer = $('#paginanummer').val;
	}
	
	$('input[name=nieuwsbrief]').focus(function(){
		if($(this).val() == 'E-mailadres')
		{
			$(this).val('');
		}
		if($(this).val() == 'E-mailaddress')
		{
			$(this).val('');
		}
	});
	$('input[name=nieuwsbrief]').blur(function(){
		if($(this).val() == '')
		{
			$(this).val('E-mailadres');
		}
		if($(this).val() == '')
		{
			$(this).val('E-mailaddress');
		}
	});	

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML (vereist voor cms)
	$('.blankwindow').attr('target','_blank');

	// 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();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent'
		});
	});
	
	// h1 automatisch omzetten in flash
	$('.fontheader1').each(function(){
		title = $(this).html();
		title = title.replace('&amp;','%26');
		$(this).html("");
		$(this).flash({
	    	src: '/framework/public/flash/h1.swf?titel=' + title,
	    	width: '633',
	    	height: '33',
			wmode: 'transparent'
		});
	});
	
	// h2 automatisch omzetten in flash
	$('.fontheader2').each(function(){
		title = $(this).html();
		title = title.replace('&amp;','%26');
		$(this).html("");
		$(this).flash({
	    	src: '/framework/public/flash/h2.swf?titel=' + title,
	    	width: '306',
	    	height: '33',
			wmode: 'transparent'
		});
	});
	
	// 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');																				
	});
	
	$(".blokhover").hover(function()
	{
		$(this).find('.blokopen').addClass('opening');
		$('.blokopen').each(function(){
			if (!$(this).hasClass('opening'))
				$(this).slideUp(200);
			});
		$('.blokopen').removeClass('opening');
		$(this).find('.blokopen').slideDown(200);
	}, function(){
		$('.blokopen').slideUp(200);
	});
	
	//## colorbox ##

	/**
	 *	-- Gebruik --
	 *	Geef een anchor de class .colorbox en de href de locatie van de afbeelding
	 *	Voor een slideshow:
	 *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
	 *	Voorbeeld slideshow:
	 *	<a class="colorbox" rel="testblaat" href="/public/img/video2.jpg">klik</a>
     *	<a class="colorbox" rel="testblaat" href="/public/img/banner.jpg">klik</a>
	 */
	//set
	$('.colorbox').colorbox({
		transition:'elastic'
	});

	/**
	* Html tonen in colorbox:
	* <a class="htmlcolorbox" rel="idvanjehtmldiv">Klik</a>
	*/	
	$('.htmlcolorbox').click(function(){
		var targetid = $(this).attr('rel');
		if (typeof targetid == 'undefined' || targetid == '') return ;
		
		$(this).colorbox({
			open: true,
			inline:true,
			title:false,
			transition:'none',
			href:"#" + targetid
		});
	});
	
	$('.submitlink').click(function(){
        $(this).parents('form:first').trigger('submit');
		return false;
	});

	//AJAX FORMS automatisch
	$('form.ajaxform').each(function(){
		var form = $(this);
		$(this).ajaxForm({
			dataType: 'json',
			beforeSubmit: function(){
				form.parent().find('.errormelding').remove();

				//vereiste velden
				var ok = true;
				form.find('.required').each(function(){
					if ($(this).val().replace(' ', '') == '' || $(this).hasClass('hint')) //leeg of bevat nog de hint
					{
						ok = 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()))
					{
						ok = false;
						$(this).addClass('formerror');
					}
				});

				if (ok)
				{
					//de hints niet meesturen
					form.find('input.hint').each(function(){
						$(this).val('');
					});
				}
				else
					return false;
			},
			success: function(response){
				if (response.success)
				{
					form.html('<p>'+response.melding+'</p>');
				}
				else if (response.melding){
					form.before($('<p class="errormelding">'+response.melding+'</p>'));
				}
			}
		})
	})
});

// Return value rounded to two decimal places
function display_value( param )
{
	stringvalue     = param.toString();
  	stringvalue     = stringvalue.replace(/\,/, ".");
	value           = round_decimals( stringvalue, 2);
	stringvalue     = value.toString();
	stringvalue     = stringvalue.replace(/\./, ",");
	return stringvalue;
}


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}


function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0

        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string
}

// Finds position of first occurrence of a string within another
function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}
