// Owner Vehicle List Data == oLVD
// omyVLD Structure Example
// [{"brand": "Chrysler", "year": "1997", "nameplate": "Cirrus"}],
var omyVLD = null;


omyVL = {

    DEBUG: false,
    
    COOKIE_MYVEHICLES: "owners.myvehicles",
    COOKIE_SELECTED: "owners.selected",
    COOKIE_DURATION: "730",

    SELECTMYSAVED: "mysaved",
    SAVED_VEHICLES_FORM: "o-my-vehicle-saved",
    SELECT_VEHICLES_FORM: oVL.SELECT_VEHICLES_FORM,
    START_HERE_MESSAGE: "o-my-vehicle-start-here",
    SELECT_INDX: 0,
    IMAGE_PATH: "/shared/images/owners/vehicles/",
    DEFAULT_IMAGE: "dodge_placeholder.jpg",

    ADD: "add",

    // Selected Vehicle Brand, Year, Vehicle, and Model Index Holders
    myOBI: -1,
    myOYI: -1,
    myOVI: -1,
    myOMI: -1,
    
    init: function() {

      var myVehicles = Cookie.get(this.COOKIE_MYVEHICLES);
      var mySelected = Cookie.get(this.COOKIE_SELECTED);

      if (mySelected) {
        this.DEBUG?(alert("cookie value:\n\n " + mySelected)):null;
        this.SELECT_INDX = mySelected;
      }
      
      if (myVehicles) {
        $(this.START_HERE_MESSAGE).setStyle('display', 'none');
        $(this.SELECT_VEHICLES_FORM).setStyle('display', 'none');
        $(this.SAVED_VEHICLES_FORM).setStyle('display', 'block');       
        omyVLD = eval( myVehicles );
        this.DEBUG?(alert("cookie value:\n\n " + myVehicles)):null;
        this.DEBUG?(alert("We heard that you own these vehicles\n\n " + this.toString())):null;
        this.loadMyVehicleList();
      } else {
       $(this.SAVED_VEHICLES_FORM).setStyle('display', 'none');
       $(this.SELECT_VEHICLES_FORM).setStyle('display', 'block');
       $(this.START_HERE_MESSAGE).setStyle('display', 'block');
      }
      
    },

    saveVehicle: function(veh) {

      this.insertVehicle(veh.myyear[veh.myyear.selectedIndex].text, veh.mybrand[veh.mybrand.selectedIndex].text, veh.myvehicle[veh.myvehicle.selectedIndex].text)
      this.setMyVehiclesCookie();
      this.loadMyVehicleList();
      //$(this.SELECT_VEHICLES_FORM).setStyle('display', 'none');
     // $(this.SAVED_VEHICLES_FORM).setStyle('display', 'block');
      this.DEBUG?(alert("From Save Vehicle\n\nloaded.length: " + omyVLD.length + "\nloaded[0].year: " + omyVLD[0].year)):null;
      
        $(this.START_HERE_MESSAGE).setStyle('display', 'none');
        $(this.SELECT_VEHICLES_FORM).setStyle('display', 'none');
        $(this.SAVED_VEHICLES_FORM).setStyle('display', 'block');
        
      return false;
    },

    addVehicle: function (year, brand, nameplate) {
      var i = 0; 
      if (omyVLD) {
        i = omyVLD.length;
      } else {
        omyVLD = new Array ();
      }
      
      omyVLD[i] = {"brand": brand, "year": year, "nameplate": nameplate};

      this.DEBUG?(alert("Added Vehicle\n\nomyVLD.length: " + omyVLD.length + "\ni: " + i + "\nYear: " + omyVLD[i].year + "\nBrand: " + omyVLD[i].brand + "\nNameplate: " + omyVLD[i].nameplate )):null;
      
    },
    
    insertVehicle: function (year, brand, nameplate) {
      var i = 0; 
      var insert = 0; 
      if (omyVLD) {
        insert = omyVLD.length;
        for (i=omyVLD.length; i > 0; i--) {
          if ( (omyVLD[i-1].year > year) || ((omyVLD[i-1].year == year)&&(omyVLD[i-1].nameplate > nameplate))) {
            omyVLD[i] = omyVLD[i-1];
            insert = i-1;
            this.DEBUG?(alert("omyVLD[i-1].year > year\n" + omyVLD[i-1].year + " > " + year + "\n\n" + this.toString())):null;
          } 
        }
      } else {
        omyVLD = new Array ();
      }
      
      omyVLD[insert] = {"brand": brand, "year": year, "nameplate": nameplate};
      this.SELECT_INDX = insert;
      this.setCookie(this.SELECT_INDX);
      

      // this.DEBUG?(alert("Added Vehicle\n\nomyVLD.length: " + omyVLD.length + "\ni: " + i + "\nYear: " + omyVLD[i].year + "\nBrand: " + omyVLD[i].brand + "\nNameplate: " + omyVLD[i].nameplate )):null;
      
    },
    
    removeSelectedVehicle : function() {
        var currSelected = $('mysaved').selectedIndex;
        var currSelectedValue = $('mysaved').options[currSelected].value;
        if(currSelectedValue != this.ADD) {
            this.removeVehicle(currSelected);
        }
    },
    
    removeVehicle: function (vehIndx) {
      var i = 0; 
      var t = 0; 
      var tmpVLD = new Array();
      this.SELECT_INDX = vehIndx;
      
      if (omyVLD) {
        for (i=0; i < omyVLD.length; i++) {
          if ( i != vehIndx ) {
            tmpVLD[t++] = omyVLD[i];
          }
        }
      }
      
      this.DEBUG?(alert("vehIndx: " + vehIndx + "\ntmpVLD.length: " + tmpVLD.length + "\nomyVLD.length: " + omyVLD.length)):null;

      omyVLD = null;
      omyVLD = tmpVLD;

      this.DEBUG?(alert("Removed Vehicle\n\nomyVLD.toString: " + this.toString())):null;

      if (omyVLD && omyVLD.length == 0) {
        this.DEBUG?(alert("Reset EMPTY start" )):null;
        omyVLD = null; 
        this.SELECT_INDX = 0;
        Cookie.remove(this.COOKIE_SELECTED);
        Cookie.remove(this.COOKIE_MYVEHICLES);
        oVL.resetForm();
        this.myOBI = -1;
        this.myOYI = -1;
        this.myOVI = -1;
        this.myOMI = -1;
        $(this.SAVED_VEHICLES_FORM).setStyle('display', 'none');
        $(this.SELECT_VEHICLES_FORM).setStyle('display', 'block');
        $(this.START_HERE_MESSAGE).setStyle('display', 'block');
        oVL.resetForm();
        
        $('o-vehicle-specs-link-known').setStyle('display', 'none');    
        $('o-vehicle-specs-link').setStyle('display', 'inline');
        
        this.DEBUG?(alert("Reset EMPTY end" )):null;
      } else {
        
        this.setMyVehiclesCookie();
        this.loadMyVehicleList();
      }

    },
    
    loadMyVehicleList: function() {
      var list = $(this.SELECTMYSAVED);
      var newopt = null;
      
      this.clearOptList(this.SELECTMYSAVED, 0);
      
      if ( omyVLD ) {
        for( var i=0; i < omyVLD.length; i++ ) {
          newopt = document.createElement("option");
          newopt.setAttribute("value", i);
          newopt.text = omyVLD[i].year + " " + omyVLD[i].brand + " " + omyVLD[i].nameplate;
          try {
            list.add(newopt, null); // standards compliant; doesn't work in IE
          }
          catch(ex) {
            list.add(newopt); // IE only
          }
        }
      }
      newopt = document.createElement("option");
      newopt.setAttribute("value", this.ADD);
      newopt.text = "-- Add Vehicle --";
      try {
        list.add(newopt, null); // standards compliant; doesn't work in IE
      }
      catch(ex) {
        list.add(newopt); // IE only
      }
      
      if ( this.SELECT_INDX >= list.length-1 && list.length > 1) {
        this.SELECT_INDX = list.length-2;
      }
      
      this.DEBUG?(alert("load vehicles\n\nthis.SELECT_INDX: " + this.SELECT_INDX + "\nlist.length: " + list.length )):null;
      list.selectedIndex = this.SELECT_INDX;

      this.getMySelectedVehicleIndex();

    },

    clearOptList: function(selectObj, startPoint) {
      var list = $(selectObj);
      var i = (startPoint==null)?1:startPoint;
      
      this.DEBUG?(alert("Clear Vehicles\n\nselectObj: " + selectObj + "\ni: " + i + "\nstartPoint: " + startPoint + "\nlist.length: " + list.length )):null;

      while ( i < list.length ) {
        list.options[i]= null;
      }
      this.DEBUG?(alert("Cleared Vehicles")):null;
    },

    actionVehicle: function(indx) {
      var list = $(this.SELECTMYSAVED);
      
      this.DEBUG?(alert("Action Vehicle\n\nindx: " + indx + "\nlist.selected.text: " + list[indx].text )):null;
      if (list[indx].value == this.ADD) {
        this.DEBUG?(alert( "Action Add Vehicle" )):null;
        oVL.resetForm();
      } else {
        this.SELECT_INDX = indx;
      }
      this.setCookie(indx);
      this.getMySelectedVehicleIndex();

    },

    setCookie: function (indx) {
      Cookie.set(this.COOKIE_SELECTED, indx, {duration: this.COOKIE_DURATION});
    },

    setMyVehiclesCookie: function() {
      if (omyVLD) {
        Cookie.set(this.COOKIE_MYVEHICLES, this.toString(), {duration: this.COOKIE_DURATION});
      }

    },

    getMySelectedVehicleIndex: function() {
      this.myOBI = this.getMyBrandIndex(omyVLD[this.SELECT_INDX].brand);
      this.myOYI = this.getMyYearIndex(this.myOBI, omyVLD[this.SELECT_INDX].year);
      this.myOVI = this.getMyVehicleIndex(this.myOBI, this.myOYI, omyVLD[this.SELECT_INDX].nameplate );
      
      this.updateMyVehicleSpecsLink();
      
      this.updateMyVehicleImage("o-my-vehicle-image");
      this.DEBUG?(this.alertMySelected()):null;
      //alert("Match:\nBrand Name: " + oVLD[this.myOBI].brand + "\nYear: " + oVLD[this.myOBI].years[this.myOYI].year + "\nNameplate: " + oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].nameplate );
    },

    updateMyVehicleImage: function(containerId) {
      
      imgtag = "<img src=\"";
      imgtag += this.myImageHref();
      imgtag += "\" alt=\"" + this.myYear() + " " + this.myBrand() + " " + this.myNameplate() + "\" />";
      $(containerId).setHTML(imgtag);
      //alert("Match:\nBrand Name: " + oVLD[this.myOBI].brand + "\nYear: " + oVLD[this.myOBI].years[this.myOYI].year + "\nNameplate: " + oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].nameplate );
    },
    
    updateMyVehicleSpecsLink : function() {
        if (this.hasSpecs()) {
            $('o-vehicle-specs-link').setStyle('display', 'none');
            $('o-vehicle-specs-wrapper').setStyle('display', 'none');
            $('o-vehicle-specs-link-known').setStyle('display', 'inline');
            $('o-vehicle-specs-link-known').href = this.mySpecsHref();
            //$('o-vehicle-specs-link-known').href = "/en/owners/vehicle_specs/index.html?year=" + this.myYear() +"&specs=" + this.myNameplate();
        } else {
            $('o-vehicle-specs-link').setStyle('display', 'none');
            $('o-vehicle-specs-wrapper').setStyle('display', 'none');
            $('o-vehicle-specs-link-known').setStyle('display', 'inline');
            $('o-vehicle-specs-link-known').href = "javascript:popupMessageLayer(320,75,$('o-popup-non-support').innerHTML);";
           // $('o-vehicle-specs-link-known').href = "/en/owners/vehicle_specs/index.html?year=" + this.myYear() +"&specs=" + this.myNameplate();
        }
    },

    getMyBrandIndex: function(brandName) {
      if (oVLD) {
        for (i=0; i < oVLD.length; i++) {
          if ( oVLD[i].brand == brandName ) {
            return i;
          } 
        }
      }
    },

    getMyYearIndex: function(brandIndx, year) {
      if (oVLD) {
        for (i=0; i < oVLD[brandIndx].years.length; i++) {
          if ( oVLD[brandIndx].years[i].year == year ) {
            return i;
          } 
        }
      }
    },

    getMyVehicleIndex: function(brandIndx, yearIndx, vehicleName) {
      if (oVLD) {
        for (i=0; i < oVLD[brandIndx].years[yearIndx].vehicles.length; i++) {
          if ( oVLD[brandIndx].years[yearIndx].vehicles[i].nameplate == vehicleName ) {
              return i;
          }
        }
      }
    },

    myBrand: function() {
      if (oVLD && this.myOBI != -1) {
        return oVLD[this.myOBI].brand;
      } else {
        return "";
      }
    },

    myBrandID: function() {
      if (oVLD && this.myOBI != -1) {
        return oVLD[this.myOBI].brand_id;
      } else {
        return "";
      }
    },

    myYear: function() {
      if (oVLD && this.myOYI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].year;
      } else {
        return "";
      }
    },

    myFamily: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].family;
      } else {
        return "";
      }
    },

    myNameplate: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].nameplate;
      } else {
        return "";
      }
    },

    myImage: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].image;
      } else {
        return "";
      }
    },
    
    myImageHref: function() {
      tmp = this.myImage();
      if ( tmp == "") {
        tmp = this.DEFAULT_IMAGE;
      } 
      return this.IMAGE_PATH + tmp;
    },
    
    myWarranty: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].warranty;
      } else {
        return "";
      }
    },
    
    hasWarranty: function() {
      if (oVLD && this.myOVI != -1) {
        if (oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].warranty != "") {
            return true;
          }
        }
      return false;
    },
    
    myWarrantyHref: function() {
      var href = "";
      if ( this.hasWarranty() ) {
        href= "/crossbrand/warranty/?brand=" + this.myBrandID().toUpperCase() + "&modelyear=" + this.myYear() + "&nameplate=" + this.myWarranty();
      } else {
        href= "/crossbrand/warranty/?brand=" + this.myBrandID().toUpperCase();
        
      }
      return href;
    },
    
    hasSchedule: function() {
      if (oVLD && this.myOVI != -1) {
        if (oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].schedule != "") {
            return true;
          }
        }
      return false;
    },
    
    mySchedule: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].schedule;
      } else {
        return "";
      }
    },
    
    myScheduleHref: function() {
      var href = "";
      if ( this.hasSchedule() ) {
        href= "/download/pdf/maintenance_schedules/"+this.mySchedule();
      }
      return href;
    },
    
    hasSpecs: function() {
      if (oVLD && this.myOVI != -1) {
        if (oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].specs != "") {
            return true;
          }
        }
      return false;
    },
    
    mySpecs: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].specs;
      } else {
        return "";
      }
    },
    
    mySpecsHref: function() {
      var href = "";
      if ( this.hasSpecs() ) {
        var myspecs = this.mySpecs();
        if ( myspecs == "autosrc" ) {
          href= "vehicle_specs/index.html?year=" + this.myYear() + "&specs=" + escape(this.myNameplate());
        } else if ( myspecs == "active" ) {
          href= "/bridge/index.html?app=vehiclespecs&family=" + this.myFamily() + "&model=Standard&year=" + this.myYear();
        } else {
          href= "/download/pdf/vehicle_specs/"+myspecs;
        }
      }
      return href;
    },
    
    hasTowing: function() {
      if (oVLD && this.myOVI != -1) {
        if (oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].towing != "") {
            return true;
          }
        }
      return false;
    },
    
    myTowing: function() {
      if (oVLD && this.myOVI != -1) {
        return oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].towing;
      } else {
        return "";
      }
    },
    
    myTowingHref: function() {
      var href = "";
      if ( this.hasTowing() ) {
        if ( this.myTowing() == "active" ) {
          href= "/bridge/index.html?app=towing&family=" + this.myFamily() + "&model=Standard&year=" + this.myYear();
        } else if ( this.myTowing() == "historical" ) {
          href= "/towing5/D/vehicle_to_weight.jsp";
        } else {
          href= "";
        }
      }
      return href;
    },
    
    myManual: function() {
      if (oVLD && this.myOVI != -1) {
        return (oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].manual || oVLD[this.myOBI].years[this.myOYI].vehicles[this.myOVI].qrg);
      } else {
        return "";
      }
    },
    
    myManualHref: function() {
      var href = "/en/owners/manuals/index.html";
      if ( this.myManual() != "" ) {
        href= "/en/owners/manuals/index.html?year="+ this.myYear() + "&nameplate=" + escape(this.myNameplate());
      }
      return href;
    },
    
    alertMySelected: function() {
      als = "Selected Vehicle";
      als += "\nBrand: " + this.myBrand();
      als += "\nYear: " + this.myYear();
      als += "\nNameplate: " + this.myNameplate();
      als += "\nFamily: " + this.myFamily();
      als += "\nImage: " + this.myImage();
      als += "\nImageHref: " + this.myImageHref();
      als += "\nWarrantyHref: " + this.myWarrantyHref();
      als += "\nSpecsHref: " + this.mySpecsHref();
      als += "\nTowingHref: " + this.myTowingHref();
      als += "\nManualHref: " + this.myManualHref();
      alert(als);
    },

    toString: function () {
      var rtnStr = null;
      if (omyVLD) {
        rtnStr = "[";
        
        for (var i=0; i < omyVLD.length; i++) {
          rtnStr += "{\"brand\":\"" +  omyVLD[i].brand + "\","
          rtnStr += "\"year\":\"" +  omyVLD[i].year + "\","
          rtnStr += "\"nameplate\":\"" +  omyVLD[i].nameplate + "\"},";
        }
        
        rtnStr = rtnStr.substr(0,rtnStr.length - 1);
        rtnStr += "]";
  
      }
      return rtnStr;
    }

}




