﻿function Mediasite() { }

Mediasite.PresentationPlayStatus =
{
    NotAvailable: "NotAvailable",
    ScheduledForLive: "ScheduledForLive",
    OpenForLive: "OpenForLive",
    Live: "Live",
    LiveEnded: "LiveEnded",
    OnDemand: "OnDemand",
    Paused: "Paused"
}
Mediasite.ScriptCommand =
{
    Slide: 'S',
    EndPresentation: 'E',
    Pause: 'P',
    Resume: 'R'
}
Mediasite.ScriptID = "MS5";

function ServicesManager(playbackTicketId)
{
    this.PlaybackTicketId=playbackTicketId;
    this.OpenReported=false;
    this.CloseReported = false;
}
    
    
ServicesManager.prototype=
{
    
    ReportViewerPageOpened: function(playerType,playStatus)
	{
        if (this.OpenReported)
	    {
	        return;
	    }
	    	    	    
	    var callManager = new ReportingCallManager();	   
		SonicFoundry.Mediasite.Player.DataAccess.PlayerService.ReportViewerPageOpened
		(
			this.PlaybackTicketId,
			playerType,
			playStatus,
			Function.createDelegate(callManager, callManager.OnSuccess),
			Function.createDelegate(callManager, callManager.OnFailure),
			'ReportViewerPageOpened'
		);
	    
	    this.OpenReported = true;
	},

	ReportViewerPageClosed: function()
	{		    		
	
	
	    if (this.CloseReported || !this.OpenReported)
	    {
	        return;
	    }
	    
	    this.CloseReported=true;
	    
 		var syncRequest = new Sys.Net.WebRequest();
 		syncRequest.set_url(String.format("{0}/ReportViewerPageClosed?playbackTicketId=%22{1}%22",SonicFoundry.Mediasite.Player.DataAccess.PlayerService.get_path(),this.PlaybackTicketId));
        syncRequest.set_httpVerb("GET");
        syncRequest.get_headers()["Content-Type"] = "application/json; charset=utf-8";        
        syncRequest.set_executor(new ServicesManager.XMLHttpSyncExecutor());        
        syncRequest.invoke();

    },
	
	CheckPresentationStatusFromDB:function(callbackDelegate)
    {

        var callManager = new StatusCheckCallManager(callbackDelegate);
		
        SonicFoundry.Mediasite.Player.DataAccess.PlayerService.GetLiveStatus
            (
                  Manifest.PresentationId,
                  Function.createDelegate(callManager, callManager.OnSuccess),
			      Function.createDelegate(callManager, callManager.OnFailure),
                  'GetLiveStatus'
            );
    },


	
	Sleep:function (naptime)
	{
      var sleeping = true;
      var now = new Date();
      var alarm;
      var startingMSeconds = now.getTime();

      while(sleeping)
      {
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > naptime)
         { 
            sleeping = false; 
         }
      }      
   }

}

function StatusCheckCallManager(callbackDelegate)
{
    this.CallbackDelegate = callbackDelegate;
    
    StatusCheckCallManager.prototype.OnSuccess = function(result, context)
    {
        if (result.PlayStatus == Mediasite.PresentationPlayStatus.ScheduledForLive)
        {
            Manifest.PlayStatus = Mediasite.PresentationPlayStatus.ScheduledForLive;
        }
        else if (result.PlayStatus == Mediasite.PresentationPlayStatus.OpenForLive)
        {
            Manifest.PlayStatus = Mediasite.PresentationPlayStatus.OpenForLive;
        }
        else if (result.PlayStatus == Mediasite.PresentationPlayStatus.Live)
        {
            Manifest.PlayStatus = Mediasite.PresentationPlayStatus.Live;
        }

        if (this.CallbackDelegate)
        {
            this.CallbackDelegate();
        }
        
    }

    StatusCheckCallManager.prototype.OnFailure = function(error, context)
    {
        if (this.CallbackDelegate)
        {
            this.CallbackDelegate();
        }
    }
}

function ReportingCallManager()
{	
	ReportingCallManager.prototype.OnSuccess = function(result, context)
	{
	}

	ReportingCallManager.prototype.OnFailure = function(error, context)
	{
	    //need to eat abort errors
	}
}

ServicesManager.XMLHttpSyncExecutor = function ServicesManager$XMLHttpSyncExecutor() {

    ServicesManager.XMLHttpSyncExecutor.initializeBase(this);
}

    function ServicesManager$XMLHttpSyncExecutor$executeRequest() {
        if (arguments.length !== 0) throw Error.parameterCount();
        this._webRequest = this.get_webRequest();

        if (this._started) {
            throw Error.invalidOperation(String.format(Sys.Res.cannotCallOnceStarted, 'executeRequest'));
        }
        if (this._webRequest === null) {
            throw Error.invalidOperation(Sys.Res.nullWebRequest);
        }

        var body = this._webRequest.get_body();
        var headers = this._webRequest.get_headers();
        this._xmlHttpRequest = new XMLHttpRequest();
        this._xmlHttpRequest.onreadystatechange = this._onReadyStateChange;

        var verb = this._webRequest.get_httpVerb();        
        this._xmlHttpRequest.open(verb, this._webRequest.getResolvedUrl(), false /*SYNC*/);
        if (headers) {
            for (var header in headers) {
                var val = headers[header];
                if (typeof(val) !== "function")
                    this._xmlHttpRequest.setRequestHeader(header, val);
            }
        }

        if (verb.toLowerCase() === "post") {
            // If it's a POST but no Content-Type was specified, default to application/x-www-form-urlencoded
            if ((headers === null) || !headers['Content-Type']) {
                this._xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            }

            // DevDiv 15893: If POST with no body, default to ""(FireFox needs this)
            if (!body) {
                body = "";
            }
        }

        this._xmlHttpRequest.send(body);
        this._started = true;
    }   

ServicesManager.XMLHttpSyncExecutor.prototype = {

    executeRequest: ServicesManager$XMLHttpSyncExecutor$executeRequest
}
ServicesManager.XMLHttpSyncExecutor.registerClass('ServicesManager.XMLHttpSyncExecutor', Sys.Net.XMLHttpExecutor);

