/****************************************************************************************/
/*																						*/
/*	Accordent TRKlib v1.00																*/
/*	=======================																*/
/*																						*/
/*	Last Edited: 09/22/2004 															*/
/*																						*/
/*  (js cookies) viewingStartTime | viewingEndTime | clipEndTime ****/
/*  (dynamic cookies) sessionComplete | lastDateTrked | vwEndTime | clipEndTime ****/
/*  (dynamic sessions) viewerID | presentationID | trackingID | sessionComplete ****/
/*																						*/
/****************************************************************************************/
/*	Request.QueryString("init")	*/
/*	Request.Cookies("clipEndTime")	*/
/*	Request.Cookies("clipStartTime")	*/
/*	Request.Cookies("viewingEndTime")	*/
/*	Request.Cookies("viewingStartTime")	*/
/****************************************************************************************/
var hasValidDuration = false;
var chkLive = 'visible';
var isLive = false;
if (chkLive == 'hidden'){
	isLive = true;
}
var pid = getvar_parent('i');
var uid = getvar_parent('u');


var redirFrmLoc = 'index.htm';
var cookieUpdRate = 500;
var dataUpdRate = 600;
var statsFULL = 1;
cookieUpdRate = Math.floor(cookieUpdRate * 1000);
dataUpdRate = Math.floor(dataUpdRate * 1000);

self.document.cookie = "pres_error=0";

function getcookie(cookiename) {
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return "";
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length;
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function conv_jsTime2Sql(time_ms){
	tempJs = parseInt(time_ms); 
	t = new Date(tempJs);
	tDay = t.getDay(); // actual day
	tMonth = new String(t.getMonth() + 1); // js 0-11 for months
	tDate = new String(t.getDate());
	tYear = new String(t.getFullYear());
	tHours = new String(t.getHours());
	tMinutes = new String(t.getMinutes());
	tSeconds = new String(t.getSeconds());
	if (tMinutes < 10) tMinutes = new String('0' + tMinutes);
	if (tSeconds < 10) tSeconds = new String('0' + tSeconds);
	tMTemp = tMonth + '/' + tDate + '/' + tYear + ' ' + tHours + ':' + tMinutes + ':' + tSeconds;
	var sqlTime = new String(tMTemp);
	return(sqlTime);
}
/****************************************************************************************/

function writeCookie_CurPos(f) {
	var initClipPos = 0;
	self.document.cookie = "clipStartTime=" + initClipPos;
	var targetClip = self.document.MediaPlayer.controls;
	var getWindowsPos = targetClip.currentPosition;
	getWindowsPos = Math.floor(getWindowsPos);
	self.document.cookie = "clipEndTime=" + getWindowsPos;
	var cookieDate = new Date();
	var cookieDate_ms = cookieDate.getTime();
	self.document.cookie = "viewingEndTime=" + cookieDate_ms;
	var viewerProg = getcookie("clipEndTime");
	var viewerTime = getcookie("viewingEndTime");
	var startTime = getcookie("viewingStartTime");
	viewerT = conv_jsTime2Sql(viewerTime);
	startT = conv_jsTime2Sql(startTime);
}

function writeCookie_Close() {
	var initClipPos = 0;
	self.document.cookie = "clipStartTime=" + initClipPos;
	var targetClip = self.document.MediaPlayer.controls;
	var getWindowsPos = targetClip.currentPosition;
	getWindowsPos = Math.floor(getWindowsPos);
	self.document.cookie = "clipEndTime=" + getWindowsPos;
	var cookieDate = new Date();
	var cookieDate_ms = cookieDate.getTime();
	self.document.cookie = "viewingEndTime=" + cookieDate_ms;
	var viewerProg = getcookie("clipEndTime");
	var viewerTime = getcookie("viewingEndTime");
	var startTime = getcookie("viewingStartTime");
	viewerT = conv_jsTime2Sql(viewerTime);
	startT = conv_jsTime2Sql(startTime);
	self.document.cookie = "sqlDateTime=" + viewerT;
	closeButtonHit();
}

function closeButtonHit() {
	window.top.rightFrame.location = "rtFrm.asp?init=2";
}

function load_window(){
	document.cookie = "lock_session=yes";
}

function exit_window(){
	document.cookie = "lock_session=no";
	window.setTimeout("window.top.close()",1000);
	writeCookie_Close();
}

function init_Load(){
	tempInit = new Date();
	initTimeStamp = tempInit.getTime();
	self.document.cookie = "viewingStartTime=" + initTimeStamp;
	chkFrames();
}

function actualTimeViewed() {
	initTm = getcookie("viewingStartTime");
	curTm = getcookie("viewingEndTime");
	timeVw = Math.ceil( (curTm - initTm)/1000 );
	return(timeVw);
}

function clearIntval() {
	clock_timer = window.clearInterval(clock_timer);
}

function chkFrames(){
	if (top.rightFrame) {
		clock_timer = window.setInterval("writeCookie_CurPos()",cookieUpdRate);
	} else {
		window.location = redirFrmLoc;
	}
}


//****************************************************************************** Duration Code
function ck_Duration() {
	var ckDuration = 0;
    var state;
	var vid = self.document.MediaPlayer;
	state = vid.PlayState;
	if (state == 3) ckDuration = 1;
	return(ckDuration);
}

function getDuration() {
	var currentDuration;
	var state;
	var vid = self.document.MediaPlayer;
	state = vid.PlayState;
	if (state == 3) currentDuration = vid.currentMedia.duration;
	return(currentDuration);
}

function getError() {
	var curError;
	var vid = self.document.MediaPlayer;
	curError = vid.error.errorCount;
	return(curError);
}

function updateChp(){
	if (window.top.chapters && window.top.chapters.sync){
		var targetClip = self.document.MediaPlayer.controls;
		var curTime = targetClip.currentPosition;
		window.top.chapters.genSync(curTime);
	}
}

var init = 0;

function errorFxn(errCnt){
	window.clearInterval(idTmr);
}


var diplayDuration = '';

function getDurationCk() {
	if(!window.top.video) {
		window.clearInterval(idTmr);
		document.write('Media files does not exist...');
		self.document.cookie = "pres_error=1";
	} else if (window.top.video.MediaPlayer) {
		var chkError = window.top.video.getError();
		if (chkError == 0) {
			var durationChk = 0; 
			durationChk = window.top.video.ck_Duration();
			if (durationChk > 0) {
				window.clearInterval(idTmr);
				setDurationCookie();
				hasValidDuration = true;
				diplayDuration = durationChk;
				//alert(diplayDuration);
			}
		} else {
			errorFxn(chkError);
		}
	}

	if(hasValidDuration){
		if(pid != "" && uid != ""){
			var myurl = "clear.asp?pid="+pid+"&uid="+uid;
			// alert(myurl);
			window.top.rightFrame.location = myurl;
		}
	}
}
	
function getMediaDurationScan(){
	if(isLive){
		self.document.cookie = "clipDuration=999999999";
		window.top.rightFrame.location = "rtfrm.asp";
	} else {
		idTmr = window.setInterval("getDurationCk()",50);
	}
}

function setDurationCookie(){
	window.clearInterval(idTmr);
	var clipDuration = getDuration();
	var clipDuration = Math.floor(clipDuration);
	self.document.cookie = "clipDuration=" + clipDuration;
	return(clipDuration);
}

var nowTime = new Date();
nowTime = new String(nowTime.getTime());
self.document.cookie = "nowTime=" + nowTime;

function getvar_parent(el, case_sensitive) { //el=str, case_sensitive=bool
	var rs="";
	if(!case_sensitive) rs="i";
	var urls = new String(window.parent.location);
	var re = new RegExp( "\\?[\\w\\W]*"+ el +"=([^\\&\\?#]*)", rs );
	var arr = re.exec(urls);
	if(arr && arr.length>1) return arr[1];
	else return '';
}
//******************************************************************************

init_Load();
// getMediaDurationScan();
self.document.cookie = "clipDuration=" + '0';	

