



function fillteam( ids, div, season ){
		
		
	$( "#" + div ).html( '<div align="center"><br><img align="absmiddle" src="images/spinner.gif"> loading players...</div>' );	
	
	var url = host + 'json/players.' + pageExt;
        
    if ( ids != "" ) url +='?ids='+ids;
    if ( season != "" ) url +='?season='+season;
    
	$.getJSON( url , 
	     function( retcode ) {       		            
	            var presidente = '';
	            var allenatore = '';
	            var portiere = '';
	            var difensore = '';
	            var centrocampista = '';
	            var attaccante = '';
	            
	            //Presidente
	            //Allenatore
	            //Attaccante
	            //Centrocampista
	            //Difensore
	            //Portiere      
	                               
                $.each(retcode , function(i,item){
				  
				    $( "#" + div ).html( '<div align="center"><br><img align="absmiddle" src="images/spinner.gif"> loading ' + retcode[i].id + ' ...</div>' );
				
	                if ( i != 0 ) html +='<br>';
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Portiere" )       portiere += fillplayer( retcode[i], true  );     
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Difensore" )      difensore += fillplayer( retcode[i], true );   
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Centrocampista" ) centrocampista += fillplayer( retcode[i], true );   
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Attaccante" )     attaccante += fillplayer( retcode[i], true );       
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Allenatore" )     allenatore += fillplayer( retcode[i], false );         
	                if ( retcode[i].stato == "0" && retcode[i].ruolo == "Presidente" )     presidente += fillplayer( retcode[i], false );         
	            });	
	            
	            var html = '';
	            
	            html += '<div class="ui-widget-content ui-corner-all" style="padding:3px; margin-bottom:2px;">'+
		                '<a href="javascript:void(0);" onclick="$(\'.lastnotplayed\').show();">Visualizza tutti i giocatori della rosa</a>'+
		                '  |  ' + 
		                '<a href="javascript:void(0);" onclick="$(\'.lastnotplayed\').hide();">Visualizza solo chi ha giocato l\'ultima partita</a>'+
		                '</div>';
	            
	            if ( ids == "" ){ 
		               html += '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Portieri</div>' + portiere + '</div>' + 
		                       '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Difensori</div>' + difensore + '</div>' + 
		                       '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Centrocampisti</div>' + centrocampista + '</div>' + 
		                       '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Attaccanti</div>' + attaccante + '</div>'+
		                       '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Allenatore</div>' + allenatore + '</div>'+
		                       '<div class="ui-widget-content ui-corner-all" style="padding:3px;"><div class="ui-widget-header ui-corner-all" style="padding:4px">Presidente</div>' + presidente + '</div>';
	            }else{
		              html +=   portiere + difensore + centrocampista + attaccante + allenatore + presidente;		         
		        }
					            
		        
		        
	            
	            $( "#" + div ).html(html);  
	            
	            
	            $(".lastnotplayed").hide();
							
	     }
				
    );		
    
   	
}

var lastMatchObj = null;
var nextMatchObj = null;

function fillplayer( player, full ){
	var html = '';
	var classLastMatchPlayed="lastplayed";
	var classLastMatchNotPlayed="lastnotplayed";
	var classPlayer= ' ' + classLastMatchNotPlayed;
	
	var dataNascita = mysqlTimeStampToDate( player.datanascita ); 
    var dataNascitaDettaglio = dataNascita.getDate() + '/' + ( dataNascita.getMonth() + 1 ) + '/' + dataNascita.getFullYear();
    
    var htmlDett = '';
    var htmlVoti = '';
    var sitoUfficiale = '';
    //player.sito_ufficiale
    if ( player.sito_ufficiale != "" ){
	   sitoUfficiale = 'Sito Ufficiale: <a target="_blank" href="'+player.sito_ufficiale+'">' +  player.sito_ufficiale + '</a><br>';
    }
	//alert( lastMatchObj.id );
	    
    if ( full ) {
	   //inserimento voti
	   if ( lastMatchObj != null && ( player.ultimo_id_match == lastMatchObj.id || f_user_id == 2 ) ) { 
		  var extraInfo = ""; 
		  if ( f_user_id == 2 ) extraInfo += "[ADMIN]"; 
	      htmlDett ='<h3 style="color:#005d98; margin:2px; margin-top:2px">Voto giocatore '+extraInfo+'</h3>';   
	      htmlDett += getFormPlayerVote( player , lastMatchObj );	
	      classPlayer = ' ' + classLastMatchPlayed;
       }
	   //pannello ultimi voti	   
	   htmlVoti +='<div class="ui-state-highlight ui-corner-all" style="margin-top:2px; padding: 2px;" id="link_player_dett_voti_'+player.id+'">'+	              
				  '<b>::</b> <a href="javascript:void(0);" class="linkplayervote" onclick="getLastPlayerVote('+player.id+');">Visualizza voti</a></div>'; 
       htmlVoti +='<div style="margin-top:2px; padding: 2px;" id="player_dett_voti_'+player.id+'"></div>';	
       htmlVoti +='<div style="margin-top:2px; padding: 2px;" id="player_graph_'+player.id+'"></div>';		   
	}
	
	if ( player.ruolo == "Allenatore" || player.ruolo == "Presidente" ){
	   classPlayer = ' ' + classLastMatchPlayed;
	}
    
	htmlDett +='<div class="ui-state-highlight ui-corner-all" style="margin-top: 2px; padding: 2px;">'+
	            '<b>::</b> <a href="javascript:void(0);" class="linkaddplayervote" onclick="$(\'#tgl_info_'+player.id+'\').toggle();">Informazioni</a>'+
	            '</div>';  
	
	var htmlVoto = "";
	var htmlInfo = "";
	
    if ( full ) {		
	   //dettagli full
	   htmlInfo += '<div class="ui-state-highlight ui-corner-all" style="margin-top:2px; padding: 2px;">'+
	               'Altezza: ' + player.altezza + ' cm<br>' +
		           'Numero di maglia: ' + player.numero_maglia + '<br>'+
		           sitoUfficiale + 
				   '</div>';    
       htmlVoto = '<div align="center"><h1 style="color:#005d98; margin-bottom:0px; margin-top:2px">' + player.ultimo_voto + '</h1><br>Ultima Media Voto</div>';
       
	}
	
	html = '<div class="ui-corner-all'+classPlayer+'"  style="border: 1px solid #cccccc; padding:3px;margin-top:2px;" onmouseover="$(this).css(\'background-color\',\'#FAF8CC\');" onmouseout="$(this).css(\'background-color\',\'\');">' + 
		       '<div style="float:left; padding:2px;margin:2px;">' +
		       '<a href="javascript:void(0);" onclick="selectTab(0);fillNews(\'\',\''+player.id+'\',\'\',0);">'+
			   '<img style="border: 1px solid #005d98;" border="0" width="100" src="' + player.path_foto + '">'+
			   '</a>' +
               htmlVoto +			   
		       '</div>' +	       
		       '<div style="float:left; padding:2px;margin:2px; width:472px">' +
		            
		            '<h2 style="color:#005d98; margin-bottom:2px; margin-top:2px">' + player.cognome + " " + player.nome + '</h2><br>' + 
				    htmlDett + 	
				    '<div id="tgl_info_'+player.id+'" style="display:none">'+
				        htmlInfo +
						'<div class="ui-state-highlight ui-corner-all" style="margin-top:2px; padding: 2px;">'+
						'Nato a: ' + player.cittanascita + ' il ' + dataNascitaDettaglio + '<br>' +
						'Nazione: ' + player.nazione + '<br>' +
						'Stagioni: ' + player.season + '<br>' +	
						'</div>' + 			
					'</div>' + 		
					htmlVoti +				
		        '</div>' + 				
		        '<div style="clear:both"/>'+
	        '</div>'; 
	        
	        
	return html;
}


function getFormPlayerVote(playerObj,matchObj){
    var html = '';		
	
	var descrMatch = matchObj.t_home + '-' + matchObj.t_away + ' ' + matchObj.goal_home + '-' + matchObj.goal_away + ' (' + matchObj.data + ')';
	
	html +='<div style="margin-top:2px; padding:2px;" class="ui-state-highlight ui-corner-all">'+
	       '<input type="hidden" id="oper_'+playerObj.id+'" value="new_player_vote">'+
	       '<input type="hidden" id="id_player_'+playerObj.id+'" value="'+playerObj.id+'">'+
	       '<input type="hidden" id="id_match_'+playerObj.id+'" value="'+matchObj.id+'">'+		   
		   '<select id="combovoto_'+playerObj.id+'">'+
		       '<option value="2">2</option>'+
			   '<option value="3">3</option>'+
			   '<option value="4">4</option>'+
			   '<option value="4.5">4,5</option>'+
			   '<option value="5">5</option>'+
			   '<option value="5.5">5,5</option>'+
			   '<option selected value="6">6</option>'+
			   '<option value="6.5">6,5</option>'+
			   '<option value="7">7</option>'+
			   '<option value="7.5">7,5</option>'+
			   '<option value="8">8</option>'+
			   '<option value="8.5">8,5</option>'+
			   '<option value="9">9</option>'+
			   '<option value="10">10</option>'+			   
		   '</select>' + 
           '&nbsp;<span id="voto_id_'+playerObj.id+'"><b>' + descrMatch + '</b></span>' + '<br>' +
           'Commento al voto<br><textarea id="commento_'+playerObj.id+'" style="width:98%"></textarea>' +
           '<b>::</b> <a href="javascript:void(0);" class="linkaddplayervote" onclick="postPlayerVote('+playerObj.id+');">Invia il tuo voto</a>&nbsp;' + 
		   '</div>';	
		   
		   
    return html;		   
		   
}



function getLastMatchObj(){

        if ( lastMatchObj != null ) return lastMatchObj;

		var today = new Date;
		
        var url = host + 'json/partite.' + pageExt;
		try{															
	    	$.getJSON( url , 
			     function( retcode ) { 
				    $.each(retcode , function(i,item){ 					
					     var dataEvento = mysqlTimeStampToDate(retcode[i].data);                                
	                     if ( dataEvento < today ){						 
							 lastMatchObj = retcode[i];
							 fillHomeLastMatch();
							 return false;
						 }
					});							
				 }				
			);	
		   
        }catch(err){
		   alert( err.description );		   
	    }				 

}


function getNextMatchObj(){

        if ( nextMatchObj != null ) return nextMatchObj;

		var today = new Date;
		
        var url = host + 'json/partite.' + pageExt;
		try{															
	    	$.getJSON( url , 
			     function( retcode ) { 
				    $.each(retcode , function(i,item){ 					
					     var dataEvento = mysqlTimeStampToDate(retcode[i].data);                                
	                     if ( dataEvento > today ){						 
							 nextMatchObj = retcode[i];									 
						 }
					});							
					fillHomeNextMatch();
				 }				
			);	
		   
        }catch(err){
		   alert( err.description );		   
	    }				 

}

function fillHomeLastMatch(){
        var html = '<table align="center">';
        html += '<tr>';                                                                 
				
   	    html += '<td>'+
				'<div class="ui-widget-content ui-corner-all" style="padding:2px;margin:1px;text-align:center;width:80px;">'+
				'<img width="50" height="50" border="0" src="' + lastMatchObj.t_home_logo + '.thumb.gif">'+
				'<br>' + lastMatchObj.t_home + '</div>'+
				'</td>';
				 		 
		html += '<td align="center" style="width:80px"><h1>' + lastMatchObj.goal_home + ":" + lastMatchObj.goal_away + '</h1></td>';
		 		
		html += '<td>'+
				'<div class="ui-widget-content ui-corner-all" style="padding:2px;margin:1px;text-align:center;width:80px;">'+
				'<img width="50" height="50" border="0" src="' + lastMatchObj.t_away_logo + '.thumb.gif">'+
				'<br>' + lastMatchObj.t_away + '</div>'+
				'</td>';	
				
		html += '</tr>';
		
		html += '<tr>';      
		html += '<td colspan="3"><div class="ui-state-highlight ui-corner-all" style="padding:2px; text-align:center;">' + lastMatchObj.data + ' - ' + lastMatchObj.torneo + 
		        '</div>' + 
		        '</td>';	
		html += '</tr>';
		
		html += '</table>';


        $("#home_last_match").html(html);

}

function fillHomeNextMatch(){
        var html = '<table align="center">';
        html += '<tr>';                                                                 
				
   	    html += '<td>'+
				'<div class="ui-widget-content ui-corner-all" style="padding:2px;margin:1px;text-align:center;width:80px;">'+
				'<img width="50" height="50" border="0" src="' + nextMatchObj.t_home_logo + '.thumb.gif">'+
				'<br>' + nextMatchObj.t_home + '</div>'+
				'</td>';
				 		 
		html += '<td align="center" style="width:80px"><h1 style="color: gray;">' + nextMatchObj.goal_home + ":" + nextMatchObj.goal_away + '</h1></td>';
		 		
		html += '<td>'+
				'<div class="ui-widget-content ui-corner-all" style="padding:2px;margin:1px;text-align:center;width:80px;">'+
				'<img width="50" height="50" border="0" src="' + nextMatchObj.t_away_logo + '.thumb.gif">'+
				'<br>' + nextMatchObj.t_away + '</div>'+
				'</td>';	
				
		html += '</tr>';
		
		html += '<tr>';      
		html += '<td colspan="3"><div class="ui-state-highlight ui-corner-all" style="padding:2px; text-align:center;">' + nextMatchObj.data + ' - ' + nextMatchObj.torneo + 
		        '</div>' + 
		        '</td>';	
		html += '</tr>';
		
		html += '</table>';


        $("#home_next_match").html(html);

}


function getLastPlayerVote(idPlayer){
    
	    $("#link_player_dett_voti_" + idPlayer ).slideUp(1000);  
		$("#player_dett_voti_" + idPlayer ).addClass('ui-state-highlight ui-corner-all');
	    $("#player_dett_voti_" + idPlayer ).html('<img align="absmiddle" src="images/spinner.gif"> Processing...');  
		
	    var html = '';
		
		var dataChart = [];
		
        var url = host + 'json/player_vote.' + pageExt + '?id_player=' + idPlayer;
		try{															
	    	$.getJSON( url , 
			     function( retcode ) { 
				    $.each(retcode , function(i,item){ 	
                           //alert( retcode[i].desc_match + ' ' + retcode[i].mediavoto );	
					       html +=  '<b>::</b> <b>' + retcode[i].desc_match + '</b> media voto <span style="font-weight:bold; color:'+retcode[i].color+'">' + retcode[i].mediavoto + '</span><br/>';						   						   
						   dataChart.push( [ ( parseInt(retcode[i].match_date)*1000 ) , parseFloat( retcode[i].mediavoto2 ) ] ); //[ parseInt( retcode[i].id_match )
						   
					});		
					
					if ( html != '' ){
                       $( "#player_dett_voti_" + idPlayer ).html( html );
					   try{
					      var options = {
							    xaxis: {
								   mode: "time",
								   timeformat: "%d/%m",
								   minTickSize: [3, "day"] 
							    }

						  };
					   
					      $("#player_graph_" + idPlayer ).css( "height", "250px" );
					      $.plot( $("#player_graph_" + idPlayer ), [dataChart], options );					   
					   }catch(err){
					      $("#player_graph_" + idPlayer ).html( 'Errore: ' + err.description );
					   }
					}else{
					   $( "#player_dett_voti_" + idPlayer ).html( 'Nessun voto inserito!<br>Voi essere il primo? Clicca su \'<b>Invia il tuo voto</b>\'' );  
					}
				 }				
			);	
			
			 

        }catch(err){
		   alert( err.description );		   
	    }				 
}

function postPlayerVote(idPlayer){
     $("#voto_id_" + idPlayer ).html('<img align="absmiddle" src="images/spinner.gif"> Processing...');     	 
	 var oper = $("#oper_" + idPlayer ).val();
	 var id_player = $("#id_player_" + idPlayer).val();
	 var id_match = $("#id_match_" + idPlayer).val();	 
	 var voto = $("#combovoto_" + idPlayer).val();
	 var commento = $("#commento_" + idPlayer).val();
	 
	 $.post( host + "json/hub.php", { oper: oper, id_player: id_player, id_match: id_match, voto: voto, f_username: f_username, commento: commento, user_id: f_user_id, f_username: f_username },
		 function(data){
			$( "#voto_id_" + idPlayer ).html( data );     
		 }
	 );	
}




function getMatchVote(idMatch){
    
	    
	    $("#link_match_dett_voti_" + idMatch ).slideUp(1000);  
		$("#match_dett_voti_" + idMatch ).addClass('ui-state-highlight ui-corner-all');
	    $("#match_dett_voti_" + idMatch ).html('<img align="absmiddle" src="images/spinner.gif"> Processing...');  
		
	    var html = '';

        var url = host + 'json/player_vote.' + pageExt + '?id_match=' + idMatch;
		try{															
	    	$.getJSON( url , 
			     function( retcode ) { 
				    html +=  '<table cellpadding="2" cellspacing="0" border="0">'; 
				    $.each(retcode , function(i,item){ 	   
					       html +=  '<tr>';
					       html +=  '<td valign="top">';
					       html +=  '<img style="border:1px" align="absmiddle" height="50px" width="36px" src="' + retcode[i].player_image + '.thumb.gif"/>';     
					       html +=  '</td>';
					       html +=  '<td valign="top">';
					       html +=  '<span style="font-size:15px; font-weight:bold; color:'+retcode[i].color+'">' + 
						            retcode[i].mediavoto + '</span> ' + 
						            '<a title="Visualizza news giocatore" style="font-size:15px; font-weight:bold;" href="javascript:void(0);" onclick="selectTab(0);fillNews(\'\',\''+retcode[i].id_player+'\',\'\',0);">'+			                          
									retcode[i].desc_player + 
									'</a>' +
									'<br>' + 
									'<span id="voti_' + idMatch + '_' +  retcode[i].id_player + '"><a href="javascript:void(0);" onclick="getAllMatchPlayerVote(\'voti_' + idMatch + '_' +  retcode[i].id_player + '\',\''+idMatch+'\',\''+retcode[i].id_player+'\');">Leggi tutti i commenti</a></span>' + 
									'</span>'+
									'<br/>';
						   html +=  '</td>';
					       html +=  '</tr>';		
							
							//getAllMatchPlayerVote		
					});		
					
					html +=  '</table>';
					
					if ( html != '' )
                       $( "#match_dett_voti_" + idMatch ).html( '<h3 style="color:#005d98; margin-bottom:2px; margin-top:2px">Media voti dei nostri lettori</h3>' +
																html  + 
																'<br/><b>E\' possibile votare nell\'area \'<i>Squadra/Voti</i>\'</b>');  					
					else
					   $( "#match_dett_voti_" + idMatch ).html( 'Nessun voto presente in archivio' );  
				 }				
			);	
			
			 

        }catch(err){
		   alert( err.description );		   
	    }				 
}


function getAllMatchPlayerVote( div, idMatch, idPlayer ){

	//http://www.inter247.it/json/player_vote.php?id_match_all_p=33&id_player=7	
	    var html = '';
	    var url = host + 'json/player_vote.' + pageExt + '?id_match_all_p=' + idMatch + '&id_player='+idPlayer;
	 
	    
	    
		try{															
	    	$.getJSON( url ,  function( retcode ) {
		    	
		    	     
		    	
				    $.each( retcode , function(i,item){ 
					    
					    html+= '<div class="ui-widget-content ui-corner-all" style="padding:2px; margin-top:1px;">Utente: <b>' + retcode[i].author + '</b> Voto: <i>'+ retcode[i].voto + '</i><br/>Commento: <i>' + retcode[i].commento + '</i></div>';
		            });
		            
		            $("#"+ div ).html(html);
		       }				
			);	
			
			
			
			
        }catch(err){
		   alert( err.description );		   
	    }
}



