var obitList = new Array();
var classCodes = new Array();
var vehiclePictures = new Array();
var activePicIndex = -1;
var VEHICLE_PIC_CELL_WIDTH = 0;


function classCodeObject(code,name)
{
  this.code = code;
  this.name = name;
}

classCodes[0] = new classCodeObject("H","Hearses");
classCodes[1] = new classCodeObject("L","Limousines");
classCodes[2] = new classCodeObject("O","Other Professional Vehicles");


function obitListObject(obitNum,make,color,vin,year,vehicleClass,manufacturer,bodyStyle,picFileName,pW,pH)
{
  var t;
  this.obitNum = obitNum;
  this.make = make;
  this.color = color;
  this.vin = vin;
  this.year = year;
  t = vehicleClass.split(';');
  this.price = 0;
  if (t != null){
    if (t.length == 1) this.vehicleClass = vehicleClass;
    else {
      this.vehicleClass = t[0];
      this.price = t[1];
    }
  }
  this.manufacturer = manufacturer;
  this.bodyStyle = bodyStyle;
  this.year = this.year.slice(0,4);
  this.picFile = picFileName;
  this.pWidth = pW;
  this.pHeight = pH;
}

function orderVehicles(a,b)
{
  if (a.vehicleClass.slice(0,1).toUpperCase() > b.vehicleClass.slice(0,1).toUpperCase()) return(1);
  if (a.vehicleClass.slice(0,1).toUpperCase() < b.vehicleClass.slice(0,1).toUpperCase()) return(-1);
  if (a.year > b.year) return(-1);
  if (a.year < b.year) return(1);
  if (a.make.toUpperCase() > b.make.toUpperCase()) return(1);
  if (a.make.toUpperCase() < b.make.toUpperCase()) return(-1);
  return(0);
}


function vehiclePicObject(fileName,w,h,pageType,picNum)
{
  this.fileName = fileName;
  if (w > VEHICLE_PIC_CELL_WIDTH){
    w = VEHICLE_PIC_CELL_WIDTH;
    h = Math.round(h * VEHICLE_PIC_CELL_WIDTH / w);
  } 
  this.w = w;
  this.h = h;
  this.pageType = pageType;
  this.picNum = picNum;
}


function orderVehPics(a,b)
{
  if (a.pageType < b.pageType) return(-1);
  if (a.pageType > b.pageType) return(1);
  if (a.picNum < b.picNum) return(-1);
  if (a.picNum > b.picNum) return(1);
  return(0);
}

function renderVehicleClass(classCode)
{
  var i;

  for(i=0;i<classCodes.length;i++){
    if (classCode == classCodes[i].code) break;
  }
  if (i == classCodes.length) return;
  document.writeln("<h2>" + classCodes[i].name + "</h2>");
}
  

function renderVehicleList()
{
  var i;
  var t;
  var vehicleClassCode = "";

  for(i=0;i<obitList.length;i++){
    if (vehicleClassCode != obitList[i].vehicleClass.slice(0,1).toUpperCase()){
      if (i > 0) document.writeln("</table>");
      vehicleClassCode = obitList[i].vehicleClass.slice(0,1).toUpperCase();
      renderVehicleClass(vehicleClassCode);
      document.writeln("<table class='vehLsTable'>");
	  document.writeln("<tr><td class='tableHead'>Year</td><td class='tableHead'>Make</td><td class='tableHead'>Color</td><td class='tableHead'>Id Num</td><td class='tableHead'>Link</td></tr>");
    }
    t = "<tr>\n<td class='vehMake'>" + obitList[i].year + "</td>\n";
    t += "<td class='vehColor'>" + obitList[i].make + "</td>\n";
    t += "<td class='vehMileage'>" + obitList[i].color + "</td>\n";
    t += "<td class='vehYear'>" + obitList[i].vin + "</td>\n";
    t += "<td>\n<a href=\"http://mustercoaches.com/cgi-bin/CompanyInternal?stdout+115+mustercoaches.com+" + obitList[i].obitNum + "+4+9103:Lcgi_html/\">\n" + "Details</a></td>\n";
    t += "</tr>\n";
    document.writeln(t);
  }
  if (obitList.length > 0){
    document.writeln("</table>");
  }


}






function renderVehicle(vehicle)
{
  var t = "";
  var h;
  var w;
  var cellHeight = getPicCellHeight();
  var selectionPic = getPicSelectionImg();


  if (vehiclePictures.length > 0){
    vehicle.pWidth = vehiclePictures[0].w;
    vehicle.pHeight = vehiclePictures[0].h;
    vehicle.picFile = vehiclePictures[0].fileName;
    t += "<tr><td id='vehPicCell' style='height: " + cellHeight + "px'>";
    t += "<img src='../" + vehicle.picFile + "' width='" + vehicle.pWidth + "' height='" + vehicle.pHeight + "'></td></tr>\n";
    activePicIndex = 0;
  }
  t += "<tr><td class='vehMakeYear'>" + selectionPic + vehicle.year + " " + vehicle.make + "</td></tr>\n";

  if (vehicle.price > 0)  t += "<tr><td class='vehInfo'>Price: " + formatPrice(vehicle.price) + "<br>\nColor: " +  vehicle.color;
  else t += "<tr><td class='vehInfo'>Color: " +  vehicle.color;
  t += "<br>Id Num: " + vehicle.vin + "<br>Manufacturer: " + vehicle.manufacturer;
  t += "<br>Body Style: " + vehicle.bodyStyle + "</td></tr>";
  document.writeln(t);

}




function getPicCellHeight()
{
  var i;
  var maxHeight = 0;

  for (i=0;i<vehiclePictures.length; i++){
    if (vehiclePictures[i].h > maxHeight) maxHeight = vehiclePictures[i].h;
  }
  return(maxHeight);
}


function getPicSelectionImg()
{
  if (vehiclePictures.length <= 1) return("");;
  return("<img src='../art/picSelectionArrows.jpg' width='157' height='23' align='right' usemap='#picSel' border='0'>");
}



function formatPrice(p)
{
  var i = 0;
  var j = 0;

  if (p < 1000) return ("$" + p);
  if (p < 1000000) {
    i = p / 1000;
    i = Math.floor(i);
    j = p - i * 1000;
    if (j < 10) j = "00" + j;
    else if (j < 100) j = "0" + j;
    return("$" + i + "," + j);
  }
  return("$" + p);
}



function switchPic(delta)
{
  var idPicCell = document.getElementById("vehPicCell");
  var t;
  var i;

  if (activePicIndex < 0) return;
  i = (activePicIndex + delta + vehiclePictures.length) % vehiclePictures.length;
  t = "<img src='../" + vehiclePictures[i].fileName;
  t +=  "' width='" + vehiclePictures[i].w + "' height='" + vehiclePictures[i].h + "'>";
  idPicCell.innerHTML = t;
  activePicIndex = i;
}
  

