
// Copyright (c) 2003 Sonic Foundry, Inc. and Sonic Foundry 
// Media Systems, Inc. Neither this code nor any portion 
// thereof may be reproduced, altered, or otherwise changed, 
// distributed or copied, without the express written 
// permission of Sonic Foundry.  
// All rights reserved.

// BEGINFILE constants.js --------------------------------------------------------------------------->

var PlayState =
{
	Undefined:		0,
    Stopped:        1,
    Paused:         2,
    Playing:        3,
    ScanForward:    4,
    ScanReverse:    5,
    Buffering:		6,
    Waiting:		7,
    MediaEnded:		8,
    Transitioning:	9,
    Ready:			10

}

var OpenState =
{
	Undefined:					0,
	PlaylistChanging:			1,
	PlaylistLocating:			2,
	PlaylistConnecting:			3,
	PlaylistLoading:			4,
	PlaylistOpening:			5,
	PlaylistOpenNoMedia:		6,
	PlaylistChanged:			7,
	MediaChanging:				8,
	MediaLocating:				9,
	MediaConnecting:			10,
	MediaLoading:				11,
	MediaOpening:				12,
	MediaOpen:					13,
	BeginCodecAcquisition:		14,
	EndCodecAcquisition:		15,
	BeginLicenseAcquisition:	16,
	EndLicenseAcquisition:		17,
	BeginIndividualization:		18,
	EndIndivididualization:		19,
	MediaWaiting:				20,
	OpeningUnknownURL:			21
}
	


function GetPlayStateName(state)
{
	switch (state)
	{
		case PlayState.Undefined:
			return "Undefined";
		case PlayState.Stopped:
			return "Stopped";
		case PlayState.Paused:
			return "Paused";
		case PlayState.Playing:
			return "Playing";
		case PlayState.ScanForward:
			return "ScanForward";
		case PlayState.ScanReverse:
			return "ScanReverse";
		case PlayState.Buffering:
			return "Buffering";
		case PlayState.Waiting:
			return "Waiting";
		case PlayState.MediaEnded:
			return "MediaEnded";
		case PlayState.Transitioning:
			return "Transitioning";
		case PlayState.Ready:
			return "Ready";
		default:
			return "Uknown State Type";
	}
}

// ENDFILE constants.js ----------------------------------------------------------------------------->

// BEGINFILE ScriptParse.js ------------------------------------------------------------------------->

function ScriptParser()
{
	this.m_debugLevel = SfDebug.Verbose;
//	this.m_debugLevel = SfDebug.Information;

	function ScriptParser.prototype.ParseScriptFromStream(sType, sParam)
	{
		this.Debug("ParseScriptFromStream called: ScriptType: " + sType + ", ScriptParam: " + sParam);
		switch(sType)
		{
			case "MSLiveEvent":
//				alert('this eventtype MSLiveEvent is not supported');
//				break;
			case "MSL":
				eval("this." + sParam + "");
				break;
		}
	}

	// this is the one which notifies to all interested Areas
	function ScriptParser.prototype.NotifyScriptEvent(oArgs)
	{
		this.Debug("NotifyScriptCommand called");
		MainHelper.EventScript.Post(oArgs);
	}
	
	function ScriptParser.prototype.Debug(str)
	{
		SfDebug.DPF(this.m_debugLevel, "ScriptParser: " + str);
	}
	
	
	// new event stuff below this------------------------------------------------------------------------
	// HE('EventType', Param1, Param2...);
	// this function is directly called by the mac player
	function ScriptParser.prototype.HE()
	{
		this.Debug("HE called");
		if (arguments.length < 1)
		{
			SfDebug.DPF(SfDebug.ErrMsgCritical, "The event must be of the form " + 
				"HE('EventType', [args....])");
			return;
		}
		
		var command = arguments[0];
		switch (command)
		{
			case "S":
				if (arguments.length < 2)
				{
					SfDebug.DPF(SfDebug.ErrMsgCritical, "ShowSlide wrong arguments: " + arguments);	
					return;
				}
				var slideNumber = Number(arguments[1]);
				var args = MainHelper.CreateShowSlideEventArgs(slideNumber);
				if (MainHelper.MaxSlideTimings < slideNumber)
				{
					MainHelper.KeepAddingToSlideTimings(args);
					MainHelper.DynamicAdd = true;
				}
				else
				{
					MainHelper.DynamicAdd = false;
				}
				
				this.NotifyScriptEvent(args);
				break;
			case "E":
				var args = new Object();
				args.Command = SfScriptCommandType.EndPresentation;
				this.NotifyScriptEvent(args);
				break;
		}
	} 

}

// ENDFILE ScriptParse.js --------------------------------------------------------------------------->


// BEGINFILE PlayerArea.js -------------------------------------------------------------------------->

PlayerArea.prototype = new AreaBase();
function PlayerArea(container, containingWindow, ID)
{
	this.m_debugLevel = SfDebug.Verbose;
	this.PlayerType = "Unknown"; // should override in derived classs
	this.StatusCheckInterval = 5000; // how long to wait?
	this.ScriptParser = new ScriptParser();
	
	function PlayerArea.prototype.Debug(str)
	{
		SfDebug.DPF(this.m_debugLevel, "PlayerArea: PlayerType = " + this.PlayerType + ": " + str);
	}
	
	function PlayerArea.prototype.OnLoad()
	{
		this.Debug("OnLoad called");
		// delegate to the other method because
		// we need to override onload for 
		// the playerarea for mac
		this.DoOnLoad();
	}
	
	function PlayerArea.prototype.DoOnLoad()
	{
		this.Debug("DoOnLoad called");
//		this.ResizeWindow();
		this.CheckPresentationStatus();
	}
	
	function PlayerArea.prototype.CheckPresentationStatus()
	{
		this.Debug("CheckPresentationStatus called");
		if (MainHelper.Presentation.IsStandAlone == true)
		{
			this.StartPlaying();
			return;
		}
		var status = MainHelper.Presentation.Status;
		switch (status)
		{
			case PresentationStatus.CaptureInProgress:
			case PresentationStatus.ReplayReady:
				this.StartPlaying();
				return;
			case PresentationStatus.Offline:
				return;
			case PresentationStatus.Locked:
				alert('This presentation is locked and can not be viewed');
				break;
			case PresentationStatus.Test:
			case PresentationStatus.NotReady:
			case PresentationStatus.CaptureReady:
				this.DoNotReadyStuff();
				return;
			default:
				alert('Unknown Presentation Status');
				return;
		}
		
	}
	
	function PlayerArea.prototype.GetEmbeddedPlayer()
	{
		var embeddedPlayer = SfDOM.FindElementFromID(document, "EmbeddedPlayer");
		if (!embeddedPlayer)
		{
			alert("Could not find embedded player");
		}
		return embeddedPlayer;
	}
	
	function PlayerArea.prototype.DoNotReadyStuff()
	{
		var image = SfDOM.FindElementFromID(document, "PlayerPresentationNotStarted");
		if (image)
		{
			image.style.display = '';
			var embeddedPlayer = this.GetEmbeddedPlayer();
			if (embeddedPlayer)
			{
				embeddedPlayer.style.display = 'none';
			}
			else
			{
				this.Debug("embedded player not found");
			}
		}
		else
		{
			this.Debug("not started image not found");
		}
		this.KeepChecking();
	}
	
	this.AttemptNumber = 1;
	function PlayerArea.prototype.KeepChecking()
	{
		this.Debug("KeepChecking called , attempt: " + this.AttemptNumber);
		var imageSource = Util.GetDocumentBase() + 
			"GetDummyImage.aspx?PresentationID=" + MainHelper.Presentation.PresentationID 
			+ "&AttemptNumber=" + this.AttemptNumber 
			+ "&Password=" + MainHelper.Presentation.TestPassword 
			+ "&Random=" + Math.random();

		this.img = document.createElement("img");
		this.img.style.display = 'none';
		this.GetDiv().appendChild(this.img);

		this.img.onerror = new Function("", this.Container + ".NotReady();");
		this.img.onload = new Function("", this.Container + ".Ready();");
		this.img.src = imageSource;
		++this.AttemptNumber;
	}
	
	function PlayerArea.prototype.NotReady()
	{
		this.Debug("NotReady called");
		this.Timer = setTimeout(this.Container + ".KeepChecking()", this.StatusCheckInterval);
	}
	
	function PlayerArea.prototype.StartPlaying()
	{
		this.Debug("StartPlaying called");
		
		this.SetupPlayer();
	}
	
	function PlayerArea.prototype.Ready()
	{
		this.Debug("Ready called");
		if (this.Timer)
		{
			clearTimeout(this.Timer);
		}
		var image = SfDOM.FindElementFromID(document, "PlayerPresentationNotStarted");
		if (image)
		{
			image.style.display = 'none';
		}
		var embeddedPlayer = this.GetEmbeddedPlayer(document, "EmbeddedPlayer");
		if (embeddedPlayer)
		{
			embeddedPlayer.style.display = '';
		}
		else
		{
			this.Debug("Embedded player not found");
		}
		
		MainHelper.Presentation.Status = PresentationStatus.CaptureInProgress;
		MainHelper.EventDataAvailable.Post(new SfEventArgs());
		this.StartPlaying();
	}
	
	function PlayerArea.prototype.ResizeWindow()
	{
		this.Debug("ResizeWindow called: ViewerWidth: " + this.ViewerWidth + ", ViewerHeight: " + this.ViewerHeight);
		if (this.ViewerWidth == -1 || this.ViewerHeight == -1)
		{
			this.Debug("no resize needed");
			return;
		}
		var extraWidth = MainHelper.GetFrameExtraWidth();
		var extraHeight = MainHelper.GetFrameExtraHeight();
		window.resizeTo(this.ViewerWidth + extraWidth, this.ViewerHeight + extraHeight);		
	}
}

// ENDFILE PlayerArea.js ---------------------------------------------------------------------------->



