var InEditor;
var CloseState = 0; // 0 - tegrity without url 1 - tegrity or editor with url 2 - editor without url 3 - editor while appling changes 4 - editor when closed 
var sDownloadPath = "http://www.microsoft.com/windows/mediaplayer/default.asp";
fid=1;
var SamiFilePath;
var g_Mute = false;
var g_Volume = 0;
mpDisplaySize = 0;

var g_playstate = 0;
var g_buffering = 0;
var g_showCantSeekError = 1;

var g_lastIF = "";

var alreadyMoved = -1;

function badObject()
{
	if(confirm("'Windows Media Player' plug-in is required to view the class.\nThe download may require several minutes to complete.\n(Download file size is approximately 2.5 Mb).\n\nDownload now?"))
	{
		window.open(sDownloadPath,"download","menubar=yes,resizeable=yes,scrollbars=yes,toolbar=yes,directories=yes,hotkeys=yes,location=yes,titlebar=yes,status=yes");
	}
	self.top.close();
}

function TUnload()
{
	g_exiting = 1;
	var width = Math.min(794,screen.Width-5);
	if(CloseState == 1)
	{
		if(event.clientX > width-10)
			if(event.clientX < width+10)
				return;
		g_exiting = 0;
		event.returnValue = "This Web site is trying to open full screen. If you press OK, WebLearner will not be accessible anymore";
	}
	if(CloseState == 2)
	{
		g_exiting = 0;
		event.returnValue = "Are you sure you want to close the editor?";
	}
		
	if(CloseState == 3)
	{
		g_exiting = 0;
		event.returnValue = "WebLearner Editor is processing a session. Pressing OK will damage the session";
	}
}

function TUnloadAsf()
{
	CloseState = 3;
	window.oMP.FileName = "";
	window.oMP.TransparentAtStart=-1;
	window.oMP.Volume = g_Volume;
	window.oMP.ShowControls = 0;
	window.oMP.ShowStatusBar = 0;
	window.oMP.ShowGotoBar=0;
	SC_Mute(g_Mute);
	MP_Mute(g_Mute);
	scframe.SC_Unload();

}

function TLoadAsf(streamName)
{
	var g_playok = 0;
	var g_playstate = 0;
	window.oMP.AutoStart = 1;
	window.oMP.FileName = streamName;
	window.oMP.DisplaySize = mpDisplaySize;
	CloseState = 2;
	TMoveMpl(800);
}

function IsLoaded()
{
	if(g_loadok == 1)
		return 1;
	return 0;
}

function TEditorClosed()
{
	CloseState = 4;
	window.close();
}

function TEditorReload()
{
	CloseState = 5;
}

function setUrlState(show)
{
	if(show == 1)
		CloseState = 1;
	else
	{
		if(InEditor == 1)
			CloseState = 2;
		else
			CloseState = 0;
	}
}

function getUrlState()
{
	return CloseState==1;
}


function MP_FileName(streamName)
{
	window.oMP.FileName = streamName;
}

function MP_ContextMenu(x)
{
	window.oMP.EnableContextMenu = x;
}

function MP_Mute(x)
{
	g_Mute = x;
	window.oMP.Mute = x;
}

function MP_Volume(x)
{
	g_Volume=x;
	window.oMP.Volume = x;
}

function MP_SelectionEnd()
{
	return window.oMP.SelectionEnd;
}

function MP_CanSeek()
{
	return window.oMP.CanSeek;
}

function MP_SetCurrentPos(x)
{
	window.oMP.CurrentPosition = x;
}

function TGetErrorCode()
{
	return window.oMP.ErrorCode;
}

function TGetErrorDescription()
{
	return window.oMP.ErrorDescription;
}

function MP_ReadyState()
{
	return window.oMP.ReadyState;
}

function MP_OpenState()
{
	return window.oMP.OpenState;
}

function MP_CurrentPos()
{
	return window.oMP.CurrentPosition;
}

function MP_Duration()
{
	return window.oMP.Duration;
}

function MP_HasErr()
{
	return window.oMP.HasError;
}

function TGetMPFileName()
{
	return window.oMP.FileName;
}

function TBufferingProgress()
{
	return window.oMP.BufferingProgress;
}

function TBufferingTime()
{
	return window.oMP.BufferingTime;
}

function MP_PlayState()
{
	return window.oMP.PlayState;
}

function HideLink(bShowSR)
{
	if (window.lastdiv)
	{
		if (typeof(eval(g_lastIF))=="object")
			eval(g_lastIF).location = "about:blank";//can't do this if I already set the location to something other than "blank"
		lastdiv.style.visibility = "hidden";
		if (bShowSR && !g_SCOutside)
			scplayer.style.top=0;

	}
	setUrlState(0);
}
 
function ShowLink(src)
{
	HideLink(false);
	setUrlState(1);
	
	if (!g_SCOutside)
		scplayer.style.top=800;
	
	ifname = 'content'+fid;
	g_lastIF = ifname;
	s='<DIV id="ifrm_div'+fid+'" STYLE="position: absolute; left: '+(mpW+aM)+'px; top: 0px; z-index: 1">';
	s+='<IFRAME name="'+ifname+'" FRAMEBORDER=0 WIDTH=640 HEIGHT=480></IFRAME>';
	s+='</DIV>';

	document.body.insertAdjacentHTML( "beforeEnd",  s);
	lastdiv = eval("ifrm_div"+fid);
	fid++;
	setTimeout("showLink1('"+ifname+"','"+src+"')",500);	
}


function showLink1(ifname,src)
{	
	s='<HTML><BODY><SCRIPT>location="'+src+'";</SCRIPT'+'></BODY></HTML>';
	eval(ifname).document.write(s);
}

function setMP()
{
	if (window.AsxFileType == 1)
		mpH = 0;
	MPs='<OBJECT ID="NSPlay" WIDTH='+mpW+' HEIGHT='+mpH+ ' ';
	MPs+='CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ';
	MPs+='OnError ="badObject()" ';
	MPs+='standby="Loading Microsoft® Media Player components..." ';
	MPs+='type="application/x-oleobject"> ';
	MPs+='<PARAM NAME="AutoStart" VALUE="0"> ';
	MPs+='<PARAM NAME="AutoRewind" VALUE="0"> ';
	MPs+='<PARAM NAME="ClickToPlay" VALUE="0"> ';
	MPs+='<PARAM NAME="Volume" VALUE="0"> ';
	MPs+='<PARAM NAME="ShowControls" VALUE="0"> ';	
	MPs+='<PARAM NAME="TransparentAtStart" VALUE="-1"> ';	
	
	if (screen.availWidth > 800)
		MPs+='<PARAM NAME="DisplaySize" VALUE="0"> ';
	else
	{
		mpDisplaySize = 4;
		MPs+='<PARAM NAME="DisplaySize" VALUE="4"> ';
	}
	
	MPs+='</OBJECT>';
	return MPs;
}

function ShouldUseUnsigned()
{
	if( (__bForceUseUnsign__.toLowerCase() == "true")|| (g_bFileProtocol && window.WLVer != "2" ))
		return 0;
		
	pck_UseUS = GetCookie('TUseUnsigned');
	if (!(pck_UseUS))
		return 2;
	else
		return pck_UseUS;
}

function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if(document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}


function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}


function SetCookie(name,value,expires,path,domain,secure)
{
	document.cookie = name + "=" + escape (value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
}

function getPlayerID()
{
	if (oMP)
	{
		var strID = oMP.ClientID;
		var idx  = strID.lastIndexOf("-");
		if (idx > 0)
			strID = strID.substring(0,idx);
		return strID;
	}
	return "";
}

function TCanPlay() 
{
	var readyState = MP_ReadyState();
	var openState = MP_OpenState();
	return (readyState >= 3 && openState == 6);
}


function TGetStatus() 
{

	if (g_loadok == 0)
		return 5;
		
	if (g_playok == 1)
	{
		if (alreadyMoved!= mpY && MP_PlayState()== 2)
		{
			TMoveMpl(mpY);
		}
		return g_playstate;
	}

	if ( MP_HasErr() )
		return 4;

	if ( MP_ReadyState() < 3 )
		return 5;

	if ( MP_OpenState() != 6 )
		return 5;

	var ps = MP_PlayState();
	if (ps == 3)
		return 2;
		
	if ( ps > 2 )
		return 5;
		
	g_playok = 1;
	g_playstate = ps;

	if (playback == "TRUE")
	{
		if ( !MP_CanSeek())
		{
			if (g_showCantSeekError)
			{	
				g_showCantSeekError = 0;
				if (oMP.FileName != "")
				{
					TStop();				
					MP_FileName("");				
					alert("Your class recording will not display properly!\nPlease use the Tegrity Web Recording Manager\nto upload or save the recording.\n\nTroubleshooting Note:\nYou must specify the correct file location\nfor the Windows Media (Netshow) Server.");
				}
			}
			g_playok = 0;
			g_playstate = 4;
		}
		else
			TGetPosition();	
	}
	
	return g_playstate;
}


function TBuffering() 
{
	if (g_buffering == 1)
	{
		var progress = TBufferingProgress();
		if (progress == 100)
			return "0";
		else
			return "1";
	}
	else
		return "0";
}


function TPlay()
{
	g_instPausing = false;
	oMP.Play();
}

function TStop()
{
	g_instPausing = false;
	oMP.Stop()
}

var g_instPausing = false;

function TPause()
{
	g_instPausing = true;
	TPauseVerify();
}

function TPauseVerify()
{
	if (MP_PlayState() == 3)
	{	
		setTimeout("TPauseVerify()",100);
	}
	else
	{	
		if (g_instPausing)
			oMP.Pause()
		g_instPausing = false;
	}
}




function MP_ReadyStateChange(ReadyState)
{
	if (g_playok ==0)
		return;

	if (ReadyState < 3)
	{
	  	g_playstate = 5;
		g_playok = 0;
	}

}

function MP_ScriptCommand(scType, scParam)
{
	if (scType == "TC" && playback == "FALSE")
	{
		g_MPTimesDelta = scParam - MP_CurrentPos()*1000;
	}
	else if (scType == "GT")
		TSetTime(scParam);
}

function MP_PlayStateChange(OldState, NewState)
{
	if (g_playok ==0)
		return;

	if (NewState>2)
	{
		g_playstate =5;
		g_playok = 0;
	}
	else
	{
		g_playstate = NewState;
	}
}

function MP_OpenStateChange( OldState, NewState)
{
	if (g_playok ==0)
		return;

	if (NewState != 6)
	{
		g_playstate = 5;
		g_playok = 0;
	}
}

function MP_Error()
{
	g_playstate = 4;
}


function MP_PositionChange(OldPos, NewPos)
{
	if ( (TegStop != -1) && (NewPos > TegStop) )
	{
		TStop();
		TSetTime(TegStop*1000.0);
	}
	else
		if ( NewPos < TegStart)
			TSetTime(TegStart*1000.0);
}

function MP_Buffering(Start)
{
	g_buffering = ((Start == 1) ? 1: 0);
}


function TGetPosition()
{
	if (g_playok != 1 || g_exiting == 1)
		return;

	if (g_playstate != 1)
	{
		CurrentTime = MP_CurrentPos();
		if ( (TegStop != -1) && (CurrentTime >= TegStop) )
			TStop();

		CurrentTime*=1000.0;
	}
	setTimeout("TGetPosition()", 1000);
}

function TMoveMpl(yPos)
{
	if (alreadyMoved != yPos)
	{//mpl.style.top doesn't always return the right answer, so alreadyMoved will hold the correct position.
		alreadyMoved = yPos;
		mpl.style.top = yPos;
	}
}