function VehicleStats::OnWake()
{
   UpdateVehicleStatsList();
   
   // get the first available car and show it
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         break;      
   }

   $vstatsSlot = %i;

   ShowVehicleStats(%i);
   
   abandonvehicle.Visible = false;
   $screenActive = true;
   
}

function UpdateVehicleStatsList()
{
   // now put the vehicles in
   
   for(%i=1;%i<=10;%i++)
   {
      %control = "vstats" @ %i;
      
      if($vehicles[%i, vehicle] !$= "")
      {
         %control.command = "ShowVehicleStats(" @ %i @ ");";
         %control.SetBitmap(expandfilename("~/data/images/icons/" @ $vehicles[%i, vehicle] @ ".png"));         
         %control.ToolTip = $vehicles[%i, vehicle];
      }      
      else
      {
         %control.text = "";      
         %control.command = "";
         %control.SetBitmap(expandfilename("~/data/images/gui/grey.png"));
      }
   }   
}

function ShowVehicleStats(%slot)
{   
   alxPlay(click);

   // set the previous control back to it's original size
   
   %control = "vstats" @ %slot;
   %pos = %control.GetPosition();
   %x = getWord(%pos, 0);
   %y = getWord(%pos, 1);
   vstats_selector.setPosition(%x, %y);
         
   $vstatsSlot = %slot;

   // update the selector

   $statsvehicle = %slot;  // used if the player wants to drop the vehicle
   
   %tires = $vehicles[%slot, tires] - $vehicles[%slot, badtires];
   
   vstats_armor.Text = $vehicles[%slot, currentarmor] SPC "/" SPC $vehicles[%slot, stockarmor];
   vstats_tires.Text = %tires SPC "/" SPC $vehicles[%slot, tires];
   vstats_engine.Text =  $vehicles[%slot, currentenginehp] SPC "/" SPC $vehicles[%slot, stockenginehp];
   
   if($vehicles[%slot, tireupgrade] > 0)
      vstats_tireupgrade.Text =  $vehicles[%slot, tireupgrade];
   else
      vstats_tireupgrade.Text =  "Stock";
   
   if($vehicles[%slot, brakes] > 0)
      vstats_brakes.Text = $vehicles[%slot, brakes];
   else
      vstats_brakes.Text = "Stock";
   
   if($vehicles[%slot, acceleration] > 0)   
      vstats_acc.Text = $vehicles[%slot, acceleration];
   else
      vstats_acc.Text = "Stock";
      
   vstats_topspeed.Text = $vehicles[%slot, topspeed];
   
   if($vehicles[%slot, steering] > 0)
      vstats_steering.Text = $vehicles[%slot, steering];
   else
      vstats_steering.Text = "Stock";

   vstats_supplycapacity.text = GetVehicleSupplyCapacity($vehicles[%slot, vehicle]);
   vstats_peoplecapacity.text = GetVehiclePeopleCapacity($vehicles[%slot, vehicle]);
   vstats_tarmor.Text =  $vehicles[%slot, currenttirearmor] SPC "/" SPC $vehicles[%slot, stocktirearmor];
   vstats_gtarmor.Text = $vehicles[%slot, currentgastankarmor] SPC "/" SPC $vehicles[%slot, stockgastankarmor];
   vstats_ram.Text = $vehicles[%slot, currentramhp] SPC "/" SPC $vehicles[%slot, stockramhp];
   vstats_windowarmor.Text = $vehicles[%slot, currentwindowarmor] SPC "/" SPC $vehicles[%slot, stockwindowarmor];
   vstats_fuel.text = GetVehicleFuelConsumption($vehicles[%slot, Vehicle]);   
   vstats_tonnage.text = GetSingleVehicleTonnage($vehicles[%slot, Vehicle]);
   vstats_vehicledamage.text = $vehicles[%slot, currentvehiclehp] SPC "/" SPC ($vehicles[%slot, stockvehiclehp]);      

   if($vehicles[%slot, mountedweapon] $= "")
   {
      vstats_weaponname.Text = "No Weapon";
      vstats_mountedweapon.Text = "N/A";
   }
   else
   {
      switch$ ($vehicles[%slot, mountedweapon])
      {
         case "machinegun":
            vstats_weaponname.Text = "Machine Gun";   
         case "crossbow":
            vstats_weaponname.Text = "Crossbow";   
         case "flamethrower":
            vstats_weaponname.Text = "Flame Thrower";   
         case "rpg":   
            vstats_weaponname.Text = "RPG";   
      }
      
      vstats_mountedweapon.Text = $vehicles[%slot, currentmountedweaponhp] SPC "/" SPC $vehicles[%slot, stockmountedweaponhp];
   }
}

function AbandonVehicle()
{   
   canvas.PopDialog(abandonvehicle);

   RemoveVehicle($statsvehicle);   
   
   $statsvehicle = "";

   VehicleStats::OnWake();   
}

function VehicleStats::OnSleep()
{
   $screenActive = false;
}

function AbandonVehicle::OnWake()
{
   // if they only have 1 vehicle left don't let them do this
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         %counter++;            
   }
   
   if(%counter == 1)
   {
      alxPlay(buzzer);
      canvas.PopDialog(abandonvehicle);
      return;            
   }

}

function AbandonVehicle::OnSleep()
{
}