xmp.createNamespace("CNN");

///////////////////////////////////////////////////////////////////////////////////////////////////

CNN.AbstractOmnitureCommand = function() { }

CNN.AbstractOmnitureCommand.prototype._createReportingObject = function(context) {
	var s = xmp.getGlobalNamespace().s_gi(this._getOmnitureAccount());
	var vid = this._getVideoId(context);
	return s;
};

CNN.AbstractOmnitureCommand.prototype._getOmnitureAccount = function() {
	return xmp.util.SettingsManager.getInstance().
		getContextNode().getString('omniture account', '');
};

CNN.AbstractOmnitureCommand.prototype._getVideoId = function(context) {
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().id;
};

CNN.AbstractOmnitureCommand.prototype.scrapeReferrer = function(ref) {
	ref = '' + ref;
	if (ref.indexOf('http://') == 0) {
		ref = ref.slice(7);
	}
	ref = ref.slice(0,ref.indexOf('/'));
	return ref;
}

CNN.AbstractOmnitureCommand.prototype._postReportingObject = function(context, s, hit, cType) {
	if (hit) {
		s.t();
	} else {
		s.tl(this, 'o', this._getCustomTriggerPrefix() + cType);
	}
};

///////////////////////////////////////////////////////////////////////////////////////////////////

CNN.EVPDomOmnitureTriggerCommand = function() { 
	this._logger = new xmp.util.internals.CategoryLogger( 'EVPDomOmnitureTriggerCommand' );
};

xmp.DERIVE_CLASS( CNN.AbstractOmnitureCommand, CNN.EVPDomOmnitureTriggerCommand );

CNN.EVPDomOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function() {
	return 'XMP Embed Domestic';
};

CNN.EVPDomOmnitureTriggerCommand.prototype.doCommand = function(context) {
	switch (context.getTriggerType()) {
		case 'start': this._doStartCommand(context); break;
		case 'end': this._doEndCommand(context); break;
		default: break;
	}
};

CNN.EVPDomOmnitureTriggerCommand.prototype._doStartCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.pageName = vid;
	s.channel = 'XMP Embed Domestic';
	try {
		s.prop1 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.prop2 = 'video';
	var subcategory = '';
	if(context.getPlayableNode().getPlayableData().getDataObject().subcategory != undefined){
		subcategory = context.getPlayableNode().getPlayableData().getDataObject().subcategory;
	}
	s.prop3 = subcategory;	
	s.eVar1 = vid;
	s.eVar2 = 'XMP Embed Domestic';
	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events = 'event1';
	this._postReportingObject(context, s, true, 'Video Start');
	s.linkTrackVars='None'; 
	s.linkTrackEvents='None';
};

CNN.EVPDomOmnitureTriggerCommand.prototype._doEndCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.linkTrackVars='eVar1,eVar2,eVar3,events'; 
	s.linkTrackEvents='event2';
	s.eVar1=vid; //same as page name
	s.eVar2='XMP Embed Domestic';
	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events='event2';
	this._postReportingObject(context, s, false, 'Video End');
};

///////////////////////////////////////////////////////////////////////////////////////////////////

CNN.EVPIntlOmnitureTriggerCommand = function() { 
	this._logger = new xmp.util.internals.CategoryLogger( 'EVPIntlOmnitureTriggerCommand' );
};

xmp.DERIVE_CLASS( CNN.AbstractOmnitureCommand, CNN.EVPIntlOmnitureTriggerCommand );

CNN.EVPIntlOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function()
{
	return 'XMP Embed International';
};

CNN.EVPIntlOmnitureTriggerCommand.prototype.doCommand = function(context)
{
	switch (context.getTriggerType())
	{
		case 'start': this._doStartCommand(context); break;
		case 'end': this._doEndCommand(context); break;
		default: break;
	}
};

CNN.EVPIntlOmnitureTriggerCommand.prototype._doStartCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.pageName = vid;
	s.channel = 'XMP Embed International';
	try {
		s.prop1 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.prop2 = 'video';
	var subcategory = '';
	if(context.getPlayableNode().getPlayableData().getDataObject().subcategory != undefined){
		subcategory = context.getPlayableNode().getPlayableData().getDataObject().subcategory;
	}
	s.prop3 = subcategory;	
	s.eVar1 = vid;
	s.eVar2 = 'XMP Embed International';
	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events = 'event1';
	this._postReportingObject(context, s, true, 'Video Start');
	s.linkTrackVars='None'; 
	s.linkTrackEvents='None';
};

CNN.EVPIntlOmnitureTriggerCommand.prototype._doEndCommand = function(context)
{
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.linkTrackVars='eVar1,eVar2,eVar3,events'; 
	s.linkTrackEvents='event2';
	s.eVar1=vid; //same as page name
	s.eVar2='XMP Embed International';
	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events='event2';

	this._postReportingObject(context, s, false, 'Video End');
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// EVPOnsiteDomOmnitureTriggerCommand
///////////////////////////////////////////////////////////////////////////////////////////////////

CNN.EVPOnsiteDomOmnitureTriggerCommand = function() { 
	this._logger = new xmp.util.internals.CategoryLogger( 'EVPOnsiteDomOmnitureTriggerCommand' );
};

xmp.DERIVE_CLASS( CNN.AbstractOmnitureCommand, CNN.EVPOnsiteDomOmnitureTriggerCommand );

CNN.EVPOnsiteDomOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function() {
	return 'XMP Embed Onsite Domestic';
};

CNN.EVPOnsiteDomOmnitureTriggerCommand.prototype.doCommand = function(context) {
	switch (context.getTriggerType()) {
		case 'start': this._doStartCommand(context); break;
		case 'end': this._doEndCommand(context); break;
		default: break;
	}
};

CNN.EVPOnsiteDomOmnitureTriggerCommand.prototype._doStartCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);

	s.channel = 'XMP Embed Onsite Domestic';
	s.eVar2 = 'XMP Embed Onsite Domestic';
	s.server = '';
	s.eVar3 = '';

	if (typeof(cnnSectionName) !== "undefined") {
		s.channel=cnnSectionName;
		s.eVar2=cnnSectionName;
	}
	if (typeof(cnnSubSectionName) !== "undefined") {
		s.server=cnnSubSectionName;
		s.eVar3=cnnSubSectionName;
	}

	s.pageName = vid;
	s.eVar1 = vid;

	try {
		s.prop1 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.prop2 = 'video';
	var subcategory = '';
	if(context.getPlayableNode().getPlayableData().getDataObject().subcategory != undefined){
		subcategory = context.getPlayableNode().getPlayableData().getDataObject().subcategory;
	}
	s.prop3 = subcategory;	

	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events = 'event1';
	this._postReportingObject(context, s, true, 'Video Start');
	s.linkTrackVars='None'; 
	s.linkTrackEvents='None';
};

CNN.EVPOnsiteDomOmnitureTriggerCommand.prototype._doEndCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.linkTrackVars='eVar1,eVar2,eVar3,events'; 
	s.linkTrackEvents='event2';
	s.eVar1=vid; //same as page name

	s.eVar2='XMP Embed Onsite Domestic';
	if (typeof(cnnSectionName) !== "undefined") {
		s.eVar2=cnnSectionName;
	}
	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events='event2';
	this._postReportingObject(context, s, false, 'Video End');
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// EVPOnsiteIntlOmnitureTriggerCommand
///////////////////////////////////////////////////////////////////////////////////////////////////

CNN.EVPOnsiteIntlOmnitureTriggerCommand = function() { 
	this._logger = new xmp.util.internals.CategoryLogger( 'EVPOnsiteIntlOmnitureTriggerCommand' );
};

xmp.DERIVE_CLASS( CNN.AbstractOmnitureCommand, CNN.EVPOnsiteIntlOmnitureTriggerCommand );

CNN.EVPOnsiteIntlOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function() {
	return 'XMP Embed Onsite International';
};

CNN.EVPOnsiteIntlOmnitureTriggerCommand.prototype.doCommand = function(context) {
	switch (context.getTriggerType()) {
		case 'start': this._doStartCommand(context); break;
		case 'end': this._doEndCommand(context); break;
		default: break;
	}
};

CNN.EVPOnsiteIntlOmnitureTriggerCommand.prototype._doStartCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);

	s.channel = 'XMP Embed Onsite International';
	s.eVar2 = 'XMP Embed Onsite International';
	s.server = '';
	s.eVar3 = '';

	if (typeof(cnnSectionName) !== "undefined") {
		s.channel=cnnSectionName;
		s.eVar2=cnnSectionName;
	}
	if (typeof(cnnSubSectionName) !== "undefined") {
		s.server=cnnSubSectionName;
		s.eVar3=cnnSubSectionName;
	}

	s.pageName = vid;
	s.eVar1 = vid;

	try {
		s.prop1 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.prop2 = 'video';
	var subcategory = '';
	if(context.getPlayableNode().getPlayableData().getDataObject().subcategory != undefined){
		subcategory = context.getPlayableNode().getPlayableData().getDataObject().subcategory;
	}
	s.prop3 = subcategory;	

	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events = 'event1';
	this._postReportingObject(context, s, true, 'Video Start');
	s.linkTrackVars='None'; 
	s.linkTrackEvents='None';
};

CNN.EVPOnsiteIntlOmnitureTriggerCommand.prototype._doEndCommand = function(context) {
	var s = this._createReportingObject(context);
	var vid = this._getVideoId(context);
	s.linkTrackVars='eVar1,eVar2,eVar3,events'; 
	s.linkTrackEvents='event2';
	s.eVar1=vid; //same as page name

	s.eVar2 = 'XMP Embed Onsite International';
	if (typeof(cnnSectionName) !== "undefined") {
		s.eVar2=cnnSectionName;
	}

	try {
		s.eVar3 = this.scrapeReferrer(document.referrer);
	} catch(err) { }
	s.events='event2';

	this._postReportingObject(context, s, false, 'Video End');
};

//////////////
// Nielsen
//////////////

CNN.EVPNielsenTriggerCommand = function() { 
	this._logger = new xmp.util.internals.CategoryLogger( 'EVPIntlNielsenTriggerCommand' );
};

CNN.EVPNielsenTriggerCommand.prototype.doCommand = function(context) {
	var isTurnerDomain = false;
	if (location.hostname.indexOf('turner.com')>0) { isTurnerDomain = true; }
	if (!isTurnerDomain) { 	// don't add tracking image to internal player
		var cnnScRandom = Math.ceil(Math.random()*1000000000);
  		var cnnScTitle = escape(context.getPlayableNode().getPlayableData().getDataObject().headline);
		switch (context.getTriggerType()) {
			case 'start':{
				var cnnScImgt1 = 'dav0-' + cnnScTitle;
				break;
			}
			case 'end':{
				var cnnScImgt1 = 'dav2-' + cnnScTitle;
				break;
			}
			default:{
				return false;
			}
		}

		var cnnScImgSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-900564&tl=' + cnnScImgt1 + '&c6=vc,b01&cc=1&rnd=' + cnnScRandom;
		cnnScImg = new Image();
		cnnScImg.src = cnnScImgSrc;
	}
};

//////////////
// Comscore
//////////////
CNN.ComscoreTriggerCommand = function() {
};

CNN.ComscoreTriggerCommand.prototype.doCommand = function(context) {
	if (context.getTriggerType() == 'start') {
		var is_video = true;
		var show_level = escape(context.getPlayableNode().getPlayableData().getDataObject().headline);
		CNN.ComscoreTriggerCommand.pingBeacon(is_video, show_level);
	}
};

/**
 * actual beacon ping broken out into its own function so AdNodeListener.prototype.handleRender can
 * use it as welol 
 * @param {boolean} is_video (is ad or video)
 * @param {Object} show_level (show title if video, empty if ad)
 */
CNN.ComscoreTriggerCommand.pingBeacon = function(is_video,show_level) {
	var urlLoc = 'http://beacon.securestudies.com/scripts/beacon.dll';
	// Content Distributor ID
	var urlQS1 = '?CXNID=3005117';
	// Content Owner ID
	var urlQS2 = '&CDID=3005117';
	// Location where viewed ID (CPID) - this is different for the embeds!
	var urlQS3 = '&CPID=8000000';
	// Genre content
	if (is_video) {
		var urlQS4 = '&CGID=020100';
	} else {
		var urlQS4 = '&CGID=010000';
	}
	// show level
	var urlQS5 = '&CBID=' + show_level;
	var comscoreUrl = urlLoc + urlQS1 + urlQS2 + urlQS3 + urlQS4 + urlQS5 + '&rnd=' + Math.ceil(Math.random()*1000000000);
	cnnCmScImg = new Image();
	cnnCmScImg.src = comscoreUrl;
};
