// omniture-tracking.js
// created by rowena grossman
// created on april 22, 2011
// updated on august 8, 2011 - include marshalls and tjmaxx and survey-related omniture prop and evar
// this is the hypothetical omniture 'page'
 
function Page (chain,title) {
	this.enOnly = false;
	this.debug = true;
	this.chain = chain;
	this.s_account = 'tjxdev';
	if (this.debug == false) {
		switch(this.chain) {
		case 'WNE': this.s_account = 'tjxwinners'; break;
		case 'WNF': this.s_account = 'tjxwinners'; break;
		case 'HSE': this.s_account = 'tjxhomesense'; break;
		case 'HSF': this.s_account = 'tjxhomesense'; break;
		case 'SSE': this.s_account = 'tjxtjxstylesense'; break;
		case 'MCE': this.s_account = 'tjxtjxmarshallsca'; break;
		case 'TJM': this.s_account = 'tjxtjmaxx'; break;
		case 'MAR': this.s_account = 'tjxmarshalls'; break;
		default: this.s_account = 'tjxdev';
		}
	}
	else {
		this.enOnly = true;
		switch(this.chain) {
		case 'TJM': this.s_account = 'tjxtjmaxxtest'; break;
		case 'MAR': this.s_account = 'tjxmarshallstest'; break;		
		default: this.s_account = 'tjxdev';
		}
	}
	
	this.title = title;	
	this.language = 'en';
	this.category = '';
	this.season = '';
	this.section = '';
	this.channel = '';
	this.pageName = '';
		
	if (this.enOnly == true) { this.language = ''; }
	if (this.language != '') { this.pageName = this.language + ':'; }
	if (this.category != '') {
		this.pageName = this.pageName + this.category + ':';
	}
	if (this.season != '') {
		this.pageName = this.pageName + this.season + ':';
	}
	this.pageName = this.pageName + this.title;

	this.trackVarsList = 'None';
	this.trackEventsList = 'None';
	this.contestId = '';
	this.adSource = '';
	this.esSource = '';
	this.surveyInfo = '';

	this.trackPage = function () {
		//alert("Send off tracking to Omniture");
		//alert(this.s_account);
		var s=s_gi(this.s_account);
		//alert(this.pageName);
		s.pageName = this.pageName;		
		s.channel = this.channel;
		s.prop3 = this.language; // legacy prop3
		switch(this.chain) {
		case 'TJM': s.prop14 = this.surveyInfo; s.eVar20 = this.surveyInfo; break;
		case 'MAR': s.prop14 = this.surveyInfo; s.eVar20 = this.surveyInfo; break;			
		default:
		  s.prop4 = this.adSource;
		  s.prop5 = this.contestId;
		  s.campaign = '';
		  s.eVar1 = this.esSource;
		  s.eVar3 = this.adSource;
		  s.eVar5 = this.contestId;			
		}
		//alert(this.trackEventsList);
		s.events = this.trackEventsList;
		var s_code=s.t();if(s_code)document.write(s_code);
	}

	// trackLink - this is for link tracking
	// we're adding this function for within the 'page' object even though each link type is not a page view, hence it doesn't have an associated pageName.
	// however, this function leverages the values (for s_account, linkTrackVars, and linkTrackEvents) already set for the page object 
	// ie vars and events we'd like to track when the link is clicked
	this.linkType = 'o'; // default to custom link type
	this.element = 'link'; // default to 'link'
	if (this.language != '') { this.linkName = this.language + ':' + this.element + ':' + this.title; }
	else { this.linkName = this.element + ':' + this.title; }

	this.trackLink = function (obj) {
		//alert("Send off tracking to Omniture");
		var s=s_gi(this.s_account);
		s.linkTrackVars=this.trackVarsList;
		s.linkTrackEvents=this.trackEventsList;
		//alert(this.linkType);
		//alert(this.element);
		//alert(this.linkName);
		//var lt;
		//if (this.linkType != 'o') {
		//	lt=obj.href!=null?s.lt(obj.href):"";
		//	if (lt=='') { s.tl(obj,this.linkType,this.linkName); }
		//}
		//else { s.tl(obj,this.linkType,this.linkName); }	
		s.tl(obj,this.linkType,this.linkName);
	}

}
Page.prototype.clearVars = function () {
	this.trackVarsList = 'None';
	this.trackEventsList = 'None';
	this.contestId = '';
	this.adSource = '';
	this.esSource = '';
	this.surveyInfo = '';
}
Page.prototype.trackVars = function (thisItem, data) {
	// sets the tracking variables (props and evars)
	var theVars;
	if (thisItem !='') {
		switch(thisItem) {
		case 'email-source': theVars = 'eVar1'; this.esSource = data; break;
		case 'ad-source': theVars = 'prop4,eVar3'; this.adSource = data; break;	
		case 'contest-id': theVars = 'prop5,eVar5'; this.contestId = data; break;
		case 'survey': theVars = 'prop14,eVar20'; this.surveyInfo = data; break;
		}
		if (this.trackVarsList == '' ) { this.trackVarsList = theVars; }
		else {
			if (this.trackVarsList.indexOf(theVars) == 0) { this.trackVarsList = this.trackVarsList + ',' + theVars; }		
		}		
	}
}
Page.prototype.trackSuccessEvent = function (thisEvent) {
	// sets the success event variable 
	var eventVar;
	if (thisEvent != '') {
		switch(thisEvent) {
		case 'email-signup': eventVar = 'event2'; break;	
		case 'store-search': eventVar = 'event3'; break;	
		case 'contest-entry': eventVar = 'event4'; break;
		}
	
		if (this.trackVarsList == '' ) { this.trackVarsList = 'events'; }	
		else {
			if (this.trackVarsList.search(/events/i) == 0) { this.trackVarsList = this.trackVarsList + ',events'; }
		}	
		if (this.trackEventsList == '' ) { this.trackEventsList = eventVar; }
		else {
			if (this.trackEventsList.indexOf(eventVar) == 0) { this.trackEventsList = this.trackEventsList + ',' + eventVar; }	
		}	
	}
}
Page.prototype.setLinkType = function (linkType) {
	// sets the link type to 'o' for custom links (default), 'e' for external links or 'd' for file downloads
	this.linkType = linkType;
	if (this.linkType == '') { this.linkType = 'o'; }
	else {
		if (this.linkType != 'e' && this.linkType != 'd') { this.linkType = 'o'; }
	}
}
Page.prototype.setElement = function (element) {
	// sets the element , default is 'link'
	this.element = element;
	if (this.element == '') { this.element = 'link'; }
}
Page.prototype.setLinkName = function (desc) {
	// sets the link name in structure language:element:desc or element:desc
	// if desc is empty, default is title
	if (this.language != '') { this.linkName = this.language + ':';  }
	if (desc == '') { this.linkName = this.linkName + this.element + ':' + this.title; }
	else { this.linkName = this.linkName + this.element + ":" + desc; }
}
