var CARS = CARS || {};
CARS.adProducts = CARS.adProducts || {};
CARS.adProducts.suggestedSearch = {
  // config
  iModel: {}, rModel: {},  iTrackingURLs: [], rTrackingURLs: [], iTrackingImages: [], rTrackingImages: [],  _invReported: false,  _resReported: 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
  triggerInvPingback: function() {
    if (this._invReported) return;
      for (i=0; i<this.iTrackingURLs.length; i++) {
		this.iTrackingImages[i] = new Image();
		this.iTrackingImages[i].src = this.iTrackingURLs[i];
       }
    this._invReported = true;
  },
   triggerResPingback: function() {
    if (this._resReported) return;
      for (i=0; i<this.rTrackingURLs.length; i++) {
		this.rTrackingImages[i] = new Image();
		this.rTrackingImages[i].src = this.rTrackingURLs[i];
       }
    this._resReported = true;
  },
  // prep and check 
  init: function(p,w) {
    if (w=="inventory") {
      this.iMake = p.make;
      this.iModel.name = p.model;
      this.iTrackingURLs = p.trackingURLs; 
    }
    if (w=="research" || w=="both") {
      this.rMake = p.make;
      this.rModel.name = p.model;
      this.rTrackingURLs = p.trackingURLs;
    }
   // if (prePopSelectByText && popModels) {
      this.fire();
  //  }
  },

  // trigger
  fire: function() {
    // pre-pop search
    if (this.iMake && document.getElementById("make_0") ) {
      prePopSelectByText(document.getElementById("make_0"), this.iMake);
      prePopSelectByText(document.getElementById("make_1"), this.iMake);
      this.iModel.modelid = convertNameToValue(newModelDefaults.options, this.iModel.name);
      document.getElementById("model_0").mkId = document.getElementById("make_0").value;
      document.getElementById("model_1").mkId = document.getElementById("make_1").value;
      if (this.iModel.modelid.length > 0) {
        document.getElementById("make_0").mdId = this.iModel.modelid;
        document.getElementById("make_1").mdId = this.iModel.modelid;
      }
      evalMenus(0, 'make', this.iModel.modelid);
      evalMenus(1, 'make', this.iModel.modelid);	
		 this.triggerInvPingback();
	}	
    //pre-pop crp
	if (this.rMake && document.crpWidget) {
      prePopSelectByText(document.crpWidget.makeid, this.rMake);
      popModels();
      prePopSelectByText(document.crpWidget.modelid, this.rModel.name);
      popYears();	
	    if (prePopSelectByText && popModels) {
		  if(this.iTrackingURLs[0] != this.rTrackingURLs[0]) {
		    this.triggerResPingback();
		  }
		}
		if (popModels) {
		  this.triggerResPingback();
		}
	     
    }
    // clear cookie if it was set
    this.clearSavedVehicle();
  }
}

