// Customized by TechnologyLeaders - 28·May·09

function WTBrand(){
	var that=this;
	this.prop_param = "WT.z_mlsid";
	this.franchise = "undefined";
	this.brands = [];
	this.ref_map = [];
	this.brand_id = "undefined";
	this.ref_code = "undefined";
	this.dcsid = "undefined";
	this.sldb_id = "undefined";
	this.campaign_id = "undefined";
	
	// Referrer to referral code map
  	this.ref_map['cyberhomes.com'] = 'Cyberhomes';
  	this.ref_map['frontdoor.com'] = 'Frontdoor';
  	this.ref_map['base.google.com'] = 'Googlebase';
  	this.ref_map['homes.com'] = 'Homes';
  	this.ref_map['homescape.com'] = 'Homescape';
  	this.ref_map['lakehomes.com'] = 'Lakehomes';
  	this.ref_map['landandfarm.com'] = 'Landandfarm';
  	this.ref_map['oodle.com'] = 'Oodle';
  	this.ref_map['realestate.aol.com'] = 'Aolrealestate';
  	this.ref_map['realestate.yahoo.com'] = 'Yahoorealestate';
  	this.ref_map['trulia.com'] = 'Trulia';
  	this.ref_map['zillow.com'] = 'Zillow';
  	this.ref_map['smarteragent.com'] = 'Smarteragent';
	// this.ref_map['coldwellbanker.com'] = 'Coldwell'; // test code
}

WTBrand.prototype.initBrands=function() {
// Brand ID lookup table uses WT.z_brandid to lookup brands[i][0]
// brand array is (WT.z_brandid,dcsid,sldb_id_parameter,franchise)
  ;var i=0;
  this.brands[i++] = new Array("Coldwell Banker","dcswakqfo0000000kfla5cyep_3f6b","ComColdwellbankerDataProperty_id","CB");
  this.brands[i++] = new Array("CB Dev","dcs31uces000000s9yurvuxep_8v6y","ComColdwellbankerDataProperty_id","CB");
  this.brands[i++] = new Array("Coldwell Banker Commercial","dcs93qttt100004b354fgx3fp_7n5f","undefined","CBC");
  this.brands[i++] = new Array("CBC Dev","dcswpn12u10000k3wemg9r3fp_2p4m","undefined","CBC");
  this.brands[i++] = new Array("ERA","dcsfhi2rb10000o2ujlbas1fp_9n3h","property","ERA");
  this.brands[i++] = new Array("ERA Dev","dcsqijx8e00000c9754mnp1fp_9i5k","property","ERA");
  this.brands[i++] = new Array("SIR","dcs0c9ssk4twkfktg7hmud867_5k4x","R","SIR");
  this.brands[i++] = new Array("Century 21","dcs4tg8aw10000086m3phxf57_9e9n","id","C21");
  this.brands[i++] = new Array("C21 Dev","dcsbrsr8u000004bfaobbnv8k_9r8k","id","C21");
  this.brands[i++] = new Array("BHG Real Estate","dcs0pxrj500000o2u707fsr96_8t4p","listingId","BHG"); 
  this.brands[i++] = new Array("BHG Dev","dcsbycr8i00000clw8t3k5q96_9g8u","listingId","BHG"); 
  this.brands[i++] = new Array("OHDC","dcsft6rnu00000s57f2jwjvep_1t1v","undefined","OHDC");
  this.brands[i++] = new Array("OHDC Dev","dcs4tg8aw10000086m3phxf57_9e9n","OHDC");
  this.brands[i++] = new Array("Test Brand","dcsq8902i10000c9j6trp9opf_9o1b","undefined","TEST"); // Realogy Test Brand dcsID
}

WTBrand.prototype.getMetaValue=function(nm){
  t=document.getElementsByTagName("META");
  for(i=0;i<t.length;i++){
    n=t[i].name.search(nm);
    if(n!=-1)return t[i].content;
  }
  return 'undefined';
}

WTBrand.prototype.lookupDcsID=function(){
  this.brand_id=this.getMetaValue('WT.z_brandid').toLowerCase();
  if(this.brand_id=='undefined')return 'undefined';
  for(var i=0;i<this.brands.length;i++) {
    if(this.brand_id==(this.brands[i][0]).toLowerCase()) {

	this.dcsid=this.brands[i][1];

	if(this.brands[i][2]=='undefined') {
		this.prop_param = 'undefined';
	} else {
		this.prop_param = this.brands[i][2];
	}

	if(this.brands[i][3]=='undefined') {
		this.franchise = 'undefined';
	} else {
		this.franchise = this.brands[i][3];	
	}

    } 
  }
  return this.dcsid;
}

WTBrand.prototype.lookupSldbId=function() {
  var value = this.getMetaValue(this.prop_param);
  if(value =='undefined') {
	var myqry=window.location.search;
	var pos=myqry.indexOf(this.prop_param+'=');
	if(pos!=-1){
		var start=pos+this.prop_param.length+1;
		var end=myqry.indexOf("&",start);
		value=myqry.substring(start,(end!=-1)?end:myqry.length);
	} else {
		value = 'undefined';
	}
  }
  this.sldb_id = value;
  return value;
}
WTBrand.prototype.lookupRefCode=function() {
	var x='undefined';
	this.ref_code='undefined';
	for ( x in this.ref_map ) {
		if(window.document.referrer.toLowerCase().indexOf(x)!=-1) {
			this.ref_code=this.ref_map[x];
		}
	}
	return(this.ref_code);
}
WTBrand.prototype.lookupCampaignID=function() {
	if ( window.location.search.indexOf("WT.mc_id")==-1
		&&this.getMetaValue("WT.mc_id")=='undefined') {

		this.lookupRefCode();
		if(this.ref_code!='undefined' ) {
			this.campaign_id='DLP'+this.franchise+this.ref_code;
		} else {
			this.campaign_id='undefined';
		}
		return(this.campaign_id);
	}
	return 'undefined';
}

WTBrand.prototype.GetId=function() {
	this.initBrands();
	this.lookupDcsID();
	this.lookupSldbId();
	this.lookupCampaignID();
	return this.dcsid;
}

if(dcsInit.enabled){
  var _brand = new WTBrand();

  _brand.GetId();

  if(_brand.sldb_id != 'undefined')WT.z_sldb_id=_brand.sldb_id;
  if(_brand.campaign_id != 'undefined')WT.mc_id=_brand.campaign_id;

   // alert('sldb: '+_brand.sldb_id+' dcsid: '+_brand.dcsid+' camp: '+_brand.campaign_id+' ref: '+window.document.referrer);
  if(_brand.dcsid!='undefined')dcsInit.dcsid=_brand.dcsid;
}
