$(function() {
	$('.event').click(function(){
		var id = $(this).attr('val');
		$('.subevent').addClass('hidden');
		$('.subevent-'+id).removeClass('hidden');
		$('.event').removeClass('active');
		$(this).addClass('active');
	});

	$('.save-event').click(function(){
		var name = $('.event-name').val();
		var text = $('.event-text').val();
		var hidden = $('.event-hidden').val();
		$.ajax({
  			url: "/api/create/",
  			type: "POST",
  			data: {'name': name, 'text': text, 'hidden': false, 'type': 'event'},
  			dataType: 'json',
  			success: function(data){
				var h = get_new_event_html(name, text, hidden, data.id);
				$('.event-div').append(h);
			},
		});
	});

	$('.delete-event').click(function(){
		var id = $(this).attr('val');
		var type = $(this).attr('type');
		$.ajax({
  			url: "/api/delete/",
  			type: "GET",
  			data: {'id': id, 'type': type},
  			dataType: 'json'
		});

		$(this).parent().remove();
	});
});

function get_new_event_html(name, text, hidden, id){
	var hidden_str = '';
	if (hidden) {
		hidden_str = 'list-group-item-warning';
	}

	var s = '<a href="#" class="list-group-item event' + hidden_str + ' val="{{event.id}}">';  
	s += name;
    s += '<button type="button" class="btn btn-default btn-xs pull-right delete-event" val="'+id+'">';
    s += '<span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span>';
    s += '</button>';
    s += '</a>';
    return s;
}
@@ -669,21 +669,46 @@
 );%0A%09%7D);%0A
-%0A%09$('
+%7D);%0A%0A%0A$(document).on(%22click%22,%22
 .delete-
@@ -708,33 +708,27 @@
 delete-event
-').click(
+%22, 
 function()%7B%0A
@@ -719,33 +719,32 @@
 t%22, function()%7B%0A
-%09
 %09var id = $(this
@@ -758,17 +758,16 @@
 'val');%0A
-%09
 %09var typ
@@ -792,17 +792,16 @@
 type');%0A
-%09
 %09$.ajax(
@@ -794,34 +794,32 @@
 pe');%0A%09$.ajax(%7B%0A
-  
 %09%09%09url: %22/api/de
@@ -822,26 +822,24 @@
 i/delete/%22,%0A
-  
 %09%09%09type: %22GE
@@ -838,26 +838,24 @@
 ype: %22GET%22,%0A
-  
 %09%09%09data: %7B'i
@@ -877,18 +877,16 @@
  type%7D,%0A
-  
 %09%09%09dataT
@@ -901,16 +901,14 @@
 on'%0A
-%09
 %09%7D);%0A%0A
-%09
 %09$(t
@@ -931,21 +931,16 @@
 move();%0A
-%09%7D);%0A
 %7D);%0A%0Afun
@@ -1122,16 +1122,17 @@
 em event
+ 
 ' + hidd

delete works for new elements