var CARS = CARS || {};
CARS.adProducts = CARS.adProducts || {};
CARS.adProducts.suggestedSearch = {
  // config
  tMake: {}, tModel: {}, trackingImages: [], trackingURLs: [], _reported: false,
  // function for clearing saved vehicle cookie
  clearSavedVehicle: function() {
    var today = new Date();
    expires = new Date(today.getTime() + 30 * 1000 * 3600 * 24).toGMTString()
    document.cookie = 'SessionInfo=;expires=' + expires + ';path=/;domain=.' + location.hostname;
  },
  // function for pingback of reporting
  triggerPingback: function() {
    if (this._reported) return;
    for (i=0; i<this.trackingURLs.length; i++) {
      this.trackingImages[i] = new Image();
      this.trackingImages[i].src = this.trackingURLs[i];
    }
    this._reported = true;
  },
  // prep and check
  init: function(p) {
    this.tMake.name = p.make;
    this.tModel.name = p.model;
    this.trackingURLs = p.trackingURLs;
    if (prePopSelectByText && popModels) {
      this.fire();
    }
  },
  // trigger
  fire: function() {
    // pre-pop search
    if (this.tMake.name.length > 0) {
      prePopSelectByText(document.getElementById("make_0"), this.tMake.name);
      prePopSelectByText(document.getElementById("make_1"), this.tMake.name);
      this.tModel.modelid = convertNameToValue(modelDefaults.options, this.tModel.name);
      document.getElementById("model_0").mkId = document.getElementById("make_0").value;
      document.getElementById("model_1").mkId = document.getElementById("make_1").value;
      if (this.tModel.modelid.length > 0) {
        document.getElementById("make_0").mdId = this.tModel.modelid;
        document.getElementById("make_1").mdId = this.tModel.modelid;
      }
      evalMenus(0, 'make', this.tModel.modelid);
      evalMenus(1, 'make', this.tModel.modelid);
    }
    //pre-pop crp
    prePopSelectByText(document.crpWidget.makeid, this.tMake.name);
    popModels(); // get models
    prePopSelectByText(document.crpWidget.modelid, this.tModel.name);
    popYears();	
    // reporting call
    this.triggerPingback();
    // clear cookie if it was set
    this.clearSavedVehicle();
  }
}
