function show_league(id){
	
	if(id == "-1"){
		$("tr").show();
	}
	else if(id != 0){
		$("tr").hide();
		$("."+id).show();
		$("#list_title").show();
	}
}

function update_team_list(type){

	var id = $("#leagueId").val();
	var team = -1;
	var data = "";
	$("#teamId").empty();
	$("#teamId").append("<option value='-1'>Choisissez une équipe</option>");
	
	if($("#id").length > 0){
		team = $("#id").val();	
	}
		
	$.ajax({
		type: "get",
		url: site_url+"/xml/getTeams.php",
		data: "type="+type+"&id="+id,
		dataType: "xml",
		success: function(xml){ //so, if data is retrieved, store it in html
			$(xml).find('equipe').each(function(){
  				var teamId = $(this).find('id').text();
                var teamName = $(this).find('nom').text();
				if(team == teamId)
					$("#teamId").append("<option value='"+teamId+"' selected='selected'>"+teamName+"</option>");
				else
					$("#teamId").append("<option value='"+teamId+"'>"+teamName+"</option>");				
            });
			
			if(type <= 1)			
				update_version_list(1);
			else if(type == 2)
				update_chant_list(0);
		}
	});
}

function show_teams(){
	
	var id = $("#leagueId").val();
	$("#teams").empty();
	var fond = "fond1";
	
	$.ajax({
		type: "get",
		url: site_url+"/xml/getTeams.php",
		data: "type=0&id="+id,
		dataType: "xml",
		success: function(xml){ //so, if data is retrieved, store it in html
			$(xml).find('equipe').each(function(){
  				var teamId = $(this).find('id').text();
                var teamName = $(this).find('nom').text();
									
				var img = document.createElement("img");
					img.src = site_url+"/images/image.php/"+teamName+".png?width=20&image="+site_url+"/images/logosclubs/"+teamId+".png";
					img.alt = teamName;
				
				var modifier = document.createElement("a");
					modifier.href = "editTeam.php?id="+teamId;
					modifier.title = "Editer";
					modifier.setAttribute("class", "editer");
					modifier.className = "editer";
					
				var supprimer = document.createElement("a");
					supprimer.href = "javascript:delete_team("+teamId+")";
					supprimer.title = "Supprimer";
					supprimer.setAttribute("class", "supprimer");
					supprimer.className = "supprimer";
				
				var tbody = document.createElement("tbody");
				var row = document.createElement("tr");
				var col = new Array();
				
				for(var i=0; i<3; i++){
					col[i] = document.createElement("td");	
				}
				
				col[0].setAttribute("class", "small_logo "+fond);
				col[0].className = fond+" small_logo";
				col[0].appendChild(img);
				
				col[1].appendChild(document.createTextNode(teamName));
				col[1].setAttribute("class", fond+" nom");
				col[1].className = fond+ " nom";
				
				col[2].appendChild(modifier);
				col[2].appendChild(document.createTextNode(" "));
				col[2].appendChild(supprimer);
				col[2].setAttribute("class", fond+ " actions");
				col[2].className = fond+ " actions";
				
				for(var i=0; i<3; i++){
					row.appendChild(col[i]);
				}
				tbody.appendChild(row);
				$("#teams").append(tbody);		
				
				if(fond == "fond1")
					fond = "fond2";
				else
					fond = "fond1";
				
            });
		}
	});
}

function update_version_list(type){
	
	var id = $("#teamId").val();
	var versionGet = -1;
	
	$("#version").empty();
	$("#version").append("<option value='-1'>Choisissez une version</option>");
	
	if($("#versionId").length > 0){
		versionGet = $("#versionId").val();	
	}

	$.ajax({
		type: "get",
		url: site_url+"/xml/getVersions.php",
		data: "type="+type+"&id="+id,
		dataType: "xml",
		success: function(xml){ //so, if data is retrieved, store it in html
			$(xml).find('version').each(function(){
  				var versionId = $(this).find('id').text();
                var version = $(this).find('nom').text();
				if(versionGet == versionId)
					$("#version").append("<option value='"+versionId+"' selected='selected'>"+version+"</option>");
				else
					$("#version").append("<option value='"+versionId+"'>"+version+"</option>");
            });	
			update_kit_list(0);
		}
	});
}

function update_kit_list(type){
	
	var id = $("#teamId").val();
	var version = $("#version").val();
	$("#kits").empty();
	var fond = "fond1";
	$.ajax({
		type: "get",
		url: site_url+"/xml/getKits.php",
		data: "type="+type+"&id="+id+"&version="+version,
		dataType: "xml",
		success: function(xml){ //so, if data is retrieved, store it in html
			$(xml).find('kit').each(function(){
						 
  				var kitId = $(this).find('id').text();
                var nom = $(this).find('nom').text();
				var auteur = $(this).find('auteur').text();
				var type = $(this).find('type').text();
				
				var img = document.createElement("img");
					img.src = site_url+"/edition/images/image.php/"+nom+"?width=50&image="+site_url+"/edition/images/kits/"+nom;
					img.alt = nom;			
					
				var modifier = document.createElement("a");
					modifier.href = "editKit.php?id="+kitId;
					modifier.title = "Editer";
					modifier.setAttribute("class", "editer");
					modifier.className = "editer";
					
				var supprimer = document.createElement("a");
					supprimer.href = "javascript:delete_kit("+kitId+")";
					supprimer.title = "Supprimer";
					supprimer.setAttribute("class", "supprimer");
					supprimer.className = "supprimer";
				
				var tbody = document.createElement("tbody");
				var row = document.createElement("tr");
				var col = new Array();
				
				for(var i=0; i<4; i++){
					col[i] = document.createElement("td");	
				}

				col[0].setAttribute("class", "apercu_kit "+fond);
				col[0].className = fond+" apercu_kit";
				col[0].appendChild(img);
				
				col[1].setAttribute("class", "type "+fond);
				col[1].className = fond+" type";
				col[1].appendChild(document.createTextNode(type));
				
				col[2].setAttribute("class", "auteur "+fond);
				col[2].className = fond+" auteur";
				col[2].appendChild(document.createTextNode(auteur));
				
				col[3].appendChild(modifier);
				col[3].appendChild(document.createTextNode(" "));
				col[3].appendChild(supprimer);
				col[3].setAttribute("class", fond+ " actions");
				col[3].className = fond+ " actions";
				
				
				for(var i=0; i<4; i++){
					row.appendChild(col[i]);
				}
				tbody.appendChild(row);
				$("#kits").append(tbody);
            });	
		}
	});
}


function update_chant_list(type){

	var id = $("#teamId").val();
	$("#chants").empty();
	var fond = "fond1";
	$.ajax({
		type: "get",
		url: site_url+"/xml/getChants.php",
		data: "type="+type+"&id="+id,
		dataType: "xml",
		success: function(xml){ //so, if data is retrieved, store it in html
			$(xml).find('chant').each(function(){
						 
  				var chantId = $(this).find('id').text();
                var titre = $(this).find('titre').text();
				var lien = $(this).find('lien').text();
				
				var flashObject = document.createElement("object");
					flashObject.type = "application/x-shockwave-flash";
					flashObject.data = site_url+"/chants/dewplayer.swf?son="+site_url+"/chants/"+lien;
					flashObject.width = "200";
					flashObject.height = "20";
					
				var flashParam = document.createElement("param");
					flashParam.name = "movie";
					flashParam.value = site_url+"/chants/dewplayer.swf?son="+site_url+"/chants/"+lien;
	
				flashObject.appendChild(flashParam);	
				
				var tbody = document.createElement("tbody");
				var row = document.createElement("tr");
				var col = new Array();
				
				for(var i=0; i<3; i++){
					col[i] = document.createElement("td");	
				}
				
				col[0].setAttribute("class", fond+ " titre");
				col[0].className = fond +" titre";
				col[0].appendChild(document.createTextNode(titre));
				
				col[1].setAttribute("class", fond+" apercu");
				col[1].className = fond+" apercu";
				col[1].appendChild(flashObject);
				
				if(admin == 1){
					var modifier = document.createElement("a");
						modifier.href = "editChant.php?id="+chantId;
						modifier.title = "Editer";
						modifier.setAttribute("class", "editer");
						modifier.className = "editer";
						
					var supprimer = document.createElement("a");
						supprimer.href = "javascript:delete_chant("+chantId+")";
						supprimer.title = "Supprimer";
						supprimer.setAttribute("class", "supprimer");
						supprimer.className = "supprimer";
						
						col[2].appendChild(modifier);
						col[2].appendChild(document.createTextNode(" "));
						col[2].appendChild(supprimer);
				}
				else{
					var telecharger = document.createElement("a");
						telecharger.href = site_url+"/chants/"+lien;
						telecharger.title = "Télécharger";
						telecharger.setAttribute("class", "sauvegarder");
						telecharger.className = "sauvegarder";
						col[2].appendChild(telecharger);
						
				}
				
				col[2].setAttribute("class", fond+" actions");
				col[2].className = fond+" actions";	

				for(var i=0; i<3; i++){
					row.appendChild(col[i]);
				}
				tbody.appendChild(row);
				$("#chants").append(tbody);
				
				if(fond == "fond1")
					fond = "fond2";
				else
					fond = "fond1";
				
            });	
		}
	});
}


function delete_kit(id){

	if(confirm("Confirmer la suppression?")){
		
		$.ajax({
			type: "get",
			url: "../admin/functions.php",
			data: "type=0&id="+id,
			success: function(html){ //so, if data is retrieved, store it in html
				if(html == "0"){
					update_kit_list(0);
					if($("#kits").children().length == 0){
						update_version_list(1);
					}
				}
			}
		});
		
	}
}


function delete_chant(id){

	if(confirm("Confirmer la suppression?")){
				
		$.ajax({
			type: "get",
			url: "../admin/functions.php",
			data: "type=1&id="+id,
			success: function(html){ //so, if data is retrieved, store it in html
				if(html == "0"){
					update_chant_list(0);
					if($("#chants").children().length == 0){
						update_team_list(2);
					}
				}
			}
		});
	}
}


function delete_team(id){
	
	if(confirm("Confirmer la suppression?")){
		
		$.ajax({
			type: "get",
			url: "../admin/functions.php",
			data: "type=2&id="+id,
			success: function(html){ //so, if data is retrieved, store it in html
				show_teams();
			}
		});
	}		
}