/**************************************************
 * This is a constructor for a JavaScript Vehicle object.
 */
function Vehicle(make, model, modClar, year, image,
                 frontx, fronty, rearx, reary, minSpeed) {
  this.imageBase = "/images/wheelrack/car_images";
  this.make = make;
  this.model = model;
  this.modClar = modClar;
  this.year = year;
  this.minSpeedRating = minSpeed;
  this.colors = new Array(); //Array of colors, undefined for no colors
  this.images = new Array(); //Array of images corresponding to colors
  this.image = new Image();
  this.image.src = this.imageBase + image.replace(/\/-/, "/0");
  this.imageMMY = parseMMY(image);
  this.frontWheel = new VWheel(frontx, fronty);
  this.rearWheel = new VWheel(rearx, reary);
  this.tireSizes = new Array();
  this.minSpeedrating = "";
}

/****************************************************
 * This function gets the descriptive make, model and year (MMY) from a vehicle
 * image path. This is used to display the MMY of the image as opposed to
 * the MMY of the vehicle searched. This utilizes JavaScript regular 
 * experssions.
 */
function parseMMY(imageName) {
  if (imageName == "" || imageName == "null") return "";
  var info = imageName.match(/^\/(.+)\/(.+)\//);
  if (info != null) {
    if (info[1] == "nophoto") {
      info[0] = "Representative Silhouette";
      info[1] = info[2].replace(/_/g, " ");
      info[2] = "";
    } else {
      info[1] = info[1].replace(/_/g, " ");
      var parts = info[2].split("_");
      info[0] = parts[parts.length-1];
      info[2] = "";
      for (i=0; i<parts.length-1; i++)
        info[2] += parts[i] + " ";
    }
    return info[0] + " " + info[1] + " " + info[2] + "\n";
  } else {
    return "Info null: " + imageName + "\n";
  }
}

Vehicle.prototype.parseMMY = parseMMY;

/************************************************************
 * This function is called when a vehicle's color is changed. It changes the 
 * images used to display the vehicle to one of the appropriate color.
 */
function changeColor(index) {
  this.image.src = this.imageBase + this.images[index];
  this.layer.document.vehicleImage.src = this.image.src;
  this.currentColorIndex = index; // Keep state for use in saving the vehicle
}

Vehicle.prototype.changeColor = changeColor;

/**************************************************
 * This is the constructor for a "Vehicle Wheel" as opposed to a wheel. The
 * VWheel is a convient way to collect the X and Y coordinates for a wheel
 * on a vehicle.
 */
function VWheel(x, y) {
  this.x = x; // X coordinate for wheel
  this.y = y; // Y coordinate for wheel
  this.wheelImage = new Image();  // Current image for this wheel
  this.coverImage = new Image();
  this.wheel = null; // Wheel object for current wheel
  this.layer = null; // layer for this wheel
  this.coverLayer = null; // Layer for the cover for existing wheel
}

/***************************************************
 * Constructos for a tire size. This uses a method to construct a 
 * tire size suitable for display and then stores the result. This stored
 * result is necessary to fix a bug that was foune in IE
 */
function tireSize(width, ratio, diameter, msg, minSpeedrating, minLoad, sortCode) {
  this.width = width;
  this.ratio = ratio;
  this.diameter = diameter;
  this.msg = msg;
  this.minSpeedrating = minSpeedrating;
  this.minLoad = minLoad;
  this.sortCode = sortCode;
  this.disSize = this.displaySize();
}

/*************************************************************
 * This function takes the tire width, ratio and diameter and constructs
 * a proper size for display.
 */
function displaySize() {
  var displayRatio = (this.ratio == "0") ? "" : this.ratio;
  return this.width + (this.width.lastIndexOf("/")==-1?"/":"") + displayRatio + "-" + this.diameter;
}

tireSize.prototype.displaySize = displaySize;

