$(document).ready(function() {
	if ($('.huidigebestanden').length>0){
		jData = eval($('.huidigebestanden').val());
		$(jData).each(function(){
			naam = $(this)[0].fields.naam;
			if (naam.length>36){
				naam = naam.substr(0, 36)+"...";
			}
			pk = $(this)[0].pk;
			//item = $(this)[0];
			
			//console.log(item.fields);
			insert_string = "<div class=\"textinput\" style=\"margin-left:120px;\">"+naam
			insert_string += '<span class="deldoc" id="del-'+pk+'">1</span></div>';
			$('.huidigebestanden').before(insert_string);
		});
		
		$('span.deldoc').click(function(){
			id = $(this).attr('id').replace('del-','');
			//console.log(id);
			$.getJSON("/documenten/"+id+"/verwijderen/?ajax=true",
		        function(data){
					if(!eval(data.result)){
						alert('Dit bestand is niet verwijderd! U heeft geen rechten voor deze handeling.')
					}else{
						curItem = $('#del-'+id)
						curItem.parent().hide('slow');
					}
		        });
		});
	}
	/*
	 * 
	 if (($('.kern-submenu').length > 0)&&($('#sidebar').length > 0)){
		//console.log($('.kern-submenu').position().top);
		$('#sidebar').css('margin-top', ($('.kern-submenu').position().top-40)+'px');
	}
	$('#sidebar').show();
	*/
	
	$('#id_datum').datepicker({
			dateFormat: 'yy-mm-dd', 
			//minDate: -2,
			numberOfMonths: 2,
			showButtonPanel: true
	});
	$.datepicker.regional['nl'];
	
	$('#id_van').timepickr({prefix:['ochtend', 'middag'], convention: 24, trigger: 'click', resetOnBlur: true});
	$('#id_tot').timepickr({prefix:['ochtend', 'middag'], convention: 24, trigger: 'click', resetOnBlur: true});

	
	function check_extension(e) {
		//hash is defined in template
		submitEl = $("input.frm-doc-add-submit");
		//console.log(hash);
		filename = $(e.currentTarget).val();
		if (filename==""){
			filename = $("#document-form input[type=file]").val();
		}
		
		if (filename != "") {
			var re = /\..+$/;
			var ext = filename.match(re);
			//var submitEl = document.getElementById(submitId);
			if (hash[ext]) {
				//console.log('hij heeft em');
				submitEl.removeAttr("disabled", "disabled");
				$('#div_id_bestand').removeClass('error');
				
				return true;
			}
			else {
				//console.log('hij heeft em niet');
				alert("Ongeldig bestandstype, selecteer een ander bestand.");
				//console.log(submitEl);
				submitEl.attr("disabled", "disabled");
				$('#div_id_bestand').addClass('error');
				
				return false;
			}
		}
	}
	
	function add_file_input(e){
		if (check_extension(e)){
			//console.log(e.currentTarget);
			new_input = $(e.currentTarget).clone();
			new_input.addClass("new-fileinput");
			new_input.val("");
			new_input.attr("id", "id_bestand-"+ ($("#div_id_bestand input[type=file]").length+1))
			new_input.attr("name", "bestand-"+ ($("#div_id_bestand input[type=file]").length+1))
			new_input.bind("change", add_file_input);
			$("#div_id_bestand").append(new_input);
			new_input.fadeIn('slow');
		}
	}	
	
	$("#evenement-form input[type=file]").bind("change", add_file_input);
	
	$("#document-form input[type=file]").bind("change", check_extension);
	
	$('.search_result_header').click(function(){
		if ($(this).parent().find('.search_result_items').css('display')=="block"){
			$(this).parent().find('.search_result_items').slideUp();
		}else{
			$(this).parent().find('.search_result_items').slideDown('fast');
		}	
	});
	
	$('.document ul').click(function(){
		if ($(this).parent().find('.doc-more-info').css('display')=="block"){
			$(this).parent().find('.doc-more-info').slideUp();
		}else{
			$(this).parent().find('.doc-more-info').slideDown('fast');
		}	
	});
	
	$('.evenement ul').click(function(){
		if ($(this).parent().find('.eve-more-info').css('display')=="block"){
			$(this).parent().find('.eve-more-info').slideUp("fast");
		}else{
			$(this).parent().find('.eve-more-info').slideDown("fast");
		}	
	});
	
	$('#eventtree li .eventgrouper').click(function(){
		//console.log($(this).find('.evenement:first').css('display'));
		if($(this).parent().find('.evenement:first').css('display')=="block"){
			$(this).parent().find('.evenement').slideUp("fast");
		}else{
			$(this).parent().find('.evenement').slideDown("fast");
		}
	});
	
	$('#id_prive').before('<div class="slotje">&nbsp;</div>');
	$('#id_prive').before('<div class="slotje_uit">&nbsp;</div>');
	$('#id_prive').click(function(){
		if(!$(this).is(":checked")){
			$('#id_prive').parent().find('.slotje').fadeOut('fast');
			$('#id_prive').parent().find('.slotje_uit').fadeIn('fast');
		}else{
			$('#id_prive').parent().find('.slotje').fadeIn('fast');
			$('#id_prive').parent().find('.slotje_uit').fadeOut('fast');
		}
	});
	
	/*
	 $('#groepen-beschikbaar input').click(function(){
		
		groepID = $(this).attr('id').replace('cb-grp-', '');
		if($(this).attr('checked')){
			window.location.href = '/groepen/add/'+groepID+'/?r='+cur_page;
		}else{
			window.location.href = '/groepen/remove/'+groepID+'/?r='+cur_page;
			
		}
		
		//
	});
	*/
	
	$('#id_beschermd').before('<div class="slotje">&nbsp;</div>');
	$('#id_beschermd').before('<div class="slotje_uit">&nbsp;</div>');
	if (!$('#id_beschermd').is(":checked")){
		$('#id_beschermd').parent().find('.slotje').fadeOut('fast');
		$('#id_beschermd').parent().find('.slotje_uit').fadeIn('fast');
	}else{
		$('#id_beschermd').parent().find('.slotje').fadeIn('fast');
		$('#id_beschermd').parent().find('.slotje_uit').fadeOut('fast');
	}
	
	$('#id_beschermd').click(function(){
		if(!$(this).is(":checked")){
			$('#id_beschermd').parent().find('.slotje').fadeOut('fast');
			$('#id_beschermd').parent().find('.slotje_uit').fadeIn('fast');
		}else{
			$('#id_beschermd').parent().find('.slotje').fadeIn('fast');
			$('#id_beschermd').parent().find('.slotje_uit').fadeOut('fast');
		}
	});
	
	$('#add-groups').click(function(){
		$('#add-groups').val().replace("\+", "-");
		if ($('#add-groups').html().substr(0, 1)=="+") {
			$('#groepen-beschikbaar').slideDown('fast');
			$('#add-groups').html($('#add-groups').html().replace("+", "-"));
		}else{
			$('#groepen-beschikbaar').slideUp('fast');
			$('#add-groups').html($('#add-groups').html().replace("-", "+"));
		}
	});
	
	
});
