/*
Filename	:	RKKsense.js
Auteur		:	Jan Willem Eshuis
Last update	:	14 januari 2009
Copyright	:	NOS Nieuwe Media
*/

function addListeners() {
	if(ply.view) {
		ply.addListener('STATE', setStatus);
	} else {
		setTimeout(addListeners,100);
	}
	
	jQuery(window).bind("beforeunload",function () {
		setStatus(oldstate,'Closed');
	});
};

var oldstate = ''
function setStatus(ost,nst) { 
	if ( (nst=='Playing') && (nst!=oldstate) ) {
		oldstate = nst;
		streamCounter.streamStart();
	}
	if (nst=='Closed') {
		oldstate = nst;
		streamCounter.streamStop();
	}
};


var events = {
	"play": ["id", "pb", "pl", "cp", "cn", "pr", "ep", "pn", "tp", "el", "dt", "ty", "br", "cs", "st", "cl", "ca", "ub", "bt", "mp", "mv", "fee", "id", "po", "sq", "sp", "sv", "li", "er"]
	, "end": ["id", "pe", "pl", "pr", "cp", "cn", "ep", "pn", "tp", "el", "dt", "ty", "br", "cs", "st", "cl", "ca", "ub", "bt", "bp", "mp", "mv", "fee", "pt", "pa", "id", "po", "pc", "pp", "sv", "sp", "li", "er"]
};

function RKKSense( params , sko_params, isdebug, pageurl) {
	this.debug = isdebug;
	this.pageurl = pageurl;
	this.url = 'http://nl.sitestat.com/klo/kzg/s?';
	this.sko = {
		sko_dt		:  	'',
		sko_pr		:  	'', // zender
		sko_stid	:  	'',
		sko_ty		:  	'',		// tv.cam = simulcast  / tc.cam = webonly
		sko_prid	:	'',
		sko_t		:	''
	}
	this.config = {
		ns_st__t : '',
		ns__type : 'hidden',
		ns_st_ev : '',	// type request
		ns_st_id : '',	// stream-url
		ns_st_pe : '1',	// is end 
		ns_st_pb : '1',	// is begin
		ns_st_pl : '',	// playlist name
		ns_st_cp : '1',
		ns_st_cn : '1',
		ns_st_pr : '',	// programma
		ns_st_ep : '',	// episode
		ns_st_pn : '1',
		ns_st_tp : '1',
		ns_st_el : '0',	// groot getal OS
		ns_st_br : '0',
		ns_st_cs : '',	// video size
		ns_st_st : 'nederland_1',	// zender?
		ns_st_cl : '0',	// groot getal OS
		ns_st_ca : '0',	// groot getal OS
		ns_st_ub : '0',
		ns_st_bt : '0',
		ns_st_bp : '0',
		ns_st_mp : 'Silverlight',
		ns_st_mv : '1.0',
		ns_st_fee: '',
		ns_st_pt : '', // total time
		ns_st_pa : '', // total time
		ns_st_po : '0',	// vanaf positie
		ns_st_pp : '0', // total pauses
		ns_st_sq : '1',
		ns_st_sp : '1',
		ns_st_sv : '1.0.0',	// script version
		ns_st_li : '0',	// can pause
		ns_st_er : '',
		ns_st_ty : '',
		ns_st_st : ''
	};
	for (itm in this.config) {
		if (params[itm] != undefined) {
			this.config[itm] = params[itm];
		}
	}
	
	for (itm in this.sko) {
		if (sko_params[itm] != undefined) {
			this.sko[itm] = sko_params[itm];
		}
	}
	if (this.debug) console.log (this.config);
	if (this.debug) console.log (this.sko);
	this.status = 0;	// Niet actief
}

RKKSense.prototype.sendStats = function (logStr) {
	var img = new Image();
   	img.src = logStr;
}

// Function start stream
RKKSense.prototype.streamStart = function() {
 	this.status 	= 1;
	this.startAt	= new Date().getTime();
	
	var url = this.url;
	url = url + this.config['ns_st_pl'];
	url = url + '&ns__t=' + new Date().getTime();
	url = url + '&ns_type=hidden';
	
	this.config['ns_st_ev'] = 'play';
	url = url + '&ns_st_ev=' + this.config['ns_st_ev'];
	
	
	var labels = events['play'];
	for (var i=0; i<labels.length;i++) {
		url = url + '&' + 'ns_st_' + labels[i] + '=' + this.config['ns_st_' + labels[i]];
	}
	for (itm in this.sko) {
		if (this.sko[itm] != undefined) {
			url = url + '&' + itm + '=' + this.sko[itm];
		}
	}
	url = url + '&jspageurl=' + this.pageurl;
	if (this.debug) console.log (url);
	this.sendStats( url);
}


// Function end stream
RKKSense.prototype.streamStop = function() {
	if (this.status==1) {
	 	this.status = 0;
		this.endAt		= new Date().getTime();
		this.duration	= this.endAt - this.startAt;
		
		var url = this.url;
		url = url + this.config['ns_st_pl'];
		url = url + '&ns__t=' + new Date().getTime();
		url = url + '&ns_type=hidden';
		
		this.config['ns_st_ev'] = 'end';
		this.config['ns_st_pt'] = this.duration;
		this.config['ns_st_pa'] = this.duration;
		
		url = url + '&ns_st_ev=' + this.config['ns_st_ev'];
		
		var labels = events['end'];
		for (var i=0; i<labels.length;i++) {
			url = url + '&' + 'ns_st_' + labels[i] + '=' + this.config['ns_st_' + labels[i]];
		}
		url = url + '&jspageurl=' + this.pageurl;
	
		this.sendStats( url );
		if (this.debug) console.log (url);
	}
}
	
