$(document).ready(function(){
	$("#birthday").datepicker({dateFormat: 'dd.mm.yy', yearRange: '-80:80'});
	$("#events").tablesorter();
	$("#users").tablesorter({sortList:[[0,1]], widgets: ["zebra"]});
	
	
	/*****************************************************
		Datepicker f�r die User Events
	******************************************************/
	$('input[@jtype=user_event]').each(function(){

		var id = this.id;
		
		$("#"+id).datepicker(
			{onClose: function(date){
                //console.log(date);
				$.post("events.php", { 'form': 'user_event', 'id':id, 'date': date }, function(status){
					if (status != "ok"){
						alert('failed to save');
					}
					else{
						event_id = id.replace(/user_event_/g, '');
						user_event_next_date_id = 'user_event_next_date_' + event_id;
						//alert(user_event_next_date_id);
						tmp_id = 'user_event_active_' + event_id;
/*						console.log(date);*/
						if (date != null){
							$.post("events.php", { 'form': 'user_event_active', 'id':tmp_id, 'set':'1' }, function(status){
								if (status == "ok"){
									$('#'+tmp_id).attr('class', 'status_green');
									
									$.get("events.php?action=get_coming_up&event_id="+event_id, function(coming_up){
										$('#'+user_event_next_date_id).html(coming_up);
										
									});
									
									info("Erfolgreich übernommen");
									
								}
								else alert('failed to save');
							});
						}
						else{
							$.post("events.php", { 'form': 'user_event_active', 'id':tmp_id, 'set':'-1' }, function(status){
								if (status == "ok"){
									$('#'+tmp_id).attr('class', 'status_red');
									$('#'+user_event_next_date_id).html('-');
									
									info("Erfolgreich übernommen");
									
								}
								else alert('failed to save');
							});
						}
							
					}
				});
			}, 
			dateFormat: 'dd.mm.yy', yearRange: '-80:10'}
		);		
	});
	

	/*****************************************************
		Datepicker für die User Events
	******************************************************/
	$('td[@jtype=user_event_active]').each(function(){

		var id = this.id;
		
	 	$('#'+id).bind("click", function(e){
			
			$.post("events.php", { 'form': 'user_event_active', 'id':id }, function(status){
				if (status == "active"){
					$('#'+id).attr('class', 'status_green');
				}
				else if (status == "inactive"){
					$('#'+id).attr('class', 'status_red');
				}
				
				else{
					alert('failed to save');
				}
				
			});
	 	})
		
	});


	
	

	
	
	
	
});
