// class stat - Ability performance/statistics/reporting
function Stat(q_wow){
this.q_WOW=q_wow;
this.q_COOKIE=null;
this.q_WB_SCREENWIDTH="";
this.q_WB_SCREENHEIGHT="";
this.q_PHASE=0;
this.q_ENV=null;
this.q_ENVN=0;
this.q_STATM=0;
//this.regLoad(now());
this.q_POLLTS=null;
this.q_CONNTS=null;
this.q_BUFFTS=null;
this.q_PLAYTS=null;
this.q_DISCTS=null;
this.q_PLAYT=now();
this.q_PLAYA=0;
this.q_PLAYC=0;
this.q_BUFFC=0;
this.q_POLLAVG=0;
this.q_BANDS=0;
this.q_FRAMR=0;
this.q_POLLA=0;
this.q_POLLPROB=0;
this.q_FRAMR=25;
this.q_WB_VERSIONNUMBER=null;
this.q_LIVEID=null;
this.q_DIAGTS=null;
this.q_DIAGREFRESH=120000;
this.q_REFRESH=6000;
this.q_MINREFRESH=5000;
this.q_MAXREFRESH=300000;//5 minutes
this.q_REPORTSTATECHANGES=false;//verbose mode, costly
this.q_REPORTEDSTATE=0;
}//end function

Stat.prototype.init=function Stat_init(q_wow){
this.q_WOW=q_wow;
this.regLoad(now());
}//end function

Stat.prototype.reg=function Stat_reg(q_name,q_arg){
var q_now=now();
switch(q_name){
case "load":this.regLoad(q_now);break;//STATM=7
case "loaded":this.q_STATM=0;break;
case "connect":this.regConnect(q_now);break;//STATM=1
case "buffer":this.regBuffer(q_now);break;//STATM=2
case "play":this.regPlay(q_now);break;//STATM=3
case "pause":this.q_STATM=4;break;
case "idle":this.q_STATM=0;break;
case "stop":this.q_STATM=5;break;
case "close":this.q_STATM=6;this.q_DISCTS=q_now;break;
case "request":this.q_POLLTS=q_now;break;
case "response":this.regResponse(q_now);break;
case "failure":this.q_POLLPROB++;break;
case "bandwidth":this.q_BANDS=0.8*this.q_BANDS+0.2*q_arg;break;
case "framerate":this.q_FRAMR=0.8*this.q_FRAMR+0.2*q_arg;break;
case "liveid":this.q_LIVEID=q_arg;break;
default:
}//end switch
}//end function

Stat.prototype.isPlaying=function Stat_isPlaying(){
return (this.q_STATM==3);
}//end function

Stat.prototype.regVersion=function Stat_regVersion(q_vernr){
if(this.q_WB_VERSIONNUMBER){
}else{
	this.q_WB_VERSIONNUMBER=q_vernr;
}//end if
}//end function

Stat.prototype.regLoad=function Stat_regLoad(q_now){
if(this.q_LOADTS){
	return;//Only register first
}else{
	this.q_LOADTS=q_now;
}//end if
this.q_STATM=7;
var q_cookie=getCookie("WB_COOKIESET");
if(q_cookie){
	if(q_cookie==""){
	   setCookie("WB_COOKIESET",""+this.q_LOADTS,1);
	}//end if
}else{
   setCookie("WB_COOKIESET",""+this.q_LOADTS,1);
}//end if
this.q_WB_COOKIESET=getCookie("WB_COOKIESET");
this.q_WB_SCREENHEIGHT=screen.height;
this.q_WB_SCREENWIDTH=screen.width;
this.q_ENV=this.getEnvironment(false);
this.q_ENVN=this.getEnvironment(true);
}//end function

Stat.prototype.regConnect=function Stat_regConnect(q_now){
if(this.q_STATM==1){
	return;
}else{
	this.q_STATM=1;
	if(this.q_CONNTS){
	}else{
		this.q_CONNTS=q_now;
	}//end if
}//end if
}//end function

Stat.prototype.regBuffer=function Stat_regBuffer(q_now){
if(this.q_STATM==2){
	return;
}else{
	this.q_STATM=2;
	if(this.q_BUFFTS){
		this.q_BUFFC++;//count buffers
	}else{
		this.regConnect(q_now);//ensure that connect has been logged
		this.q_BUFFTS=q_now;
		this.q_BUFFC=1;//first buffer
	}//end if
}//end if
}//end function

Stat.prototype.getConnectDuration=function Stat_getConnectDuration(){
var q_lifetime=now()-this.q_LOADTS;
if(q_lifetime){//no play statistics after 30 seconds
	return (q_lifetime);//return show lifetime so far
}else{
	return 0;
}//end if
}//end function

Stat.prototype.getPlay=function Stat_getPlay(){
if(this.q_PLAYA<=0){
	return 60000;
}else if(this.q_PLAYA>864000000||this.q_PLAYA>this.getField("LOADTS")){//240h
	return 60000;
}else{
	return Math.floor(0.5+this.q_PLAYA);
}//end if
}//end function

Stat.prototype.regPlay=function Stat_regPlay(q_now){
if(this.q_STATM==3){
	if(Math.abs(q_now-this.q_PLAYT)<10000){
		this.q_PLAYA=this.q_PLAYA+q_now-this.q_PLAYT;//accumulate playing time
	}//end if
	this.q_PLAYT=q_now;//Reset playtime counter
	this.q_PLAYC++;//count playpolls - roughly accumulating playing time if called every second
}else{
	this.q_STATM=3;
	this.q_PLAYA=this.q_PLAYA+800;//compensate halfsecond for outages
	this.q_PLAYC=this.q_PLAYC+1;//count playpolls - roughly accumulating playing time if called every second
	this.q_PLAYT=q_now;//Reset playtime counter
	if(this.q_PLAYTS){
	}else{
		this.q_PLAYTS=q_now;
		this.q_PHASE=3;
		if(this.q_CONNTS){
		}else{
			this.q_CONNTS=q_now;
		}//end if
		if(this.q_BUFFTS){
		}else{
			this.q_BUFFTS=q_now;
		}//end if
	}//end if
}//end if
}//end function

Stat.prototype.regResponse=function Stat_regResponse(q_now){
if(this.q_POLLAVG){
	this.q_POLLA=(q_now-this.q_POLLTS);
	this.q_POLLAVG=Math.floor(0.8*this.q_POLLAVG+0.2*Math.floor((q_now-this.q_POLLTS)));
}else{
	this.q_POLLA=(q_now-this.q_POLLTS);
	this.q_POLLAVG=Math.floor((q_now-this.q_POLLTS));
}//end if
}//end function

function getPlatform(){
switch(getPlatformN()){
case 1:return "WIN";
case 2:return "MAC";
case 3:return "LINUX";
case 4:return "IPAD";
case 4:return "IPHONE";
default :return "?";
}//end switch
}//end function

function getPlatformN(){
var ua=window.navigator.userAgent;
if (WIN()){
	return 1;
}else if (MAC()){
	return 2;
}else if ((ua.indexOf("Unix")>-1)||(ua.indexOf("Linux")>-1)){
	return 3;
}else if (ua.indexOf("iPad")>-1){
	return 4;
}else if (ua.indexOf("iPhone")>-1){
	return 5;
}else{
   return 8;
}//endif
}//end function

function getBrowser(){
switch(getBrowserN()){
case 1:return "IE";
case 2:return "FX";
case 3:return "SF";
case 4:return "MZ";
case 5:return "NS";
case 6:return "OP";
case 7:return "AOL";
default :return "?";
}//end switch
}//end function

function getBrowserN(){
var ua=window.navigator.userAgent;
if(ua.indexOf("AOL")>-1){ 
   return 7;
}else if(ua.indexOf("Opera/")>-1){ 
   return 6;
}else if(FX()){ 
   return 2;
}else if(IE()){ 
   return 1;
}else if(SF()){ 
   return 3;
}else if(!NS() && (ua.indexOf("Gecko/")>-1)){ 
   return 4;
}else if(NS()){ 
   return 5;
}else{
   return 8;
}//endif
}//end function

Stat.prototype.getEnvironment=function(q_asNumber){
var q_f=this.getField("WB_CODENAME");
if(q_asNumber){
	var q_sformatN=(q_f=="WM")?1:(q_f=="RM")?2:(q_f=="QT")?3:(q_f=="FL")?4:8;
	return getPlatformN() + "" + getBrowserN() + "" + q_sformatN;
}else{
	this.q_WB_PLATFORM=getPlatform();
	this.q_WB_NAME = getBrowser();
	return this.q_WB_PLATFORM+"/"+this.q_WB_NAME+"/" +q_f;
}//end if
}//end function

//moved cookie handling to general.js

Stat.prototype.getPhase=function Stat_getPhase(q_now){
if(this.q_LIVEID){
}else{
	if(this.q_POLLTS){
		if(q_now>this.q_POLLTS+30000){
			return "requestliveid";//Phase 0=no live id yet
		}else{
			return "wait";
		}//end if
	}else{
		this.q_POLLTS=q_now;
		return "requestliveid";//Phase 0=no live id yet
	}//end if
}//end if
if(this.q_PHASE==3){
	this.q_DIAGTS=q_now-this.q_DIAGREFRESH+25000;//Schedule a database update in 25 seconds
	this.q_PHASE=4;
	this.q_REPORTEDSTATE=this.q_STATM;
	return "notifyplaystart";//play just started
}else if(this.isTimeToUpdateDatabase(q_now)){
	this.q_DIAGTS=q_now;
	this.q_REPORTEDSTATE=this.q_STATM;
	return "notifydatabase";//update database 
}else if(this.isTimeToNotify(q_now)){
	this.q_REPORTEDSTATE=this.q_STATM;
	return "notifyalive";//continuos update Liveclient
}else{
	return "wait";//unknown - do nothing
}//end if
}//end function

Stat.prototype.isTimeToNotify=function Stat_isTimeToNotify(q_now){
if(this.q_MAXREFRESH==0){
	return true;//Do maximal refresh - for keying speakernames
}else if(q_now>this.q_POLLTS+this.q_REFRESH && this.q_STATM==3){
	return true;
}else if(this.q_STATM!=3 && q_now>this.q_POLLTS+60000){
	return true;
}else if(this.q_REPORTSTATECHANGES==true && this.q_REPORTEDSTATE!=this.q_STATM && q_now>this.q_POLLTS+this.q_MINREFRESH && this.q_PHASE==4){
	return true;
}else{
	return false;
}//end if
}//end function


Stat.prototype.isTimeToUpdateDatabase=function Stat_isTimeToUpdateDatabase(q_now){
return (q_now>this.q_DIAGTS+this.q_DIAGREFRESH);
}//end function

function q_maincom(){
if(gServcom){
}//end if
}//end function

Stat.prototype.getTask=function Stat_getTask(){
	return this.getUrl(this.getPhase(now()));
}//end function

Stat.prototype.hasLiveid=function Stat_getUrl(q_action){
if(this.q_LIVEID){
	return this.q_LIVEID>0;
}else{
	return false;
}//end if
}//end function

Stat.prototype.getUrl=function Stat_getUrl(q_action){
if(div("stat")){div("stat").innerHTML=q_action;}//end if
var q_action2=q_action;
if(q_action=="notifyplaystart"||q_action=="notifydatabase"||q_action=="notifyalive"){
	q_action2=iif(this.hasLiveid(),q_action,"requestliveid");//demand to get liveid
}//end if
switch(q_action2){
case "wait": return "";
case "requestliveid": return this.getField("HOMEBASE")+"a_maincomr1.asp?"+this.getQs(q_action);
case "notifyplaystart": return this.getField("HOMEBASE")+"a_maincomr2.asp?"+this.getQs(q_action);
case "notifydatabase": return this.getField("HOMEBASE")+"a_maincomr2.asp?"+this.getQs(q_action);
case "notifyalive": return this.getField("HOMEBASE")+"a_maincomr4.asp?"+this.getQs(q_action);
case "notifyclosing": return this.getField("HOMEBASE")+"a_maincomr2.asp?"+this.getQs(q_action);
case "interaction": return this.getField("HOMEBASE")+"mainsubmit2.asp?"+this.getQs(q_action);
case "getchapterlist": return this.getField("HOMEBASE")+"a_getchapterlist.asp?"+this.getQs(q_action);
case "getimagescrolllist": return this.getField("HOMEBASE")+"getimagescrolllist.asp?"+this.getQs(q_action);
default: return "";
}//end switch
}//end function

Stat.prototype.getQs=function Stat_getQs(q_s){
//NOWTS used to calculate absolute timestamp
switch(q_s){
case "requestliveid": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","STREAMID","CHANNEL","VIEWER_ID","AUTH_NAME","AUTH_UID","AUTH_EMAIL","AUTH_PHONE","AUTH_COMPANY","AUTH_CCNUMBER","WB_COOKIESET","WB_PLATFORM","WB_NAME","WB_CODENAME","WB_SCREENWIDTH","WB_SCREENHEIGHT","WB_USERAGENT","WB_BANDWIDTH","LOADTS","AUTH_IP","WB_DATETIME","STATM","NOWTS","ENVN");
case "notifyplaystart": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","WB_VERSIONNUMBER","STATM","CONNTS","BUFFTS","PLAYTS","UPDATETS","PLAYA","NOWTS","STREAMID","ENVN");
case "notifydatabase": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","STATM","UPDATETS","PLAYA","BANDS","FRAMR","POLLAVG","POLLPROB","BUFFC","NOWTS","STREAMID","ENVN");
case "notifyalive": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","STATM","PLAYA","BANDS","FRAMR","POLLAVG","STREAMID","ENVN");
case "notifyclosing": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","STATM","UPDATETS","DISCTS","PLAYA","BANDS","FRAMR","POLLAVG","POLLPROB","BUFFC","NOWTS","STREAMID","COMMENT","CONTENT","TECH","ENVN");
case "interaction": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","STATM","PLAYA","BANDS","FRAMR","POLLAVG","STREAMID","REPLYTYPE","REPLYTEXT","REPLYNAME","REPLYEMAIL","ENVN");
case "getchapterlist": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","CHANNEL","STREAMID");
case "getimagescrolllist": return this.setQuerystring( "LIVEID","PRODUCER_ID","FOLDERNAME","SELECTEDIMAGESCROLLDATA","CMDIMAGESCROLLDATA");
case "pageurl": return this.setQuerystring("PRODUCER_ID","FOLDERNAME","STREAMID","MEDIASRC");
default: return;
}//end switch
}//end function

Stat.prototype.getInteractionUrl=function Stat_getInteractionUrl(q_REPLYTYPE, q_REPLYNAME,q_REPLYEMAIL,q_REPLYTEXT){
this.q_REPLYTYPE=q_REPLYTYPE;
this.q_REPLYNAME=q_REPLYNAME;//answer_id
this.q_REPLYEMAIL=q_REPLYEMAIL;//question_id
this.q_REPLYTEXT=q_REPLYTEXT;
return this.getUrl("interaction");
}//end function


//------- parameter translation for querystrings
Stat.prototype.argtest=function Stat_argtest(){
return this.setQuerystring("PRODUCER_ID","FOLDERNAME","LIVEID","STREAMID");
//alert(k);
}//end function

Stat.prototype.setQuerystring=function Stat_setQuerystring(){
//return array2querystring(this.getValuearray(this.setQuerystring.arguments));
q_sep="";
q_str="";
for (q_i=0;q_i<this.setQuerystring.arguments.length;q_i++){
try{
	q_str=q_str+q_sep+this.setQuerystring.arguments[q_i]+"="+encodeURIComponent(this.getField(this.setQuerystring.arguments[q_i]));
	q_sep="&";
}catch(errObj){
}//end catch
}//next
return q_str;
}//end function

function array2querystring(q_arr){
q_sep="";
q_str="";
for (q_i=0;q_i<q_arr.length;q_i++){
	q_str=q_str+q_sep+q_arr[q_i][0]+"="+encodeURIComponent(q_arr[q_i][1]);
	q_sep="&";
}//next
return q_str;
}//end function

Stat.prototype.getAllValues=function Stat_getAllValues(){
return (gStat.getValuearray("LIVEID","PRODUCER_ID","FOLDERNAME","STREAMID","CHANNEL","VIEWER_ID","AUTH_NAME","AUTH_UID","AUTH_EMAIL","AUTH_PHONE","AUTH_COMPANY","AUTH_CCNUMBER","AUTH_EXTRA","AUTH_IP","WB_COOKIESET","WB_PLATFORM","WB_NAME","WB_CODENAME","WB_VERSIONNUMBER","WB_SCREENWIDTH","WB_SCREENHEIGHT","WB_USERAGENT","WB_BANDWIDTH","WB_DATETIME","LOADTS","CONNTS","BUFFTS","PLAYTS","UPDATETS","DISCTS","STATM","PLAYA","PLAYC","BANDS","FRAMR","POLLAVG","POLLPROB","BUFFC","ENVN","NOWTS","REPLYTYPE","REPLYNAME","REPLYEMAIL","REPLYTEXT"));
}//end function

Stat.prototype.getValuearray=function Stat_getValuearray(){
var q_arr=new Array();
for (q_i=0;q_i<this.getValuearray.arguments.length;q_i++){
	q_arr[q_arr.length]=new Array(this.getValuearray.arguments[q_i],this.getField(this.getValuearray.arguments[q_i]));
}//next
return q_arr;
}//end function

//NEXT TODO IS DESIGNING R1..R4
Stat.prototype.getField=function getField(q_par){
switch (q_par){
case "LIVEID": return this.q_LIVEID;
case "PRODUCER_ID": return this.q_WOW.q_PRODUCERID;
case "FOLDERNAME": return this.q_WOW.q_FOLDERNAME;
case "STREAMID": return this.q_WOW.q_STREAMID;
case "CHANNEL": return this.q_WOW.q_CHANNEL;
case "VIEWER_ID": return this.q_WOW.q_VIEWERNAME;//New usage for this db field
case "AUTH_NAME": return this.q_WOW.q_AUTH_NAME;
case "AUTH_UID": return this.q_WOW.q_AUTH_UID;
case "AUTH_EMAIL": return this.q_WOW.q_AUTH_EMAIL;
case "AUTH_PHONE": return this.q_WOW.q_AUTH_PHONE;
case "AUTH_COMPANY": return this.q_WOW.q_AUTH_COMPANY;
case "AUTH_CCNUMBER": return this.q_WOW.q_AUTH_CCNUMBER;
case "AUTH_EXTRA": return this.q_WOW.q_AUTH_EXTRA;
case "AUTH_IP": return "";//Server variable
case "WB_COOKIESET": return this.q_WB_COOKIESET;
case "WB_PLATFORM": return this.q_WB_PLATFORM;
case "WB_NAME": return this.q_WB_NAME;
case "WB_CODENAME": return this.q_WOW.q_SFORMAT;//New usage for this db field
case "WB_VERSIONNUMBER": return this.q_WB_VERSIONNUMBER;//New usage for this db field
case "WB_SCREENWIDTH": return this.q_WB_SCREENWIDTH;
case "WB_SCREENHEIGHT": return this.q_WB_SCREENHEIGHT;
case "WB_USERAGENT": return "";//Server variable window.navigator.userAgent;
case "WB_BANDWIDTH": return this.q_WOW.q_WB_BANDWIDTH;
case "WB_DATETIME": return "";//Server variable
case "LOADTS": return this.relativeTS(this.q_LOADTS);
case "CONNTS": return this.relativeTS(this.q_CONNTS);
case "BUFFTS": return this.relativeTS(this.q_BUFFTS);
case "PLAYTS": return this.relativeTS(this.q_PLAYTS);
case "UPDATETS": return this.relativeTS(now());//this.q_POLLTS;//New usage for this db field
case "DISCTS": return this.relativeTS(now());//this.q_DISCTS;
case "STATM": return this.q_STATM;
case "PLAYA": return this.getPlay();
case "PLAYC": return this.q_PLAYC;
case "BANDS": return Math.floor(0.5+this.q_BANDS);
case "FRAMR": return Math.floor(0.5+this.q_FRAMR);
case "POLLAVG": return Math.floor(0.5+100*this.q_POLLAVG)/100;
case "POLLPROB": return this.q_POLLPROB;
case "BUFFC": return this.q_BUFFC;
case "ENVN": return this.q_ENVN;
case "ARRIVE_TS": return this.relativeTS(this.q_LOADTS);
case "NOWTS": return now();
case "REPLYTYPE": return this.q_REPLYTYPE;
case "REPLYNAME": return this.q_REPLYNAME;
case "REPLYEMAIL": return this.q_REPLYEMAIL;
case "REPLYTEXT": return this.q_REPLYTEXT;
case "SELECTEDIMAGESCROLLDATA": return this.SELECTEDIMAGESCROLLDATA;
case "CMDIMAGESCROLLDATA": return this.CMDIMAGESCROLLDATA;
case "URL": return this.q_WOW.q_URL;
case "MEDIASRC": return this.q_WOW.q_URL;
case "HOMEBASE": return this.q_WOW.q_HOMEBASE;
default: return this.getWOWField(q_par);
}//end switch
return "";
}//end function

Stat.prototype.relativeTS=function Stat_relativeTS(a){
if(a){
	//return a;
	if(now()>=a){
		return now()-a;
	}else{
		return 0;
	}//end if
}else{
	return 0;
}//end if
}//end function

Stat.prototype.getWOWField=function getWOWField(q_par){
try{
	return eval("q_WOW.q_"+q_par);
}catch(errObj){
	return "[unknown field "+q_par+"]";
}//end catch
}//end function


