jQuery.noConflict();

jQuery(document).ready(function(){ 

	jQuery("form").submit( function() {
		jQuery("#clubList").append('<td colspan="2" style="text-align:center;"><img src="/typo3conf/ext/announcement/pi1/loading.gif"></td>');
	});
	
	getSelection();
});

function getSelection() {

	jQuery("#clubList td").append('<img src="/typo3conf/ext/announcement/pi1/loading.gif">');
	
	jQuery.ajax({ 
		//erweitere aufzurufenden Link 
		url: "index.php",
		type: "GET",
		data: {
			eID: "tx_announcement_pi1",
			cmd: "init",
			sex: jQuery("#fSex").val(),
			event: jQuery("#fEvent").val(),
			club: jQuery("#fClub").val()
		},
		dataType: "json",
		
		// Es hat funktioniert? 
		success: function(response) {
			if(response.length > 0) { 

				var new_content = "";
				jQuery.each( response, function(i, row) {

					new_content+='<tr id="club'+row[0]+'">';
					new_content+=' <td style="vertical-align:bottom;">';
					new_content+='  <div style="cursor: pointer; margin-top:4px;" onClick="toggleClubMemberList('+row[0]+')">';
					new_content+='    <img src="/typo3conf/ext/announcement/pi1/plus.png" style="margin:1px 3px 5px 0; float:left;" alt="">';
					new_content+='    <span style="font-weight:bold;">'+row[1]+'</span>';
					new_content+='  </div>';
					new_content+=' </td><td style="text-align:right; vertical-align:bottom;">('+row[2]+' Teilnehmer)</td>';
					new_content+='</tr>';
					
				} );				

				// remove load symbol and add list of clubs
				jQuery("#clubList").empty().after(new_content);
			}	
		}, 
 
		// Schade, es war ein Fehler 
		error: function(error) { 
			jQuery("#clubList").empty().after('<tr><td colspan="5">Keine Daten gefunden!!!</td></tr>');
		} 
	}); 
}

function toggleClubMemberList(id) {
	if (jQuery("#mem"+id).length)
	{
		if (jQuery("#mem"+id+":hidden").length)
		{
			// toggle symbol and function in club-line
			jQuery("#mem"+id).show();
			jQuery("#club"+id+" img").attr("src", "/typo3conf/ext/announcement/pi1/minus.png");
		}
		else
		{
			// toggle symbol and function in club-line
			jQuery("#mem"+id).hide();
			jQuery("#club"+id+" img").attr("src", "/typo3conf/ext/announcement/pi1/plus.png");
		}
	}
	else
	{
		addClubMemberList(id);
		// toggle symbol and function in club-line
		jQuery("#club"+id+" img").attr("src", "/typo3conf/ext/announcement/pi1/minus.png");
		jQuery("#club"+id+" div").attr("onClick", "toggleClubMemberList("+id+")");
	}
}

function addClubMemberList(id) {
	// delet onChange event to prevent a second call of the ajax function
	jQuery("#club"+id+" span").attr("onClick", "");
	// add load symbol
	jQuery("#club"+id).after('<tr><td style="padding-left:22px;"><img src="/typo3conf/ext/announcement/pi1/load.gif"></td></tr>');

	// do the ajax call
	jQuery.ajax({ 
		//erweitere aufzurufenden Link 
		url: "index.php",
		type: "GET",
		data: {
			eID: "tx_announcement_pi1",
			cmd: "person",
			sex: jQuery("#fSex").val(),
			event: jQuery("#fEvent").val(),
			club_id: id
		},
		dataType: "json",
		
		// Es hat funktioniert? 
		success: function(response) { 
			//alert(response);
			if(response.length > 0) { 
			
				// add tabe of announcements for this club
				var new_content = '<tr id="mem'+id+'" style="display: table-row;"><td colspan="5"><table width="100%" style="text-align:left;">';
				new_content += '<colgroup><col width="5%"><col width="10%"><col width="40%"><col width="35%"><col width="10%"></colgroup>';
				
				// Add title row for this list
				new_content += '<tr><th></th>';
//ohne Startnummer			
//				new_content += '<th></th>';
// mit Startnummer
				new_content += '<th>Startnr.</th>';
				new_content += '<th>Name</th><th>Vorname</th><th style="text-align:right;">Event</th>';
				new_content += '</tr>';

				jQuery.each(response, function(i, row) {
					var sex;
					switch (row[3]) {
						case '0': sex='(m)'; break;
						case '1': sex='(w)'; break;
					}
					var event;
					switch (row[4]) {
						case '0': event='laufen'; break;
						case '1': event='skaten'; break;
						case '2': event='walken'; break;
					}
					new_content += '<tr style="color:#DF1200;">'
					new_content += '  <td>&nbsp;</td>'
// Ohne Startnummer
//					new_content += '  <td>&nbsp;</td>';
// mit Startnummer
					new_content += '  <td>'+row[0]+'</td>';
					new_content += '  <td>'+row[1]+'</td>';
					new_content += '  <td>'+row[2]+'   '+sex+'</td>';
					new_content += '  <td align="right">'+event+'</td>';
					new_content += '</tr>'+"\n"; 
				});
				
				new_content+= "</table></td></tr>";
        
				// remove load symbol
				jQuery("#club"+id+" + tr").remove();
				// add list of persons
				jQuery("#club"+id).after(new_content);
			}
		}, 
 
		// Schade, es war ein Fehler 
		error: function(error) { 
			//alert(error);
			// remove load symbol
			jQuery("#club"+id+" + tr").remove();
			jQuery("#club"+id).after('<tr><td colspan="2">Keine Daten gefunden!!!</td></tr>');
		} 
	}); 
	return false;
}
