$(document).ready(function(){
	/*
    * Function : itemDetailsShareSearch()
    * This function will be called when someone whants to search inside the share container
	*/
	
	$(document).off('keydown','.share-popup input');
	$(document).on('keydown','.share-popup input',function(){
		shareSearch($(this)); 
	});	
	 
	function shareSearch(obj){
		var value = $(obj).val();
		var count = 0;
		$('.share-popup .user').each(function(){
			if(count > 0){
				if($(this).text().toUpperCase().indexOf(value.toUpperCase()) != -1){
					$(this).show();
				}else{
					$(this).hide();   
				}
			}
			count++; 
		});
	}

	
	/*==============================================*/
	/*            Check all share checkboxes        */
	/*==============================================*/	
	 
	$(document).off('change','.share-popup .share-all input');
	$(document).on('change','.share-popup .share-all input', function(e){ 
		$('input[name="share_search"]').val('');
		$('.share-popup .user').show();
		if(!$(this).hasClass('all-checked')){
			// Checkes all the checkboxes
			$('.share-popup input:checkbox').prop('checked','checked');
			// Add a class so we kan uncheck the checkboxes the next time we click on the ".check-all".
			$(this).addClass('all-checked');
		}else{ 
			// Unchecks all he checkboxes
			$('.share-popup input:checkbox').prop('checked', false);
			// Remove the class so we can check the inputs the next time we click on the ".check-all".
			$(this).removeClass('all-checked');
		} 
    });
	
	
/*===================================================================================================================================================================
	Open sendItemToUsers
===================================================================================================================================================================*/			

	/*
    * Function : sendItemToUsers()
    * This function will be called when someone whants to send an item
    */
	$(document).off('click','.share-popup .send-share');
	$(document).on('click','.share-popup .send-share', function(){ 
		var count 		= 0;  
		var postData 	= {};
		var itemId 		= $(this).parents('.parent').attr('data-item-id');
		if(itemId < 1 || itemId == '' || itemId == null){
			itemId = $('.top-wrapper.detail').attr('data-item-id');
		}
		$(".share-popup input[type=checkbox]:checked").each(function(){
			postData[$(this).attr('data-user-id')] = $(this).attr('data-user-id');
			count++;
		});
		
		var ics = 0;
		if(count == 0){
			$(this).showNotification('Geen gebruiker geselecteerd' ,0); 
			return false;
		}

		if($('.general-popup-top-right').hasClass('module_meeting')){

			if($('input[name="sendMeetingIcs"]').is(':checked')){
				ics = 1;	
			} 
		}

		$(this).ajax("/item/sendItemToUsers",{data : postData, 'personalText':  $('#personalText').val(), emailText : $('#email_tekst').val(), 'ics' : ics, emailSubject : $('#email_onderwerp').val(), 'itemId' : itemId}, function(data) {
			$('.sended-info .user').hide();
			// Nu degene weer zichtbaar maken welke wel geselecteerd waren
			$.each( postData, function( key, value ) {
				$('.user_'+value).show(); 
			});  
			$('.general-popup.active .general-popup-right').css("min-width","calc(100% - 0px").css("background-color","#f4f4f4");
			$('.general-popup-bottom .item-title').html('"'+$('.parent_'+itemId+' .input-title').val()+'" is gedeeld met:');
			$('.general-popup-left-group, .general-popup-content-right, .send-share, .general-popup-bottom').hide();
			$('.sended-info').fadeIn();
		});
		
	});	
	
	
});