var CurrentTime = 0;
var g_Browser = navigator.appName;
var oMP;
var g_exiting = 0;
var g_loadok = 0;
var UrlArray;
var UrlIdArray;
var UrlArrayIndex = 0;
var g_bFileProtocol = (window.location.protocol.toLowerCase() == "file:");
var g_mailFrm;
var g_firstTime = true;
var g_scormInitialized = false;
var g_playok = 0;
var WebUpdate_loadedOK = false;

var g_MPTimesDelta = -1; // the difference between stream time, and scriptCommand time, in live


function closeWin()
{
	window.top.close();
}

function setLoadedOK()
{
	WebUpdate_loadedOK=true;
}

function getLoadedOK()
{
	return WebUpdate_loadedOK;
}

function setTitle()
{
	if (window.__Instructor__.length > 0)
		
		document.write('<title>'+window.__Title__+' - '+window.__Instructor__+'</title>');
}


function IsPlayerEmbedded()
{
	return (navigator.platform.substr(0, 3).toLowerCase() == "win");
}

function getSearchAsArray()
{
	var results = new Array();
	var input = location.search.substring(1);
	if (input) 
	{
		var srchArray = input.split("&");
		var tempArray = new Array();
		for (i = 0; i < srchArray.length; i++) 
		{
			tempArray = srchArray[i].split("=");
			results[tempArray[0]] = unescape(tempArray[1]);
		}
	}
	return results;
}

function TegRemoveIlegal(strOrig)
{
	var i,strNoIlegal="";
	var strLegal=":0123456789";
	for(i=0; i<strOrig.length; i++) 
	{
		if (strLegal.indexOf(strOrig.substr(i,1))>=0) 
			strNoIlegal += strOrig.substr(i,1);
	}
	return strNoIlegal;
}


function GetTegTime(strTime)
{
	var  nSeconds=0;
	var timeArray;
	if( ( typeof(strTime)!="string" ) || (strTime.length==0) )
	{
		return -1;
	} 
	else 
	{
		strTime = TegRemoveIlegal(strTime);
		timeArray = strTime.split(":");
		if (timeArray.length<=3) 
		{
			var i, factor;
			for (i=(timeArray.length-1),factor=1; !(i<0); i--,factor*=60) 
			{
				if ( isFinite(timeArray[i]) ) 
					nSeconds += factor * timeArray[i];
				else 
					return -1;
			}
		}
	}

	return nSeconds;
}

function SetSearchStringVars()
{
	var searchArray=getSearchAsArray();

	window.TegPos = searchArray["pos"];	
	if( (typeof(window.TegPos) != "undefined") && (!isNaN(window.TegPos)) )
		window.TegPos = parseInt(window.TegPos);
	else
		window.TegPos = (-1);

	var tmpTegStart=Math.max(GetTegTime(searchArray["start"]), 0);
	var tmpTegSessionStart = 0;
	if( (window.TegSessionStart) && ( typeof(window.TegSessionStart)=="string" ) )
		tmpTegSessionStart = GetTegTime(window.TegSessionStart);
	window.TegStart = Math.max(tmpTegStart,tmpTegSessionStart);

	var tmpTegStop=GetTegTime(searchArray["stop"]);
	var tmpTegSessionStop = (-1);
	if( (window.TegSessionStop) && ( typeof(window.TegSessionStop)=="string" ) )
		tmpTegSessionStop = GetTegTime(window.TegSessionStop);
	if( (tmpTegStop >= 0) && (tmpTegSessionStop >= 0) )
		window.TegStop = Math.min(tmpTegStop,tmpTegSessionStop);
	else
		window.TegStop = Math.max(tmpTegStop,tmpTegSessionStop);
	if (window.TegStop >= 0 && (window.TegStop < window.TegStart) )
		window.TegStop = window.TegStart;

	window.media = searchArray["media"];
	window.trackid = searchArray["trackid"];
	window.aicc_sid = searchArray["aicc_sid"];
	window.aicc_url = searchArray["aicc_url"];
}


function OpenPlayerStream(streamName)
{
	MP_FileName(streamName);
	g_loadok = 1;
	return 1;
}

function TSelectionEnd()
{
	return MP_SelectionEnd();
}

function TSetTime(t)
{
	if (MP_CanSeek() )
	{
		CurrentTime = t;
		tt = t+"/1000.0";
		tt = eval(tt);
		MP_SetCurrentPos(tt);
	}
}

function TGetTime()
{
	nRet = 0;
	if (g_MPTimesDelta >= 0)
		nRet = (MP_CurrentPos()*1000 + g_MPTimesDelta) ;
	else
		nRet = CurrentTime;
		
	return Math.floor(nRet); //bug in Sun VM plugin: overflow (?) when fraction too long -> will return -1
		
}

function TpGetPosition()
{
	g_exiting = 0;
	TGetPosition();
}



function TGetVideoCodec() //!!##only relevant to MP6.4 (7 comes with all needed codecs), need to add a QT dummy codec name/id
{
	rv = -1;
	if (window.__AsxFileType__ == 2)
		rv = 0;
	else  if (window.TVideoCodec.toLowerCase() == "vdo")
		rv = 1;
	else if (window.TVideoCodec.toLowerCase() == "mp43")
		rv = 2;
	return rv;
}

function InitializePage ()
{
	UrlArray = new Array();
	UrlIdArray = new Array();
	
	oMP = document.NSPlay;
	
	if ( (playback != "TRUE") && (NetShowLiveExpediteTime>5000) && (Collaboration != "1"))
	{
		MP_ContextMenu(false);
		MP_Mute(true);
	}
}


function TOpenURL(path,id,pause,isEditor,isSC)
{
	if (isEditor)
	{
		window.UrlArray[id] = window.open(path);
		if(pause)
			window.UrlIdArray[window.UrlIdArray.length] = id;
	}
	else
	{
		if(!window.UrlArray[id] || window.UrlArray[id].closed)
		{
			window.UrlArray[id] = window.open(path);
			if(pause)
				window.UrlIdArray[window.UrlIdArray.length] = id;
		}
	}
}

function TShowDocument(path) //replaces the not-working showDocument in mac
{
	window.open(path);
}

//this function should not be entered in editor mode, due to an IE bug that fails when trying
//to get the property window.closed on the last browser window that was closed
function urlMonitor()
{
	var flag = 0;
	for(var i=0;i<window.UrlIdArray.length;i++)
	{
		if((!window.UrlArray[window.UrlIdArray[i]]) || (window.UrlArray[window.UrlIdArray[i]].closed))
		{
			window.UrlArray[window.UrlIdArray[i]] = null;
			for(var j=i+1;j<window.UrlIdArray.length;j++)
				window.UrlIdArray[j-1] = window.UrlIdArray[j];
			window.UrlIdArray.length = (window.UrlIdArray.length > 0)? window.UrlIdArray.length -1:0;
			if (window.UrlIdArray.length == 0)
				flag = 1;
		}
	}
	return flag;
}

function closeMonitoredUrls()
{
	for(var i=UrlIdArray.length-1;i>=0;i--)
	{
		if((UrlArray[UrlIdArray[i]]) && (typeof(UrlArray[UrlIdArray[i]])!="undefined")  && (!UrlArray[UrlIdArray[i]].closed))
		{
			UrlArray[UrlIdArray[i]].close();
		}
	}

}

function time_string(time_in_millisec)
{
	ticks = parseInt(time_in_millisec /1000);
	seconds = ticks % 60;
	if (String(seconds).length == 1)
		seconds = "0" + seconds;
	time_str = ":"+ seconds;
	if (ticks >= 60)
	{
		ticks = parseInt(ticks /60);
		minutes = ticks % 60;
		if (String(minutes).length == 1)
			minutes = "0" + minutes;
		time_str = minutes + time_str;
		if (ticks >= 60 )
			time_str = parseInt(ticks /60) + ":" + time_str;
	}
	else
		time_str = "00" + time_str;
	return (time_str);
}

function send_mail(dest)
{
	ClickPos = TGetTime()
	str = dest;
	str += "?subject="+__Title__;
	str += "&body=At time: " + time_string(ClickPos);
	str += "%0c%0a";
	str += "Class URL: ";
	_url = escape(location.toString());
	if (_url.lastIndexOf(".htm") == _url.length-4)
	{
		idx = _url.lastIndexOf("/");
		if (idx>0)
		{
			_url = _url.substring(0,idx+1) +"default.htm";
		}
	}
	if (location.protocol.indexOf("file")<0)
	{
		_url = _url+"?pos="+Math.floor(ClickPos);
	}
	str += "<" + _url + ">";
	if (g_Browser == "Netscape")
	{
		window.location = str;
	}
	else
	{
		if(g_firstTime)
		{
			s='<DIV id="mailFrm_div" STYLE="position: absolute; left: 0px; top: 0px">';
			s+='<IFRAME ID="mailFrm" FRAMEBORDER=0 WIDTH=0 HEIGHT=0></IFRAME>';
			s+='</DIV>';
			document.body.insertAdjacentHTML( "beforeEnd", s);
			g_firstTime = false;
		}
		s='<HTML><BODY><SCRIPT>location="'+str+'";</SCRIPT></BODY></HTML>';
		g_mailFrm = eval("mailFrm");
		g_mailFrm.document.write(s);
	}
}

function TCloseURL(id)
{
	if(window.UrlArray[id])
	{
		window.UrlArray[id].close();
		window.UrlArray[id] = null;
	}
}


function getDownloadingMessage(x) //!!##need some adptations to sun VM signing/jar-caching mechanisms
{
	var msg='';
	msg += '<DIV ID="loading" STYLE="position:absolute; left:' + (x) + 'px; top:0px">';
	msg += '<TABLE BORDER=0 WIDTH="100%" HEIGHT="100%">';
	msg += '<TR HEIGHT=300><TD ALIGN="center"><img src="download.gif"></TD></TR>';
	
	if (g_Browser != "Netscape")
	{
		if (navigator.userAgent.indexOf("Windows NT 5.1")>-1)
		{
			msg += '<TR HEIGHT=20 ALIGN="center"><TD>';
			msg += '<FONT FACE="Arial" SIZE="2" COLOR="White">';
			msg += "If you don't have the Microsoft Java virtual machine and can't download<BR>";
			msg += 'it from Microsoft, please <A href="http://www.tegrity.com/MSJavaVM.htm">click here</A>';
			msg += '</FONT>';
			msg += '</TD></TR>';

		}
	
		if (!g_bFileProtocol)
		{
			msg += '<TR HEIGHT=200><TD ALIGN="center" VALIGN="bottom">';
			msg += '<FONT FACE="Arial" SIZE="2" COLOR="White">';							
			msg += 'If a Security Warning message appears Tegrity recommends<BR>';
			msg += 'to click on &#39;Yes&#39; when prompted. This will minimize the loading<BR>';
			msg += 'time the next time you view a WebLearner session.';
			msg += '</FONT>';
			msg += '</TD></TR>';
		}
	}

	msg += '</TABLE>';
	msg += '</DIV>';
	return msg;
}

function getMaxVideoWidth()
{
	Stream2 = window.__AsxFileType__ & 2;
	Stream3 = window.__AsxFileType__ & 4;
	nMax = 0;
	if ( Stream2 == 2 && window.VideoWidth.length)
		nMax = Number(window.VideoWidth);
	if ( Stream3 == 4 && window.Video2Width.length && window.Video2Width > nMax)
		nMax = Number(window.Video2Width);
	return nMax;
}

function setSizes()
{
	iW = ( screen.availWidth>800 ) ? 640 : 630;
	iH = 480;

	if (window.WLVer == "2")
	{
		mpW = 0;
		mpH = 0;
		aW = window.document.body.clientWidth;
		aH = window.document.body.clientHeight;
		aM = 0;
	}
	else
	{
		if (screen.availWidth>800)
		{
			aW = 640;
			mpW = getMaxVideoWidth();
			mpW = mpW > 0 ? mpW : 160;
		}
		else
		{
			aW = 630;
			mpW = 160;
		}
		mpH = (mpW*3)/4;
		aH = 552;
		aM = 3;;
	}
}

function getIW()
{
	return iW;
}
function getIH()
{
	return iH;
}
function getScreenWidth()
{
	return window.screen.availWidth; //##need to fix this, may not be the whole width
}
function getShowCC(){ return window.UseClosedCaptions;}


function getScormAPI()
{
	var theAPI = findScormAPI(this.top);
	if (theAPI == null)
		if (typeof(this.opener) != "undefined")
			if (this.opener != null)
			{
				if (!this.opener.closed && typeof(this.opener.top) != "undefined")
				{
					theAPI = findScormAPI(this.opener.top);
					if (theAPI == null)
						if (typeof(this.opener.opener) != "undefined" )
						{
							if (this.opener.opener != null)
							{
								if (!this.opener.opener.closed && typeof(this.opener.opener.top) != "undefined" )
									theAPI = findScormAPI(this.opener.opener.top);
							}
						}
				}
	        }	
	return theAPI;
}

function findScormAPI(win) 
{
	if (win.API != null)
		return win.API;
	if (win.length > 0)  // does the window have frames?
	{
		for (var i=0;i<win.length;i++)
		{
			var theAPI = findScormAPI(win.frames[i]);
			if (theAPI != null)
	            return theAPI;
		}
	}
	return null;
}
function ScormInitialize()
{
	if (g_scormInitialized==false)
	{
		api = getScormAPI();
		if (api)
			api.LMSInitialize("");
		else
			return false;
	}
	return true;
}

function ScormSetInitialized(bSet)
{
	g_scormInitialized = bSet
}

function ScormGetInitialized()
{
	return g_scormInitialized;
}

function ScormGetLastError()
{
	api = getScormAPI();
	if (api)
		return api.LMSGetLastError(""); //retval==0 if no error
}
function ScormGetErrorString(errCode)
{
	api = getScormAPI();
	if (api)
		return api.LMSGetErrorString(errCode);
}
function ScormGetValue(paramName)
{
	api = getScormAPI();
	if (api)
		return api.LMSGetValue(paramName);
}
function ScormSetValue(paramName,paramValue)
{
	api = getScormAPI();
	if (api)
		api.LMSSetValue(paramName,paramValue);
	else
		return false;
	return true;
}
function ScormCommit()
{
	api = getScormAPI();
	if (api)
		api.LMSCommit("");
	else
		return false;
	return true;
}
function ScormFinish()
{
	if (ScormGetInitialized()==true)
	{
		api = getScormAPI();
		if (api)
			api.LMSFinish("");
		else
			return false;
		ScormSetInitialized( false );
	}
	return true;
}  


 