$(document).ready(function(){
	// 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'
		});
	});
	
	// videoplayer
	$('.videoplayer').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',
			allowfullscreen: 'true'
		});
	});
	
	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');
	
//	$(".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(){
        $('.flashzaal').each(init_flashzaal);

		var targetid = $(this).attr('rel');
		if (typeof targetid == 'undefined' || targetid == '') return ;
		
		$(this).colorbox({
			open: true,
			inline:true,
			transition:'none',
			href:"#" + targetid
		});
	});
	
	$('.submitlink').click(function(){
		$(this).parents('form:first').append('<input type="hidden" name="javascriptsubmit" value="true" />').trigger('submit');
        $(this).removeClass('submitlink');
		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>'));
				}
			}
		})
	})
});

/**
 * Use colorbox to display a slideshow
 */
function showPhoto(path, items, index)
{
	if (typeof $().colorbox != "function" || typeof path != "string" || typeof items != "object")
		return false;
	
	if (typeof index == "undefined")
		index = 0;
	index = parseInt(index);
	if (index == NaN)
		index = 0;
	if (index >= items.length)
		return false;

	var container = $('<div/>').hide();
	$('body').append(container);
	var photolinks = new Array();
	var colorboxConfig = {
		previous: 'Vorige',
		next: 'Volgende',
		current: 'Afbeelding {current} van {total}',
		onClosed: function(){
			container.remove();
		}
	};
	
	for (var i = 0; i < items.length; i++)
	{
		photolinks[i] = $('<a rel="openalbum" href="' + path + items[i] + '">Afbeelding ' + (i+1) + '</a>').colorbox(colorboxConfig);
		container.append(photolinks[i]);
	}

	//open item of index
	photolinks[index].trigger('click');
}

// 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;
}
