$(document).ready(function() {

	var voteDialog = $('#dialog_vote');
	
	if (voteDialog.length > 0) {
	
		/* Setup voting if available */
	
		setupVoting();
	
	} 

});

$(document).ready(function() {
	
	dialogs = $('div[id^="dialog"]');

	dialogs.each(function(){
		
		var dialogId = '#'+$(this).attr('id');
			
		if (dialogId != '#dialog_vote') {
			
			var close = $(dialogId + " .title a");
			
			var save = $(dialogId + ' input[name="speichern"]');
			
			var abort = $(dialogId + ' input[name="abbrechen"]');
			
			var mask = $('#mask');
		
			
			close.click(function(e){
				
				e.preventDefault();
				
				$(this).parents('div[id^="dialog"]').first().fadeOut(1000);
				
				$('#mask').fadeOut(1000);
				
			});
			
			save.click(function(e){
				
				e.preventDefault();
					
				$(this).parents('form').submit();
			
			});
			
			abort.click(function(e){
				
				e.preventDefault();
				
				$(this).parents('div[id^="dialog"]').first().fadeOut(1000);
				
				$('#mask').fadeOut(1000);
				
			});	
	
			
	    	var target = $(this).attr('id').replace(/dialog_/g, "");
			
			if (dialogId.search(/tab-2d/) == -1) {			
					
					$("#form_"+target).ajaxForm({
						
						type: 'POST',
						
						data: {
							
							eID: EID,
							
							tx_bsfcselfediting_bsfc_content_id: target,
							
							cmd: 0x200
							
						},
						
						dataType: 'xml',
										
						success: handleResponse				
					});
	
			} else {
			
			    $("#form_"+target).submit(function(){
	        
	            // Prevent form from sending
	            
	            return false;
	        
	        });
					
					$(dialogId+' input[name="speichern"]').click(function() {
	            	
	            	var employees = {};
	            	
	            	var i=0;
	            	
	            	$('tr.employee').each(function(){
	            		
	            		var firstname = $('span.firstname', this).text();
	            		
	            		var lastname = $('span.lastname', this).text();
						
						var appointment = $('span.appointment', this).text();
	            		
	            		
	            		var employee = {firstname: firstname, lastname: lastname, appointment: appointment};
	            		
	            		employees[i++] = employee;
	            		
	            	});
	            	
	            	
	            	$.ajax({            	
	            	
	            		url: 'index.php',
	            		
	            		type: 'POST',
						
						dataType: 'xml',
	            		
	            		async: false,
	            		
	            		cache: false,
	            		
	            		data: {
						
						   cmd: 0x200,	
						
	                       eID: EID,
	                       
	                       tx_bsfcselfediting_bsfc_content_id: target,
	                        
	                       employees: employees
	                       
	                       },                        
	            		
						success: employeeSend
	            	});
	            	
	            	return false;
	
	          });
					
			}
			
		}
	});	
	
	
	$('a.editlink').each(function(){
		
		$(this).click(function(e) {
			
			var id = $(this).attr('rel');
			
			e.preventDefault();
			
			$.ajax({
			
				type: 'POST',
				
	  		url: 'index.php',
	  		
				async: false,
				
				cache: false,
				
	 			success: mapDialogFields,
	 			
				error: handleError,
				
	  		dataType: 'xml',	
	  		
	  		data: {
	  		
							eID: EID,	
							
							tx_bsfcselfediting_bsfc_content_id: id
              	
						}
						
				});
	    	
			
				
	    	return false;
		
			}
		);
	
	});
	
	$('#mask').click(function(e){
		
			dialogs = $('div[id^="dialog"]');
			
			dialogs.each(function(){
			
				$(this).fadeOut(1000);
				
				});
		
			e.preventDefault();
			
			$(dialogId).fadeOut(1000);
			
			this.fadeOut(1000);
			
		});	
	
});
