	
function videoTracker() {
	doSlides = false;
	for(var i=0;i<atabs.length;i++){
		if(atabs[i].restype==3) doSlides = true;
	}
	
	startTime = new Date().valueOf()

	//timeclock = setInterval("clock(" + startTime + ")",5000);
	timeclock = setInterval("timer(2," + topicid + ',' + band + ',' + stream + ',' + orderitem + ")",5000);

	try {
		document.player.Controls.Play();
		//document.svlPlayer.Controls.Play();
	}catch(e){
		return true;
	}
	
}

var lastupdate = 0;
var lasthello = 0;
var audienceid = 0;
var lasttype = "";
var displayedfinalmsg = false;
var globalCounter = 0;


function timer(mode, topic, band, stream, orderitem) {
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var tindex = (hours*3600) + (minutes*60) + seconds;
	//alert(tindex);
	
	if(itsalive == "1") {
		//alert(tindex);
		if(lastupdate == 0) {
			lastupdate = tindex;

			Ext.Ajax.request({
				url: 'update-status-v7.php',
				method: 'POST',
				params: {
					mode: 'live',
					action: 'start',
					topicid: topicid,
					custid: custid, 
					semid: semid,
					iscurrentlybroadcasting: iscurrentlybroadcasting,
					stime: lastupdate,
					updatetime: updatetime
				},
				success: makeParams	
			});
				
		}
		else if(parseInt(tindex) > (parseInt(lastupdate)+parseInt(refreshrate))) {
			lastupdate = tindex;

			Ext.Ajax.request({
				url: 'update-status-v7.php',
				method: 'POST',
				params: {
					mode: 'live',
					action: 'log',
					topicid: topicid,
					custid: custid, 
					audienceid: audienceid,
					semid: semid,
					iscurrentlybroadcasting: iscurrentlybroadcasting,
					stime: lastupdate,
					updatetime: updatetime
				},
				success: makeParams	
			});

		}
		if(iscurrentlybroadcasting == "0") {
			
		}
		
	}
	else {
		//alert(document.player.currentPosition);
	//temporarily disabled this JS timer solution in favor of WM scriptcommand	
		if(parseInt(advtimer)==2) {
			//alert(advtimer+" ,"+lasthello+", "+tindex+", "+hellorate);
			if(lasthello == 0) {
				lasthello = tindex;
			}
			//alert(parseInt(lasthello)+parseInt(hellorate));
			else if(parseInt(tindex) > (parseInt(lasthello)+parseInt(hellorate))) {
				//alert("else");
				lasthello = tindex;
				lasttype = "attend";
				//lasttype = (lasttype=="attend")? "poll":"attend";
				fPop.seturlPop("popquestion-v7.php?params=mode:"+lasttype+";topicid:"+topicid+";custid:"+custid);
				//alert("popquestion-v7.php?params=mode:attendance;topicid:"+topicid+";custid:"+custid);
				//fPop.popit();
				
			}
			
		}
		checkFinished(mode, topic, band, stream, orderitem);
	}
	return true;
}


function checkFinished(mode, topic, band, stream, orderitem)  {

	if (mode == 1)
		processRealPlayer(topic, band, stream, orderitem);
	else if (mode == 2)
		processWMediaPlayer(topic, band, stream, orderitem);
	else if (mode == 3)
		processQuickTime();
	
}

function getVideoDuration () {
	try {
		svlplayer = document.getElementById("svlPlayer");
		mediaElement = svlplayer.content.findName("mediaPlayer");
		return parseInt(mediaElement.NaturalDuration.Seconds)
	}
	catch(e) {
		return 0;
	}
}

function getCurrentPosition () {
	try {
		svlplayer = document.getElementById("svlPlayer");
		mediaElement = svlplayer.content.findName("mediaPlayer");
		return parseInt(mediaElement.Position.Seconds);
	}
	catch(e) {
		return globalCounter;
	}
}

function updateVideoCounter(s) {
	globalCounter = parseInt(s);
}

var creditdonelogged = false;
var videodonelogged = false;

var lastdialog = 1;
var wintime;
function processWMediaPlayer(topic, band, stream, orderitem)  {
	var updateurl = "";
	var creditratio = 0.5;
	if(provider==73)
		creditratio = 0.9;	
	//alert(curSlideNum);

	dur = getVideoDuration();
	cpos = getCurrentPosition();
	
	lastf_pos = 0;

	if (archivecheckpoints) {
		testCheckpointTime(cpos);
	}

	if(typeof(window.dialogtimes) != "undefined") {
		if(dialogtimes.count>0) {
	  	f_cpos = cpos
	  	if(popisup==false) {	
			var result = "";
			dt = dialogtimes.times;
			for (var i in dt) {
				if(parseInt(dt[i].time) <= f_cpos && f_cpos < parseInt(dt[i].endtime)) {
					//var dtdiff = parseInt(dt[i].endtime)-parseInt(dt[i].time);
					var dtdiff = parseInt(dt[i].endtime)-f_cpos;
					//var cldiff = parseInt(dt[i].closetime)-f_cpos;
					//alert(cldiff);
					//wintime = setTimeout(sp.vidPause,dtdiff*1000);	
					wintime = setTimeout("sv_pauseVid("+i+")",dtdiff*1000);
					//if(cldiff>0)
					//	clstime = setTimeout("sv_closePop("+i+")",cldiff*1000);
					win.launch(dt[i].questionid, i);
					currentpop = i;
	
					break;
				}
			}
		  }
		  if(popisup==true) {
			if(f_cpos > dialogtimes.times[currentpop].closetime)
				win.hide();
			}
		}
	}
	
	//see if the slides page is open in the materials frame
	try {
		slidewindow = document.getElementById("ppwindow");
	}
	catch(e) {
		//if(!slidewindow) return;
	}
	
	var slidesActive = 1;
	if(!doSlides) slidesActive = 0;
	
	var videodone = (cpos + 3 >= dur && dur > 10);
	//alert(videodone+", "+cpos+", "+dur);
	
	var creditdone = (cpos + 3 >= (dur*creditratio) && dur > 10);

	if (creditdonelogged && cpos == 0) {
	    videodone = true;
	}
	
	if (quizdebug == "yes") //alert('looped' + cpos);

	if(creditdone && static=="0" && parseInt(advtimer)==0) {
		if(!creditdonelogged) {
			updateurl = "update-streaming-status-3.php?topic=" + topic + "&id=" + orderitem + "&mode=credit";
			document.getElementById("datapipe").src = updateurl;
			creditdonelogged = true;
		}
	}

	//the video is over
	if(videodone && static=="0" && parseInt(advtimer)==0)   {
		if (!videodonelogged) {
			if (quizdebug == "yes") alert ("should go to the quiz now");
			updateurl = "update-streaming-status-3.php?topic=" + topic + "&id=" + orderitem + "&mode=done";
			document.getElementById("datapipe").src = updateurl;
			videodonelogged = true;
		}
	}
	
	else {
		
		if(slidesActive==1) {
		  try {
			if(syncmethod != 2 && syncmethod != 5) {
				var fullpath = slidewindow.location.toString();
				fullpath = fullpath.substr(0,fullpath.lastIndexOf("/")+1);		
				slidewindow.MHTMLPrefix = fullpath;
			}
			//it's the first slide if the current position is less than the first slide time
			var firstslide = (cpos>=0 && cpos<=parseInt(slideTimes[0]));
			
			var lastslideindex = numSlides - 1;
			var lastslide = (cpos>=slideTimes[lastslideindex] && cpos<dur);
			
			if (firstslide){//if it's on the first slide:
				slideNum = parseInt(slideToGoTo[0]);
				if (syncmethod != 2) {
					if(curSlideNum != slideNum){
						if(debug=="true")
							alert ("First - cur: " + curSlideNum + "!= slideNum: " + slideNum);
	
						setcurrentSlide(slideNum);
						curSlideNum = slideNum;
					}
				}
			}
			else if (lastslide){//if it's on the last slide:
				slideNum = parseInt(slideToGoTo[numSlides-1]);
				if(curSlideNum != slideNum){
					setcurrentSlide(slideNum);
					curSlideNum = slideNum;
				}
			}
			
			else {//if it's on any other slide:
				//alert ("other slide");
				var i = 0;
				while( i < (numSlides-1) ){
					var goslide = i;
					if ( parseInt(slideTimes[i])<=cpos && cpos<parseInt(slideTimes[i+1]) ){
						//if(syncmethod==2) goslide +=1;
						slideNum = parseInt(slideToGoTo[goslide]);
					}
					if(curSlideNum != slideNum){
						if(debug=="true") alert ("cur: " + curSlideNum + " slideNum: " + slideNum);
						curSlideNum = slideNum;
						setcurrentSlide(slideNum);
					}
					
					i++;	
				}
			}
		  }
		  catch(e) {
				//skip it;	
		  }
		}
	}
	//statusMonitor(cpos,dur);
}

function sv_pauseVid(qnum) {
	dt = dialogtimes.times;
	if(dt[qnum]["answered"] == "false")
		pauseVideo();
}
function sv_closePop(qnum) {
	alert('close');
	if(currentpop==qnum && popisup)
		win.hide();
}

function statusMonitor(cpos,dur) {
	//alert(cpos/dur);
	if(cpos == dur) {
		
	}
	
}

//called from datapipe iframe when topic is complete
function topicDone() {
	if (hasquiz) {
		alert("Please complete the following quiz");
		Ext.getCmp('tabsmain').setActiveTab(quiztab);	
	} else {
		var msg = "";
		var topicslength = atopics.length;
		var curtopic = 0;
		for(var t=0;t<atopics.length;t++) {
			if(atopics[t].id == topicid) {
				curtopic = (t+1);
				break;
			}
		}
		if(topicslength > curtopic) {
			msg = "You have finished viewing this topic.\t\n";
			msg += "Would you like to go to the next topic?";	
			if(confirm(msg)) {
				goTopicNum(atopics[curtopic].id);
			}		
		}
		else {
			if (!displayedfinalmsg) {
				msg = "You have finished viewing this topic.\t\n";
				msg += "This is the final topic for your seminar.";	
				alert(msg);
				displayedfinalmsg = true;
			}
		}
	}
}

function exitFullScreen () {
	try {
		if (issilverlight) {
			if (svlplayer.Content.fullScreen) {
				svlplayer.Content.fullScreen = false;
			}
			
			return true;
		}
		
		if (ismobile) {
			try {
				flashplayer_api.webkitExitFullscreen();
			} catch (e) {
				//do nothing	
			}
			return true;
		}
		
		if (isflash) {
			
			return true;
		}
	} catch (e) {
		return false;	
	}

}

function pauseVideo() {
	try {
		if (issilverlight) {
			
			svlplayer = document.getElementById("svlPlayer");
			mediaElement = svlplayer.content.findName("mediaPlayer");
			
			mediaElement.Pause();
			
			
		} 
		if (isflash) {
			$f().pause();	
		}
	} catch (e) {
		return false;	
	}
}

function playVideo() {
	try {
		if (issilverlight) {	
			svlplayer = document.getElementById("svlPlayer");
			mediaElement = svlplayer.content.findName("mediaPlayer");
			
			mediaElement.Play();
		}
		if (isflash) {
			$f().play();	
		}
	} catch (e) {
		return false;
	}
}

function checkpointLogger(param) {
	var aparam = param.split("-");
	
	exitFullScreen();
	
	if (itsalive < 1) {
		pauseVideo();
	} else {
		try {
			Ext.getCmp('checkpointwin').close();
			clearTimeout(checkpointclear);
		} catch (e) {
			//do nothing	
		}
	}

	Ext.Ajax.request({
		url: 'popquestion-v7.php',
		success: function (a, b) {
			/* Commented out because it caused a 'Cant click the checkpoint bug' when viewing materials in the materials tab */
			   
			if (slidetype != 4) {
			
				Ext.getCmp('tabsmain').setActiveTab('creditframe');
			}
		
		else {
				Ext.getCmp('tabsmain').setActiveTab('videotab');
		}
			if (doCheckpointSound) {
				soundManager.play('checkpoint_notification');
			}

			checkwin = new Ext.Window({
				title: "",
				bodyStyle: 'padding:10px;background:#ffffff;font-size:10pt',
				html: a.responseText,
				closable: false,
				width:350,
				autoHeight:true,
				modal: true,
				id: 'checkpointwin',
				buttons: [{
					text: 'OK',
					handler: function (a, b, c) {
						a.setDisabled(true);
						a.setText("Sending...");
						
						Ext.Ajax.request({
							url: 'popquestion-v7.php',
							success: function (a, b) {
								if (itsalive < 1) {
									playVideo();
								}
								if (ismobile) {
									flashplayer_api.play();
								}
								Ext.getCmp('checkpointwin').close();	
								try {
									creditframe = document.getElementById('creditwindow');
									if (creditframe) {
										creditframe.src = creditframe.src;
									}
								} catch (e) {
									//do nothing	
								}
							},
							params: { 
								answer: 'confirm',
								topicid: topicid,
								custid: custid,
								checkpoint: aparam[0],
								allpoints: aparam[1], 
								vindex: aparam[2],
								orderitem: orderitem,
								livemode: itsalive
							}
						});
						
					}
				}]
			});
			
			checkwin.show();
			
			if (itsalive > 0) {
				checkpointclear = setTimeout("Ext.getCmp('checkpointwin').close()", 60000);	
			}
		},
		failure: function () {
		//	alert('Checkpoint Failed');   
		},
		params: { 
			mode: 'checkpoint',
			topicid: topicid,
			custid: custid,
			checkpoint: aparam[0],
			allpoints: aparam[1], 
			vindex: aparam[2],
			orderitem: orderitem
		}
	});


}

function getQueryArgs(vglobal) {
	var args = {};
	var loc = window.location.href;
	var q = loc.indexOf("?");
	if (q==-1) return false;
	loc = loc.substring(q+1);
	var pairs = loc.split("&");
	for (var i=0; i<pairs.length;i++){
		if (vglobal) eval(pairs[i]);
		var keyval = pairs[i].split("=");
		args[keyval[0]] = unescape(keyval[1]);
	}
	return args;
  
}

