//NOTE - localConfig.js should be included BEFORE this script
//This script calls a specified URL every <lifelinePeriod> milliseconds
//it is used to log the duration of a user's visit to the page containing this script
//MediaMetricServlet?mediametricid=121&mediametricsessionid=23211&mode=log

//set lifelinePeriod = 10000
//set lifelineMaxRuntime = 36000000

function mediametric (name,lifelinePeriod,lifelineMaxRuntime) {
	this.name = name;
	this.lifelinePeriod = lifelinePeriod; //milliseconds
	this.lifelineMaxRuntime = lifelineMaxRuntime; //milliseconds
	this.lifelineRuntime = 0; //how long this function has been running
	this.requestCounter=0; //increments with each call to the servlet
	
	this.mediametricid=""; //from querystring
	this.mediametricsessionid=""; //from querystring
	this.key=""; //from querystring
	this.eventid=""; //from querystring
	this.eventuserid=""; //from querystring
	
	var qstring=(location.search);
	qstring=qstring.substring(1);
	var pairs=qstring.split("&");
	for(var i=0;i<pairs.length;++i){
		var pairsplit=pairs[i].split("=")
		if ("mediametricid"==pairsplit[0]) this.mediametricid=pairsplit[1];
		if ("mediametricsessionid"==pairsplit[0]) this.mediametricsessionid=pairsplit[1];
		if ("key"==pairsplit[0]) this.key=pairsplit[1];
		if ("eventid"==pairsplit[0]) this.eventid=pairsplit[1];
		if ("eventuserid"==pairsplit[0]) this.eventuserid=pairsplit[1];
	}
	
	this.toid=null;
	this.Lifeline=Lifeline;
}

//called every <lifelinePeriod> milliseconds
function Lifeline(syncwait) {

	if (this.lifelineRuntime >= this.lifelineMaxRuntime) {
		clearTimeout(this.toid);
		return false;
	}
	if (this.mediametricid=="" || this.mediametricsessionid=="") {
		clearTimeout(this.toid);
		return false;
	}

	this.lifelineRuntime += this.lifelinePeriod;
	
	var now = new Date();
	var ms = now.getTime();
	var cacheinterval=(Math.round(ms/1000));//the url will be unique for 1 second
	var useImg=true;

	var reqString="http://event.on24.com/utilApp/MediaMetricServlet";
	var reqParams="mode=log"
	+"&mediametricid="+this.mediametricid
	+"&mediametricsessionid="+this.mediametricsessionid
	+"&eventid="+this.eventid
	+"&eventuserid="+this.eventuserid
	+"&key="+this.key
	+"&counter="+this.requestCounter
	+"&cacheinterval="+cacheinterval;
	
	try{
		var xmlhttp = getXMLHTTP();
		if(xmlhttp){
			xmlhttp.open("GET",reqString + "?" + reqParams,true);
			//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.send(null);
			useImg=false;
		}
	}catch(err){}
	
	if(useImg){
		var reqImage = new Image(); //create request image object	
		reqImage.src = reqString+"?"+reqParams; //make the request to log use
	}
	//if (this.lifelinePeriod==1000)  alert(reqImage.src);
	//if (this.eventid=="9401") alert(syncwait + reqString);
	
	window.status="Duration: "+(this.requestCounter++)+" minute(s)...";
	
	//add a forced delay of syncwait count (default=30000) to allow the src to execute
	if (""+syncwait!="undefined"){
		var i=0;
		for(i = 1; i <= syncwait; i++)
	    	var jjj=i;
	}
	clearTimeout(this.toid);	
	this.toid=setTimeout(this.name+".Lifeline(0)", this.lifelinePeriod);
	return false;
}

var mm1 = new mediametric ("mm1",120000,36000000);
//mm1.Lifeline(0); call is moved to default.html
