<?php

##############################################################################
# high.php3                                                                  #
#                                                                            #
# Copyright ViralGames SARL 2000                                             #
##############################################################################

include("config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_drapeaux.php3"); # contient la bib_exploitation
include($BIB."bib_showGames.php3"); 
include($BIB."bib_tables.php3"); 

# LOCAL CONSTANTS #####################################
$DATE_ARCHIVAGE = getExploitationFile("date_archivage.php3");
/*
$DATE_ARCHIVAGE = "2003-06-30 01:00:01";
$LINE_TYPE_SCORES = 1;
$LINE_TYPE_OLDSCORES = 2;
$LINE_TYPE_FRIENDSCORES = 3;
*/
######################################################
$SORTING_POINTS = 0;
$SORTING_KFS = 1;
$SORTING_NIVEAUX = 2;
	
	$types_classement[0]['Id'] = 1;
	$types_classement[0]['Name'] = "Mes amis & moi";

	$types_classement[1]['Id'] = 2;
	$types_classement[1]['Name'] = "Session en cours";

	$types_classement[2]['Id'] = 3;
	$types_classement[2]['Name'] = "Session précédente";
	/*if ($ORIGIN != $ORIGINE_M6)
	{
		$types_classement[3]['Id'] = 4;
		$types_classement[3]['Name'] = "Foufoot";
	}*/
#<img src=\"$IMAGES_DIR/Icones/picto_new.gif\" border=0> --> news
	$param_url = "type_classement";

if ($type_classement == "")
	$type_classement = $types_classement[0]['Id'];

/*
if ($type_classement == $types_classement[0]['Id'])
	$sorting="points";
else
	$sorting="KF";
*/

$MAX_SCORES = 15;

$WITH_FRUITS = 1;

$sess = open_page($sessid);

function tableNavDown($page, $rank, $lines, $sorting, $linkClass, $width="100%", $bgcolor)
{
	global $sess, $type_classement;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX;
	global $rank2;
?>
<table border=0 cellspacing=0 width="<?php echo $width ?>">
	<tr>
		<td class=smallDark align=center bgcolor="<?php echo $bgcolor ?>">
			<?php
				if ($rank > round($lines/2) or $rank2 > round($lines/2))
				{
			?>
			<a class=<?php echo $linkClass ?> href="<?php echo $sess->link3($page, "sorting=".$sorting."&type_classement=".$type_classement."&rank=0&rank2=0"); ?>">Les Premiers</a>
			|
			<a class=<?php echo $linkClass ?> href="<?php echo $sess->link3($page, "sorting=".$sorting."&type_classement=".$type_classement."&rank=" . strval($rank-$lines)."&rank2=".strval($rank2-$lines)); ?>"><< Les précédents</a>
			|
			<?php
			}
			?>
			<a class=<?php echo $linkClass ?> href="<?php echo $sess->link3($page, "sorting=".$sorting."&type_classement=".$type_classement."&rank=" . strval(max($rank+$lines, 0))."&rank2=". strval(max($rank2+$lines, 0))); ?>">Les suivants >></a>
		</td>
	</tr>
</table>
<?php
}

function show_titles($color2, $color4, $type_classement, $sorting)
{
	global $sess, $HIGH_PAGE, $IMAGES_DIR;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX;
	global $name;
	?>
    <tr bgcolor="<?php echo $color2 ?>"> 
       <td height=12 class="smallBoldDark" align=right colspan=5 >  
     
         <br style=font-size:4px>
       </td>
     </tr>
    <tr bgcolor="<?php echo $color2?>" height=12> 
       <td class="smallBoldDark" align=center colspan=4 ></td>
       <td class="smallBoldDark" align=center colspan=1 ><?php
	if ($sorting == $SORTING_POINTS)
		echo "<img src=\"".$IMAGES_DIR."fleche_bas.gif\">";
	?></td>
	<td class="smallBoldDark" align=center colspan=1 ><?php
	if ($sorting == $SORTING_KFS)
		echo "<img src=\"".$IMAGES_DIR."fleche_bas.gif\">";
?></td>
	<td class="smallBoldDark" align=center colspan=1 ><?php
	if ($sorting == $SORTING_NIVEAUX)
		echo "<img src=\"".$IMAGES_DIR."fleche_bas.gif\">";
?></td>
     </tr>
    <tr bgcolor=<?php echo $color4?> height=15> 
       <td class="smallBoldDark" align=center colspan=3 >
         Place&nbsp;&nbsp;&nbsp;
       </td>
       <td class="smallBoldDark" align=center> 
         Pseudo&nbsp;&nbsp;&nbsp;
       </td>
       <td class="smallBoldDark" align=center><a class=smallDarkLink href="<?php echo $sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_POINTS."&name=".urlencode($name)); ?>">Points</a>&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center>
       <a class=smallDarkLink  href="<?php echo $sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_KFS."&name=".urlencode($name)); ?>"><img align=absbottom border=0 src="<?php echo $IMAGES_DIR ?>icone_kf.jpg"></a>
       </td>
       <td class="smallBoldDark" align=center>
	   <a class=smallDarkLink href="<?php echo $sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_NIVEAUX."&name=".urlencode($name)); ?>">
       <img align=absbottom border=0 src="<?php echo $IMAGES_DIR ?>Icones/picto_niveau.gif">
       </a>
       </td>
     </tr>
    <tr bgcolor="<?php echo $color2?>"> 
       <td height=4 class="smallBoldDark" align=right colspan=5 >  
         <br style=font-size:4px>
       </td>
     </tr>
	<?php
}


function showOldScores($PlayerId, $rank, $color1, $color2, $color3, $color4, $sorting, $date_archive, $type_classement, $color5 = "")
{
	global $MAX_SCORES, $IMAGES_DIR, $FRUIT_LIST, $INVISIBLE_FRUIT, $TREE_PAGE;
	global $sess, $tour;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX, $BG_COLOR_FOND_9 ;
	
	if ($color5 == "")
		$color5 = $color4;
	
	if ($sorting == $SORTING_POINTS)
		$SQLSorting = "Score";
	elseif ($sorting == $SORTING_KFS)
		$SQLSorting = "KF";
	elseif ($sorting == $SORTING_NIVEAUX)
		$SQLSorting = "Niveau";
		
	
	
		
	$query = "SELECT Score, KF, KFextra, Niveau, PlayerId AS Id, ScoreRankVariation, KFRankVariation, NiveauRankVariation, PlayerName AS NickName".
		" FROM PlayerScores ".
		" WHERE Date='".$date_archive."' ".
		" ORDER BY ".$SQLSorting." DESC, PlayerId ASC LIMIT ".max($rank-15,0).", $MAX_SCORES";
	
	#debug("query:".$query);
		
	
		$rowscores = main_db_query($query);
		
	$stop = 0;
	$begin = $rank - 15;
	
	if ($begin < 0)
		$begin=0;

	$tour = db_fetch_array(main_db_query("SELECT * FROM Tours WHERE ShowTime < '".$date_archive."' AND EndTime >'".$date_archive."'"));
	
	
	echo "<br style=font-size:5px>&nbsp;<span class=smallBold>".$tour['Name']."</span><span class=small> du ".date_mysql($tour['ShowTime'])." au ".date_mysql($tour['EndTime'])."<br>&nbsp; Retrouvez ci-dessous le tableau des scores de la dernière session !</span>";
	
	echo "<table height=300 valign=top width=\"100%\" border=0 cellpadding=0 cellspacing=1>  ";
	show_titles($color2, $color4, $type_classement, $sorting);
	
	for ($i = 1; $i <= $MAX_SCORES; $i++)
	{
	  	$j = $begin + $i;
	  	$color = ($i % 2 == 1)?$color1:$color2;
	  	
	  	if (($j == ($rank)) && ($PlayerId!=0))
			$color = $color5;
		
		echo "<tr onmouseover=\"setPointer(this, '".$color3."', '".$color."');\" onmouseout=\"setPointer(this, '".$color."', '".$color."');\">";
		
		if ($stop == 0)
		{
	    	if ($searched_player = db_fetch_array($rowscores))
			{
		      $fruit = $IMAGES_DIR.$FRUIT_LIST[intval($searched_player[Fruit])];
				//$rootPlayer = getplayer2($searched_player[RootPId]);
				
				//debug("rootPlayer:".$rootPlayer['NickName']);
				
				switch($sorting)
				{
					case $SORTING_POINTS;
						$KF = "class=wbt3";
						$points = "class=smallDark bgcolor=".$color;
						$niveaux = "class=wbt3";
						break;
						
					case $SORTING_KFS;
						$points = "class=wbt3";
						$niveaux = "class=wbt3";
						$KF = "class=smallDark bgcolor=".$color;
						break;
						
					case $SORTING_NIVEAUX;
					default:
						$points = "class=wbt3";
						$niveaux = "class=smallDark bgcolor=".$color;
						$KF = "class=wbt3";
						break;
				}
				
				
				show_line($j, $rank, $PlayerId, $searched_player, $color, $SQLSorting);
				
				?>
				<td align=right class="smallDark" bgcolor="<?php echo $color ?>" ><?php echo ($searched_player[KFextra] > 0)? "+".$searched_player[KFextra] : ""; ?>&nbsp;</td>
				<?php
?> 
<?php
		    }
			else
		      $stop = 1;
	  	}
	  	
		if ($stop == 1)
		{
	?> 
	            <td height="33" bgcolor="<?php echo $color?>" class="smallDark" colspan=2>  
					-
	            </td>
	            
	            <?php
	  	}
	echo "</tr>";
	}
echo "</table>";

}

function showFriendScores($PlayerId, $color1, $color2, $color3, $color4, $sorting)
{
	global $MAX_SCORES, $IMAGES_DIR, $FRUIT_LIST, $INVISIBLE_FRUIT, $TREE_PAGE, $ADD_FRIENDS_PAGE;
	global $rank, $sess;
	global $type_classement;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX;
	//global $LINE_TYPE_FRIENDSCORES;
	
	if ($sorting == $SORTING_POINTS)
		$SQLSorting = "Score";
	elseif ($sorting == $SORTING_KFS)
		$SQLSorting = "KF";
	else
		$SQLSorting = "Niveau";
	
	$query = "SELECT Players.KFRankVariation, Players.ScoreRankVariation, Players.NiveauRankVariation, PlayerOptions.Value as PlayerOptionsId,NickName,Score,KF, Niveau, Fruit,Time,NbJokerDispo FROM PlayerOptions LEFT JOIN Players ON PlayerOptions.Value=Players.Id WHERE PlayerId='$PlayerId' ORDER BY ".$SQLSorting." DESC, Time ASC ";
	$player_options_rows=main_db_query($query);

?>
<table height=300 valign=top width="100%" border=0 cellpadding=0 cellspacing=1>  
<?php
	show_titles($color2, $color4, $type_classement, $sorting);

	# affiche les options du player
	$j=0;
	while($player_options=db_fetch_array($player_options_rows))
	{
	  	$j++;
	  	$color = ($j % 2 == 1)?$color1:$color2;

		echo "<tr onmouseover=\"setPointer(this, '".$color3."', '".$color."');\" onmouseout=\"setPointer(this, '".$color."', '".$color."');\">";
		#debug("rank1");
		if (!empty($player_options['Score']))
		show_line(rank($player_options), -1, $PlayerId, $player_options, $color, $SQLSorting);
		echo "</tr>";
	}
	if ($j==0)
	{
		echo "<tr><td valign=top class=smallDark align=center colspan=7><br>La liste perso vous permet de surveiller les joueurs de votre choix.&nbsp;&nbsp;<br><br></td></tr>";
		if ($sess->memberid > 0)
			echo "<tr><td valign=top class=smallDark align=center colspan=7><br><img src=".$IMAGES_DIR."triangleorange.gif> <a class=whiteLink href=".$sess->link3($ADD_FRIENDS_PAGE, "").">".C_CREERPERSO."</a>&nbsp;&nbsp;<br><br></td></tr>";
		else
			echo "<tr><td valign=top class=smallDark align=right colspan=7><br>Vous devez être identifié pour créer votre liste perso.&nbsp;&nbsp;<br><br></td></tr>";
	}
	else
		echo "<tr><td valign=top class=smallDark align=right colspan=7><br><img src=".$IMAGES_DIR."triangleorange.gif> <a class=whiteLink href=".$sess->link3($ADD_FRIENDS_PAGE, "").">".C_MODIFIERPERSO."</a>&nbsp;&nbsp;<br></td></tr>";
		
?>		
		</table>
		<br style="font-size:5px">
<?php
}


function show_line($j, $rank, $PlayerId, $searched_player, $color, $SQLSorting)
{
	global $IMAGES_DIR, $FRUIT_LIST, $FRUIT_NAMES, $TREE_PAGE, $STATS_PAGE, $sess,$CARTONS_JAUNES_PAGE;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX;
	global $WITH_FRUITS;
	global $LINE_TYPE_SCORES, $LINE_TYPE_OLDSCORES, $LINE_TYPE_FRIENDSCORES;
	global $type_classement;
	

	

?>
	            <td valign=middle bgcolor="<?php echo $color?>" class="smallDark" align=right ><?php 
	            
					if ($WITH_FRUITS == 1)
					{
						$fruit = $IMAGES_DIR.$FRUIT_LIST[intval($searched_player['Fruit'])];
						echo "<a onMouseover=\"showtip(this,event,'".addslashes($FRUIT_NAMES[intval($searched_player['Fruit'])])."')\" onMouseout=\"hidetip()\" href=\"".$sess->link3($STATS_PAGE, "")."\"><img src=\"".$fruit."\" border=0></a>&nbsp;";
					}
					?></td>
	            <td valign=middle bgcolor="<?php echo $color?>" class="smallDark" align=right ><?php 
						echo $j.".&nbsp;";
	              	# la variation est stockée en inverse de classement
					?></td>
					<td bgcolor="<?php echo $color?>" class="smallDark" align=right width=65 nowrap>
					<?php
					
	              	$variation = - $searched_player[$SQLSorting.'RankVariation'];
	              	$string_variation = $variation;

	              	if ($variation<0)
	              	{
	              		echo "<font size=1 color=D02F2F>".$string_variation."</font>";
	              		echo '&nbsp;<img src="'.$IMAGES_DIR.'fleches/FRouge.gif">';
	              	}
	              	elseif ($variation>0)
	              	{
	              		echo "<font size=1 color=70BF3F>+".$string_variation."</font>";
	              		echo '&nbsp;<img src="'.$IMAGES_DIR.'fleches/FVerte.gif">';
	              	}
	              	else
	              	{
	              		echo "-";
	              	}
	              	
	              ?>&nbsp;
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=right> 
				<a class=smallDarkLink href="<?php echo $sess->popup2($TREE_PAGE, "id=$searched_player[Id]"); ?>"><?php echo $searched_player[NickName]?></a>&nbsp;
				<?php
					if ($sess->memberid > 0)
					echo "&nbsp;<a class=smallDarkLink onMouseover=\"showtip(this,event,'Coller un carton jaune')\" onMouseout=\"hidetip()\" href=\"javascript:void(null)\" onclick=\"javascript:window.location.replace('".$sess->link3($CARTONS_JAUNES_PAGE,"ToNickName=".$searched_player[NickName],"POST")."');\"><img valign=absbottom src=\"".$IMAGES_DIR."carton_jaune.gif\" border=0></a>&nbsp;";
				?>
	            </td>
	            <td align=right class="smallDark" bgcolor="<?php echo $color?>" > 
				<?php
					 	$field = $searched_player[Score];
					 	
					 	# si on est dans les anciens scores
					 	# les points trop long sont tronques
					 	# on met un tooltip
					 	if($type_classement != 4)
	                	{
	                		if(strlen($field)>9)
	                		{
	                			/*
	                			$field_cut = substr($field, 0, 4);
	                			$field_cut = number_format($field_cut, 0, '', ' ');
								$field_cut = str_pad($field_cut, 10, " M...", STR_PAD_RIGHT);
								*/
								$field_cut = number_format(substr($field, 0, -6), 0, '', ' ')." M...";
								?>
									<a class=linkToolTip  href="#" onMouseover="showtip(this,event,'<?php echo number_format($field, 0, '', ' '); ?> points')" onMouseout="hidetip()"><?php echo $field_cut; ?></a>
			 					<?php
	                		}
	                		else
	                			echo number_format($field, 0, '', ' ');
	                	}
	                	else
	                		echo number_format($field, 0, '', ' ');

			           	?>&nbsp;
		            </td>
	            <td align=right class="smallDark" bgcolor="<?php echo $color?>" > 
			         <?php
					 	$field = $searched_player[KF];
	                	
	                	echo number_format($field, 0, '', ' ') ?>&nbsp;
	            </td>
	            <td align=right class="smallDark" bgcolor="<?php echo $color?>" > 
			         <?php
			         #si liste perso
			         if($type_classement == 2)
			        	$playerId = $searched_player['PlayerOptionsId'];
			         else
			         	$playerId = $searched_player['Id'];
			         	
			         	# on compte ses perfects
						/*$sQuery= "SELECT Count(*) AS nbPerfect FROM Paris WHERE Result=3 AND PlayerId=".$playerId;
						$rowsPlayers = main_db_query($sQuery);
						$rowPlayer = db_fetch_assoc($rowsPlayers);*/
					 	$field = $searched_player[Niveau];
	                	echo $field 
	                	?>&nbsp;
	            </td>
<?php
}

function showScores($PlayerId, $color1, $color2, $color3, $color4, $sorting, $color5 = "")
{
	global $MAX_SCORES, $IMAGES_DIR, $FRUIT_LIST, $INVISIBLE_FRUIT, $TREE_PAGE;
	global $rank, $sess, $tour;
	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX, $BG_COLOR_FOND_9 ;

	if ($color5 == "")
		$color5 = $color4;
	
	if ($sorting == $SORTING_POINTS)
		$SQLSorting = "Score";
	elseif ($sorting == $SORTING_KFS)
		$SQLSorting = "KF";
	else
		$SQLSorting = "Niveau";
	//$rank = rank($PlayerId, $SQLSorting);
	$query = "SELECT *".
		" FROM Players ".
		" ORDER BY ".$SQLSorting." DESC, Time ASC LIMIT ".max($rank-15,0).", $MAX_SCORES";

	//echo $query;
		$rowscores = main_db_query($query);
		
	$stop = 0;
	$begin = $rank - 15;
	
	if ($begin < 0)
		$begin=0;
		
	echo "<br style=font-size:5px>&nbsp;<span class=smallBold>".$tour['Name']."</span><span class=small> du ".date_mysql($tour['ShowTime'])." au ".date_mysql($tour['EndTime']);
	
	echo "<table height=300 valign=top width=\"100%\" border=0 cellpadding=0 cellspacing=1>  ";
	show_titles($color2, $color4, $type_classement, $sorting);

	for ($i = 1; $i <= $MAX_SCORES; $i++)
	{
	  	$j = $begin + $i;
	  	$color = ($i % 2 == 1)?$color1:$color2;
	  	
  		if (($j == $rank) && ($PlayerId!=0))
			$color = $color5;
		
		echo "<tr onmouseover=\"setPointer(this, '".$color3."', '".$color."');\" onmouseout=\"setPointer(this, '".$color."', '".$color."');\">";
		if ($stop == 0)
		{
	    	if ($searched_player = db_fetch_array($rowscores))
			{
		      $fruit = $IMAGES_DIR.$FRUIT_LIST[intval($searched_player[Fruit])];
				//$rootPlayer = getplayer2($searched_player[RootPId]);
				
				switch($sorting)
				{
					case $SORTING_POINTS;
						$KF = "class=wbt3";
						$points = "class=smallDark bgcolor=".$color;
						$niveaux = "class=wbt3";
						break;
						
					case $SORTING_KFS;
						$points = "class=wbt3";
						$niveaux = "class=wbt3";
						$KF = "class=smallDark bgcolor=".$color;
						break;
						
					case $SORTING_NIVEAUX;
					default:
						$points = "class=wbt3";
						$niveaux = "class=smallDark bgcolor=".$color;
						$KF = "class=wbt3";
						break;
				}
				show_line($j, $rank, $PlayerId, $searched_player, $color, $SQLSorting);
?> 
<?php
		    }
			else
		      $stop = 1;
	  	}
	  	
		if ($stop == 1)
		{
	?> 
	            <td height="33" bgcolor="<?php echo $color?>" class="smallDark" colspan=2>  
					-
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=center>
	             -
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=center>
	             -
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=center>
	             -
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=center>
	             -
	            </td>
	            <td bgcolor="<?php echo $color?>" class="smallDark" align=center>
	             -
	            </td>
	            <?php
	  	}
	echo "</tr>";
	}
echo "</table>";
}

function showFoufoot($PlayerId, $color1, $color2, $color3, $color4, $sorting, $color5 = "",$DATE_ARCHIVAGE)
{
  # globals
	global $MAX_SCORES, $IMAGES_DIR, $FRUIT_LIST, $INVISIBLE_FRUIT, $TREE_PAGE;
	global $rank, $sess, $tour;
	global $BG_COLOR_FOND_9 ;
 	global $HIGH_PAGE, $FORUM_PAGE;
	global $name;
	global $type_classement;
	
	# constantes
	$SORTING_POS      = 1;
	$SORTING_PARTIES  = 2;
	$SORTING_REUSSITE = 3;
  	$SORTING_SERIE = 4;

  # init
	if ($sorting == $SORTING_REUSSITE)
		$SQLSorting = "Reussite DESC";
	elseif ($sorting == $SORTING_PARTIES)
		$SQLSorting = "NbParties DESC";
	elseif ($sorting == $SORTING_SERIE)
		$SQLSorting = "Serie DESC";
	else
		$SQLSorting = "Funnies DESC";
	
	$strsql = "SELECT * FROM Foufoot
		          ORDER BY $SQLSorting, Pseudo ASC 
		          LIMIT ".max($rank-15,0).", $MAX_SCORES";
		     
  	//echo $strsql;
  	
	$rowscores = main_db_query($strsql);
	
	$stop = 0;
	$begin = max($rank-15,0);
	$color5 = ($color5=="") ? $color4 : $color5;
	$sort_arrow[$sorting] = "<img src=\"".$IMAGES_DIR."fleche_bas.gif\">";
	
	
?>
  <br>
<table cellpadding=0 cellspacing=5 border=0 width=100%>
	<tr>
		<td valign=top width=220><?php echo $PlayerId ?>
		  <div align=center class=smallBoldDark>
		    Ce classement est celui des joueurs KestuFoot sur le jeu FouFoot
		    <br><a href="http://www.kestufoot.com/redirect.php3?news=5002&url=http://www.foufoot.com/default.asp&cobrand=kestufoot" target="_blank">
		      <img src="<?=$IMAGES_DIR?>/jouer_foufoot.gif" border=0></a>
		  </div>
		</td>
		<td class=smallDark valign=top align=center>
			<a class=smallDarkLink href="<?php echo $sess->link3($FORUM_PAGE, "") ?>"><b>Cliquez ici</b> pour trouver vos adversaires sur le forum !</a>
		</td>
	</tr>
</table>
	<table height=300 valign=top width=100% border=0 cellpadding=0 cellspacing=1>
    <tr bgcolor="<?=$color2?>" height=12> 
      <td class="smallBoldDark" align=center colspan=2 >&nbsp;</td>
      <td class="smallBoldDark" align=center colspan=1 ><?=$sort_arrow[$SORTING_POS]?>&nbsp;</td>
	    <td class="smallBoldDark" align=center colspan=1 ><?=$sort_arrow[$SORTING_PARTIES]?>&nbsp;</td>
	    <td class="smallBoldDark" align=center colspan=1 ><?=$sort_arrow[$SORTING_REUSSITE]?>&nbsp;</td>
	    <td class="smallBoldDark" align=center colspan=1 ><?=$sort_arrow[$SORTING_SERIE]?>&nbsp;</td>
    </tr>
    <tr bgcolor=<?=$color4?> height=15> 
       <td class="smallBoldDark" align=center>
         Place&nbsp;&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center> 
         Pseudo&nbsp;&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center>
        <a class=smallDarkLink href="<?=$sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_POS."&name=".urlencode($name)); ?>">
          Funnies</a>&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center>
         <a class=smallDarkLink  href="<?=$sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_PARTIES."&name=".urlencode($name)); ?>">
          Parties</a>&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center>
         <a class=smallDarkLink href="<?=$sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_REUSSITE."&name=".urlencode($name)); ?>">
           R&eacute;ussite</a>&nbsp;&nbsp;</td>
       <td class="smallBoldDark" align=center>
         <a class=smallDarkLink href="<?=$sess->link3($HIGH_PAGE, "type_classement=".$type_classement."&visu=visu&sorting=".$SORTING_SERIE."&name=".urlencode($name)); ?>">
           S&eacute;rie</a>&nbsp;&nbsp;</td>
    </tr>
    <tr bgcolor="<?=$color2?>"> 
       <td height=4 class="smallBoldDark" align=right colspan=5>
         <br style=font-size:4px></td>
    </tr>
<?php
	for ($i = 1; $i <= $MAX_SCORES; $i++)
	{
	  	$j = $begin + $i;
	  	$color = ($i % 2 == 1) ? $color1 : $color2;
  		if (($j == $rank) && ($PlayerId!=0))
			  $color = $color5;
		
		if ($stop == 0)
		{
	    		if ($player = db_fetch_array($rowscores))
			{
				
?>
			  <tr onmouseover="setPointer(this,'<?=$color3?>','<?=$color?>');" onmouseout="setPointer(this,'<?=$color?>','<?=$color?>');">
				  <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[Pos]?>&nbsp;&nbsp;&nbsp;</td>
				  <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[Pseudo]?>&nbsp;&nbsp;&nbsp;</td>
				  <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[Funnies]?>&nbsp;&nbsp;&nbsp;</td>
				  <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[NbParties]?>&nbsp;&nbsp;&nbsp;</td>
				  <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[Reussite]?>&nbsp;&nbsp;&nbsp;</td>
          <td align=right class=smallDark bgcolor=<?=$color?>><?=$player[Serie]?>&nbsp;&nbsp;&nbsp;</td>
				</tr>
<?
		  }
			else
		      $stop = 1;
	  }
		if ($stop == 1)
		{
?> 
    <tr onmouseover="setPointer(this,'<?=$color3?>','<?=$color?>');" onmouseout="setPointer(this,'<?=$color?>','<?=$color?>');" height="10">
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
      <td bgcolor="<?=$color?>" class="smallDark" align=center>-</td>
    </tr>
<?
	 	}
	}
  echo "</table>";
}



if (!isset($SearchType))
	$SearchType=1;

	global $SORTING_POINTS;
	global $SORTING_KFS;
	global $SORTING_NIVEAUX;
switch($sorting)
{
	case $SORTING_KFS;
		$SQLSorting = "KF";
		$pointsClass = "wbt3";
		$KFClass = "whiteBoldTitle";
		$NiveauClass = "wbt3";
		$imgPointsSorting = "&nbsp;";
		$imgKFSorting = '<img src="'.$IMAGES_DIR.'icone_classement.gif">';
		$imgNiveauSorting = "&nbsp;";
		break;
		
	case $SORTING_NIVEAUX;
		$SQLSorting = "Niveau";
		$pointsClass = "wbt3";
		$KFClass = "wbt3";
		$NiveauClass = "whiteBoldTitle";
		$imgPointsSorting = "&nbsp;";
		$imgKFSorting = '<img src="'.$IMAGES_DIR.'icone_classement.gif">';
		$imgNiveauSorting = "&nbsp;";
		break;

	case $SORTING_POINTS;
	default:
		$SQLSorting = "Score";
		$pointsClass = "whiteBoldTitle";
		$NiveauClass = "wbt3";
		$KFClass = "wbt3";
		$imgPointsSorting = "&nbsp;";
		$imgKFSorting = "&nbsp;";
		$imgNiveauSorting = '<img src="'.$IMAGES_DIR.'icone_classement.gif">';
		break;
}

if (isset($rank))
{
  if ($rank <= 0)
    $rank=5;

}
else
{
  if (isset($visu))
  {
	if (!empty($name))
	{
	  if ($SearchType==1)
		{
		    $query = main_db_query("SELECT * FROM Players WHERE NickName='$name'");
		    $query2 = main_db_query("SELECT * FROM PlayerScores WHERE PlayerName='$name' AND Date='".$DATE_ARCHIVAGE."'");
	   	 
		}
		else
		{
			# recherche par nom
			$query = main_db_query("SELECT * FROM Players LEFT JOIN Members ON Members.Id=Players.MemberId WHERE Name='$name'");
			$query2 = main_db_query("SELECT * FROM PlayerScores LEFT JOIN Players ON PlayerScores.PlayerId=Players.Id LEFT JOIN Members ON Members.Id=Players.MemberId WHERE Name='$name'  AND Date='".$DATE_ARCHIVAGE."'");
			
			
			# s'il y a des homonymes, on affiche en erreur les pseudos trouvés
			if (db_num_rows($query)>1)
			{
				$error = "Il y a plusieurs joueurs trouvés :<br>";
				for($r=0;$r<db_num_rows($query)-1;$r++)
				{
					$row = db_fetch_array($query);
					$error .= $row[NickName].", ";
				}
				$row = db_fetch_array($query);
				$error .= $row[NickName];
				
				db_data_seek($query,0);
		  	}
		  	if (db_num_rows($query2)>1)
			{
				$error = "Il y a plusieurs joueurs trouvés :<br>";
				for($r=0;$r<db_num_rows($query2)-1;$r++)
				{
					$row = db_fetch_array($query2);
					$error .= $row[PlayerName].", ";
				}
				$row = db_fetch_array($query2);
				$error .= $row[PlayerName];
				
				db_data_seek($query2,0);
		  	}
		  	
		  
		}
	}
	
	$aPlayer = db_fetch_array($query);
	$aPlayerScores = db_fetch_array($query2);

  if ((!empty($aPlayer) and ($type_classement != 3)) || (!empty($aPlayerScores) and ($type_classement == 3)))
	{
		
		if (!empty($aPlayerScores[$SQLSorting]) and ($type_classement == 3))
		{
			#debug("rank2");
			$rank2 = rank($aPlayerScores,$SQLSorting,$DATE_ARCHIVAGE);
		}	
		elseif (!empty($aPlayer[$SQLSorting]))
		{
      		#debug("rank3");
      		$rank = rank($aPlayer, $SQLSorting);
      	}
      
      
  	}
	else 
	{
		if ($SearchType==1)
		  	$tmp = "Le pseudo";
		else
			$tmp = "Le nom";
			
      		if (!empty($name))
	  	  $error = "$tmp $name est introuvable";
	  	 
      		$rank=5;
      		$rank2=5;
  	}
  	
 
}
  else if ($sess->memberid != -1 && $type_classement!=4)
  {
    
    $aPlayer = getplayer($sess->memberid);
	if ($type_classement == 3)
	{
    	$aPlayerScores = getplayerscore($aPlayer['Id'], $DATE_ARCHIVAGE);
    	
    	if ($aPlayerScores)
    	{
    		#debug("rank4");
    		$rank2 = rank($aPlayerScores,$SQLSorting,$DATE_ARCHIVAGE);
    		$name=$aPlayerScores['PlayerName'];
    	}
    }
    elseif (!empty($aPlayer[$SQLSorting]))
    {
    	#debug("rank5");
    	$rank = rank($aPlayer, $SQLSorting) ;
    	$name = $aPlayer['NickName'];
    }
    
    
    
  }
  elseif($type_classement==4)
  {
    $rank = (isset($rank)) ? $rank: 0; 
    $rank2 = (isset($rank2)) ? $rank2: 0;  
  }    
  else
  {
  	
    $rank=5;
    $rank2=5;
  }
}
/*
$query = main_db_query("SELECT COUNT(Id) AS NbPlayers FROM Players");
$nbPlayersRow = db_fetch_array($query);
$nbPlayers = $nbPlayersRow[NbPlayers];
*/
$date = date("Y-m-d H:i:s", my_local_time());

$tour = db_fetch_array(main_db_query("SELECT * FROM Tours WHERE ShowTime<'".$date."' AND EndTime>'".$date."'"));
#$lastTour = db_fetch_array(main_db_query("SELECT * FROM Tours WHERE EndTime<='".$tour['ShowTime']."' ORDER BY EndTime DESC LIMIT 0,1"));

$page_courante="";
$sub_header = ($sess->memberid == -1)?"subh_howtowin.inc":"subh_connecte.inc";
$title = "<h1 class='h1table'>Mes classements</h1> ";
include("header.inc");
include("classements.inc");
include("footer.inc");
close_page($sess);
?>
