<?php
/*
 *
 * @copyright (c) 2010 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */
include_once('path.inc.php'); // get the path ;)
include_once(ROOT_PATH.'/include/config.inc.php');
include_once(ROOT_PATH.'/include/parse.inc.php');
include_once(ROOT_PATH.'/include/designfunctions.inc.php');

$page = validateUnsignedInteger($_GET['page'], null);

$row = mysql_fetch_assoc(mysql_query('Select max(runde) as maximum from highscore'));
$maxpages = $row['maximum'];

$url = '<a href="index.php?as=halloffame&page=###PAGE###">###LABEL###</a>';

if(!is_numeric($page) || $page < 1){
	$page = 0;
} else if($page > $maxpages){
	$page = $maxpages;
}


// Determine the importance of the Highscores (the less occurece, the more important)
$qry = mysql_query('Select count(*) as anzahl, art from highscore where runde = '.($maxpages-$page).' group by art order by anzahl,art asc');
while($row = mysql_fetch_assoc($qry)){
	$events[] = $row;
}

$qry = mysql_query('Select * from highscore where runde = '.($maxpages-$page).' order by art, datum');


echo '<table cellpadding="0" cellspacing="0" width="100%" height="69">'."\n";
echo '<tr>'."\n";
echo '	<th class="content" colspan="3">Halle der Helden aus Runde '.($maxpages - $page).'</th>'."\n";
echo '</tr>'."\n";

// Now generate the strings
while($row = mysql_fetch_assoc($qry)){
	if($old != $row['art']){
		$eventhighscore[$row['art']] .= '<tr>'."\n";
		$eventhighscore[$row['art']] .=  '	<th class="content" height="39" colspan="3">'.$row['art'].'</th>'."\n";
		$eventhighscore[$row['art']] .=  '</tr>'."\n";
		$eventhighscore[$row['art']] .=  '<tr>'."\n";
		$eventhighscore[$row['art']] .=  '	<th class="content" height="22" width="100" align="center">Datum</th>'."\n";
		$eventhighscore[$row['art']] .=  '	<th class="content" height="22" width="100" align="center">Sieger</th>'."\n";
		$eventhighscore[$row['art']] .=  '	<th class="content" height="22" width="100" align="center">Trainer</th>'."\n";
		$eventhighscore[$row['art']] .=  '</tr>'."\n";
		$old = $row['art'];
	}

	$eventhighscore[$row['art']] .=  '<tr>'."\n";
	$eventhighscore[$row['art']] .=  '	<td class="content" height="25" width="100" align="center">'.$row['datum'].'</td>'."\n";
	if(!is_null($row['charid'])){
		$eventhighscore[$row['art']] .=  '	<td class="content" height="25" width="100" align="center">'.displayCharLink($row['charid'], $row['charname']).'</td>'."\n";
	} else {
		$eventhighscore[$row['art']] .=  '	<td class="content" height="25" width="100" align="center">'.$row['charname'].'</td>'."\n";
	}
	if(!is_null($row['userid'])){
		$eventhighscore[$row['art']] .=  '	<td class="content" height="25" width="100" align="center">'.displayUserLink($row['userid'], $row['username']).'</td>'."\n";
	} else{
		$eventhighscore[$row['art']] .=  '	<td class="content" height="25" width="100" align="center">'.$row['username'].'</td>'."\n";
	}
	$eventhighscore[$row['art']] .=  '</tr>'."\n";
}

// Now echo the stuff!!
for($i=0;$i<count($events);$i++){
	echo $eventhighscore[$events[$i]['art']];
}

echo '<tr>'."\n";
echo '	<td align="center" valign="bottom" height="22" width="100%" colspan="3">'."\n";
echo '		<table width="100%">'."\n";
echo displayPagelinks($page, $maxpages, $url);
echo '		</table>'."\n";
echo '	</td>'."\n";
echo '</tr>'."\n";

echo '</table>'."\n";