Array.prototype.unique = function () {
  var r = new Array();
  o:for(var i = 0, n = this.length; i < n; i++)
  {
    for(var x = 0, y = r.length; x < y; x++)
    {
      if(r[x]==this[i])
      {
        continue o;
      }
    }
    r[r.length] = this[i];
  }
  return r;
}

CARS = window.CARS || {};
CARS.adProducts = CARS.adProducts || {};
CARS.adProducts.retarget = {
  ids: '',
  cc: function (name,value,days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
  },
  rc: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length,c.length); }
    }
    return null;
  },
  catLookup: function(catID) {
    catDef = {  '118979':'Compact',
                '118980':'Midsize', 
                '118981':'FullSize',
                '118982':'Luxury',
                '118984':'SUV',
                '118985':'LuxurySUV',
                '118986':'Hybrid',
                '118987':'Van',
                '118988':'Sports',
                '118989':'Trucks',
				'610891':'Crossover'
              };
    return catDef[catID];
  },
  decodeCat: function(catID) {
    catName = this.catLookup(catID);
    cats = this.rc('catRetarget');
    if (cats) {
      cats = catName + '|' + cats;
      cats = cats.split('|');
      cats = cats.unique();
      if (cats.length) cats = cats.join('|');
    } else {
      cats = catName;
    }
    this.cc('catRetarget',cats,30);
    return;
  },
  dropCookie: function(value,source) {
    if (value == '') return;
    for (i=0; i<source.length; i++) {
      var splitAdIds = source[i].split("|");
      if(splitAdIds[1] == value) {
        this.decodeCat(splitAdIds[0]);
      }
    }
  }
};