<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $lgyear=$_SESSION['lgyear'];

  $yr=$_GET['year'];

  $slid=$_GET['sub_league_id'];

  $position=$_GET['position'];

  if ($position=='all') {$position="";}



  if (($slid!="")&&($yr!=""))

  {

	  include($_SESSION['adminpath']."/dbopen.php");

	  $query="SELECT name,designated_hitter FROM sub_leagues WHERE league_id=$lgid AND sub_league_id=$slid;";

	  $result=mysqli_query($db,$query);

	  $row=mysqli_fetch_array($result);

	  $slName=$row['name'];

	  $useDH=$row['designated_hitter'];

	  $query="SELECT value FROM ootp_sql_admin WHERE setting='dh$slid';";

	  $result=mysqli_query($db,$query);

	  $row=mysqli_fetch_array($result);

	  $dhYr=$row['value'];

	  mysqli_close($db);

	  if ($dhYr!="")

	  {

		  if (($useDH==1)&&($dhYr>$yr)) {$useDH=0;}

		  elseif (($useDH==0)&&($dhYr>$yr)) {$useDH=1;}

	  }



	  $title="$yr $slName ";

	  if ($position!="") {$title.=get_pos($position)." Summary";}

	  else {$title.=" Positional Leaders";}

  }

  else {$title="Positional Leaders";}



  if (($position==10)&&($useDH==0))

  {

	  $position='all';

	  $title="Positional Leaders";

  }



  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($lgid,$_SESSION['lgpath'],$title);

  nav_toolbar("history","History");

  nav_toolbar("leagues","",$lgid,$slid,"",$yr,"position^bp");

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";



  ## Close if unselected

  if (($slid=="")||($yr==""))

  {

	  echo "<p>Please choose a sub-league and year.</p>\n";

	  ################## END OF PAGE #########################

	  echo " </div>   <!-- END contentpane DIV -->\n";

	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  ################## START OF PAGE CONTENT #########################

  include($_SESSION['adminpath']."/dbopen.php");



  #Get Team Names

  $query="SELECT team_id,name,nickname,abbr";

  if ($lgyear==$yr) {$query.=" FROM teams WHERE league_id=$lgid AND allstar_team=0";}

  else {$query.=",made_playoffs,won_playoffs FROM team_history WHERE league_id=$lgid AND year=$yr";}

  $query.=" AND sub_league_id=$slid;";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  $tlist="";

  $tmCnt=0;

  while ($row=mysqli_fetch_array($result))

  {

	  $tid=$row['team_id'];

	  $teams[$tid]['name']=$row['name']." ".$row['nickname'];

	  $teams[$tid]['city']=$row['name'];

	  $teams[$tid]['abbr']=$row['abbr'];

	  $teams[$tid]['made_playoffs']=$row['made_playoffs'];

	  $teams[$tid]['won_playoffs']=$row['won_playoffs'];

	  $tlist.="$tid,";

	  $tmCnt++;

  }

  $tlist=trim($tlist,",");





  # Get Positions

  if ($useDH==1)

  {

      $query="SELECT t1.player_id,pa,ip FROM (SELECT player_id,SUM(pa) as pa FROM players_career_batting_stats WHERE league_id=$lgid AND team_id IN ($tlist) AND split_id=1 AND year=$yr GROUP BY player_id) as t1 LEFT JOIN (SELECT player_id,SUM(ip)+SUM(ipf)/3 as ip FROM players_career_fielding_stats WHERE league_id=$lgid AND year=$yr AND team_id IN ($tlist) AND position!=10 GROUP BY player_id) as t2 ON t1.player_id=t2.player_id WHERE pa>ip OR ip IS NULL";

      if ($_GET['showq']==1) {echo "$query<br/>\n";}

      $result=mysqli_query($db,$query);

      $posPids[10]="";

      if (mysqli_num_rows($result))

      {

	      while ($row=mysqli_fetch_array($result))

	      {

		      $pid=$row['player_id'];

		      $posPids[10].=",$pid";

	      }

	      $posPids[10]=trim($posPids[10],",");

      }

  }

  $query="SET @pid:=0;";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  $query="SELECT player_id,position FROM (SELECT if(@pid!=player_id,1,0) as priPos,@pid:=player_id as player_id,position,g,ip FROM (SELECT player_id,position,SUM(g) as g,SUM(ip)+SUM(ipf)/3 as ip FROM players_career_fielding_stats WHERE league_id=$lgid AND year=$yr AND position>1 AND position<10 AND team_id IN ($tlist)";

  if ($posPids[10]!="") {$query.=" AND player_id NOT IN (".$posPids[10].")";}

  $query.=" GROUP BY player_id,position ORDER BY player_id,ip DESC,g DESC) as x) as t WHERE priPos=1";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  for ($pos=2;$pos<10;$pos++) {$posPids[$pos]="";}



	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $pos=$row['position'];

		  $posPids[$pos].=",$pid";

	  }



	  $statArray=Array("h","hr","rbi","sb","avg","ops","war");

	  $statCnt=count($statArray)+1;



	  for ($pos=2;$pos<=10;$pos++)

	  {

		  if (($position!="")&&($pos!=$position)) {continue;}

		  $plist=trim($posPids[$pos],",");

		  if ($plist=="") {continue;}



		  ## Get League Leaders

		  $statArray=Array("avg","obp","slg","ops","g","gs","ab","pa","h","d","t","hr","r","rbi","tb","ebh","sb","cs","bb","k","war","zr");

		  $statCnt=0;

		  echo "<div class='textbox' style='width:100%;'>   <!-- open leaders -->\n";

		  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open leaders -->\n";

		  echo "  <tr class='title2'><td colspan=4>".get_pos($pos)." Leaders</td></tr>\n";

		  echo "  <tr>";

		  foreach ($statArray as $key => $statLbl)

		  {

			  if (($pos==10)&&($statLbl=="zr")) {continue;}    ## Skip Zone Rating for Designated Hitters



			  if ((($statCnt%4)==0)&&($statCnt!=0))   ## Start New Row Every 4 Stats

			  {

				  echo "  </tr>\n";

				  echo "  <tr>\n";

			  }



			  $squery=stat_query($statLbl,"","ts");

			  $ssort=stat_sort($statLbl);

			  $stext=stat_text($statLbl);



			  #Get 5th place

			  $query="SELECT player_id,";

			  switch ($statLbl)

			  {

			  case "avg":

			  case "obp":

			  case "slg":

			  case "ops":

				  $query.="ts.g,ROUND($squery,5) as $statLbl FROM players_career_batting_stats as pcs,";

				  if ($lgyear==$yr) {$query.="team_record";} else {$query.="team_history_record";}

				  $query.=" as ts WHERE pcs.split_id=1 AND pcs.team_id=ts.team_id AND pcs.year=$yr";

				  if ($lgyear!=$yr) {$query.=" AND pcs.year=ts.year";}

				  $query.=" AND pcs.team_id IN ($tlist) AND pcs.player_id IN ($plist) GROUP BY player_id ORDER BY $statLbl $ssort LIMIT 4,1";

				  break;

			  case "zr":

				  $query.="$squery as $statLbl FROM players_career_fielding_stats WHERE split_id<=1 AND year=$yr AND team_id IN ($tlist) AND player_id IN ($plist) GROUP BY player_id ORDER BY $statLbl $ssort LIMIT 4,1";

				  break;

			  default:

				  $query.="$squery as $statLbl FROM players_career_batting_stats WHERE split_id=1 AND year=$yr AND team_id IN ($tlist) AND player_id IN ($plist) GROUP BY player_id ORDER BY $statLbl $ssort LIMIT 4,1";

				  break;

			  }

			  if ($_GET['showq']==1) {echo "$query<br/>\n";}

			  $result=mysqli_query($db,$query);

			  $row=mysqli_fetch_array($result);

			  $fifth=$row[$statLbl];		  

			  #Get Top 5

			  $query="SELECT p.player_id,p.first_name,p.last_name,pcs.team_id,COUNT(DISTINCT pcs.team_id) as nTeams,";

			  switch ($statLbl)

			  {

			  case "avg":

			  case "obp":

			  case "slg":

			  case "ops":

				  $query.="ts.g,if(SUM(pa)<(3.1*SUM(ts.g)),1,0) as qval,ROUND($squery,5) as $statLbl FROM players as p,players_career_batting_stats as pcs,";

				  if ($lgyear==$yr) {$query.="team_record";} else {$query.="team_history_record";}

				  $query.=" as ts WHERE pcs.split_id=1 AND pcs.team_id=ts.team_id AND pcs.year=$yr";

				  if ($lgyear!=$yr) {$query.=" AND pcs.year=ts.year";}

				  $query.=" AND p.player_id=pcs.player_id AND pcs.team_id IN ($tlist) AND pcs.player_id IN ($plist) GROUP BY player_id HAVING ROUND($squery,5) ".stat_sort($statLbl,1,1)." $fifth ORDER BY $statLbl $ssort";

				  break;

			  case "zr":

				  $query.="$squery as $statLbl FROM players as p,players_career_fielding_stats as pcs WHERE pcs.split_id=0 AND pcs.year=$yr AND p.player_id=pcs.player_id AND pcs.team_id IN ($tlist) AND pcs.player_id IN ($plist) GROUP BY player_id HAVING $squery ".stat_sort($statLbl,1,1)." $fifth ORDER BY $statLbl $ssort";

				  break;

			  default:

				  $query.="$squery as $statLbl FROM players as p,players_career_batting_stats as pcs WHERE pcs.split_id=1 AND pcs.year=$yr AND p.player_id=pcs.player_id AND pcs.team_id IN ($tlist) AND pcs.player_id IN ($plist) GROUP BY player_id HAVING $squery ".stat_sort($statLbl,1,1)." $fifth ORDER BY $statLbl $ssort";

				  break;

			  }

			  if ($_GET['showq']==1) {echo "$query<br/>\n";}

			  $result=mysqli_query($db,$query);

			  $totRowCnt=mysqli_num_rows($result);



			  echo "   <td style='width:200px;vertical-align:top;'>\n";

			  echo "    <table border=0 cellpadding=2 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open stat table -->\n";

			  echo "     <tr class='title'><td colspan=3 style='vertical-align:top;'>$stext</td></tr>\n";

			  $rowCnt=0;

			  while ($row=mysqli_fetch_array($result))

			  {

				  if ($rowCnt>=5) {continue;}

				  $pid=$row['player_id'];

				  $tid=$row['team_id'];

				  $statVal=$row[$statLbl];

				  if ($rowCnt==0) {$leaderVal=$statVal;}



				  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {$cls='b';}

				  else {$cls='s';}

				  $cls.=($rowCnt%2+1);



				  echo "     <tr class='$cls'>";

				  if (($totRowCnt>5)&&($rowCnt==4))

				  {

					  echo "<td colspan=2>".($totRowCnt-4);

					  if ($prevStat==$statVal) {echo " more";}

					  echo " players tied with</td>";

				  }

				  else

				  {

					  echo "<td class='".$cls."_l'><a href='./player.php?player_id=$pid'";

					  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {echo " style='font-weight:bold;'";}

					  echo ">".$row['first_name'][0].". ".$row['last_name']."</a>";

					  if ($row['qval']==1) {echo "*";}

					  echo "</td>";

					  echo "<td class='".$cls."_l'><a href='./team_hist.php?team_id=$tid&year=$yr'";

					  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {echo " style='font-weight:bold;'";}

					  echo ">".$teams[$tid]['abbr']."</a>";

					  if ($row['nTeams']>1) {echo "<sup>(".$row['nTeams'].")</sup>";}

					  echo "</td>";

				  }

				  echo "<td>".stat_format($statLbl,$statVal)."</td>";

				  echo "</tr>\n";

				  $rowCnt++;

				  $prevStat=$statVal;

			  }

			  echo "    </table>\n";

			  echo "   </td>\n";

			  $statCnt++;

		  }



		  if (($statCnt%4)>0) {echo "   <td colspan=".(4-$statCnt%4).">&nbsp;</td>\n";}

		  echo "  </tr>\n";

		  echo " </table>   <!-- close leaders -->\n";

		  echo "</div>   <!-- close leaders -->\n";

		  echo "<div class='textbox' style='border:0;'>* indicates hitless at-bats added to reach qualification</div>\n";



		  ## Positional Register

		  if ($position!="")

		  {

			  #Batting Stats

			  $statArray=Array("g","gs","pa","ab","r","h","d","t","hr","rbi","bb","k","sb","cs","avg","obp","slg","ops","war");



			  $query="SELECT p.player_id,first_name,last_name,bats,throws,pcs.team_id,COUNT(DISTINCT pcs.team_id) as nTeams";

			  foreach ($statArray as $key => $statLbl)

			  {

				  $query.=",".stat_query($statLbl)." as $statLbl";

			  }

			  $query.=" FROM players as p,players_career_batting_stats as pcs WHERE p.player_id=pcs.player_id AND split_id=1 AND year=$yr AND pcs.team_id IN ($tlist) and p.player_id IN ($plist) GROUP BY player_id ORDER BY pa DESC;";

			  if ($_GET['showq']==1) {echo "$query<br/>\n";}

			  $result=mysqli_query($db,$query);

			  $totRowCnt=mysqli_num_rows($result);

			  if ($totRowCnt)

			  {

				  echo "<div class='textbox' style='width:100%;'>   <!-- open position register -->\n";

				  echo " <table border=0 cellpadding=0 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open position register -->\n";

				  echo "  <tr class='title2'><td colspan=4 style='padding:4px';>".get_pos($pos)." Batting Register</td></tr>\n";

				  echo "  <tr><td>";

				  echo "   <table border=0 cellpadding=2 cellspacing=0 class='sortable' width='100%'>\n";

				  echo "    <thead><tr class='headline'>";

				  echo "<td class='hsc2_l'>Player</td>";

				  echo "<td class='hsc2_l'>B</td>";

				  echo "<td class='hsc2_l'>T</td>";

				  echo "<td class='hsc2_l'>Tm</td>";

				  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

				  echo "</tr></thead>\n";



				  $rowCnt=0;

				  while ($row=mysqli_fetch_array($result))

				  {

					  $pid=$row['player_id'];

					  $tid=$row['team_id'];



					  $cls='s'.($rowCnt%2+1);

					  echo "    <tr class='$cls'>";

					  echo "<td class='".$cls."_l' sorttable_customkey='".$row['last_name'].", ".$row['first_name']."'><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a></td>";

					  echo "<td class='".$cls."_l'>".stat_format('bats',$row['bats'])."</td>";

					  echo "<td class='".$cls."_l'>".stat_format('throws',$row['throws'])."</td>";

					  echo "<td class='".$cls."_l'><a href='./team_summary.php?year=$yr&team_id=$tid'>".$teams[$tid]['abbr']."</a>";

					  if ($row['nTeams']>1) {echo "<sup>(".$row['nTeams'].")</sup>";}

					  echo "</td>";

					  foreach ($statArray as $key => $statLbl) {echo "<td>".stat_format($statLbl,$row[$statLbl])."</td>";}

					  $rowCnt++;

				  }

				  echo "    <tfoot>\n";

				  echo "     <tr class='headline'>";

				  echo "<td class='hsc2_l'>Player</td>";

				  echo "<td class='hsc2_l'>B</td>";

				  echo "<td class='hsc2_l'>T</td>";

				  echo "<td class='hsc2_l'>Tm</td>";

				  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

				  echo "</tr>\n";



				  $e=explode(" GROUP BY",$query);

				  $query=$e[0];

				  if ($_GET['showq']==1) {echo "$query<br/>\n";}

				  $result=mysqli_query($db,$query);

				  $row=mysqli_fetch_array($result);

				  echo "     <tr class='headline'>";

				  echo "<td class='hsc2_l' colspan=4>Totals ($totRowCnt players)</td>";

				  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_format($statLbl,$row[$statLbl])."</td>";}

				  echo "</tr>\n";



				  echo "    </tfoot>\n";

				  echo "   </table>\n";

				  echo "  </td></tr>\n";

				  echo " </table>   <!-- close position register -->\n";

				  echo "</div>   <!-- close position register -->\n";

				  echo "<div class='textbox' style='border:0;'>NOTE: Only batters with ".strtolower(get_pos($position))." as their primary position included in batting register</div>\n";

			  }



		  }	  

	  }



  }



  #Starting Pitching Leaders

  $posArray=Array(11,21);

  foreach ($posArray as $key => $pos)

  {

	  if (($position!="")&&($pos!=$position)) {continue;}

	

	  $statArray=Array("w","l","era","whip","winpct","g","ip","bf","k","bb","er","r","gs","gf","cg","sho","hper9","kper9","rper9","hrper9","kperbb","s","war");



	  $statCnt=0;

	  echo "<div class='textbox' style='width:100%;'>   <!-- open leaders -->\n";

	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open leaders -->\n";

	  echo "  <tr class='title2'><td colspan=4>".get_pos($pos)." Leaders</td></tr>\n";

	  echo "  <tr>";

	  foreach ($statArray as $key => $statLbl)

	  {

		  if ((($statCnt%4)==0)&&($statCnt!=0))   ## Start New Row Every 4 Stats

		  {

			  echo "  </tr>\n";

			  echo "  <tr>\n";

		  }



		  if ($statLbl=='winpct') {$squery=stat_query($statLbl,"pcs");}

		  else {$squery=stat_query($statLbl,"");}

		  $ssort=stat_sort($statLbl);

		  $stext=stat_text($statLbl);



		  #Get 5th place

		  $query="SELECT player_id,";

		  switch ($statLbl)

		  {

		  case "era":

		  case "whip":

		  case "winpct":

		  case "hper9":

		  case "kper9":

		  case "rper9":

		  case "hrper9":

		  case "kperbb":

			  $query.="ts.g,ROUND($squery,5) as $statLbl FROM players_career_pitching_stats as pcs,";

			  if ($lgyear==$yr) {$query.="team_record";} else {$query.="team_history_record";}

			  $query.=" as ts WHERE pcs.split_id=1 AND pcs.team_id=ts.team_id AND pcs.year=$yr";

			  if ($lgyear!=$yr) {$query.=" AND pcs.year=ts.year";}

			  $query.=" AND pcs.team_id IN ($tlist) GROUP BY player_id";

			  switch ($pos)

			  {

			  case 11:

				  $query.=" HAVING NOT ((SUM(pcs.g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(pcs.g))<=4)";

				  $query.=" AND (SUM(ip)+SUM(ipf)/3)/SUM(ts.g)>=";

				  $query.="1.0";

				  break;

			  case 21:

				  $query.=" HAVING (SUM(pcs.g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(pcs.g))<=4";

				  $query.=" AND (SUM(ip)+SUM(ipf)/3)/SUM(ts.g)>=";

				  $query.="0.4";

				  break;

			  }

			  $query.=" ORDER BY $statLbl $ssort LIMIT 4,1";

			  break;

		  default:

			  $query.="$squery as $statLbl FROM players_career_pitching_stats WHERE split_id=1 AND year=$yr AND team_id IN ($tlist) GROUP BY player_id";

			  switch ($pos)

			  {

			  case 11:

				  $query.=" HAVING NOT ((SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4)";

				  break;

			  case 21:

				  $query.=" HAVING (SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4";

				  break;

			  }

			  $query.=" ORDER BY $statLbl $ssort LIMIT 4,1";

			  break;

		  }

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $result=mysqli_query($db,$query);

		  $row=mysqli_fetch_array($result);

		  $fifth=$row[$statLbl];		  

		  #Get Top 5

		  $query="SELECT p.player_id,p.first_name,p.last_name,pcs.team_id,COUNT(DISTINCT pcs.team_id) as nTeams,";

		  switch ($statLbl)

		  {

		  case "era":

		  case "whip":

		  case "winpct":

		  case "hper9":

		  case "kper9":

		  case "rper9":

		  case "hrper9":

		  case "kperbb":

			  $query.="ts.g,ROUND($squery,5) as $statLbl FROM players as p,players_career_pitching_stats as pcs,";

			  if ($lgyear==$yr) {$query.="team_record";} else {$query.="team_history_record";}

			  $query.=" as ts WHERE pcs.split_id=1 AND pcs.team_id=ts.team_id AND pcs.year=$yr";

			  if ($lgyear!=$yr) {$query.=" AND pcs.year=ts.year";}

			  $query.=" AND p.player_id=pcs.player_id AND pcs.team_id IN ($tlist) GROUP BY player_id";

			  switch ($pos)

			  {

			  case 11:

				  $query.=" HAVING NOT ((SUM(pcs.g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(pcs.g))<=4)";

				  $query.=" AND (SUM(ip)+SUM(ipf)/3)/SUM(ts.g)>=";

				  $query.="1.0";

				  break;

			  case 21:

				  $query.=" HAVING (SUM(pcs.g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(pcs.g))<=4";

				  $query.=" AND (SUM(ip)+SUM(ipf)/3)/SUM(ts.g)>=";

				  $query.="0.4";

				  break;

			  }

			  $query.=" AND ROUND($squery,5) ".stat_sort($statLbl,1,1)." $fifth ORDER BY $statLbl $ssort";

			  break;

		  default:

			  $query.="$squery as $statLbl FROM players as p,players_career_pitching_stats as pcs WHERE pcs.split_id=1 AND pcs.year=$yr AND p.player_id=pcs.player_id AND pcs.team_id IN ($tlist) GROUP BY player_id";

			  switch ($pos)

			  {

			  case 11:

				  $query.=" HAVING NOT ((SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4)";

				  break;

			  case 21:

				  $query.=" HAVING (SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4";

				  break;

			  }

			  $query.=" AND $squery ".stat_sort($statLbl,1,1)." $fifth ORDER BY $statLbl $ssort";

			  break;

		  }

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $result=mysqli_query($db,$query);

		  $totRowCnt=mysqli_num_rows($result);



		  echo "   <td style='width:200px;vertical-align:top;'>\n";

		  echo "    <table border=0 cellpadding=2 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open stat table -->\n";

		  echo "     <tr class='title'><td colspan=3 style='vertical-align:top;'>$stext</td></tr>\n";

		  $rowCnt=0;

		  while ($row=mysqli_fetch_array($result))

		  {

			  if ($rowCnt>=5) {continue;}

			  $pid=$row['player_id'];

			  $tid=$row['team_id'];

			  $statVal=$row[$statLbl];

			  if ($rowCnt==0) {$leaderVal=$statVal;}



			  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {$cls='b';}

			  else {$cls='s';}

			  $cls.=($rowCnt%2+1);



			  echo "     <tr class='$cls'>";

			  if (($totRowCnt>5)&&($rowCnt==4))

			  {

				  echo "<td colspan=2>".($totRowCnt-4);

				  if ($prevStat==$statVal) {echo " more";}

				  echo " players tied with</td>";

			  }

			  else

			  {

				  echo "<td class='".$cls."_l'><a href='./player.php?player_id=$pid'";

				  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {echo " style='font-weight:bold;'";}

				  echo ">".$row['first_name'][0].". ".$row['last_name']."</a>";

				  echo "</td>";

				  echo "<td class='".$cls."_l'><a href='./team_hist.php?team_id=$tid&year=$yr'";

				  if (($statVal==$leaderVal)&&(($rowCnt!=4)||($totRowCnt<6))) {echo " style='font-weight:bold;'";}

				  echo ">".$teams[$tid]['abbr']."</a>";

				  if ($row['nTeams']>1) {echo "<sup>(".$row['nTeams'].")</sup>";}

				  echo "</td>";

			  }

			  echo "<td>".stat_format($statLbl,$statVal)."</td>";

			  echo "</tr>\n";

			  $rowCnt++;

			  $prevStat=$statVal;

		  }

		  echo "    </table>\n";

		  echo "   </td>\n";

		  $statCnt++;

	  }



	  if (($statCnt%4)>0) {echo "   <td colspan=".(4-$statCnt%4).">&nbsp;</td>\n";}

	  echo "  </tr>\n";

	  echo " </table>   <!-- close leaders -->\n";

	  echo "</div>   <!-- close leaders -->\n";

	  echo "<div class='textbox' style='border:0;'>NOTE: Relief pitchers defined as having more relief appearances than starts and no more than 4 innings per appearance.  Starting pitchers include all others.</div>\n";



	  ## Positional Register

	  if ($position!="")

	  {

		  #Pitching Stats

		  $statArray=Array("w","l","winpct","s","era",'g','gs','cg','sho','ip','ha','hra','bb','k','hld','whip','oavg','babip',"war");



		  $query="SELECT p.player_id,first_name,last_name,bats,throws,pcs.team_id,COUNT(DISTINCT pcs.team_id) as nTeams";

		  foreach ($statArray as $key => $statLbl)

		  {

			  $query.=",".stat_query($statLbl)." as $statLbl";

		  }

		  $query.=" FROM players as p,players_career_pitching_stats as pcs WHERE p.player_id=pcs.player_id AND split_id=1 AND year=$yr AND pcs.team_id IN ($tlist) GROUP BY player_id";

		  switch ($pos)

		  {

		  case 11:

			  $query.=" HAVING NOT ((SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4)";

			  break;

		  case 21:

			  $query.=" HAVING (SUM(g)-SUM(gs))>SUM(gs) AND ((SUM(ip)+SUM(ipf)/3)/SUM(g))<=4";

			  break;

		  }

		  $query.=" ORDER BY ip DESC;";

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $result=mysqli_query($db,$query);

		  $totRowCnt=mysqli_num_rows($result);

		  if ($totRowCnt)

		  {

			  echo "<div class='textbox' style='width:100%;'>   <!-- open position register -->\n";

			  echo " <table border=0 cellpadding=0 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open position register -->\n";

			  echo "  <tr class='title2'><td colspan=4 style='padding:4px';>".get_pos($pos)." Register</td></tr>\n";

			  echo "  <tr><td>";

			  echo "   <table border=0 cellpadding=2 cellspacing=0 class='sortable' width='100%'>\n";

			  echo "    <thead><tr class='headline'>";

			  echo "<td class='hsc2_l'>Player</td>";

			  echo "<td class='hsc2_l'>B</td>";

			  echo "<td class='hsc2_l'>T</td>";

			  echo "<td class='hsc2_l'>Tm</td>";

			  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

			  echo "</tr></thead>\n";



			  $rowCnt=0;

			  $plist="";

			  while ($row=mysqli_fetch_array($result))

			  {

				  $pid=$row['player_id'];

				  $tid=$row['team_id'];

				  $plist.=",$pid";



				  $cls='s'.($rowCnt%2+1);

				  echo "    <tr class='$cls'>";

				  echo "<td class='".$cls."_l' sorttable_customkey='".$row['last_name'].", ".$row['first_name']."'><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a></td>";

				  echo "<td class='".$cls."_l'>".stat_format('bats',$row['bats'])."</td>";

				  echo "<td class='".$cls."_l'>".stat_format('throws',$row['throws'])."</td>";

				  echo "<td class='".$cls."_l'><a href='./team_summary.php?year=$yr&team_id=$tid'>".$teams[$tid]['abbr']."</a>";

				  if ($row['nTeams']>1) {echo "<sup>(".$row['nTeams'].")</sup>";}

				  echo "</td>";

				  foreach ($statArray as $key => $statLbl) {echo "<td>".stat_format($statLbl,$row[$statLbl])."</td>";}

				  $rowCnt++;

			  }

			  echo "    <tfoot>\n";

			  echo "     <tr class='headline'>";

			  echo "<td class='hsc2_l'>Player</td>";

			  echo "<td class='hsc2_l'>B</td>";

			  echo "<td class='hsc2_l'>T</td>";

			  echo "<td class='hsc2_l'>Tm</td>";

			  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

			  echo "</tr>\n";



			  $e=explode(" GROUP BY",$query);

			  $query=$e[0];

			  if ($_GET['showq']==1) {echo "$query<br/>\n";}

			  $result=mysqli_query($db,$query);

			  $row=mysqli_fetch_array($result);

			  echo "     <tr class='headline'>";

			  echo "<td class='hsc2_l' colspan=4>Totals ($totRowCnt players)</td>";

			  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_format($statLbl,$row[$statLbl])."</td>";}

			  echo "</tr>\n";



			  echo "    </tfoot>\n";

			  echo "   </table>\n";

			  echo "  </td></tr>\n";

			  echo " </table>   <!-- close position register -->\n";

			  echo "</div>   <!-- close position register -->\n";

		  }

	  }

  }





  #Fielding Stats

  if ($position!="")

  {

	  if ($position==2) {$statArray=Array("g","gs","po","a","dp","tp","tc","e","fpct","inn","rng","zr","rto","sba","rtoPct");}

	  else {$statArray=Array("g","gs","po","a","dp","tp","tc","e","fpct","inn","rng","zr");}



	  echo "<div class='textbox' style='width:100%;'>   <!-- open fielding register -->\n";

	  echo " <table border=0 cellpadding=0 cellspacing=0 width='100%' style='border=1px solid black;margin-left:auto;margin-right:auto;'>   <!-- open fielding register -->\n";

	  echo "  <tr class='title2'><td colspan=4 style='padding:4px';>".get_pos($position)." Fielding Register</td></tr>\n";

	  echo "  <tr><td>";

	  echo "   <table border=0 cellpadding=2 cellspacing=0 class='sortable' width='100%'>\n";

	  echo "    <thead><tr class='headline'>";



	  echo "<td class='hsc2_l'>Player</td>";

	  echo "<td class='hsc2_l'>B</td>";

	  echo "<td class='hsc2_l'>T</td>";

	  echo "<td class='hsc2_l'>Tm</td>";

	  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

	  echo "</tr></thead>\n";



	  $query="SELECT p.player_id,first_name,last_name,bats,throws,pcs.team_id,COUNT(DISTINCT pcs.team_id) as nTeams";

	  foreach ($statArray as $key => $statLbl)

	  {

		  $query.=",".stat_query($statLbl)." as $statLbl";

	  }

	  $query.=" FROM players as p,players_career_fielding_stats as pcs WHERE p.player_id=pcs.player_id AND year=$yr AND pcs.team_id IN ($tlist) AND pcs.position=";

	  if ($position>10)

	  {

		  $plist=trim($plist,",");

		  $query.="1 AND p.player_id IN ($plist)";

	  }

	  else {$query.=$position;}

	  $query.=" GROUP BY player_id ORDER BY inn DESC;";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  $totRowCnt=mysqli_num_rows($result);

	  $rowCnt=0;

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $tid=$row['team_id'];



		  $cls='s'.($rowCnt%2+1);

		  echo "    <tr class='$cls'>";

		  echo "<td class='".$cls."_l' sorttable_customkey='".$row['last_name'].", ".$row['first_name']."'><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a></td>";

		  echo "<td class='".$cls."_l'>".stat_format('bats',$row['bats'])."</td>";

		  echo "<td class='".$cls."_l'>".stat_format('throws',$row['throws'])."</td>";

		  echo "<td class='".$cls."_l'><a href='./team_summary.php?year=$yr&team_id=$tid'>".$teams[$tid]['abbr']."</a>";

		  if ($row['nTeams']>1) {echo "<sup>(".$row['nTeams'].")</sup>";}

		  echo "</td>";

		  foreach ($statArray as $key => $statLbl) {echo "<td>".stat_format($statLbl,$row[$statLbl])."</td>";}

		  $rowCnt++;

	  }

	  echo "    <tfoot>\n";

	  echo "     <tr class='headline'>";

	  echo "<td class='hsc2_l'>Player</td>";

	  echo "<td class='hsc2_l'>B</td>";

	  echo "<td class='hsc2_l'>T</td>";

	  echo "<td class='hsc2_l'>Tm</td>";

	  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_text($statLbl,2)."</td>";}

	  echo "</tr>\n";



	  $e=explode(" GROUP BY",$query);

	  $query=$e[0];

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  $row=mysqli_fetch_array($result);

	  echo "     <tr class='headline'>";

	  echo "<td class='hsc2_l' colspan=4>Totals ($totRowCnt players)</td>";

	  foreach ($statArray as $key => $statLbl) {echo "<td class='hsc2'>".stat_format($statLbl,$row[$statLbl])."</td>";}

	  echo "</tr>\n";



	  echo "    </tfoot>\n";

	  echo "   </table>\n";

	  echo "  </td></tr>\n";

	  echo " </table>   <!-- close fielding register -->\n";

	  echo "</div>   <!-- close fielding register -->\n";

	  if ($position<=10) {echo "<div class='textbox' style='border:0;'>NOTE: All players to appear at ".strtolower(get_pos($position))." included in fielding register</div>\n";}

  }





  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";

  page_foot($_SESSION['lgpath']);





?>

