/*
BzzMetrics Object
*/
function BzzMetrics(){
	this.info = {"events": new Array()};

	if(metrics_omniture_active){
		this.omniture_s = s;
	}
}

/* 
Set Info
sets all the BzzMetric properties in generic terms so
they can be used by any analytics program
*/
BzzMetrics.prototype.setInfo = function(name, value){
	name = name.toLowerCase();
	if(name=='events'){
		events = value.split(",");
		for(i in events){
			trimmed = events[i].replace(/^\s+|\s+$/g, '');
			this.setInfo('event', trimmed);
		}
	}else if(name=='event'){
		this.info.events[this.info.events.length]=value;
	}else{
		this.info[name]=value;
	}
}

/*
Activate
runs all necessary code to track page
*/
BzzMetrics.prototype.activate = function(){
	this.parseUrl();
	this.parseMetaTags();
	
	if(metrics_omniture_active){
		this.setAllOmniture();
		this.writeOmniture();
	}
}

/*
Write Omniture
outputs omniture specific activation code
*/
BzzMetrics.prototype.writeOmniture = function(){	
	//Output
	var s_code=s.t();
	if(s_code)document.write(s_code);
}

/*
Set Omniture
sets the omniture specific tracking value and keeps track of 
what values were updated
*/
BzzMetrics.prototype.setOmniture = function(propName, propValue){	
	if(propName=='events'){
		if(s.events && s.events !=''){
			s.events = s.events + ',' + propValue;
			this.omniture_events_used = this.omniture_events_used + "," + propValue;
		}else{
			s.events = propValue;
			this.omniture_events_used = propValue;
		}
		this.omniture_vars_used = this.omniture_vars_used + ",events";
	}else{
		s[propName]=propValue;
		this.omniture_vars_used = this.omniture_vars_used + "," + propName;
	}
}

/*
Set All Omniture
sets all the omniture specific tracking values based on the
generic data stored in the BzzMetric info property
*/
BzzMetrics.prototype.setAllOmniture = function(){	
	// begin naming
	if(this.info.pagename){
		//s.pageName = this.info.pagename;
		this.setOmniture('pageName',this.info.pagename);
		//s.hier1 = this.info.pagename;
		this.setOmniture('hier1',this.info.pagename);
	
		var naming = this.info.pagename.split(":");
	
		if(naming.length > 1){
			this.setOmniture('channel',naming[0]); 
		}
		
		if(naming.length > 2){
			this.setOmniture('prop1',naming[0] + ":" + naming[1]); 
		}

		if(naming.length > 3){
			this.setOmniture('prop2',naming[0] + ":" + naming[1] + ":" +naming[2]); 
		}
	}
	// end naming

	// other properties
	for(infoType in this.info){
		switch(infoType){
			case 'pagetype':
				this.setOmniture('pageType', this.info[infoType]); 
				break;
			case 'externalpromotion':
				this.setOmniture('campaign', this.info[infoType]); 
				break;
			case 'visitorstate':
				this.setOmniture('state', this.info[infoType]); 
				break;
			case 'visitorzip':
				this.setOmniture('zip', this.info[infoType]); 
				break;
			case 'contenttype':
				this.setOmniture('prop3', this.info[infoType]); 
				break;
			case 'contenttitle':
				this.setOmniture('prop4', this.info[infoType]); 
				break;
			case 'contentid':
				this.setOmniture('prop5', this.info[infoType]); 
				break;
			case 'internalsearchterm':
				this.setOmniture('prop6', this.info[infoType].toLowerCase()); 
				break;
			case 'agentstatus':
				if(this.info[infoType]=='Agent' || $j.cookie('om_AgentStatus')=='Agent'){
					$j.cookie('om_AgentStatus','Agent',{expires:1000,path:'/',domain:'bzzagent.com'});
					this.setOmniture('prop10', 'Agent');
				}else{
					this.setOmniture('prop10', 'Anonymous'); 			
				}
				break;
			case 'contentcreator':
				this.setOmniture('prop12', this.info[infoType]); 
				break;
			case 'feedcount':
				this.setOmniture('prop16', this.info[infoType]); 
				break;


			//eVars
			case 'user':
				this.setOmniture('eVar11', this.info[infoType]); 
				$j.cookie('om_Agent',this.info[infoType],{expires:1000,path:'/',domain:'bzzagent.com'});
				break;
			case 'internalpromotion':
				this.setOmniture('eVar14', this.info[infoType]); 
				break;
			case 'agentlevel':
				this.setOmniture('eVar17', this.info[infoType]); 
				break;
			case 'partnername':
				this.setOmniture('eVar20', this.info[infoType]); 
				break;
			case 'campaignident':
				this.setOmniture('eVar21', this.info[infoType]); 
				break;
			case 'frogident':
				this.setOmniture('eVar22', this.info[infoType]); 
				break;
			case 'pollident':
				this.setOmniture('eVar23', this.info[infoType]); 
				break;
			case 'formname':
				this.setOmniture('eVar24', this.info[infoType]); 
				break;
			case 'polltype':
				this.setOmniture('eVar25', this.info[infoType]); 
				break;
			case 'bzzscapeident':
				this.setOmniture('eVar26', this.info[infoType]); 
				break;
		}
	}
	
	//events
	for(i in this.info.events){
		//Omniture events should only be sent if there is
		//a change in thier pertinent evars.
		switch(this.info.events[i].toLowerCase()){
			case 'registrationcomplete':
				this.setOmniture('events','event2'); 
				break;
			case 'login':
				if(s.getValOnce(s.eVar11,'om_3_e11')!=''){ //user
					this.setOmniture('events','event3'); 
				}
				break;
			case 'bzzcampaignjoin':
				this.setOmniture('events','event4'); 
				this.setOmniture('events','event25'); //Campaign Success
				break;
			case 'referralcomplete':
				this.setOmniture('events','event5'); 
				break;
			case 'referralsuccess':
				this.setOmniture('events','event6'); 
				break;
			case 'formstart':
				this.setOmniture('events','event7'); 
				//set the form name to the page name
				this.setOmniture('eVar24',this.info.pagename);
				break;
			case 'formcomplete':
				this.setOmniture('events','event8'); 
				break;
			case 'pollcomplete':
				this.setOmniture('events','event9'); 
				break;
			case 'bzzreportcomplete':
				this.setOmniture('events','event10'); 
				this.setOmniture('events','event25'); //Campaign Success
				break;
			case 'frogvisit':
				this.setOmniture('events','event11'); 
				break;
			case 'frogopinioncomplete':
				this.setOmniture('events','event12'); 
				this.setOmniture('events','event26'); //Frog Success
				break;
			case 'frogpollcomplete':
				this.setOmniture('events','event13'); 
				this.setOmniture('events','event26'); //Frog Success
				break;
			case 'partnervisit':
				this.setOmniture('events','event14');
				break;
			case 'partnersuccess':
				this.setOmniture('events','event15');
				break;
			case 'frogsharecomplete':
				this.setOmniture('events','event16'); 
				this.setOmniture('events','event26'); //Frog Success
				break;
			case 'frogsharesuccess':
				this.setOmniture('events','event17'); 
				break;
			case 'internalpromotion':
				this.setOmniture('events','event18'); 
				break;
			case 'hirebzzcomplete':
				this.setOmniture('events','event19'); 
				break;
			case 'createbzzscape':
				this.setOmniture('events','event20'); 
				this.setOmniture('events','event27'); //BzzScapeSuccess
				break;
			case 'createbzzscapeobject':
				this.setOmniture('events','event21'); 
				this.setOmniture('events','event27'); //BzzScapeSuccess
				break;
			case 'bzzscapeobjectcomment':
				this.setOmniture('events','event22'); 
				this.setOmniture('events','event27'); //BzzScapeSuccess
				break;
			case 'ratebzzscapeobject':
				this.setOmniture('events','event23'); 
				this.setOmniture('events','event27'); //BzzScapeSuccess
				break;
			case 'createfrog':
				this.setOmniture('events','event24'); 
				this.setOmniture('events','event26'); //Frog Success
				break;
			case 'updateprofile':
				this.setOmniture('events','event28'); 
				break;
			case 'socialmediashare':
				this.setOmniture('events','event29'); 
				break;
		}
	}
}

/*
Parse URL
parses the url for any useful paramaters and adds them to the
BzzMetric info property
*/
BzzMetrics.prototype.parseUrl = function(){
	//Requires jQuery url parser
	$j.url.setUrl(document.location);
	//	alert("0: "+$.url.segment(0)+"\n"+"\n"+"campaignId: "+$.url.param("campaignId"));

	//campaign ident
	if($j.url.param("campaignId")){
		this.setInfo('campaignident',$j.url.param("campaignId"));
	}
	
	if($j.url.param("campaignIdent")){
		this.setInfo('campaignident',$j.url.param("campaignIdent"));
	}

	//frog ident
	if($j.url.param("frogIdent")){
		this.setInfo('frogident',$j.url.param("frogIdent"));
	}
	
	//internal promo
	if($j.url.param("ipid")){
		this.setInfo('event','internalpromotion');
		this.setInfo('internalpromotion',$j.url.param("ipid"));
	}

	//external promo
	if($j.url.param("epid")){
		this.setInfo('event','externalpromotion');
		this.setInfo('externalpromotion',$j.url.param("epid"));
	}
	
	//search
	if($j.url.param("searchstr")){
		this.setInfo('internalsearchterm',$j.url.param("searchstr"));
	}

	//create bzzscape search
	if($j.url.segment(1)=="CreateBzzscape.do" && $j.url.param("action")=='newuniverse2'){
		this.setInfo('internalsearchterm',$j.url.param("bzzscapename"));
	}

	//poll ident
	if($j.url.segment(1)=="ViewPoll.jsp"){
		if($j.url.param("id")){
			this.setInfo('pollident',$j.url.param("id"));
		}	
	}
	
	//frog referral
	if($j.url.segment(1)=="FrogHome.do"){
		if($j.url.param("referral")=="true"){
			this.setInfo('event','frogShareSuccess');
		}	
	}

	//page
	if($j.url.segment(0)=="pages" && $j.url.segment(1)=="Page.do"){
		this.setInfo('pagename',"agent site:general:uncategorized pages:"+$j.url.param("page").toLowerCase());
	}

	//go
	if($j.url.segment(0)=="go"){
		i=1;
		goNames = new Array();
		while($j.url.segment(i)!=null && i <100){
			goNames[goNames.length] = $j.url.segment(i).toLowerCase();
			i=i+1;
		}
		
		this.setInfo('pagename',"agent site:new agent recruitment:custom landing pages:"+goNames.join(" "));
	}

	//campaign pages
	if($j.url.segment(0)=="member" && $j.url.segment(1)=="campaigns"){
		i=2;
		goNames = new Array();
		while($j.url.segment(i)!=null && i <100){
			goNames[goNames.length] = $j.url.segment(i).toLowerCase();
			i=i+1;
		}
		
		this.setInfo('pagename',"agent site:campaigns:participant information:custom page (" + goNames.join(" ") + ")");
	}
	if($j.url.segment(0)=="member" && $j.url.segment(1)=="CampaignPage.do"){
		this.setInfo('pagename',"agent site:campaigns:participant information:custom page (" + $j.url.param("campaignIdent") + " " + $j.url.param("pid") + ")");
	}
}

/*
Parse Meta Tags
parses the meta tags on the page and adds them to the
BzzMetric info property
*/
BzzMetrics.prototype.parseMetaTags = function(){
	//parse page names
	metaName = $j("meta[name='bzzPageName']").attr('content');
	if(metaName){
		this.setInfo('pageName',metaName);
	}

	//parse events
	metaEvents = $j("meta[name='bzzEvents']").attr('content');
	if(metaEvents){
		this.setInfo('events', metaEvents);
	}
	
	//parse campaign ident
	metaName = $j("meta[name='campaign_ident']").attr('content');
	if(metaName){
		this.setInfo('campaignIdent',metaName);
	}

	//parse poll ident
	metaName = $j("meta[name='bzzPollIdent']").attr('content');
	if(metaName){
		this.setInfo('pollIdent',metaName);
	}
	
	//parse poll category
	metaName = $j("meta[name='bzzPollCategory']").attr('content');
	if(metaName){
	
		catMap = {"0":"unknown", 
			"1":"Pre-Campaign", 
			"2":"Campaign Pass", 
			"3":"BzzReport", 
			"4":"Post-Campaign", 
			"8":"Mid-Campaign", 
			"12":"Campaign Functional", 
			"13":"Pre-Qual", 
			"14":"Profile", 
			"15":"Other", 
			"16":"Frog Review Master", 
			"17":"Frog Review Instance"
		};
	
		this.setInfo('pollType',catMap[metaName]);
	}
	
	//parse bzzscape ident
	metaName = $j("meta[name='bzzScapeIdent']").attr('content');
	if(metaName){
		this.setInfo('bzzScapeIdent',metaName);
	}
	
	//parse content creator
	metaName = $j("meta[name='bzzContentCreator']").attr('content');
	if(metaName){
		this.setInfo('contentCreator',metaName);
	}

	//parse bzzfeed count
	metaName = $j("meta[name='bzzFeedCount']").attr('content');
	if(metaName){
		this.setInfo('feedCount',metaName);
	}

	//parse internal promotions
	metaName = $j("meta[name='bzzInternalPromotion']").attr('content');
	if(metaName){
		this.setInfo('event','internalpromotion');
		this.setInfo('internalpromotion',metaName);
	}
}

/*
Custom Link
sends a custom link call to the active analytics programs
*/
BzzMetrics.prototype.customLink = function(linkObject, linkName, customInfo, linkType){

	linkName = this.info.pagename+":"+linkName;
	this.info.events = new Array();
	
	if(customInfo){
		for(infoType in customInfo){
			this.setInfo(infoType,customInfo[infoType]);
		}
	}
	
	if(metrics_omniture_active){
		this.omnitureCustomLink(linkObject,linkName,linkType);
	}
	if(metrics_google_active){
		this.googleCustomLink(linkName);		
	}
}	

/*
Omniture Custom Link
sends a custom link call to omniture
*/
BzzMetrics.prototype.omnitureCustomLink = function(linkObject, linkName, linkType){
	if(!linkType){
		linkType = 'o';
	}
	this.omniture_events_used='';

	if(!s.pageURL){
		s = this.omniture_s;
	}
	
	s.events='';

	this.setAllOmniture();
	s.linkTrackVars=this.omniture_vars_used;
	s.linkTrackEvents=this.omniture_events_used;
	s.tl(linkObject, linkType, linkName);
}	

/*
Google Custom Link
sends a custom link call to google
*/
BzzMetrics.prototype.googleCustomLink = function(linkName){
	linkName = linkName.replace(/[\/\s\.\?\&]/g, '_');
	linkName = linkName.replace(/:/g, '/');
	googstr = "/track/" + linkName;
}

/*
Custom Page
sends a custom page call to active analytics programs
*/
BzzMetrics.prototype.customPage = function(customInfo){
	this.info = {"events": new Array()};
	
	for(infoType in customInfo){
		this.setInfo(infoType,customInfo[infoType]);
	}

	if(metrics_omniture_active){
		this.omnitureCustomPage();
	}
}	

/*
Omniture Custom Page
sends a custom page call to omniture
*/
BzzMetrics.prototype.omnitureCustomPage = function(){
	s=s_gi(metrics_omniture_account);
	s.manageVars("c");
	this.setAllOmniture();
	s.t();
}

/*
Show It
debugging function
*/
BzzMetrics.prototype.showIt = function(){
	var out="";
	for(i in bzzMetrics.info){
		out=out+"\n"+i+" -> "+bzzMetrics.info[i];
	}
	out=out+"\n";
	for(i in s){
		if(typeof s[i] == 'string'&&s[i].length<100&&s[i]!=''&&i.substring(0,2)!='g_'){
			out=out+"\n"+i+" -> "+s[i];
		}
	}
	alert(out);
}


//Instantiate the BzzMetrics object
bzzMetrics = new BzzMetrics();


