$(document).ready(function(){

	$(".reacties").click(function(){
		id=$(this).attr('id').substr(5);
		if($("#comments_"+id).is(':visible')){
			$("#comments_"+id).hide('slow');
		}else{
			$("#comments_"+id).show('slow');
		}
	});

	function showFormRequest(formData, jqForm, options){
		return true; 
	}

	function showFormResponse(responseText, statusText, xhr, form){
		
		$(".commentForm").each(function(index){
			if($(this).is(':visible')){
				var id=$(this).attr('name').substr(13);

				if(responseText=='captcha_error'){
					$("#captcha_error_"+id).html('Foutieve code');
				}

				
			}
		});
		

		if(trim(responseText)=='ok'){
			$(".comment_form").hide('slow');
			var d=new Date();
			var datetime=d.getDate()+"-"+d.getMonth()+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes();
			var html='<div class="comment"><div class="comment_header">posted '+datetime+' by '+$("#name_"+id).val()+'</div><div class="comment_body">'+$("#comment_"+id).val()+'</div></div>';
			$("#comments_"+id).prepend(html);
		}

		if(responseText=='submit_error'){
		}
	}


	var options = { 
        beforeSubmit:  showFormRequest,   
        success:       showFormResponse  
    }; 
		
	$('.commentForm').ajaxForm(options); 
	

	$(".reageer_button").click(function(){
		var id=$(this).attr('id').substr(7);
		if($("#comment_form_"+id).is(':visible')){
			$(".comment_form").hide('slow');
		}else{
			$(".comment_form").hide('slow');
			$("#comment_form_"+id).show('slow');
		}
	});
	var remove_comment_options = { 
		beforeSubmit:  showRemoveRequest,   
		success:       showRemoveResponse  
	}; 

	

	$('.remove_comment').ajaxForm(remove_comment_options);

	function showRemoveRequest(formData, jqForm, options){
		
		return true; 
	}

	function showRemoveResponse(responseText, statusText, xhr, form){
		$("#comment_"+trim(responseText)).remove();	
	}
})


function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}
