<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $slid=$_GET['sub_league_id'];





  if ($slid!="")

  {

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

	$query="SELECT name 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'];

	mysqli_close($db);

	$title=$slName." - Team Strength Chart";

  }

  else {$title="Team Strength Chart";}



  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);

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

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



  ## Close if unselected

  if ($slid=="")

  {

	  echo "<p>Please choose a sub-league.</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");



  if ($slid=="")

  {

	  echo "Please choose a league:<br/>\n";



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

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

	  while ($row=mysqli_fetch_array($result))

	  {

		  echo "<a href='./league_strength.php?sub_league_id=".$row['sub_league_id']."'>".$row['name']."</a><br/>\n";

	  }

	  

	  mysqli_close($db);



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

	page_foot($_SESSION['lgpath']);

	exit;

  }





  ## Get team abbreviations

  $query="SELECT team_id,year,abbr FROM team_history WHERE league_id=$lgid AND sub_league_id=$slid;";

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

  while ($row=mysqli_fetch_array($result))

  {

  	  $tid=$row['team_id'];

	  $yr=$row['year'];

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

  }



  ## Get number of teams

  $query="SELECT MAX(tmCnt) as maxTmCnt FROM (SELECT year,COUNT(DISTINCT team_id) as tmCnt FROM team_history_record WHERE league_id=$lgid AND sub_league_id=$slid GROUP BY year) as t";

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

  $row=mysqli_fetch_array($result);

  $maxTmCnt=$row['maxTmCnt'];



  ## Get records

  $query="SELECT year,sub_league_id,team_id,w,l,w/(w+l) as pct FROM `team_history_record` WHERE league_id=$lgid AND sub_league_id=$slid ORDER BY year DESC,pct DESC";

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

  echo " <table border=0 cellpadding=2 cellspacing=0 style='margin-left:auto;margin-right:auto;'>\n";

  echo "  <tr class='headline'><td class='hsc2_l'>Year</td><td colspan=$maxTmCnt style='text-align:center;'>Best <---  Team Record (Win%)  ---> Worst</td></tr>\n";

  $prevYr=0;

  $yrCnt=0;

  $rowCnt=0;

  while ($row=mysqli_fetch_array($result))

  {

	  $yr=$row['year'];

	  $tid=$row['team_id'];

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



	  if ($yr!=$prevYr)

  	  {

		  #Close previous year

		  if ($prevYr!=0)

		  {

  			  if ($yrCnt<$maxTmCnt) {echo "<td colspan=".($maxTmCnt-$yrCnt).">&nbsp;</td>";}

			  echo "</tr>\n";

		  	  $rowCnt++;

		  }



		  #Start new year

		  $yrCnt=0;

		  echo "  <tr class='$cls'><td class='".$cls."_l' onclick=\"window.location='./league_summary.php?sub_league_id=".$slid."&year=".$yr."'\"><b>$yr</b></td>";

  	  }



	  #Determine heat map color

	  $pct=$row['pct'];

	  echo "<td style='";

  	  if ($pct>=.7) {echo "background-color:#a12708;color:#FFFFFF;";}

	  elseif ($pct>=.68) {echo "background-color:#be310c;color:#FFFFFF;";}

  	  elseif ($pct>=.66) {echo "background-color:#da3a11;color:#FFFFFF;";}

	  elseif ($pct>=.64) {echo "background-color:#f34518;color:#FFFFFF;";}

  	  elseif ($pct>=.62) {echo "background-color:#f36918;color:#FFFFFF;";}

	  elseif ($pct>=.60) {echo "background-color:#f38818;color:#FFFFFF;";}

  	  elseif ($pct>=.58) {echo "background-color:#f3a118;color:#FFFFFF;";}

	  elseif ($pct>=.56) {echo "background-color:#f3ba18;color:#FFFFFF;";}

  	  elseif ($pct>=.54) {echo "background-color:#f3d318;color:#FFFFFF;";}

	  elseif ($pct>=.52) {echo "background-color:#f6ec5f;color:#AAAAAA;";}

  	  elseif ($pct>=.50) {echo "background-color:#fdf69e;color:#AAAAAA;";}

	  elseif ($pct>=.48) {echo "background-color:#dcebf6;color:#AAAAAA;";}

  	  elseif ($pct>=.46) {echo "background-color:#c7dff4;color:#FFFFFF;";}

	  elseif ($pct>=.44) {echo "background-color:#add1ef;color:#FFFFFF;";}

  	  elseif ($pct>=.42) {echo "background-color:#8ec0e9;color:#FFFFFF;";}

	  elseif ($pct>=.40) {echo "background-color:#73afe1;color:#FFFFFF;";}

  	  elseif ($pct>=.38) {echo "background-color:#59a0d9;color:#FFFFFF;";}

	  elseif ($pct>=.36) {echo "background-color:#4190cf;color:#FFFFFF;";}

  	  elseif ($pct>=.34) {echo "background-color:#2b7fc1;color:#FFFFFF;";}

	  elseif ($pct>=.32) {echo "background-color:#186bad;color:#FFFFFF;";}

  	  elseif ($pct>=.30) {echo "background-color:#0e5b98;color:#FFFFFF;";}

	  elseif ($pct>=.28) {echo "background-color:#064c82;color:#FFFFFF;";}

  	  else {echo "background-color:#043b66;color:#FFFFFF;";}

	  echo "' title='".$teams[$tid][$yr]['abbr']."  ".$row['w']."-".$row['l']."' onclick=\"window.location='./team_summary.php?team_id=".$tid."&year=".$yr."'\">";

	  $pct=sprintf("%.3f",$pct);

	  if ($pct<1) {$pct=strstr($pct,".");}

	  echo $pct;

  	  echo "</td>";

  

	  $prevYr=$yr;

	  $yrCnt++;

  }



  #Close out table

  if ($yrCnt<$maxTmCnt) {echo "<td colspan=".($maxTmCnt-$yrCnt).">&nbsp;</td>";}

  echo "</tr>\n";

  echo " </table>\n";



  echo " <p style='text-align:center;'>Hover over a box to see the team abbreviation.<br/>Click on a box to jump to that team's page for that season.</p>\n";



  mysqli_close($db);



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

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



  echo "<script type='text/javascript'>\n";

  echo " <!--\n";

  echo "   document.getElementById('year').disabled=1;\n";

  echo " -->\n";

  echo "</script>\n";



  page_foot($_SESSION['lgpath']);





?>

