//can store the includes in a string in the global.incs
// application("Video_reportingIncludes") = "<script src="">"
var functionsArray = new Array;
var arrIndex = 0;

/*takes an videoEvent Object - changes it to params since "eval" is called and only supports strings
using a video Event is cleaner since new properties can be added without having to change the call in 
all the places it occures in the player code.
Properties so far are: 
1. eventType = Event Type (Duration, Pause, ..)
2. title = Video Title
3. curPos = Postition in video when this event was logged
4. clipId = Clip Id
5. clipAdTag = Content Classification of the Clip 
6. hostPage = Hosting Page Name, Category name/Id
7. affiliateName = Affiliate Name */
function logVideoEvent(p_objVideoEvent){
    //in case duration is unavailable set it to 0 so we do record a hit
    if (p_objVideoEvent.curPos == null) {
		p_objVideoEvent.curPos = 0;
	}
    
	if (p_objVideoEvent.curPos > 0) {
		p_objVideoEvent.curPos = Math.floor(p_objVideoEvent.curPos);
	}
	
	//only log if there is a valid clipid - for wmp there are a few hardcoded placeholder entries to control the playlist behaviour that need to be filtered for reporting
	if (p_objVideoEvent.title != "" && 
	       p_objVideoEvent.title != null && 
	       unescape(p_objVideoEvent.title) != "playlist" && 
	       unescape(p_objVideoEvent.title) != "&nbsp;" &&
	       unescape(p_objVideoEvent.title).indexOf("pxl_trans.gif") < 0 &&
	       unescape(p_objVideoEvent.clipId).indexOf("pxl_trans.gif") < 0 ) 
	{
	    var strFunctionCall;
	    var strInputParams;
	    
	    //construct input params string
	    strInputParams = "('" + p_objVideoEvent.eventType + "',";
	    strInputParams += "'" + p_objVideoEvent.title + "',";
	    strInputParams += "'" + p_objVideoEvent.curPos + "',";
	    strInputParams += "'" + p_objVideoEvent.clipId + "',";
	    strInputParams += "'" + p_objVideoEvent.clipAdTag + "',";
	    strInputParams += "'" + p_objVideoEvent.hostPage + "',";
	    strInputParams += "'" + p_objVideoEvent.affiliateName + "',";
	    strInputParams += "'" + p_objVideoEvent.ownerAffiliateNo + "',";
	    strInputParams += "'" + p_objVideoEvent.isAd + "',";
	    strInputParams += "'" + p_objVideoEvent.ciid + "',";
	    strInputParams += "'" + p_objVideoEvent.referer + "',";
	    strInputParams += "'" + p_objVideoEvent.baseUrl + "')";
	
	    //loop thru all registred functions in functionsArray
	    for (var x=0; x<functionsArray.length; x++){
		    //construct function call
		    strFunctionCall = functionsArray[x] + strInputParams;
			
		    //make the function call using eval
		    eval(strFunctionCall);
		    //alert(strFunctionCall);
	    }

	}
}


/*
This function is called from flash. since we cannot use javascript objects in flash 
we have to pass in string values to this function. Much like when using
the javascript Eval() function. 
*/
function logVideoEventParameters(p_strEventType, p_strTitle, p_strCurPos, p_strClipId, p_strClipAdTag, p_strHostPage, p_strAffiliateName, p_strOwnerAffiliateNo, p_isAd, p_ciid, p_strReferer, p_baseUrl, p_reportingKeywords, p_uri, p_len, p_pctViewed, p_location, p_contentSource, p_keywords, p_playerType){
	//only log if there is a valid clipid

	if (p_strTitle != "" && p_strClipId != "undefined") {
		var strFunctionCall;
		var strInputParams;
		
		//escape 
		p_strTitle      = escape(p_strTitle);
		p_strClipAdTag  = escape(p_strClipAdTag);
		p_strHostPage   = escape(p_strHostPage);
		p_contentSource	= escape(p_contentSource);
		p_keywords		= escape(p_keywords);
		p_playerType	= escape(p_playerType);
				
		//construct input params string
		strInputParams = "('" + p_strEventType + "',";
		strInputParams += "'" + p_strTitle + "',";
		strInputParams += "'" + p_strCurPos + "',";
		strInputParams += "'" + p_strClipId + "',";
		strInputParams += "'" + p_strClipAdTag + "',";
		strInputParams += "'" + p_strHostPage + "',";
		strInputParams += "'" + p_strAffiliateName + "',";
		strInputParams += "'" + p_strOwnerAffiliateNo + "',";
		strInputParams += "'" + p_isAd + "',";
		strInputParams += "'" + p_ciid + "',";
		strInputParams += "'" + p_strReferer + "',";
		strInputParams += "'" + p_baseUrl + "',";
		strInputParams += "'" + p_reportingKeywords + "',";
		strInputParams += "'" + p_uri + "',";
		strInputParams += "'" + p_len + "',";
		strInputParams += "'" + p_pctViewed + "',";
		strInputParams += "'" + p_location + "',";
		strInputParams += "'" + p_contentSource + "',";
		strInputParams += "'" + p_keywords + "',";
		strInputParams += "'" + p_playerType + "')";

		//loop thru all registred functions in functionsArray
		for (var x=0; x<functionsArray.length; x++){
		    try{
			    //construct function call
			    strFunctionCall = functionsArray[x] + strInputParams;
    			
			    //make the function call using eval
			    eval(strFunctionCall);
			} catch(e){
			    //maybe make a reporting call indicating an error so at least
			    //we can determine if a reporting call is failing
			}
		}
	}
}

//returns a time interval string that a video was viewed till
//"5-10 seconds", "10-5 seconds"...
function getVideoTimeInterval(p_pos){
	var intLowRange, intHighRange, strTimeInterval;
	var intIntervalDenominator = 5;	//should be able to override thru global.incs or as a query string param for custom videos

	//calculate which time interval the current position falls into
	p_pos			= Math.floor(p_pos/intIntervalDenominator);
	intLowRange		= p_pos * intIntervalDenominator;
	intHighRange	= intLowRange + intIntervalDenominator;
	strTimeInterval	= intLowRange + "-" + intHighRange + " seconds";
	
	return strTimeInterval;		
}

//registers the function in the global functionsArray
function registerFunctionName(p_strFunctionName){
	functionsArray[arrIndex] = p_strFunctionName;
	arrIndex = arrIndex + 1;
}
