tkutils.createNamespace("TBS_XMP");

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// TODO - need the following global values set up somehow (SSI or JS file):
// 1) cnnCurHour
// 2) cnnCurMin
// 3) cnnCurDay
// 4) cnnSectionName
// 5) cnnSubSectionName
// 6) whatever other values are needed to implement 
//     'TBS_XMP.MosaicOmnitureTriggerCommand.prototype._reportSpecificStartValues'
//     (see the TODO comment for that function).

TBS_XMP.AbstractOmnitureTriggerCommand = function()
{
};

// Static 'now' date on client machine
TBS_XMP.AbstractOmnitureTriggerCommand._clientStartMs = (new Date()).getTime();

TBS_XMP.AbstractOmnitureTriggerCommand.prototype.doCommand = function(context)
{
	switch (context.getTriggerType())
	{
		case 'start': this._doStartCommand(context); break;
		case 'mid': this._doMidCommand(context); break;
		case 'end': this._doEndCommand(context); break;
		default: break;
	}
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._doStartCommand = function(context)
{
	var s = this._createReportingObject(context);
	var videoId = this._getVideoContentId(context);
	var franchiseId = this._getVideoFranchiseId(context);
	var franchiseName = this._getVideoFranchiseName(context);
	var videoName = this._getVideoContentName(context);
	var videoTitle = this._getVideoTitle(context);
	var categoryId = this._getVideoCategoryId(context);
	var categoryName = this._getVideoCategoryName(context);
	var contentTypeId = this._getVideoContentTypeId(context);
	var contentTypeName = this._getVideoContentTypeName(context);
	var parentId = this._getVideoParentId(context);
	var parentName = this._getVideoParentName(context);
	
	// send Omniture start
	if (parentName !== "") {
		s.prop5 = parentName + ":(" + parentId + ")";
	}
	if (franchiseId !== "") {
		s.prop1 = franchiseName + ":(" + franchiseId + ")";
		s.eVar8 = franchiseName + ":(" + franchiseId + ")";
	}
	
	s.pageName = "Videos:" + videoName + ":(0,," + videoId + "|" + franchiseId + "|,00)";
	s.server = "Content";
	s.channel = "Videos";
	s.prop3 = contentTypeName + ":(" + contentTypeId + ")";
	s.prop4 = categoryName + ":(" + categoryId + ")";
	s.prop6 = videoName + ":(0,," + videoId + "|" + franchiseId + "|,00)";
	s.eVar1 = "Videos: Content";
	s.eVar2 = videoName + ":(0,," + videoId + "|" + franchiseId + "|,00)";
	s.eVar3 = videoName + ":(0,," + videoId + "|" + franchiseId + "|,00)";
	s.eVar7 =  categoryName + ":(" + categoryId + ")";
	s.eVar10 = "Videos";
	s.events = "event1,event2"; 

	this._postReportingObject(context, s, true, 'start');	
	
	// VideoCensus ("dav0-" means start)
	var ScImgSrc;
	var ScRandom = Math.ceil(Math.random()*1000000000);
	ScImgSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-100429';
	ScImgSrc += '&tl=dav0-' + escape(videoId + " / " + videoTitle);    
	ScImgSrc += '&cg=' + escape(franchiseName);     
	ScImgSrc += '&c6=vc,c05' + escape('');            
	ScImgSrc += '&cc=1';
	ScImgSrc += '&rnd=' + ScRandom;
		
	var davImg = "";
	davImg = new Image();
	davImg.src = ScImgSrc;
	
	lastTrackedVideo.videoId = videoId;
	lastTrackedVideo.videoTitle = videoTitle;
	lastTrackedVideo.franchiseName = franchiseName;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._doMidCommand = function(context)
{
	// no longer tracking
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._doEndCommand = function(context)
{
	var s = this._createReportingObject(context);
	var videoId = this._getVideoContentId(context);
	var videoName = this._getVideoContentName(context);
	var videoTitle = this._getVideoTitle(context);
	var franchiseName = this._getVideoFranchiseName(context);
	
	s.events = "event3"; 
	s.linkTrackVars = "eVar2,eVar3,events";
	s.linkTrackEvents = "event3";
	this._postReportingObject(context, s, false, 'end');	
	
	// VideoCensus ("dav2-" means end)
	var ScImgSrc;
	var ScRandom = Math.ceil(Math.random()*1000000000);
	ScImgSrc = 'http://secure-us.imrworldwide.com/cgi-bin/m?ci=us-100429';
	ScImgSrc += '&tl=dav2-' + escape(videoId + " / " + videoTitle);  
	ScImgSrc += '&cg=' + escape(franchiseName);      
	ScImgSrc += '&c6=vc,c05' + escape('');            
	ScImgSrc += '&cc=1';
	ScImgSrc += '&rnd=' + ScRandom;
		
	var davImg = "";
	davImg = new Image();
	davImg.src = ScImgSrc;
	
	// video finished playing so clear the lastTrackedVideo object
	lastTrackedVideo = new Object();
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._postReportingObject = function(context, s, hit, cType)
{
	if (hit) {
		s.t();
	} else {
		s.tl(tkutils.getGlobalNamespace(), 'o', this._getCustomTriggerPrefix() + cType);
	}
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._createReportingObject = function(context)
{
	var s = tkutils.getGlobalNamespace().s_gi(this._getOmnitureAccount());
	s.visitorNamespace="tbs"
	s.trackingServer="metrics.tbs.com"
	return s;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoContentId = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().contentId;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoFranchiseId = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().franchiseId;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoFranchiseName = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().franchiseName;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoContentName = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().contentName;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoTitle = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().headline;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoCategoryId = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().categoryId;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoCategoryName = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().category;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoContentTypeId = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().contentTypeId;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoContentTypeName = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().contentTypeName;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoParentId = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().parentId;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoParentName = function(context)
{
	var node = context.getPlayableNode();
	return node.getPlayableData().getDataObject().parentName;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getOmnitureAccount = function()
{
	return xmp.baseplayer.BasePlayer.getSettingsManager().
		getContextNode().getString('omniture account', '');
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._reportSpecificStartValues = function(context, s)
{
	// default does nothing
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getVideoIdEvar = function()
{
	throw new Error('must override');
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getPageType = function()
{
	throw new Error('must override');
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getLogger = function()
{
	throw new Error('must override');
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function()
{
	throw new Error('must override');
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._didAdPlayBefore = function(context)
{
	var lba = this._getLookBehindNodeArray(context);
	for (var i=0; i<lba.length; i++)
	{
		var node = lba[i];
		if (node.getNodeTypeName() === 'Content')
		{
			return false;
		}
		// NOTE: 'isAd' set by CNNRules.CannotHaveTwoSequentialAdsRule
		if (node.getPlayableData().getRuntimeMetadata('isAd', false))
		{
			return true;
		}
	}
	return false;
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._getLookBehindNodeArray = function(context)
{
	var listener = context.getMetadata('nodeListener', {});
	return listener.getPlayer().getLookBehindNodeArray();
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._calcTimeStrings = function()
{
	return this._calcTimeStringsGeneric(tkutils.getGlobalNamespace().cnnCurHour, 
		tkutils.getGlobalNamespace().cnnCurMin, 
		tkutils.getGlobalNamespace().cnnCurDay);
};

TBS_XMP.AbstractOmnitureTriggerCommand.prototype._calcTimeStringsGeneric = function(curHour, curMin, curDOW)
{
	var retVal = { h15: '', dow: '' };
	if (typeof(curHour)==='undefined'||typeof(curMin)==='undefined'||typeof(curDOW)==='undefined')
	{
		return retVal;
	}
	var diffMs = (new Date()).getTime() - TBS_XMP.AbstractOmnitureTriggerCommand._clientStartMs;
	if (diffMs < 0)
	{	// did user set system clock into the past after xmp started?
		return retVal;
	}
	var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	var doyi = 0; // day of week index
	for (var i=0; i<days.length; i++)
	{
		if (curDOW === days[i])
		{
			doyi = i;
			break;
		}
	}
	// NOTE: Jan 1, 1995 was a Sunday.  Note that this scheme works so long as 
	// diffMs is not enough to take us past Feb 28, because leap years will mess things up, 
	// but highly doubtful someone will leave xmp running for more than 59 days.
	// Note that since we are only calculating hours, 15-minute intervals, and day of the week, 
	// it is OK to do calculations with a date in the past.
	var calcStartMs = Date.UTC(1995, 0, (1 + doyi), curHour, curMin);
	var finalDate = new Date(calcStartMs + diffMs);
	retVal.dow = days[finalDate.getUTCDay()];
	var min15 = 15 * (Math.floor(finalDate.getUTCMinutes()/15));
	retVal.h15 = finalDate.getUTCHours().toString() + ':' + ((min15 === 0) ? '00' : min15.toString());
	if (retVal.h15.length === 4) { retVal.h15 = '0' + retVal.h15; }	
	return retVal;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

TBS_XMP.LaughLabOmnitureTriggerCommand = function()
{
	this._logger = xmp.baseplayer.BasePlayer.createCategoryLogger('LaughLabOmnitureTriggerCommand');
};

xmp.DERIVE_CLASS( TBS_XMP.AbstractOmnitureTriggerCommand, TBS_XMP.LaughLabOmnitureTriggerCommand );

TBS_XMP.LaughLabOmnitureTriggerCommand.prototype._getVideoIdEvar = function()
{
	// laughlab isn't storing the video asx url
	return '';
};

TBS_XMP.LaughLabOmnitureTriggerCommand.prototype._getPageType = function()
{
	return 'LaughLab';
};

TBS_XMP.LaughLabOmnitureTriggerCommand.prototype._getCustomTriggerPrefix = function()
{
	return 'video link: ';
};

TBS_XMP.LaughLabOmnitureTriggerCommand.prototype._getLogger = function()
{
	return this._logger;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

TBS_XMP.WebstatTriggerCommand = function()
{
};

TBS_XMP.WebstatTriggerCommand.prototype.doCommand = function(context)
{
	switch (context.getTriggerType())
	{
		case 'start': this._doStartCommand(context); break;
		default: break;
	}
};

TBS_XMP.WebstatTriggerCommand.prototype._doStartCommand = function(context)
{
	var node = context.getPlayableNode();
	var pingUrl = node.getPlayableData().getDataObject().id+'.vidt';
	xmp.net.AjaxRequestManager.ping('videoTracking', pingUrl);
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
