/**
 * VWTracking class to handle find a dealer 
 * tracking tags.
 * 
 * Depends on jQuery.
 * 
 * Currently handled:
 * Doubleclick spotlight
 * Omniture page landing
 * 
 * @author erik.daniel
 */
 /*global $,channel, omniture,vwServerName,vwDomain,vwModelYear,document */
function VWTracker(model) {
	this.setModel(model);
	this.tagarray = [];
}

/**
 * Set current model to assign model
 * specific tracking tags.
 * 
 * @param model {String} 
 */
VWTracker.prototype.setModel = function(model) {
	
	if(model === undefined || model === null){
		this.model = 'dealer';
	}
	else
	{
		this.model = model;
	}
	this.setTags();

};

/**
 * Set Doubleclick and Omniture tags.
 */
VWTracker.prototype.setTags = function() {
	this.dc = {};
	this.dc.model = this.model;
	this.dc.baseHref = 'http://fls.doubleclick.net/activityi;src=1033942;';
	
	switch (this.model) {
	case "cc":
		this.setDCObject("cc", "ccfadlp","ccfadrfl");
		this.setOmniture("cc","models:cc:find_dealer");
		break;
	case "eos":
		this.setDCObject('eos', 'eodfadld','eosfadfl');
		this.setOmniture("eos","models:eos:find_dealer");
		break;
	case "gti":
		this.setDCObject('gti', 'gtifadld', 'gtifadfl');
		this.setOmniture("GTI","models:GTI:find_dealer");
		break;
	case "jetta":
		this.setDCObject('jetta', 'jetfadld','jetfadfl');
		this.setOmniture("jetta","models:jetta:find_dealer");
		break;
	case "newbeetle":
		this.setDCObject('newbeet', 'newbfadl','nbfadfl');
		this.setOmniture('newbeetle',"models:new beetle:find_dealer");
		break;
	case "newbeetlecon":
		this.setDCObject('nbc', 'beet1038','nbcfadfl');
		this.setOmniture("newbeetlecon","models:new beetle convertible:find_dealer");
		break;
	case "passat":
		this.setDCObject('passat', 'passa664', 'pasfadfl');
		this.setOmniture("passat","models:passat:find_dealer");
		break;
	case "golf":
	case "rabbit":
		this.setDCObject('rabbit', 'rabbi278','rabfadfl');
		this.setOmniture("golf","models:golf:find_dealer");
		break;
	case "passatwagon":
		this.setDCObject('passwagn', 'passa908', 'pwgfadfl');
		this.setOmniture("passatwagon","models:passat wagon:find_dealer");
		break;
	case "routan":
		this.setDCObject('routan', 'fadland', 'roufadfl');
		this.setOmniture("routan","models:routan:find_dealer");
		break;
	case "touareg":
		this.setDCObject('touareg', 'truar031','toufadfl');
		this.setOmniture("touareg","models:touareg:find_dealer");
		break;
	case "tiguan":
		this.setDCObject('tiguan', 'tigufad', 'tigfadfl');
		this.setOmniture("tiguan","models:tiguan:find_dealer");
		break;
	case "jettasportwagen":
		this.setDCObject('jettawag', 'sportfad','sportfadfl');
		this.setOmniture("jettasportwagen","models:jettasportwagen:find_dealer");
		break;
	default:
		this.setDCObject(this.model,'deale298', 'fadresfl');
		this.setOmniture("", "dealer search:global search");
	}
};

/**
 * Set Doubleclick tags and add to tag array.
 */
VWTracker.prototype.setDCObject = function(type, cat, searchcat) {
	this.dc.type = type;
	this.dc.cat = cat;
  this.dc.searchcat = searchcat;
	this.dc.url = this.dc.baseHref + 'type=' + this.dc.type + ';cat=' + this.dc.cat + ';ord=1;num=';
	this.dc.surl = this.dc.baseHref + 'type=' + this.dc.type + ';cat=' + this.dc.searchcat + ';ord=1;num=';
};


/**
 * Set Omniture tags.
 */
VWTracker.prototype.setOmniture = function(nameplate, pagename) {
    var channel = channel;
		if(nameplate === '') {
			channel = 'dealer search';
		}
		else {
			channel = 'models';
		}
    omniture.channel = channel;
    if(pagename){
      omniture.pagename = pagename;
    }
    if(nameplate){
      omniture.nameplate = nameplate;
    }
};

VWTracker.prototype.trackOmni = function() {
  try{
    var omni_code = omniture.t();
    if(omni_code){
      document.write(omni_code);
    }
  } catch(err){}
}

VWTracker.prototype.trackDoubleClick = function() {
  this._send(this.dc.url);
}

VWTracker.prototype.trackDoubleClickSearch = function() {
  this._send(this.dc.surl);
}
/**
*
* Fire omniture tracking for dealer clicks.
* param searchObj object
*   searchObj.event number
*   searchObj.dealerID number
*   searchObj.zipcode number
*/
VWTracker.prototype.trackDealer = function(searchObj) {

  // clone omniture
  var omni = omniture;

  // set dealer vars
  omni.events = 'event' + searchObj.ev;
  omni.c4 = searchObj.dealerID;
  omni.v4 = searchObj.dealerID;
  omni.c12 = searchObj.zipcode;

  this.trackOmni();
};

VWTracker.prototype.track = function(type) {
  if(type == 'search') {
    this.trackDoubleClickSearch();
  }
  this.trackOmni();
};


VWTracker.prototype._send = function(url) {
	var ifr = document.createElement('iframe');
	ifr.src=url + Math.floor(Math.random() * 10000000) + '?';
  ifr.height = 1;
  ifr.width = 1;
  document.getElementsByTagName('body')[0].appendChild(ifr);
};


