//
//	Media Player Library - Cross browser / Cross player library.
//	(c) 2001-2003  Interactive Video Technologies
//
//	Basic classes are as follows:
//
//		MediaPlayers - Factory for generating MediaPlayer objects
//		MediaPlayer  - Instantiated once per video component on a page.
//		MediaStreams - Container holding all MediaStream objects for a MediaPlayer
//		MediaStream  - Class holding a single stream that can be displayed in a MediaPlayer
//
//	Global Variables:
//
//		WM, RM, QT - Constants for player type
//		PLAYER_xxx - Constants representing current play state
//		players    - Single global instantiation of a MediaPlayers object
//
//	NOTES:
//
//		Use players.getPlayer(name) where name is an elemID of a video object to get the player object for
//		a particular component
//

//
//  2001/2002    J.Busfield   Original creation of classes
//  10/23/2002   B.Law        Added enable_OnAuthorChange to RP RegisterEvents code to handle Akamai embedded timestamps
//  01/09/2003   B.Law        Reworked class to use more conventional way of defining a class and documented things
//  01/10/2003   B.Law        Moved a bunch of functionality in here that used to get deployed by VideoComponent.jsp
//  01/10/2003   B.Law        Made getCurrentTime and jumpToTime take into account offsets for the stream that is playing
//  01/17/2003   B.Law        Added concept of Start Time to a stream which is essentially just another offset... but not relative from stream to stream... instead it is a soft-chop value that some versions of WMP (6.4 for example) need to be aware of.
//  05/01/2003   B.Law        Added code to support Windows Media Player 9
//  05/01/2003   B.Law        Added getObjectHtml method to return HTML that should be embedded in page for this video stream.
//  05/05/2003   B.Law        Added VC_getObjectHtml method.
//  06/02/2003   B.Law        Added setScreenState method to handle full screen mode.
//  07/15/2003   B.Law        Added setVolume method.
//  07/18/2003   B.Law        Added code to handle autoSize flag for Real Videos - required changing size after clip dimensions are determined via javascript.
//  08/14/2003   J.B.         Moved SetFileName code here.
//  12/08/2003   G.Minami     Added parameter to LogEvent calls to send stats immediately so can track this event realtime
//  12/09/2003   B.Law        Added QueryString (qs) as optional parameter to AddStream. This querystring will be appended to the stream URL.
//  12/09/2003   B.Law        Added getAkamaiTokenQS(...) method to return stream querystring for Akamai authenticated stream.
//  12/16/2003   B.Law        Autosize does not work with Real/NS... can't resize an embed, and you must specify a width/height... so made Real/NS combo always use video component size.
//  12/17/2003   B.Law        Fixed problem with duration in WMP9... had to use currentMedia.duration.
//  12/17/2003   J.B.         In GetPlayState added check for ReadyState==4 before retrieving playstate for WMP 6.4
//  01/04/2003   J.B.         Added check for time<0 to jumpToTime. The offset is creating times less than 0 in some cases. This probably shouldn't be happening but this check is good just in case
//  01/13/2004   B.Law        Made so RM and WMP9 both ignore StartTime
//  01/26/2004   G.Minami     if using frames send stats LogEvent function call to first frame
//  03/02/2004   J.B.         Changed Netscape 7.1 WMP support to activex
//  03/19/2004   B.Law        Added reference to g_RealVideoDefault for default Real Player video. Using an empty string causes inconsistent errors.
//  03/19/2004   B.Law        Made Start() method use the new doPlayWhenReady() to play Real Media to fix a timing issue.
//  02/29/2004   J.B.         Changed NS7 Real video resize code in VC_setRealSize and VC_getRealHtml to act more like IE
//  04/02/2004   B.Law        Now recognizes "SDP" as a Quicktime type.
//  06/11/2004   B.Law        Moved CheckEvent code into here.
//  06/12/2004   B.Law        Added lastTime so rewind can be tracked.
//  06/15/2004   B.Law        Made doTimeWarp take into account initial values.
//  09/08/2004   B.Law        Modified VC_setRealSize to use a helper function and a setTimeout to set width/height... issue with Mozilla hanging if done inline... some kind of timing issue apparently.
//

var WM = 1;
var RM = 2;
var QT = 3;

//state definitions moved to ivt_inc.js

function GetSelectedPlayer(elemID,need_wmp,need_real,need_qt)
{
	if(!need_wmp && !need_real && !need_qt)
		return -1;

	var requestedPlayerStr = GetParamFromQueryString("mp", document.location.search)
	if (requestedPlayerStr == "") 
	{
		try
		{
			if (window.parent)
				requestedPlayerStr = GetParamFromQueryString("mp", window.parent.document.location.search);
		}
		catch(e){}
	}

	if (requestedPlayerStr == "3")
		requestedPlayer = QT;
	else if (requestedPlayerStr == "2")
		requestedPlayer = RM;
	else
		requestedPlayer = WM;

	var selected_player = 0;

var is_qt = true;

	var can_use_wmp = false;
	var can_use_real = false;
	var can_use_qt = false;
	if (need_wmp)
		if(is_wmp) can_use_wmp = true;
	if (need_real)
		if(is_real) can_use_real = true;
	if (need_qt)
		if(is_qt) can_use_qt = true;

	if (requestedPlayer == WM) {
		if(can_use_wmp) selected_player= WM;
		else if(can_use_real) selected_player= RM;
		else if(can_use_qt) selected_player= QT;
	}
	if (requestedPlayer == RM) {
		if (can_use_real) selected_player= RM;
		else if (can_use_wmp) selected_player= WM;
		else if (can_use_qt) selected_player= QT;
	}
	if (requestedPlayer == QT) {
		if (can_use_qt) selected_player= QT;
		else if (can_use_wmp) selected_player= WM;
		else if (can_use_real) selected_player= RM;
	}
	return selected_player;
}

function GetSelectedBandwidth()
{
	var requestedBandwidthStr = GetParamFromQueryString("bw", document.location.search)
	if (requestedBandwidthStr == "") 
	{
		try
		{
			if(window.parent)
				requestedBandwidthStr = GetParamFromQueryString("bw", window.parent.document.location.search);
		}
		catch(e){}
	}
	if (requestedBandwidthStr == "")
		requestedBandwidthStr = "0";
	return parseInt(requestedBandwidthStr);
}

function GetVideoFileType(fileName)
{
	var playerType = WM;
	var ext=fileName.substring(fileName.lastIndexOf(".")+1);
	if(ext.toUpperCase() == "RM" || ext.toUpperCase() == "RAM")
		playerType = RM;
	if(ext.toUpperCase() == "MOV"  ||  ext.toUpperCase() == "SDP")
		playerType = QT;
	
	return playerType;

}
////////////////////////////////////////////////////////////////////////////////////////////////

function MediaPlayer(name,componentName,handleEvents) 
{
	//
	// Methods:
	//	Start(bandwidth) - Gets first stream width bandwidth <= passed in value and sets the video source. If AutoPlay is on, it also starts the player if necessary.
	//	AddStream(url, type, bandwidth, relOffset, startTime, qs) - Adds a stream to the stream collection for this object. URL is the URL of the stream, type is the constant WM or RM, bandwidth is a bandwidth #, relOffset is the offset in milliseconds relative to the rest of the videos in the collection, and startTime is the start time of this video clip.
	//	GetFirstStream(bandwidth) - Returns the URL of the first stream in the collection with a bandwidth <= the passed in bandwidth and of the current Media Type (RP or WMP)
	//	RegisterEventObservers() - Configures proxy applet required by Netscape to communicate with player plugins
	//	getCurrentTime() - Returns current position in video in milliseconds (taking into account various offsets)
	//	jumpToTime(time) - Jumps to the indicated time (in milliseconds) into the video
	//	getPlayState() - Returns the current state of the player
	//  getObjectHtml() - Returns HTML to embed on page for this stream
	//

	this.GetPlayerElement = function(name)
	{
		if(this.type==QT)
			return window.frames[name+"-Frame"].document.getElementById(name);
		if(this.type==RM)
			return window.frames[name+"-Frame"].document.getElementById("Video1");
		return GetElement(name);
	}

	this.name = name;
	this.type = Element(componentName)["Player Type"];
	this.player = this.GetPlayerElement(name);
	this.componentName = componentName;
	this.handleEvents = handleEvents;
	this.oldScreenState = 0;
	this.qtPlayState = 0;
	this.videoSize = null;
	this.lastTime = 0;

	this.streams = new MediaStreams();
	this.currentStreamIndex = 0;

	this.getWmpVersion = function()
	{
		return VC_getWmpVersion();
//		if (this.player.SetFileName == null) return 9;
//		return 6;
	}

	this.Start = function(bandwidth)
	{
		if (this.handleEvents)
			this.RegisterEventObservers();

		this.videoSize = null;
		var url = this.GetFirstStream(bandwidth);
		if(url == "")
			return;
		if (this.type == RM) {
			this.player.SetAutoGoToURL(false);
			this.player.SetSource(url);

			if (Element(this.componentName)["Auto Start"].toUpperCase()=="TRUE") {
// Had to change to use setTimeout because of intermittent timing issue with IE/Real
//				this.player.DoPlay();
				setTimeout("players.getPlayer('" + this.name + "').doPlayWhenReady()", 200);
			}

			CallLogEvent(this.componentName,"VideoComponent","PlayRM",new Date(),0,true);
		}
		else if(this.type == QT)
		{
			if (Element(this.componentName)["Auto Start"].toUpperCase()=="TRUE")
				this.qtPlayState = PLAYER_PLAYING;
			this.player.SetResetPropertiesOnReload(false); 
			if(this.player.GetURL()=="")
			{
//				PreventUnload();
//				document.location.reload(false);
//				return;
			}
			this.player.SetURL(url);
			CallLogEvent(this.componentName,"VideoComponent","PlayQT",new Date(),0,true);
		}
		else {
			if (this.getWmpVersion() < 9) {
				if (NS4)
					this.player.SetFileName(url);
				else
					this.player.FileName = url;		
			}
			else {
				if (NS4) {
					this.player.setURL(url);
				}
				else {
					this.player.URL = url;
				}
			}
			CallLogEvent(this.componentName,"VideoComponent","PlayWM",new Date(),0,true);	
		}
	}

	this.AddStream = function(url, type, bandwidth, relOffset, startTime, qs)
	{
		if (!relOffset) relOffset = 0;
		if (!startTime) startTime = 0;

		// Ignore startTime for RM or WMP9
		if (type == RM) startTime = 0;
		if (type == WM  &&  this.getWmpVersion() >= 9) startTime = 0;

		this.streams.Add(url, type, bandwidth, relOffset, startTime, qs);
	}

	this.GetFirstStream = function(bandwidth)
	{
		var url = "";
		this.currentStreamIndex = this.streams.GetFirstStreamPosition(this.type, bandwidth);
		if(this.currentStreamIndex != -1)
		{
			url = this.streams.GetStreamURL(this.type, this.currentStreamIndex);
			this.relOffset = this.streams.GetStreamRelOffset(this.type, this.currentStreamIndex);
			this.startTime = this.streams.GetStreamStartTime(this.type, this.currentStreamIndex);

			// Add querystring to stream URL if there is one
			var qs = this.streams.GetStreamQS(this.type, this.currentStreamIndex);
			if (qs != null) {
				if (qs.length > 0) {
					url += (url.indexOf("?") > 0 ? "&" : "?") + qs;
				}
			}
		}
		return url;
		
	}

	this.RegisterEventObservers = function()
	{
		if (!NS4) return;

		if (this.type == WM  &&  VC_getWmpVersion() < 9) {
			navigator.plugins.refresh();
			var proxy = document.applets.MPProxy;
			proxy.setByProxyDSScriptCommandObserver(this.player, true);
		}
		if (this.type == RM) {
			var proxy = document.applets.RPProxy;
			proxy.setPlayer(this.player, "Video");
			proxy.enable_onGoToURL(true);
			proxy.enable_OnAuthorChange(true);
		}
	}

	this.getOriginalVideoSize = function()
	{
		if(this.videoSize != null)
			return this.videoSize;

		var w=0;
		var h=0;
		if(this.type == WM)
		{
			if(this.getWmpVersion() < 9)
			{
				if (NS4)
				{
					w = this.player.GetImageSourceWidth();
					h = this.player.GetImageSourceHeight();
					
				}
				else
				{
					w = this.player.ImageSourceWidth;
					h = this.player.ImageSourceHeight;
				}
			}
			else
			{
				w = this.player.currentMedia.imageSourceWidth;
				h = this.player.currentMedia.imageSourceHeight;
			}
		}
		if(this.type == RM)
		{
			w = this.player.GetClipWidth();
			h = this.player.GetClipHeight()
		}

		if(w!=0 && h!=0)
		{
			this.videoSize = new Object();
			this.videoSize.w = w;
			this.videoSize.h = h;
		}
		return this.videoSize;
	}

	this.getCurrentTime = function()
	{
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				if (NS4) return this.player.GetCurrentPosition() * 1000 - this.relOffset - this.startTime;
				return this.player.CurrentPosition * 1000 - this.relOffset - this.startTime;
			}
			else {
				if (NS4) return this.player.getControls().getCurrentPosition() * 1000 - this.relOffset - this.startTime;
				return this.player.controls.currentPosition * 1000 - this.relOffset - this.startTime;
			}
		}
		if (this.type == RM) {
			if (this.player.GetPlayState() == 0) return 0;
			return this.player.GetPosition() - this.relOffset - this.startTime;
		}
		if (this.type == QT) {
			if (this.player.GetPluginStatus() == "Playable" || this.player.GetPluginStatus() == "Complete")
				return this.player.GetTime() / this.player.GetTimeScale()*1000 - this.relOffset - this.startTime;
			else if(this.player.GetPluginStatus() == "Loading" )
			{
				try
				{
					return this.player.GetTime() / this.player.GetTimeScale()*1000 - this.relOffset - this.startTime;
				}
				catch(e) { return 0;}
			}
			else
				return 0;
		}
	}

	this.setWidth = function(elemID,val)
	{
		if (this.type == WM) 
		{
			SetStyle(elemID + '-0','width',val);
		}
		else
		{
			document.getElementById(elemID+'-0-Frame').style.width = val;
			//this.player.style.width = val;
			this.player.width = val;
		}
	}

	this.setHeight = function(elemID,val)
	{
		if (this.type == WM) 
		{
			SetStyle(elemID + '-0','height',val);
		}
		else
		{
			document.getElementById(elemID+'-0-Frame').style.height = val;
			//this.player.style.height = val;
			this.player.height = val;
		}
	}

	this.getTotalTime = function()
	{
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				if (NS4) return this.player.GetDuration() * 1000 - this.relOffset - this.startTime;
				return this.player.Duration * 1000 - this.relOffset - this.startTime;
			}
			else {
				if (NS4) return this.player.getCurrentMedia().getDuration() * 1000 - this.relOffset - this.startTime;
				return this.player.currentMedia.duration * 1000 - this.relOffset - this.startTime;
			}
		}
		if (this.type == RM) {
			return this.player.GetLength() - this.relOffset - this.startTime;
		}
		if (this.type == QT) {
			if (this.player.GetPluginStatus() == "Playable" || this.player.GetPluginStatus() == "Complete")
				return this.player.GetDuration() *1000 / this.player.GetTimeScale() - this.relOffset - this.startTime;
			else if(this.player.GetPluginStatus() == "Loading")
			{
				try
				{
					return this.player.GetDuration() *1000 / this.player.GetTimeScale() - this.relOffset - this.startTime;
				}
				catch(e) {return 0;}
			}
			else
				return 0;
		}
	}

	this.jumpToTime = function(time)
	{
		time += (this.relOffset + this.startTime);
		if(time<0)
			time=0;
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				if (NS4)
					this.player.SetCurrentPosition(time/1000);
				else
					this.player.CurrentPosition = time/1000;
			}
			else {
				if (NS4)
					this.player.getControls().setCurrentPosition(time / 1000);
				else
					this.player.controls.currentPosition = time / 1000;
			}
		}
		if (this.type == RM) {
			// Make sure video is in a state that SetPosition will work in
			if (this.player.CanPlay()) {
				this.player.DoPlay();
				this.jumpCount = 0;
				setTimeout("players.getPlayer('" + this.name + "').doJumpTimeHelper(" + time + ")", 100);
//				setTimeout("Video_DoJumpTime('" + elemID + "', " + time + ")", 100);
			}
			else {
				this.player.SetPosition(time);
			}
		}
		if (this.type == QT) {
			// Make sure video is in a state that SetPosition will work in
			if (this.player.GetPluginStatus() == "Playable" || this.player.GetPluginStatus() == "Complete") 
			{
				this.doStop();
				this.player.SetTime((time * this.player.GetTimeScale()/1000)+10) ;
				setTimeout("players.getPlayer('" + this.name + "').doPlay()", 100);
			}
			else if(this.player.GetPluginStatus() == "Loading")
			{
				try
				{
					this.doStop();
					this.player.SetTime((time * this.player.GetTimeScale()/1000)+10) ;
					setTimeout("players.getPlayer('" + this.name + "').doPlay()", 100);
				}
				catch(e) {}
			}
		}
	}

	this.doJumpTimeHelper = function(time)
	{
		if (this.player.CanPlay()) {
			if (++this.jumpCount < 50) {
				setTimeout("players.getPlayer('" + this.name + "').doJumpTimeHelper(" + time + ")", 100);
//				setTimeout(""Video_DoJumpTime('"" + elemID + ""', "" + time + "")"", 100);" & vbCRLF & _
			}
		}
		else {
			this.player.SetPosition(time);
		}
	}

	this.hasError = function()
	{
		var hasError = false;
		if (this.type == WM) 
		{
			if (this.getWmpVersion() < 9) 
			{
				if (NS4) 
				{
					hasError = this.player.GetHasError();
				}
				else
				{
					hasError = this.player.HasError;
				}
			}
		}
		return hasError;
	}

	this.getBufferingProgressText = function()
	{
		var bufferProgress = null;
		if (this.type == WM) 
		{
			if (this.getWmpVersion() < 9) 
			{
				if (NS4) 
				{
					bufferProgress = this.player.GetBufferingProgress();
				}
				else
				{
					bufferProgress = this.player.BufferingProgress;
				}
			}
			else 
			{
				if (NS4) 
				{
					bufferProgress = this.player.getNetwork().getBufferingProgress();
				}
				else 
				{
					bufferProgress = this.player.network.bufferingProgress;
				}
			}
			bufferProgress = "("+bufferProgress+"%)";
		}
		if (this.type == RM) 
		{
			if (NS4  ||  is_ns5up)
				bufferProgress = this.player.GetLastStatus();
			else
				bufferProgress = this.player.GetLastMessage();

			if (bufferProgress.indexOf('Communicating') >= 0)
			{
				var index1 = bufferProgress.indexOf("(");
				var index2 = bufferProgress.indexOf(")");
				bufferProgress = bufferProgress.substring(index1,index2+1);
			}			
		}
		return bufferProgress;
	}

	this.getPlayState = function()
	{
		var state=0;

		if (this.player == null) return PLAYER_CLOSED;
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				if (NS4) {
					if(this.player.GetReadyState()==4)
						state = this.player.GetPlayState();
					if (state == PLAYER_PLAYING  &&  (this.player.GetBufferingProgress() > 0 && this.player.GetBufferingProgress() < 90)) state = PLAYER_BUFFERING;
				}
				else {
					if(this.player.ReadyState==4)
						state =  this.player.PlayState;
					if (state == PLAYER_PLAYING  &&  (this.player.BufferingProgress > 0 && this.player.BufferingProgress < 90)) state = PLAYER_BUFFERING;
				}
				if (state == 5 || state == 7) state = PLAYER_SEEKING;
				else if (state == 6 || state == 8) state = PLAYER_CLOSED;
			}
			else {
				var bufferProgress = 100;
				if (NS4) {
					state = this.player.getPlayState();
					bufferProgress = this.player.getNetwork().getBufferingProgress();
				}
				else {
					state = this.player.playState;
					bufferProgress = this.player.network.bufferingProgress;
				}
				if (state == 0) state = PLAYER_CLOSED;		// Undefined
				else if (state == 1) state = PLAYER_STOPPED;		// Stopped
				else if (state == 2) state = PLAYER_PAUSED;		// Paused
				else if (state == 3) state = PLAYER_PLAYING;		// Playing
				else if (state == 4  ||  state == 5) state = PLAYER_SEEKING;    // ScanForward / ScanReverse
				else if (state == 6  ||  state == 7) state = PLAYER_BUFFERING;	// Buffering / Waiting
				else if (state == 8) state = PLAYER_STOPPED;		// Ended
				else if (state == 9) state = PLAYER_SEEKING;		// Transitioning / preparing new media
				else if (state == 10) state = PLAYER_STOPPED;	// Ready
				else if (state == 11) state = PLAYER_SEEKING;	// Reconnecting
				if (state == PLAYER_PLAYING  && bufferProgress > 0 && bufferProgress < 90) state = PLAYER_BUFFERING;
			}
		}
		if (this.type == RM) {
			state = this.player.GetPlayState();
//window.status = state
			if (state == 0) state = PLAYER_STOPPED;
			else if (state == 1) state = PLAYER_CONNECTING;
			else if (state == 2) state = PLAYER_OPENING;
			else if (state == 3) state = PLAYER_PLAYING;
			else if (state == 4) state = PLAYER_PAUSED;
			else if (state == 5) state = PLAYER_SEEKING;
			if (state == PLAYER_OPENING) {
				var status = '';
				if (NS4  ||  is_ns5up)
					status = this.player.GetLastStatus();
				else
					status = this.player.GetLastMessage();
				if (status != null)
				{
					if (status.indexOf('Communicating') >= 0) state = PLAYER_BUFFERING;
				}
			}
		}
		if(this.type == QT)
		{
			var status = this.player.GetPluginStatus();
//window.status = status;
			if(status.substring(0,5).toUpperCase()=="ERROR")
				state = PLAYER_STOPPED;
			else if(status=="Waiting")
				state = PLAYER_CONNECTING;
			else if(state=="Loading")
				state = PLAYER_OPENING;
			else if(this.player.GetTime() == this.player.GetDuration())
				state = PLAYER_STOPPED;
			else
				state = this.qtPlayState;
		}
		return state;
	}

	this.doStop = function()
	{
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				this.player.Stop();
				if (NS4)
					this.player.SetCurrentPosition(0);
				else
					this.player.CurrentPosition = 0;
			}
			else {
				if (NS4) {
					this.player.getControls().stop();
					this.player.getControls().setCurrentPosition(0);
				}
				else {
					this.player.controls.stop();
					this.player.controls.currentPosition = 0;
				}
			}
		}
		if (this.type == RM) {
			this.player.DoStop();
			this.player.SetPosition(0);
		}
		if (this.type == QT) {
			this.qtPlayState = PLAYER_STOPPED;
			this.player.Stop();
			this.player.SetTime(0);
		}
	}

	this.doPause = function()
	{
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				this.player.Pause();
			}
			else {
				if (NS4)
					this.player.getControls().pause();
				else
					this.player.controls.pause();
			}
		}
		if (this.type == RM) {
			this.player.DoPause();
		}
		if (this.type == QT) {
			this.qtPlayState = PLAYER_PAUSED;
			this.player.SetRate(0);
		}
	}

	this.doPlay = function()
	{
		if (this.type == WM) {
			if (this.getPlayState() == PLAYER_SEEKING) { setTimeout("Video_DoPlay('" + this.name + "');",100); return; }
			if (this.getWmpVersion() < 9) {
				this.player.Play();
			}
			else {
				if (NS4)
					this.player.getControls().play();
				else
					this.player.controls.play();
			}
		}
		if (this.type == RM) {
			if (this.player.CanPlay())
				this.player.DoPlay();
			else
				return false;
		}
		if (this.type == QT) {
			if (this.player.GetPluginStatus() == "Playable" || this.player.GetPluginStatus() == "Complete")
			{	
				this.qtPlayState = PLAYER_PLAYING;
				this.player.Play();
			}
			else if(this.player.GetPluginStatus() == "Loading")
			{
				try
				{
					this.player.Play();
					this.qtPlayState = PLAYER_PLAYING;
				}
				catch(e) { return false;}
			}
			else return false;
		}
		return true;
	}

	this.doPlayWhenReady = function()
	{
		if (this.doPlay()) return;
		setTimeout("players.getPlayer('" + this.name + "').doPlayWhenReady()", 200);
	}

	this.setScreenState = function(newState)
	{
		var doFullScreen = (newState == 3);

		if (this.oldScreenState == newState) return;
		this.oldScreenState = newState;

		if (this.type == WM) {
			if (this.getWmpVersion() < 9) {
				if (NS4)
					this.player.SetDisplaySize(newState);
				else
					this.player.DisplaySize = newState;
				return;
			}

			// WMP9+
			if (NS4)
				this.player.setFullScreen(doFullScreen);
			else
				this.player.fullScreen = doFullScreen;
			return;
		}

		if (this.type == RM) {
			if (doFullScreen) {
				this.player.SetFullScreen();
				return;
			}
			else {
				this.player.SetOriginalSize();
				return;
			}
		}
	}

	this.setFileName = function(fname)
	{
		var newType = GetVideoFileType(fname);

		if(this.type != newType)
		{
			this.doStop();
			VC_CreateVideo(this.componentName,newType);
//			ReplaceElementInnerHTML(GetElement(this.componentName), Video_GetPlayerHTML(this.componentName,newType));
			this.type = newType;
			this.player = this.GetPlayerElement(name);
		}

		Element(this.componentName)["Player Type"] = this.type;
		if (this.type  == WM) 
		{
			if (this.getWmpVersion() < 9) 
			{
				if(NS4)
					this.player.SetFileName(fname);
				else
					this.player.FileName = fname;
			}
			else 
			{
				if (NS4)
					this.player.setURL(fname);
				else
					this.player.URL = fname;
			}

		}
		if (this.type == RM) 
		{
			this.player.DoStop();
			this.player.SetSource(fname);
			this.player.DoPlay();
		}
		if (this.type == QT) 
		{
			if (Element(this.componentName)["Auto Start"].toUpperCase()=="TRUE")
				this.qtPlayState = PLAYER_PLAYING;
			this.player.SetResetPropertiesOnReload(false); 
			this.player.SetURL(fname);
		}

	}

	this.setVolume = function(pct)
	{
		if (pct < 0) pct = 0;
		if (pct > 100) pct = 100;
		if (this.type == WM) {
			if (this.getWmpVersion() < 9) 
			{
				if (pct < 1) pct = 1;
				if (NS4)
					this.player.SetVolume(Math.round(2303*base10log(pct/100)));
				else
					this.player.Volume = Math.round(2303*base10log(pct/100));
			}
			else {
				if (NS4)
					this.player.getSettings().setVolume(pct);
				else
					this.player.settings.volume = pct;
			}
		}

		if (this.type == RM) {
			this.player.SetVolume(pct);
		}

		if (this.type == QT) {
			this.player.SetVolume(parseInt(255 * pct / 100));
		}
	}

	this.getVolume = function()
	{
		if (this.type == WM) 
		{
			if (this.getWmpVersion() < 9) 
			{
				if (NS4)
					return Math.round(Math.pow(10,(this.player.GetVolume())/2303)*100);
				else
					return Math.round(Math.pow(10,(this.player.Volume)/2303)*100);
			}
			else {
				if (NS4)
					return this.player.getSettings().getVolume();
				else
					return  this.player.settings.volume;
			}
		}

		if (this.type == RM) {
			return this.player.GetVolume();
		}

		if (this.type == QT) {
			return (this.player.GetVolume()*100)/255;
		}
	}

/*
	this.getObjectHtml = function(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen)
	{
		return VC_getObjectHtml(this.type, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen)
	}
*/

	/** Perform synchronized events for this video object */
	this.checkEvents = function()
	{
		var e = Element(this.componentName);
		Video_CheckStateChange(this.componentName);
		var inpoint = (e.mpObj.inpoint);
		if (isNaN(inpoint)) inpoint = 0;
		var currentTime = this.getCurrentTime() + (inpoint);

		// If video time "warped" (rewind/ff/jump to time), resync presentation now
		if (Math.abs(currentTime - this.lastTime) > TIMEWARP_TOLERANCE  &&  this.lastTime > 0) {
			doTimeWarp(this.componentName, currentTime);
		}
		else do {
			currentTime = this.getCurrentTime() + (inpoint);

			// If there is no event list, exit now
			if (!e._tA) break;

			// If event before the current event has not even occurred, back up the current event
			while (e._cIndex > 0  &&  e._tA[e._cIndex-1] > currentTime)
				e._cIndex--;

			// If we are already to end of list, exit now
			if (e._cIndex >= e._tA.length) break;

			var tn = e._tA[e._cIndex];
			if (tn<=currentTime) {
				ParseScriptCommand(e._cIndex, e._pA[e._cIndex]);
				e._cIndex++;
			}
		} while (tn <= currentTime);

		this.lastTime = currentTime;
		NotifyListeners(this.componentName, "OnTimeChange", new Array(currentTime, this.getTotalTime()),new Array("NUMBER","NUMBER"));
	}
}


//----------------------------------------------------------------------------
function MediaPlayers()
{
	this.mediaPlayerArray = new Array();
	this.playerHashtable = new Object();

	this.Add = function(name,componentName,handleEvents)
	{
		var mp = new MediaPlayer(name,componentName,handleEvents);
		this.mediaPlayerArray[this.mediaPlayerArray.length] = mp;
		this.playerHashtable[name] = mp;
		return mp;
	}

	this.getPlayer = function(name) { return this.playerHashtable[name]; }
}

var players = new MediaPlayers();

function MediaStream(url, type, bandwidth, relOffset, startTime, qs)
{
	this.url = url;					//http://ms100.videotechnologies.com/smirror/ivt
	this.type = type;				//WM RM QT
	this.bandwidth = bandwidth;		//100000 200000 56000
	this.relOffset = relOffset;
	this.startTime = startTime;
	this.qs = qs;
}

function MediaStreams()
{
	this.mediaStreamArray = new Array();

	this.Add = function(url, type, bandwidth, relOffset, startTime, qs)
	{
		if (this.mediaStreamArray[type] == null) this.mediaStreamArray[type] = new Array();
		this.mediaStreamArray[type][this.mediaStreamArray[type].length] = new MediaStream(url, type, bandwidth, relOffset, startTime, qs);
		var a = this.mediaStreamArray[type];
		for (var i=0; i<a.length; i++) {
			for (var j=i+1; j<a.length; j++) {
				if (a[i].bandwidth < a[j].bandwidth) {
					var temp = a[j];
					a[j] = a[i];
					a[i] = temp;
				}
			}
		}
	}

	this.GetStreamCount         = function(type)                 { return this.mediaStreamArray[type].length; }
	this.GetFirstStreamPosition = function(type,bandwidth)       { return this.GetStreamPositionFromIndex(0,type,bandwidth); }
	this.GetNextStreamPosition  = function(index,type,bandwidth) { return this.GetStreamPositionFromIndex(index,type,bandwidth); }
	this.GetStream              = function(type,index)           { return this.mediaStreamArray[type][index]; }
	this.GetStreamURL           = function(type,index)           { return this.GetStream(type,index).url; }
	this.GetStreamRelOffset     = function(type,index)           { return this.GetStream(type,index).relOffset; }
	this.GetStreamStartTime     = function(type,index)           { return this.GetStream(type,index).startTime; }
	this.GetStreamQS            = function(type,index)           { return this.GetStream(type,index).qs; }

	this.GetStreamPositionFromIndex = function(index,type,bandwidth)
	{
		if (this.mediaStreamArray[type] == null) return -1;
		if (bandwidth==null) return index;
	
		for (var i=index;i<this.GetStreamCount(type);i++) {
			if(this.mediaStreamArray[type][i].bandwidth <= bandwidth)
				return i;
		}
		return this.GetStreamCount(type)-1;
	}
}


function VC_getWmpVersionObject()
{
	// Special case of Get Version used for getting the instantiated object
	// Force Netscape to use the 6.4 player because of known issues
	if (NS4  ||  is_ns5up)
		return 6;

	// Return WMP version according to detect.js
	return getWmpVersion();
}

function VC_getWmpVersion()
{
	// Return WMP version according to detect.js
	return getWmpVersion();
}

function VC_getQtHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean)
{
	//var src = g_RealVideoDefault;
	var src = "320";//Setting this to something other than "" seems to fix an occasional crashing issiue
	// If Netscape, return an <EMBED> tag for Real Media
	if (NS4  ||  is_ns5up) 
	{
		mpDebug("Creating NS/QT Player EMBED");
		return "" +
			"<EMBED enablejavascript='true'" +
			" id='" + sLabel + "-0' " +
			" name='" + sLabel + "-0' " +
			" width=" + w +
			" height=" + h +
			" CONTROLLER='" + showControls + "' " +
			" BGCOLOR='black' " +
			" AUTOSTART="+autoStart +
			" type='video/quicktime' " +
			" SRC='" + src + "' " +
			">" +
			"</EMBED>";
	}

	// Assume IE at this point and return the <OBJECT> tag
	mpDebug("Creating IE/QT Player OBJECT");
	return "" +
		"<OBJECT " +
		" classid=CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B " +
		" id='" + sLabel + "-0' " +
		" name='" + spLabel + "' " +
		" width=" + w +
		" height=" + h +
		">" +
		"	<PARAM NAME='controller' VALUE='"+showControls+"'>" +
		"	<PARAM NAME='type' VALUE='video/quicktime'>" +
		"	<PARAM NAME='autoplay' VALUE='"+autoStart+"'>" +
		"	<PARAM NAME='QTSRC' VALUE=''>" +
		"	<PARAM NAME='SRC' VALUE='" + src + "'>" +
		"	<PARAM NAME='BGCOLOR' VALUE='black'>" +
		"	<PARAM NAME='pluginspage' VALUE='http://www.apple.com/quicktime/download/indext.html'>" +
		"</OBJECT>";

}

function VC_setRealSize(sLabel)
{
	var obj = GetElement(sLabel + "-0");
	var div = GetElement(sLabel);

	if (obj == null) {
		mpDebug("Could not detect video width/height");
		return;
	}

	var w = obj.GetClipWidth();
	var h = obj.GetClipHeight();

	if (w < 1) {
		setTimeout("VC_setRealSize('" + sLabel + "')", 200);
		return;
	}

	mpDebug("Detected video width=" + w + ",height=" + h);	
	if (NS4 ) {
		div.width = w;
		div.height = h;
	}
	else {
		setTimeout("VC_setSize2('" + sLabel + "', " + w + ", " + h + ")", 500);
	}
}

function VC_setSize2(sLabel, w, h)
{
	if (!IE4) return;
	var div = GetElement(sLabel);
	div.style.width = w;
	div.style.height = h;
}

function VC_getRealHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean)
{
	//var src = unescape(Element(sLabel)["File Name"]);
	//var src = g_RealVideoDefault;
	var src = "";
	
	var controls = "IMAGEWINDOW";

	// Don't show IMAGEWINDOW if not enough room
	if (h <= 30  &&  (showControls || showStatusBar))
		controls = "";

	if (showControls) controls = controls + (controls.length > 0 ? ",":"") + "CONTROLPANEL";
	if (showStatusBar) controls = controls + (controls.length > 0 ? ",":"") + "POSITIONFIELD";

	if (autoSize) {
		if (!(NS4)) {
			setTimeout("VC_setRealSize('" + sLabel + "')", 1000);
		}
	}

	// If Netscape, return an <EMBED> tag for Real Media
	if (NS4  ||  is_ns5up) 
	{
		mpDebug("Creating NS/Real Player EMBED");
		if(is_ns5up)
		{
			if(autoSize)
			{
				w="100%";
				h="100%";
			}
		}
		return "" +
			"<EMBED SCRIPTCALLBACKS='OnAuthorChange'" +
			" id='Video1' " +
			" name='Video1' " +
			" width=" + w +
			" height=" + h +
			" CENTER='true' " +
			" CONTROLS='" + controls + "' " +
			" BACKGROUNDCOLOR='black' " +
			" AUTOGOTOURL='false' " +
			" NOLOGO='true' " +
			" PREFETCH='false' " +
			(isNaN(volume) ? "":" VOLUME='" + volume + "' ") +
			" AUTOSTART='true' " +
			" type='audio/x-pn-realaudio-plugin' " +
			" SRC='" + src + "' " +
			">" +
			"</EMBED>";
	}

	// Assume IE at this point and return the <OBJECT> tag
	mpDebug("Creating IE/Real Player OBJECT");
	return "" +
		"<OBJECT " +
		" classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA " +
		" id='Video1' " +
		" name='Video1' " +
		(autoSize ?
			" width=100%" +
			" height=100%"
			:
			" width=" + w +
			" height=" + h
		) +
		">" +
		"	<PARAM NAME='AUTOGOTOURL' VALUE='false'>" +
		"	<PARAM NAME='AUTOSTART' VALUE='false'>" +
		"	<PARAM NAME='SHUFFLE' VALUE='false'>" +
		"	<PARAM NAME='PREFETCH' VALUE='false'>" +
		(isNaN(volume) ? "":"	<PARAM NAME='VOLUME' VALUE='" + volume + "'>") +
		"	<PARAM NAME='NOLABELS' VALUE='false'>" +
		"	<PARAM NAME='SRC' VALUE='" + src + "'>" +
		"	<PARAM NAME='LOOP' VALUE='false'>" +
		"	<PARAM NAME='NUMLOOP' VALUE='false'>" +
		"	<PARAM NAME='BACKGROUNDCOLOR' VALUE='black'>" +
		"	<PARAM NAME='NOLOGO' VALUE='-1'>" +
		"	<PARAM NAME='CONTROLS' VALUE='" + controls + "'>" +
		(autoSize ?
			"	<PARAM NAME='MAINTAINASPECT' VALUE='true'>"
			:
			"	<PARAM NAME='CENTER' VALUE='true'>"
		) +
		"</OBJECT>";
}

function VC_getWmpHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean)
{
	// If WMP version < 9, return old 6.4 code for embedding the player
	if (VC_getWmpVersionObject() < 9) {

		// Netscape uses an <EMBED> tag
		if (NS4) {
			mpDebug("Creating NS/WMP6.4 EMBED");
			return "" +
				"<EMBED" +
				" type='video/x-ms-asf-plugin'" +
				" name='" + sLabel + "-0'" +
				" id='" + sLabel + "-0'" +
				" WIDTH=" + w +
				" HEIGHT=" + h +
				" pluginspage='http://www.microsoft.com/windows/mediaplayer/download/default.asp'" +
				" AutoStart='" + (autoStart ? "-1":"0") + "'" +
				" AutoSize='" + (autoSize ? "-1":"0") + "'" +
				" AllowChangeDisplaySize='-1'" +
				" AnimationAtStart='0'" +
				" ClickToPlay='0'" +
				" DisplaySize='" + (expandToFit ? "4":"0") + "'" +
				" EnableContextMenu='0'" +
				" Filename=''" +
				" SendOpenStateChangeEvents='-1'" +
				" SendPlayStateChangeEvents='-1'" +
				" ShowControls='" + (showControls ? "-1":"0") + "'" +
				" ShowStatusBar='" + (showStatusBar ? "-1":"0") + "'" +
				" TransparentAtStart='-1'" +
				(isNaN(volume) ? "":" Volume='" + (Math.round(5000*Math.log(volume/100)/2.30258509299)) + "'") +
				"></EMBED>";
		}

		// Assume IE at this point and use an <OBJECT> tag
		mpDebug("Creating IE/WMP6.4 OBJECT");
		return "" +
			"<OBJECT " +
			" classid=CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" +
			" codeBase='../mpie4ful.exe#Version=6,0,0,0'" +
			" id='" + sLabel + "-0'" +
			" name='" + sLabel + "-0'" +
			" width=" + w +
			" height=" + h + 
			" type='application/x-oleobject' " +
			" standby='Loading Microsoft Media Player components...'>" +
			"	<PARAM NAME='AudioStream' VALUE='-1'>" +
			"	<PARAM NAME='AnimationAtStart' VALUE='0'>" +
			"	<PARAM NAME='AllowScan' VALUE='-1'>" +
			"	<PARAM NAME='AllowChangeDisplaySize' VALUE='-1'>" +
			"	<PARAM NAME='AutoSize' VALUE='" + (autoSize ? "-1":"0") + "'>" +
			"	<PARAM NAME='AutoStart' VALUE='" + (autoStart ? "-1":"0") + "'>" +
			"	<PARAM NAME='Balance' VALUE='0'>" +
			"	<PARAM NAME='BaseURL' VALUE=''>" +
			"	<PARAM NAME='BufferingTime' VALUE='5'>" +
			"	<PARAM NAME='CaptioningID' VALUE=''>" +
			"	<PARAM NAME='ClickToPlay' VALUE='0'>" +
			"	<PARAM NAME='CursorType' VALUE='0'>" +
			"	<PARAM NAME='CurrentPosition' VALUE='0'>" +
			"	<PARAM NAME='CurrentMarker' VALUE='0'>" +
			"	<PARAM NAME='DefaultFrame' VALUE=''>" +
			"	<PARAM NAME='DisplayBackColor' VALUE='0'>" +
			"	<PARAM NAME='DisplayForeColor' VALUE='16777215'>" +
			"	<PARAM NAME='DisplayMode' VALUE='0'>" +
			(fullScreen ?
				"	<PARAM NAME='DisplaySize' VALUE='3'>"
				:
				(expandToFit ?
					"	<PARAM NAME='DisplaySize' VALUE='4'>"
					:
					"	<PARAM NAME='DisplaySize' VALUE='0'>"
				)
			) +
			"	<PARAM NAME='Enabled' VALUE='-1'>" +
			"	<PARAM NAME='EnableContextMenu' VALUE='0'>" +
			"	<PARAM NAME='EnablePositionControls' VALUE='-1'>" +
			"	<PARAM NAME='EnableFullScreenControls' VALUE='0'>" +
			"	<PARAM NAME='EnableTracker' VALUE='-1'>" +
			"	<PARAM NAME='Filename' VALUE=''>" +
			"	<PARAM NAME='InvokeURLs' VALUE='-1'>" +
			"	<PARAM NAME='Language' VALUE='-1'>" +
			"	<PARAM NAME='Mute' VALUE='0'>" +
			"	<PARAM NAME='PlayCount' VALUE='1'>" +
			"	<PARAM NAME='PreviewMode' VALUE='0'>" +
			"	<PARAM NAME='Rate' VALUE='1'>" +
			"	<PARAM NAME='SAMILang' VALUE=''>" +
			"	<PARAM NAME='SAMIStyle' VALUE=''>" +
			"	<PARAM NAME='SAMIFileName' VALUE=''>" +
			"	<PARAM NAME='SelectionStart' VALUE='0'>" +
			"	<PARAM NAME='SelectionEnd' VALUE='0'>" +
			"	<PARAM NAME='SendOpenStateChangeEvents' VALUE='-1'>" +
			"	<PARAM NAME='SendWarningEvents' VALUE='-1'>" +
			"	<PARAM NAME='SendErrorEvents' VALUE='-1'>" +
			"	<PARAM NAME='SendKeyboardEvents' VALUE='0'>" +
			"	<PARAM NAME='SendMouseClickEvents' VALUE='0'>" +
			"	<PARAM NAME='SendMouseMoveEvents' VALUE='0'>" +
			"	<PARAM NAME='SendPlayStateChangeEvents' VALUE='-1'>" +
			"	<PARAM NAME='ShowCaptioning' VALUE='0'>" +
			"	<PARAM NAME='ShowControls' VALUE='" + (showControls ? "-1":"0") + "'>" +
			"	<PARAM NAME='ShowStatusBar' VALUE='" + (showStatusBar ? "-1":"0") + "'>" +
			"	<PARAM NAME='ShowAudioControls' VALUE='-1'>" +
			"	<PARAM NAME='ShowDisplay' VALUE='0'>" +
			"	<PARAM NAME='ShowGotoBar' VALUE='0'>" +
			"	<PARAM NAME='ShowPositionControls' VALUE='-1'>" +
			"	<PARAM NAME='ShowTracker' VALUE='-1'>" +
			"	<PARAM NAME='TransparentAtStart' VALUE='-1'>" +
			"	<PARAM NAME='VideoBorderWidth' VALUE='0'>" +
			"	<PARAM NAME='VideoBorderColor' VALUE='0'>" +
			"	<PARAM NAME='VideoBorder3D' VALUE='0'>" +
			(isNaN(volume) ||  volume.length < 1 ? "":"	<PARAM NAME='Volume' VALUE='" + (Math.round(5000*Math.log(volume/100)/2.30258509299)) + "'>") +
//			"	<PARAM NAME='WindowlessVideo' VALUE='" + (fullScreen ? "0":"-1") + "'>" +
			"	<PARAM NAME='WindowlessVideo' VALUE='0'>" +
			"</OBJECT>";
	}

	// At this point, we know it is WMP 9 +
	

/*
	NOTE: With Netscape, WMP 9 has some serious issues... the code here would return the correct
	tags according to Microsoft's site, but for now, we force 6.4 player if user has Netscape.

	Newsgroups cover the issues with NS/WMP9, but here are the major issues that we don't have
	a good solution for:
	
		With NS4, the APPLET issues a security warning that user must respond to.
		With NS7, the APPLET requires JRE 1.3, but NS7 installs JRE 1.4 by default.
*/

	// Here we know IE / WMP9+
	var sParams = "";
	var sBegin = "";
	var sEnd = "";

	sParams = "" +
		" <PARAM name='EventPrefix' value='" + sLabelClean + "'>" +
		" <PARAM name='enableContextMenu' value='False'>" +
		" <PARAM name='invokeURLs' value='False'>" +
		" <PARAM name='autoStart' value='" + (autoStart ? "True":"False") + "'>" +
		" <PARAM name='stretchToFit' value='" + (expandToFit ? "True":"False") + "'>" +
		" <PARAM name='uiMode' value='" + (showControls ||  showStatusBar ? "mini":"none") + "'>" +
		" <PARAM name='volume' value='" + volume + "'>" +
		" <PARAM name='fullScreen' value='" + (fullScreen ? "True":"False") + "'>";
				
	if (NS4) {
		mpDebug("Creating NS/WMP9 APPLET");
		// Netscape uses an <APPLET> tag for WMP9
		sBegin = "" +
			"<APPLET " +
			" ID='" + sLabel + "-0' " +
			" NAME='" + sLabel + "-0' " +
			(autoSize ?
				""
				:
				" width='" + w + "' " +
				" height='" + h + "' "
			) +
			" CODE='WMPNS.WMP' " +
			" MAYSCRIPT>" +
			"	<PARAM NAME='EventPrefix' VALUE='" + sLabelClean + "'/>";

		sEnd = "</APPLET>";
	}
	else {
		mpDebug("Creating IE/WMP9 OBJECT");
		// IE still uses an <OBJECT> tag
		sBegin = "" +
			"<OBJECT " +
			" ID='" + sLabel + "-0' " +
			" NAME='" + sLabel + "-0' " +
 				" CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' " +
			(autoSize ? 
				""
				:
				" width='" + w + "' " +
				" height='" + h + "' "
			) +
			">";

		sEnd = "</OBJECT>";
	}
	return sBegin + sParams + sEnd;
}


function VC_getObjectHtml(type, sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean)
{
	if (type==-1) return "";
	if (type == RM) return VC_getRealHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean);
	if (type == WM) return VC_getWmpHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean);
	if (type == QT) return VC_getQtHtml(sLabel, w, h, autoStart, autoSize, expandToFit, showControls, showStatusBar, volume, fullScreen, spLabel, sLabelClean);
	
	return "Unknown Media Type";
}


function VC_CreateVideo(elemID,type)
{

	if (type == QT || type == RM)
	{
		var iframe_elem = document.createElement('iframe');
		iframe_elem.name = elemID+"-0-Frame";
		iframe_elem.id = elemID+"-0-Frame";
		iframe_elem.width=Element(elemID)["Width"];
		iframe_elem.height=Element(elemID)["Height"];
		iframe_elem.style.border=0;
		iframe_elem.frameBorder=0;
		iframe_elem.scrolling="no";
		
		var div_elem = document.getElementById(elemID);
		var children = div_elem.childNodes;
		for(var i=0;i<children.length;i++)
			div_elem.removeChild(children[i]);
		div_elem.appendChild(iframe_elem);
		w = window.frames[elemID+"-0-Frame"];

		w.document.open();
//		alert(GetCallbackScript("Video1")+"<BODY leftmargin=0 topmargin=0>"+Video_GetPlayerHTML(elemID,type)+"</BODY>")
		w.document.write(GetCallbackScript("Video1")+"<BODY leftmargin=0 topmargin=0>"+Video_GetPlayerHTML(elemID,type)+"</BODY>");
		w.document.close();
	}
	else
	{
		ReplaceElementInnerHTML(GetElement(elemID), Video_GetPlayerHTML(elemID,type));

	}
}

function getAkamaiTokenQS(event_id, cp_code, metafile_key)
{
	var t = Math.ceil(new Date().getTime());
	var key1 = calcMD5(t + cp_code + getCookieID() + event_id + metafile_key);
//	var key2 = calcMD5(t) + calcMD5(cp_code) + calcMD5(getCookieID()) + calcMD5(event_id) + calcMD5(metafile_key);

	return "usr=" + getCookieID() + "&cp_code=" + cp_code + "&time=" + t + "&event_id=" + event_id + "&key=" + key1;
}

function GetCallbackScript(name)
{
	//vbscript required for Real events in IE
	
	str = "" + 
	"<SCRIPT language='VBScript'>\n" +
	"Sub " + name + "_OnGotoURL(URL,target)\n" +
	"	parent.Video_onGoToURL URL,target\n" +
	"End Sub\n" +
	"Sub " + name + "_OnAuthorChange(author)\n" +
	"	parent.Video_onGoToURL unescape(unescape(author)),\"\"\n" +
	"End Sub\n" +
	"</SCRIPT>\n\n" +

	"<SCRIPT for='"+name+"' event='ScriptCommand(scType,scParam)' language=Javascript>\n" + 
	"if (parent.IE4) {\n" + 
	"//alert(scType+':'+scParam);\n" + 
	"	parent.OnDSScriptCommandEvt(scType,scParam);\n" + 
	"}\n" + 
	"</SCRIPT>\n" + 

	"<SCRIPT for='"+name+"' event='OnGotoURL(URL,target)' language=Javascript>\n" + 
	"if (parent.IE4) {\n" + 
	"//alert(URL+':'+target)\n" + 
	"//	parent.Video_onGoToURL(URL,target);\n" + 
	"}\n" + 
	"</SCRIPT>\n" + 

	"<SCRIPT for='"+name+"' event='OnPreFetchComplete()' language=Javascript>\n" + 
	"if (parent.IE4) {\n" + 
	"//alert('Prefetch')\n" + 
	"}\n" + 
	"</SCRIPT>\n" + 

	"<SCRIPT for='"+name+"' event='OnAuthorChange(author)' language=Javascript>\n" + 
	"if (parent.IE4) {\n" + 
	"//alert('Javascript:'+unescape(unescape(author)))\n" + 
	"//	parent.Video_onGoToURL(unescape(unescape(author)),'');\n" + 
	"}\n" + 
	"</SCRIPT>\n" + 

	"<SCRIPT language=Javascript>\n" + 
	"function "+name+"_OnAuthorChange(author)\n" + 
	"{\n" + 
	"//alert('AuthorVV'+unescape(author))\n" + 
	"	parent.Video_onGoToURL(unescape(author),'');\n" + 
	"}\n" +

	"function "+name+"_OnPreFetchComplete()\n" + 
	"{\n" + 
	"//alert('Prefetch Complete')\n" + 
	"}\n" +
	"</SCRIPT>\n";
	
	return str;

}
/*
	"function "+name+"_OnGotoURL(URL,target)\n" + 
	"{\n" + 
	"//alert('Cap'+URL+':'+target)\n" + 
	"	parent.Video_onGoToURL(unescape(URL),target);\n" + 
	"}\n" + 

	"function "+name+"_onGoToURL(URL,target)\n" + 
	"{\n" + 
	"//alert('Low'+URL+':'+target)\n" + 
	"	parent.Video_onGoToURL(unescape(URL),target);\n" + 
	"}\n" + 

*/
