<?
$width0 = $_REQUEST['width0'];
if ($width0 < 100) $width0=245;
$width1 = $_REQUEST['width1'];
if ($width1 < 100) $width1=245;
$height0 = $_REQUEST['height0'];
if ($height0 < 40) $height0=70;
$height1 = $_REQUEST['height1'];
if ($height1 < 40) $height1=70;
$wheel0 = $_REQUEST['wheel0'];
if ($wheel0 < 14) $wheel0=16;
$wheel1 = $_REQUEST['wheel1'];
if ($wheel1 < 14) $wheel1=16;

$spacer = $_REQUEST['spacer'];
if (! $spacer) $spacer=0;
$backspace = $_REQUEST['backspace'];
if (! $backspace) $backspace = 6;


$stk_width = $width0 / 25.4;
$stk_wall = (($height0 / 100) * $stk_width);
$stk_height = (2 * $stk_wall) + $wheel0;
$stk_circum = $stk_height * pi();
$stk_wrad = $wheel0 / 2;
$stk_trad = $stk_height / 2;

$new_width = $width1 / 25.4;
$new_wall = (($height1 / 100) * $new_width);
$new_height = (2 * $new_wall) + $wheel1;
$new_cirum = $new_height * pi();
$diff = (($new_cirum / $stk_circum) * 100) - 100;
$new_wrad = $wheel1 / 2;
$new_trad = $new_height / 2;

$tire_mult = 4;
$tire_max = max($stk_height, $new_height);

$toptire_mult = 2.75;
$fenderX = 16.5;
$fenderY = 36.5;
$axlPiv = 1.375;


?><html>
<head>
<title>Wheel Calculator</title>
<style>
body {
 font-family: sans-serif;
 font-size: 12pt;
 color: black;
}
table,th,td {
 font-size: 10pt;
 padding: 2px;
 text-align: center;
}
table {
 margin: 0 auto 0 auto;
 border-collapse: separate;
 border-spacing: 5px 0;
}
th { background-color: #fdd; margin: 0; }
td { margin: 0; }
#sel,#tires,#adj {
 border-top: 1px solid red;
 border-bottom: 1px solid red;
 border-radius: 15px;
 -moz-border-radius: 15px;
 margin: 0 auto 0 auto;
 position: relative;
}
#sel {
 width: 325px;
 text-align: right;
 padding: 10px 50px 10px 0;
 white-space: nowrap;
}
#tires {
 width: 375px;
 height: <?echo intval($tire_max * $tire_mult - 1)?>px;
 padding: 0;
 text-align: center;
}
#stktire {
 width: <?echo $wheel0 * $tire_mult?>px;
 height: <?echo $wheel0 * $tire_mult?>px;
 background-color: #bbb;
 border: <?echo intval($stk_wall * $tire_mult)?>px solid #111;
 border-radius: <?echo intval((($wheel0/2)+$stk_wall) * $tire_mult)?>px;
 -moz-border-radius: <?echo intval((($wheel0/2)+$stk_wall) * $tire_mult)?>px ;
 display: inline-block;
 margin: 0 5px;
}
#newtire {
 width: <?echo $wheel1 * $tire_mult?>px;
 height: <?echo $wheel1 * $tire_mult?>px;
 background-color: #bbb;
 border: <?echo intval($new_wall * $tire_mult)?>px solid #111;
 border-radius: <?echo intval((($wheel1/2)+$new_wall) * $tire_mult)?>px;
 -moz-border-radius: <?echo intval((($wheel1/2)+$new_wall) * $tire_mult)?>px ;
 display: inline-block;
 margin: 0 5px;
}
#tiresize {
 text-align: center;
 font-size: 10px;
 padding: 0 35px;
}
<?
 $fendX = $fenderX * $toptire_mult;
 $fendY = $fenderY * $toptire_mult;
 $fendTop = (($tire_max * $tire_mult) - ($fendY + 3)) / 2;
?>
#fender {
 border-width: 3px 3px 3px 0;
 border-color: #a00;
 border-style: double;
 border-radius: 3px;
 height: <?echo intval($fendY)?>px;
 width: <?echo intval($fendX)?>px;
 top: -<?echo intval($fendTop)?>px;
 margin: 0 0 0 <?echo 12 * $toptire_mult?>px;
 position: relative;
 display: inline-block;
}
<?
 $topTireX = $new_width * $toptire_mult;
 $topTireY = $new_height * $toptire_mult;
 $topTireTp = (($fenderY * $toptire_mult) - ($new_height * $toptire_mult)) / 2 - 0.5;
 $topTireLt = ($backspace - $axlPiv + $spacer) * $toptire_mult;
?>
#toptire {
 border: 1px dashed #111;
 border-radius: 6px;
 height: <?echo intval($topTireY)?>px;
 width: <?echo intval($topTireX)?>px;
 display: inline-block;
 position: absolute;
 -webkit-transform: rotate(0deg);
 -moz-transform: rotate(0deg);
 top: <?echo intval($topTireTp)?>px;
 left: -<?echo $topTireLt?>px;
}
<?
 $pivRt = ((($axlPiv - $spacer) * $toptire_mult) - 5);
 $pivTp = ($topTireY / 2) - 2.5;
?>
#pivot {
 background-color: red;
 border-radius: 2.5px;
 display: inline-block;
 position: absolute;
 height: 5px;
 width: 5px;
 top: <?echo intval($pivTp)?>px;
 right: <?echo $pivRt?>px;
}
<?
 $arcX = $stk_height * $toptire_mult * 1.19;
 $arcR = $arcX / 2;
 $arcLt = $topTireLt + ($arcX - ($new_height * $toptire_mult)) + (2.5 * $toptire_mult);
?>
#arc0 {
 border-left: 1px dashed #0cc;
 border-radius: <?echo intval($arcR)?>px;
 height: <?echo intval($arcX)?>px;
 width: <?echo intval($arcX)?>px;
 position: absolute;
 top: <?echo intval(($fendY - $arcX) / 2 + 0.5)?>px;
 left: -<?echo intval($arcLt)?>px;
}
<?
 $arcX = $new_height * $toptire_mult * 1.19;
 $arcR = $arcX / 2;
 $arcLt = $topTireLt + ($arcX - ($new_height * $toptire_mult)) + (2.5 * $toptire_mult);
?>
#arc1 {
 border-left: 1px dashed #00f;
 border-radius: <?echo intval($arcR)?>px;
 height: <?echo intval($arcX)?>px;
 width: <?echo intval($arcX)?>px;
 position: absolute;
 top: <?echo intval(($fendY - $arcX) / 2 + 0.5)?>px;
 left: -<?echo intval($arcLt)?>px;
}
#adj {
 width: 375px;
 text-align: center;
 padding: 8px 0 3px 0;
}
#kph { font-size: 9px; color: #400000; }
#btn {
 border: 2px outset #844;
 background-color: #fdd;
 font-size: 12px;
 padding: 2px 5px;
 position: absolute;
 right: -<?echo 22 * $toptire_mult?>px;
 top: 46px;
 cursor: pointer;
}
</style>

<script type="text/javascript">
var deg = 0;
var dir = 3;
var tmrAni;

function moveTire() {
 deg = deg + dir;
 if (Math.abs(deg) >= 45) dir = -dir;
 var tire = document.getElementById("toptire");
 tire.style.MozTransform = "rotate(" + deg + "deg)";

 var rad = (deg * Math.PI) / 180;
 var radius = <?echo ($backspace - $axlPiv + $spacer) * $toptire_mult?>;
// var centerX = <?echo $axlPiv * $toptire_mult?>;
// var centerX = -<?echo $spacer * $toptire_mult?>;
 var centerX = 0;
 var centerY = <?echo intval($topTireTp)?>;
 tire.style.left = centerX - (Math.cos(rad) * radius);
 tire.style.top = centerY - (Math.sin(rad) * radius);

 // left:-18.5;
// alert(tire.style.left);
// animate();
}
function animate() {
 var btn = document.getElementById("btn");
 if (btn.style.borderStyle != "inset") {
  tmrAni = setInterval("moveTire()",100);
  btn.style.borderStyle = "inset";
 } else {
  clearInterval(tmrAni);
  btn.style.borderStyle = "outset";
 }
}
</script>
</head>
<body>
<form name="tire" action="tire_size.php" method="post">

<br />

<!-- Select tire sizes -->
<div id="sel">
Stock tire size:&nbsp;
<input type="text" name="width0" size="2" value="<?echo $width0;?>" />&nbsp;/&nbsp;
<input type="text" name="height0" size="2" value="<?echo $height0;?>" />&nbsp;-&nbsp;
<input type="text" name="wheel0" size="2" value="<?echo $wheel0;?>" />
<br />

New tire size:&nbsp;
<input type="text" name="width1" size="2" value="<?echo $width1;?>" />&nbsp;/&nbsp;
<input type="text" name="height1" size="2" value="<?echo $height1;?>" />&nbsp;-&nbsp;
<input type="text" name="wheel1" size="2" value="<?echo $wheel1;?>" />
<br />

Wheel spacer:&nbsp;
<input type="text" name="spacer" size="1" value="<?echo $spacer?>" />&quot;
<br />
<br />
<input type="submit" value="Calculate" />
</div>

<br />

<!-- Show tire images -->
<div id="tires">
 <span id="stktire"></span>
 <span id="newtire"></span>
 <div id="fender">
  <span id="toptire"><span id="pivot"></span></span>
  <span id="arc0"></span>
  <span id="arc1"></span>
 </div>
  <span id="btn" onclick="animate()">Animate</span>
</div>
<div align="center">
<span id="tiresize"><?echo "$width0/$height0-$wheel0"?></span>
<span id="tiresize"><?echo "$width1/$height1-$wheel1"?></span>
</div>

<br />

<!-- Disaply tire parameters -->
<table>
 <tr>
  <th>Tire</th>
  <th>Height</th>
  <th>Width</th>
  <th>Sidewall</th>
  <th>Adjust</th>
 </tr>
 <tr>
  <td><?echo "$width0/$height0-$wheel0"?></td>
  <td><?echo number_format($stk_height, 2)?></td>
  <td><?echo number_format($stk_width, 2)?></td>
  <td><?echo number_format($stk_wall, 2)?></td>
  <td>---</td>
 </tr>

 <tr>
  <td><?echo "$width1/$height1-$wheel1"?></td>
  <td><?echo number_format($new_height, 2)?></td>
  <td><?echo number_format($new_width, 2)?></td>
  <td><?echo number_format($new_wall, 2)?></td>
  <td><?echo number_format($diff, 2)?>%</td>
 </tr>
</table>

<br />

<!-- Speedometer adjustment chart -->
<div id="adj"><table>
 <tr>
  <th>Speedometer</th>
  <th>Actual Speed</th>
 </tr>
<?
for ($mph=25; $mph<=75; $mph+=10) {
  $kph = number_format(1.609344 * $mph, 1);
  $madj = number_format($mph + ($mph * ($diff/100)), 2);
  $kadj = number_format($kph + ($kph * ($diff/100)), 1);
?>
 <tr>
  <td><?echo $mph?> mph <span id="kph">(<?echo $kph?> kph)</span></td>
  <td><?echo $madj?> mph <span id="kph">(<?echo $kadj?> kph)</span></td>
 </tr>
<? } ?>
</table></div>

</form>
</body>
</html>
