var nbaOmEvent = {
	defaultTrackProps:['pageName','server',
	               'prop1','prop2','prop3','prop7','prop8','prop12','prop13','prop16','prop17','prop18','prop19','prop20','prop22','prop23',
	               'eVar1','eVar2','eVar3','eVar8','eVar9'],
	allTrackProps:[],
	allTrackEvents:[],
	types:{
		article: ['CVP','EVP'],
		live: ['League Pass','KBP'],
		main: ['CVP'],
		viral: ['CVP','EVP']
	},
	players:{
		bb: ['BB','League Pass'],
		cvp: ['CVP'],
		evp: ['EVP'],
		kbp: ['KBP'],
		xmp: ['LEXMP','XMP']
		
	},
	sites: ['nba','wnba']
};
nbaOmEvent.checkMatch=function(checkValue,checkList) {
	for (var i=0;i<checkList.length;i++) {
		if (checkValue == checkList[i]) {
			return true;
		}
	}
	return false;
}
nbaOmEvent.clearLinkEvents=function() {
	this.allTrackEvents = [];
	s.linkTrackEvents = this.allTrackEvents.join(',');
	return true;
}
nbaOmEvent.clearMediaEvents=function() {
	this.allTrackEvents = [];
	s.Media.trackEvents = this.allTrackEvents.join(',');
	return true;
}
nbaOmEvent.clearLinkProps=function() {
	this.allTrackProps = [];
	s.linkTrackVars = this.allTrackProps.join(',');
	return true;
}
nbaOmEvent.clearMediaProps=function() {
	this.allTrackProps = [];
	s.Media.trackVars = this.allTrackProps.join(',');
	return true;
}
nbaOmEvent.getFullPlayerType=function(playerType) {

	/* Main or Article VOD? */
	if (w.nbaOmPlayerType) {
		//Non-CVP Players
		this.fullPlayerType = w.nbaOmPlayerType;
	} else {
		//CVP Players
		/* Team or League Player? */
		this.fullPlayerType = (this.videoSource ? this.videoSource : 'U');
		/* What is the context this video is playing in? */
		if (this.videoContext) {
			this.fullPlayerType += this.videoContext;
		} else if (this.checkMatch(w.nbaOmSecondDomain,this.sites) && this.checkMatch(playerType,this.types.main) && ((this.fullPlayerType == 'L' && w.nbaOmFirstFolder == 'video') || (this.fullPlayerType == 'T' && w.nbaOmSecondFolder == 'video'))) {
			this.fullPlayerType += 'M';
		} else if (this.checkMatch(w.nbaOmSecondDomain,this.sites) && this.checkMatch(playerType,this.types.article)) {
			this.fullPlayerType += 'A';
		} else if (this.checkMatch(playerType,this.types.viral)) {
			this.fullPlayerType += 'V';
		} else if (this.checkMatch(playerType,this.types.live) && (w.nbaOmFirstFolder == 'live1' || w.nbaOmFirstFolder == 'live2')) {
			this.fullPlayerType += 'L';
		} else {
			this.fullPlayerType += 'U';
		}
	}
	/* Set the correct player type */
	if (this.checkMatch(playerType,this.players.cvp)) {
		this.fullPlayerType += 'CVP';
	} else if (this.checkMatch(playerType,this.players.xmp)) {
		this.fullPlayerType += 'XMP';
	} else if (this.checkMatch(playerType,this.players.kbp)) {
		this.fullPlayerType += 'KBP';
	} else if (this.checkMatch(playerType,this.players.evp)) {
		this.fullPlayerType += 'EVP';
	} else if (this.checkMatch(playerType,this.players.bb)) {
		this.fullPlayerType += 'BB';
	} else {
		this.fullPlayerType += playerType;
	}
	return this.fullPlayerType;
}
nbaOmEvent.onActiveTab=function() {
	if (this.activeTab) {
		this.setDefaultLinkProps();
		w.nbaOmPageTitle=w.nbaOmFullPath+this.activeTab.hash;
		this.currentEvent="Active Tab";
		return this.sendMicro();
	}
}
nbaOmEvent.onAudioStart=function(){
	if (this.audioMatchup) {
		this.setDefaultLinkProps();
		w.nbaOmPageTitle = this.audioMatchup;
		this.setLinkEvent('event11');
		this.currentEvent="Audio League Pass";
		return this.sendMicro();
	}
}
nbaOmEvent.onCheckoutStart=function(){
	if (this.orderProducts && this.orderProducts.length > 0) {
		this.setDefaultLinkProps();
		this.setLinkProp('products',';'+this.orderProducts+';1;');
		this.setLinkEvent('scCheckout');
		this.currentEvent="Checkout Start";
	}
	return true;
}
nbaOmEvent.onDVRStart=function(){
	this.setDefaultLinkProps();
	this.videoFullPlayerType = this.getFullPlayerType(this.videoPlayerType);
	this.setLinkProp('prop4',this.videoId);
	this.setLinkProp('prop5',this.videoTitle);
	this.setLinkProp('prop6',this.videoFullPlayerType);
	this.setLinkProp('prop10',this.videoCategory);
	this.setLinkProp('eVar4');
	this.setLinkProp('eVar5');
	this.setLinkProp('eVar6');
	this.setLinkProp('eVar7');
	this.setLinkProp('eVar17');
	this.setLinkProp('eVar18');
	this.setLinkProp('eVar10',this.videoUserType);
	this.setLinkProp('eVar11',this.videoMatchup);
	this.setLinkProp('eVar13',this.flowName);
	this.setLinkProp('eVar15',this.videoCDNBitrate);
	this.setLinkProp('eVar19',this.flowTeams);
	this.setLinkEvent('event1');
	this.currentEvent="DVR Start";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onDVRComplete=function(){
	this.setDefaultLinkProps();
	this.videoFullPlayerType = this.getFullPlayerType(this.videoPlayerType);
	this.setLinkProp('prop4',this.videoId);
	this.setLinkProp('prop5',this.videoTitle);
	this.setLinkProp('prop6',this.videoFullPlayerType);
	this.setLinkProp('prop10',this.videoCategory);
	this.setLinkProp('eVar4');
	this.setLinkProp('eVar5');
	this.setLinkProp('eVar6');
	this.setLinkProp('eVar7');
	this.setLinkProp('eVar17');
	this.setLinkProp('eVar18');
	this.setLinkProp('eVar10',this.videoUserType);
	this.setLinkProp('eVar11',this.videoMatchup);
	this.setLinkProp('eVar13',this.flowName);
	this.setLinkProp('eVar15',this.videoCDNBitrate);
	this.setLinkProp('eVar19',this.flowTeams);
	this.setLinkEvent('event2');
	this.currentEvent="DVR Complete";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onFlowStart=function(){
	this.setDefaultLinkProps();
	this.setLinkProp('eVar13',this.flowName);
	if (this.flowUserType) {
		this.setLinkProp('eVar10',this.flowUserType);
	}
	if (this.flowTeams) {
		this.setLinkProp('eVar19',this.flowTeams);
	}
	this.setLinkEvent('event1');
	this.currentEvent="Flow Start";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onFlowComplete=function(){
	this.setDefaultLinkProps();
	this.setLinkProp('eVar13',this.flowName);
	if (this.flowUserType) {
		this.setLinkProp('eVar10',this.flowUserType);
	}
	if (this.flowTeams) {
		this.setLinkProp('eVar19',this.flowTeams);
	}
	this.setLinkEvent('event2');
	this.currentEvent="Flow Complete";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onOrderStart=function(){
	if (this.orderProducts && this.orderProducts.length > 0) {
		this.setDefaultLinkProps();
		this.setLinkProp('eVar13',"LeaguePass-Order");
		this.setLinkProp('eVar10',this.orderProducts);
		this.setLinkProp('products',';'+this.orderProducts+';1;');
		this.setLinkEvent('event1');
		this.setLinkEvent('scAdd');
		this.currentEvent="Order Start";
		if (this.microCall) {
			return this.sendMicro();
		} else {
			return true;
		}
	}
}
nbaOmEvent.onOrderComplete=function(){
	if (this.orderProducts && this.orderProducts.length > 0) {
		this.setDefaultLinkProps();
		this.setLinkProp('eVar13',"LeaguePass-Order");
		this.setLinkProp('eVar10',this.orderProducts);
		this.setLinkProp('products',';'+this.orderProducts+';1;');
		this.setLinkEvent('event2');
		this.setLinkEvent('purchase');
		this.currentEvent="Order Complete";
	}
	return true;
}
nbaOmEvent.onRegistrationStart=function(){
	this.setDefaultLinkProps();
	this.setLinkProp('eVar14',this.regTarget);
	this.setLinkProp('eVar16',this.regType);
	this.setLinkEvent('event9');
	this.currentEvent="Registration Start";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onRegistrationComplete=function(){
	/* Set the default props because they are needed for this micro-call. */
	this.setDefaultLinkProps();
	this.setLinkProp('eVar14',this.regTarget);
	this.setLinkProp('eVar16',this.regType);
	this.setLinkEvent('event10');
	this.currentEvent="Registration Complete";
	if (this.microCall) {
		return this.sendMicro();
	} else {
		return true;
	}
}
nbaOmEvent.onSegmentStart=function(media, n){
	return true;
}
nbaOmEvent.onStreamStart=function(){
	/* Set the default props because they are needed for this micro-call. */
	this.setDefaultMediaProps();
	this.videoFullPlayerType = this.getFullPlayerType(this.videoPlayerType);
	this.setMediaProp('prop4',this.videoId);
	this.setMediaProp('prop5',this.videoTitle);
	this.setMediaProp('prop6',this.videoFullPlayerType);
	this.setMediaProp('prop10',this.videoCategory);
	this.setMediaProp('eVar4');
	this.setMediaProp('eVar5');
	this.setMediaProp('eVar6');
	this.setMediaProp('eVar7');
	this.setMediaProp('eVar17');
	this.setMediaProp('eVar18');

	/* Set League-Pass Specific values. */
	if (this.videoFullPlayerType=='LPBB') {
		this.setMediaProp('eVar10',this.videoUserType);
		this.setMediaProp('eVar11',this.videoMatchup);
		this.setMediaProp('eVar15',this.videoCDNBitrate);
		this.setMediaProp('eVar19',this.videoTeams);
		if (this.videoType) {
			switch(this.videoType) {
				case 'live-audio':
					this.setMediaEvent('event12');
				break;
				case 'live-video':
					this.setMediaEvent('event3');
				break;
				default:
					this.setMediaEvent('event4');
			}
		} else {
			this.setMediaEvent('event4');
		}
	} else {
		if (this.videoType && this.videoType=='live-video') {
			this.setMediaEvent('event7');
		} else {
			this.setMediaEvent('event8');
		}
	}
	this.sendMediaPlay();

	// other tracking pings for video content
	if (w.nbaOmSecondDomain == 'nba' && this.videoId) {
		/* Nielsen Request */
	    var nielsenSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-100429&tl=dav0-'+
	    encodeURIComponent(this.videoId) +
	    '&c6=vc,c08&cc=1&rnd=' +
	    Math.ceil(Math.random() * 1000000000);
	    var tempImg = new Image();
	    tempImg.src = nielsenSrc;
	}

	// other tracking pings for video content
	if (this.videoId) {
		// searchignite.com start
		var siimage = new Image(); 
		siimage.src = 'http://track.searchignite.com/si/CM/Tracking/TransactionTracking.aspx?siclientid=3114&transactionamount=0&SICustTransType=12679&jscript=0&rnd=' + Math.ceil(Math.random() * 1000000000);

		//doubleclick start			
		var doubleclick_image = new Image();
		var axel = Math.random();
		var a = axel * 10000000000000;
		doubleclick_image.src = 'http://ad.doubleclick.net/activity;src=1990986;type=nbaco993;cat=nbavi811;ord='+ a + '?';
	}
	return true;
}
nbaOmEvent.onStreamEnd=function(){
    // Nielsen (on end)
    if (w.nbaOmSecondDomain == 'nba' && this.videoId) {
        var nielsenSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-100429&tl=dav2-' +
        encodeURIComponent(this.videoId) +
        '&c6=vc,c08&cc=1&rnd=' +
        Math.ceil(Math.random() * 1000000000);
        var tempImg = new Image();
        tempImg.src = nielsenSrc;
    }
    
	// other tracking pings for video content
	if (this.videoId) {
		// searchignite.com video-end
		var siimage = new Image(); 
		siimage.src = 'http://track.searchignite.com/si/CM/Tracking/TransactionTracking.aspx?siclientid=3114&transactionamount=0&SICustTransType=12680&jscript=0&rnd=' + Math.ceil(Math.random() * 1000000000);
	}

	return true;
}
nbaOmEvent.onSplashView=function(){
	return true;
}
nbaOmEvent.sendMediaPlay=function() {
	s.Media.open(this.videoId,this.videoRunTime,this.videoFullPlayerType);
	s.Media.play(this.videoId,0);
	s.Media.track(this.videoId);
	s.Media.close(this.videoId);
	this.clearMediaProps();
	this.clearMediaEvents();
	return true;
}
nbaOmEvent.sendMicro=function(){
	s.tl(this,'o',this.currentEvent);
	this.clearLinkProps();
	this.clearLinkEvents();
	return true;
}
nbaOmEvent.setDefaultLinkProps=function(){
	this.allTrackProps = this.defaultTrackProps.slice(0);
	s.linkTrackVars = this.allTrackProps.join(',');
	return true;
}
nbaOmEvent.setDefaultMediaProps=function(){
	this.allTrackProps = this.defaultTrackProps.slice(0);
	s.Media.trackVars = this.allTrackProps.join(',');
	return true;
}
nbaOmEvent.setLinkEvent=function(eventValue){
	if (!this.checkMatch('events',this.allTrackProps)) {
		this.allTrackProps.push('events');
		s.linkTrackVars = this.allTrackProps.join(',');
	}
	if (!this.checkMatch(eventValue,this.allTrackEvents)) {
		this.allTrackEvents.push(eventValue);
		s.linkTrackEvents = this.allTrackEvents.join(',');
		s.events = this.allTrackEvents.join(',');
	}
	return true;
}
nbaOmEvent.setMediaEvent=function(eventValue){
	if (!this.checkMatch('events',this.allTrackProps)) {
		this.allTrackProps.push('events');
		s.Media.trackVars = this.allTrackProps.join(',');
	}
	if (!this.checkMatch(eventValue,this.allTrackEvents)) {
		this.allTrackEvents.push(eventValue);
		s.Media.trackEvents = this.allTrackEvents.join(',');
		s.events = this.allTrackEvents.join(',');
	}
	return true;
}
nbaOmEvent.setLinkProp=function(propName,propValue){
	if (!this.checkMatch(propName,this.allTrackProps)) {
		this.allTrackProps.push(propName);
		s.linkTrackVars = this.allTrackProps.join(',');
	}
	if (propValue) {
		s[propName] = propValue;
	} else {
		s[propName] = null;
	}
	return true;
}
nbaOmEvent.setMediaProp=function(propName,propValue){
	if (!this.checkMatch(propName,this.allTrackProps)) {
		this.allTrackProps.push(propName);
		s.Media.trackVars = this.allTrackProps.join(',');
	}
	if (propValue) {
		s[propName] = propValue;
	} else {
		s[propName] = null;
	}
	return true;
}

/* "Handler" functions for the Audio League Pass Application */

function handleALPOmnitureSubmit(homeCity,visitingCity) {
	try {
		nbaOmEvent.audioMatchup =  "Audio League Pass: NBA-" + w.teamCity[visitingCity] + ' @ NBA-' + w.teamCity[homeCity];
		nbaOmEvent.microCall = true;
		nbaOmEvent.onAudioStart();
		return true;
	} catch(e) {
		return false;
	}
}

/* "Handler" functions for the League Pass Broad Band Application */

function handleStreamStart(id, title, homeTeam, visitingTeam, mode, mediatype, cdn, bitrate, teams, usertype) {
    try {
    	nbaOmEvent.videoId = id;
    	nbaOmEvent.videoTitle = title;
		nbaOmEvent.videoTeams=teams;
		nbaOmEvent.videoRunTime = 'unknown';
		nbaOmEvent.videoSource = 'L';
		nbaOmEvent.videoContext = 'P';
		nbaOmEvent.videoPlayerType = 'BB';
		nbaOmEvent.videoType = mediatype;
		nbaOmEvent.videoCategory = mediatype;
	    nbaOmEvent.videoMatchup = visitingTeam +' @ ' + homeTeam;
	    nbaOmEvent.videoCDNBitrate = cdn + "-" + bitrate;
	    nbaOmEvent.videoUserType = usertype;
		nbaOmEvent.onStreamStart();
		return true;
    } catch(e) {
    	return false;
    }
}

function handleStreamEnd(id, title, homeTeam, visitingTeam, mode, mediatype, cdn, bitrate, teams, usertype) {
	try {
    	nbaOmEvent.videoId = id;
    	nbaOmEvent.videoTitle = title;
		nbaOmEvent.videoRunTime = 'unknown';
		nbaOmEvent.videoSource = 'L';
		nbaOmEvent.videoContext = 'P';
		nbaOmEvent.videoPlayerType = 'BB';
		nbaOmEvent.videoType = mediatype;
		nbaOmEvent.videoCategory = mediatype;
	    nbaOmEvent.videoMatchup = visitingTeam +' @ ' + homeTeam;
	    nbaOmEvent.videoCDNBitrate = cdn + "-" + bitrate;
	    nbaOmEvent.videoUserType = usertype;
		nbaOmEvent.onStreamEnd();
		return true;
	} catch(e) {
		return false;
	}
}

function handleFlowStart(flowname, teams, usertype) {
	try {
		nbaOmEvent.flowName=flowname;
		nbaOmEvent.flowTeams=teams;
		nbaOmEvent.flowUserType=usertype;
		nbaOmEvent.microCall=true;
		nbaOmEvent.onFlowStart();
		return true;
	} catch(e) {
		return false;
	}
}

function handleFlowEnd(flowname, teams, usertype) {
	try {
		nbaOmEvent.flowName=flowname;
		nbaOmEvent.flowTeams=teams;
		nbaOmEvent.flowUserType=usertype;
		nbaOmEvent.microCall=true;
		nbaOmEvent.onFlowComplete();
		return true;
	} catch(e) {
		return false;
	}
}

function handleDVRStart(id, title, homeTeam, visitingTeam, mode, mediatype, cdn, bitrate, teams, usertype) {
	try {
		nbaOmEvent.flowName='lpbb-dvr';
		nbaOmEvent.flowTeams=teams;
		nbaOmEvent.videoId = id;
		nbaOmEvent.videoTitle = title;
		nbaOmEvent.videoRunTime = 'unknown';
		nbaOmEvent.videoSource = 'L';
		nbaOmEvent.videoContext = 'P';
		nbaOmEvent.videoPlayerType = 'BB';
		nbaOmEvent.videoType = mediatype;
		nbaOmEvent.videoCategory = mediatype;
	    nbaOmEvent.videoMatchup = visitingTeam +' @ ' + homeTeam;
	    nbaOmEvent.videoCDNBitrate = cdn + "-" + bitrate;
	    nbaOmEvent.videoUserType = usertype;
		nbaOmEvent.microCall=true;
		nbaOmEvent.onDVRStart();
		return true;
	} catch(e) {
		return false;
	}
}

function handleDVREnd(id, title, homeTeam, visitingTeam, mode, mediatype, cdn, bitrate, teams, usertype) {
	try {
		nbaOmEvent.flowName='lpbb-dvr';
		nbaOmEvent.flowTeams=teams;
		nbaOmEvent.videoId = id;
		nbaOmEvent.videoTitle = title;
		nbaOmEvent.videoRunTime = 'unknown';
		nbaOmEvent.videoSource = 'L';
		nbaOmEvent.videoContext = 'P';
		nbaOmEvent.videoPlayerType = 'BB';
		nbaOmEvent.videoType = mediatype;
		nbaOmEvent.videoCategory = mediatype;
	    nbaOmEvent.videoMatchup = visitingTeam +' @ ' + homeTeam;
	    nbaOmEvent.videoCDNBitrate = cdn + "-" + bitrate;
	    nbaOmEvent.videoUserType = usertype;
		nbaOmEvent.microCall=true;
		nbaOmEvent.onDVRComplete();
		return true;
	} catch(e) {
		return false;
	}
}

function handleLandingPageStart(uri) {
	if (uri) {
		if (uri.search(/saleskey=([A-Za-z0-9_\-]+)?/) != -1) {
			nbaOmEvent.orderProducts=uri.match(/saleskey=([A-Za-z0-9_\-]+)?/)[1];
			nbaOmEvent.microCall=true;
			nbaOmEvent.onOrderStart();
		} else if (uri.search(/\/flow\/preview/) != -1) {
			//Ugly, ugly hack for the free preview.
			nbaOmEvent.setDefaultLinkProps();
			nbaOmEvent.setLinkProp('eVar13',"LeaguePass-Preview");
			nbaOmEvent.setLinkProp('eVar10',"lpbbhalfsfp");
			nbaOmEvent.setLinkProp('products',';lpbbhalfsfp;1;');
			nbaOmEvent.setLinkEvent('event1');
			nbaOmEvent.setLinkEvent('scAdd');
			nbaOmEvent.currentEvent="Order Start";
			nbaOmEvent.sendMicro();
		} else {
			nbaOmEvent.orderProducts="none";
			nbaOmEvent.microCall=true;
			nbaOmEvent.onOrderStart();
		}
		window.setTimeout("window.location='"+uri+"';",500);
	}
}