
MediaPlayerArea = function(container, containingWindow, id)
{
    this.ID = id;
    this.Container = container;

    this.ScriptParser = new SfMediaPlayer.ScriptParser();
    this.Volume = null;
    this.timerManager = null;
    this.partitionManager = null;
    this.embeddedPlayer = null;
    this.pci = null;

    this.ScheduledForLiveCheckInterval = 15000;
    this.OpenForLiveCheckInterval = 5000;
    this.SkipAmount = 3000;
    this.StopTime = null;
    this.currentMediaState = -1;
    this.currentMediaDuration = 0;
    this.LastSeekPosition = 0;
    this.LastMediaError = null;

    this.OnLoad = MediaPlayerArea.prototype.OnLoad = function()
    {
        this.partitionManager = new SfMediaPlayer.PartitionManager(Manifest.Slides);
        this.CheckPresentationStatus();
    }

    this.AddEventHandlers = function()
    {
        mPlayer.EventManager.Events.addHandler(SfKernel.EventType.Script, this.ScriptEventHandler.bind(this));
        mPlayer.EventManager.Events.addHandler(SfKernel.EventType.SliderNotify, this.SliderNotifyEventHandler.bind(this));

        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.Play, this.PlayEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.Pause, this.PauseEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.Stop, this.StopEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.Mute, this.MuteEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.SetVolume, this.SetVolumeEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.NavigateToSlide, this.NavigateToSlideEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.NavigateToTime, this.NavigateToTimeEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.NavigateToChapter, this.NavigateToChapterEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.SkipBack, this.SkipBackEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.SkipForward, this.SkipForwardEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.FullScreen, this.FullScreenEventHandler.bind(this));
        mPlayer.EventManager.CommandEvents.addHandler(SfKernel.CommandEventId.ChangePlaybackSpeed, this.ChangePlaybackSpeedEventHandler.bind(this));
    }

    this.CheckPresentationStatus = function()
    {
        var status = Manifest.PlayStatus;

        switch (status)
        {
            case SfKernel.PresentationPlayStatus.Live:
                this.StartLivePlayback(Manifest.Slides.length);
                return;
            case SfKernel.PresentationPlayStatus.OnDemand:
                this.StartPlaying();
                return;
            case SfKernel.PresentationPlayStatus.LiveEnded:
                return;
            case SfKernel.PresentationPlayStatus.ScheduledForLive:
            case SfKernel.PresentationPlayStatus.OpenForLive:
                this.DoNotReadyStuff();
                return;
            default:
                Sys.Debug.trace('Unknown Presentation Status: ' + status);
                return;
        }
    }

    this.DoNotReadyStuff = function()
    {
        this.HideMediaPlayerDiv();

        var status = Manifest.PlayStatus;
        if (status == SfKernel.PresentationPlayStatus.ScheduledForLive)
        {
            window.setTimeout(this.Container + ".CheckPresentationStatusFromDB()", this.ScheduledForLiveCheckInterval);
        }
        else if (status == SfKernel.PresentationPlayStatus.OpenForLive)
        {
            window.setTimeout(this.Container + ".CheckPresentationStatusFromDB()", this.OpenForLiveCheckInterval);
        }
        else
        {
            Sys.Debug.trace('Invalid status for a live presentation: ' + status);
        }
    }

    this.CheckPresentationStatusFromDB = function()
    {
        SonicFoundry.Mediasite.Player.DataAccess.PlayerService.GetLiveStatus
		(
			Manifest.PresentationId,
			Function.createDelegate(this, this.GetPresentationStatusInformationOnSuccess),
			Function.createDelegate(this, this.GetPresentationStatusInformationOnFailure),
			'GetLiveStatus'
		);
    }

    this.GetPresentationStatusInformationOnSuccess = function(result, context)
    {
        if (result.PlayStatus == SfKernel.PresentationPlayStatus.ScheduledForLive)
        {
            window.setTimeout(this.Container + ".CheckPresentationStatusFromDB()", this.ScheduledForLiveCheckInterval);
        }
        else if (result.PlayStatus == SfKernel.PresentationPlayStatus.OpenForLive)
        {
            window.setTimeout(this.Container + ".CheckPresentationStatusFromDB()", this.OpenForLiveCheckInterval);
        }
        else if (result.PlayStatus == SfKernel.PresentationPlayStatus.Live)
        {
            Manifest.PlayStatus = SfKernel.PresentationPlayStatus.Live;
            this.StartLivePlayback(result.CurrentSlide);
        }
    }

    this.GetPresentationStatusInformationOnFailure = function(error, context)
    {
        Sys.Debug.trace("GetPresentationStatusInformationOnFailure(): " + error.Message);
        window.setTimeout(this.Container + ".CheckPresentationStatusFromDB()", this.ScheduledForLiveCheckInterval);
    }

    this.StartLivePlayback = function(currentSlideNumber)
    {
        this.AddSlideTimingsIfRequired(currentSlideNumber);
        this.HandleLiveStatusDisplay(Manifest.PlayStatus);
        mPlayer.EventManager.PostEvent(SfKernel.EventType.LivePlaybackStarted, this, null);
        this.StartPlaying();
    }

    this.HandleMediaPlayerError = function(errorMsg)
    {
        if (mPlayer.PresentationEnded)
        {
            return;
        }

        if (Manifest.PlayStatus == SfKernel.PresentationPlayStatus.Live)
        {
            this.LastMediaError = errorMsg;

            SonicFoundry.Mediasite.Player.DataAccess.PlayerService.GetLiveStatus
		    (
			    Manifest.PresentationId,
			    Function.createDelegate(this, this.CheckLiveErrorOnSuccess),
			    Function.createDelegate(this, this.GetPresentationStatusInformationOnFailure),
			    'GetLiveStatus'
		    );
        }
        else
        {
            this.ShowMediaErrorDialog(errorMsg);
        }
    }

    this.CheckLiveErrorOnSuccess = function(result, context)
    {
        if (result.PlayStatus != SfKernel.PresentationPlayStatus.Live)
        {
            mPlayer.EventManager.PostEvent(SfKernel.EventType.Script, this, { Command: SfKernel.ScriptCmdType.EndPresentation });
        }
        else
        {
            this.ShowMediaErrorDialog(this.LastMediaError);
        }
    }

    this.ShowMediaErrorDialog = function(errorMsg)
    {
        var presentationFailureErrorDialog = new DialogBox('mediaErrorDialog');
        presentationFailureErrorDialog.Message = errorMsg;
        presentationFailureErrorDialog.Title = 'Mediasite Player Error';
        presentationFailureErrorDialog.Id = 'mediaErrorDialog';
        presentationFailureErrorDialog.okButton = true;
        presentationFailureErrorDialog.OnLoad();
        return false;
    }

    this.ShowMediaPlayerDiv = function()
    {
        var playerElement = $('PlayerVideo');
        if (playerElement)
        {
            playerElement.style.display = '';
        }

        var notReady = $('PlayerNotStarted');
        if (notReady)
        {
            notReady.style.display = 'none';
        }
    }

    this.HideMediaPlayerDiv = function()
    {
        var playerElement = $('PlayerVideo');
        if (playerElement)
        {
            playerElement.style.height = '1px';
        }

        var notReady = $('PlayerNotStarted');
        if (notReady)
        {
            notReady.style.display = '';
        }
    }

    //	this.HideVideo = function()
    //	{
    //	    var playerElement = $('PlayerVideo');
    //	    if(playerElement)
    //	    {
    //	        
    //	    }
    //	}

    this.HandleAudioOnly = function()
    {
        if (!Manifest.HasVideo)
        {
            var fullScreenButton = $('btnFullScreen');
            if (fullScreenButton)
            {
                fullScreenButton.style.display = 'none';
            }

            var embeddedPlayer = $('EmbeddedPlayer');
            embeddedPlayer.style.height = '0px';
            var audioOnlyImage = $('PlayerAudioOnly');
            if (!this.IsCompact())
            {
                var videoContainer = $('VideoContainer');
                var playerElement = $('PlayerVideo');
                var presentationCard = $('PresentationCardArea');
                var presentationCardScrollDiv = $('PresentationCardAreaScrollDiv');
                var playerContainer = $('PlayerContainer');
                if (audioOnlyImage)
                {
                    audioOnlyImage.style.display = 'none';
                }
                videoContainer.style.height = '0px';
                playerElement.style.height = '0px';
                var currentPCHeight = presentationCard.offsetHeight;
                var currentPCTop = presentationCard.offsetTop;
                var currentMediaTop = playerContainer.offsetTop;
                var videoContainerLeft = videoContainer.offsetLeft;

                var titleBannerHeight = $('TitleBanner').offsetHeight;
                var mediaPlayerHeight = $('CommandBar').offsetHeight + $('StatusBar').offsetHeight + $('PlayerControls').offsetHeight;
                var newPCHeight = (LayoutOptions.PlayerHeight - mediaPlayerHeight - titleBannerHeight - 10);
                presentationCard.style.height = (newPCHeight) + "px";
                presentationCardScrollDiv.style.height = ((newPCHeight) - 12) + "px";

                if (LayoutOptions.SlideHeight == 0 || LayoutOptions.SlideWidth == 0)
                {
                    presentationCard.style.left = (videoContainerLeft + 3) + 'px';
                    presentationCard.style.width = (LayoutOptions.PlayerWidth - 8) + 'px';
                }

                if (LayoutOptions.DefaultPosition == '1' || LayoutOptions.DefaultPosition == '2')
                {
                    presentationCard.style.top = (titleBannerHeight + mediaPlayerHeight + 6) + "px";
                }
                if (LayoutOptions.DefaultPosition == '3' || LayoutOptions.DefaultPosition == '4')
                {
                    playerContainer.style.top = (titleBannerHeight + newPCHeight + 35) + "px";
                }
            }
            else
            {
                if (audioOnlyImage)
                {
                    audioOnlyImage.style.display = 'block';
                }
            }
        }
    }

    this.IsCompact = function()
    {
        var isCompact = (LayoutOptions.VideoWidth == '400' && LayoutOptions.SlideWidth == '360') ? true : false;
        return isCompact;
    }

    this.HandleLiveStatusDisplay = function(status)
    {
        var liveIndicator = $('LiveIndicatorAreaImg');
        if (!liveIndicator || Manifest.PlayStatus == SfKernel.PresentationPlayStatus.OnDemand)
        {
            return;
        }

        switch (status)
        {
            case SfKernel.PresentationPlayStatus.Live:
                liveIndicator.style.display = '';
                liveIndicator.setAttribute('title', Localization.LiveIndicatorResource.LiveTooltip);
                liveIndicator.setAttribute('alt', Localization.LiveIndicatorResource.LiveTooltip);
                liveIndicator.src = LayoutOptions.ThemeImageBase + '/liveIndicator.gif';
                break;
            case SfKernel.PresentationPlayStatus.Paused:
                liveIndicator.style.display = '';
                liveIndicator.setAttribute('title', Localization.LiveIndicatorResource.PausedTooltip);
                liveIndicator.setAttribute('alt', Localization.LiveIndicatorResource.PausedTooltip);
                liveIndicator.src = LayoutOptions.ThemeImageBase + '/liveIndicator_paused.gif';
                break;
            default:
                liveIndicator.style.display = 'none';
                break;
        }
    }

    this.AddSlideTimingsIfRequired = function(currentSlideNumber)
    {
        if (currentSlideNumber == -1)
        {
            return;
        }
        mPlayer.KeepAddingToSlideTimings(currentSlideNumber);
    }

    this.StartPlaying = function()
    {
        this.SetupPlayer();
        this.ShowMediaPlayerDiv();
    }

    this.CheckIfLiveEnded = function(playState)
    {
        if (Manifest.PlayStatus != SfKernel.PresentationPlayStatus.Live)
        {
            return;
        }

        if (playState == SfKernel.MediaState.Stopped || playState == SfKernel.MediaState.Paused)
        {
            SonicFoundry.Mediasite.Player.DataAccess.PlayerService.GetLiveStatus
		    (
			    Manifest.PresentationId,
			    Function.createDelegate(this, this.LiveStatusCheckStopOnSuccess),
			    Function.createDelegate(this, this.GetPresentationStatusInformationOnFailure),
			    'GetLiveStatus'
		    );
        }

    }

    this.LiveStatusCheckStopOnSuccess = function(result, context)
    {
        if (result.PlayStatus != SfKernel.PresentationPlayStatus.Live)
        {
            mPlayer.EventManager.PostEvent(SfKernel.EventType.Script, this, { Command: SfKernel.ScriptCmdType.EndPresentation });
        }
    }

    this.LiveStatusCheckStartOnSuccess = function(result, context)
    {
        if (result.PlayStatus != SfKernel.PresentationPlayStatus.Live)
        {
            mPlayer.EventManager.PostEvent(SfKernel.EventType.Script, this, { Command: SfKernel.ScriptCmdType.EndPresentation });
        }
        else
        {
            if (result.CurrentSlide > 0 && result.CurrentSlide > mPlayer.CurrentSlideNumber)
            {
                for (var i = mPlayer.CurrentSlideNumber; i < result.CurrentSlide; i++)
                {
                    this.ScriptParser.HE('S', i + 1);
                }
            }
        }
    }

    this.AdjustSize = function()
    {
        $('PlayerVideo').style.width = LayoutOptions.VideoWidth + 'px';
        $('PlayerVideo').style.height = LayoutOptions.VideoHeight + 'px';
        $('VideoContainer').style.width = LayoutOptions.VideoWidth + 'px';
        $('VideoContainer').style.height = LayoutOptions.VideoHeight + 'px';

        if ($('EmbeddedPlayer'))
        {
            $('EmbeddedPlayer').style.width = LayoutOptions.VideoWidth + 'px';
            $('EmbeddedPlayer').style.height = LayoutOptions.VideoHeight + 'px';
        }
    }

    this.InitVolume = function()
    {
        var initVolume = new MediasitePlayerCookie().GetValue("Volume");
        if (initVolume == undefined)
        {
            initVolume = 50;
        }
        this.Volume.SetVolume(initVolume);
        this.Volume.InitializeVolume(initVolume);
    }

    this.CheckStartStopTimes = function()
    {
        if (Manifest.PlayStatus == SfKernel.PresentationPlayStatus.OnDemand)
        {
            var playFrom = SfKernel.GetQueryStringValue("playfrom");
            var duration = SfKernel.GetQueryStringValue("duration");
            if (playFrom != null)
            {
                this.SetStartTime(Number(playFrom));
                if (duration != null)
                {
                    this.StopTime = Number(playFrom) + Number(duration);
                }
            }
            else if (duration != null)
            {
                this.StopTime = Number(duration);
            }

            var autoStart = SfKernel.GetQueryStringValue("autostart");

            if (autoStart != null && autoStart.toLowerCase() == "false")
            {
                this.Pause();
            }

        }
    }

    this.PostMediaLengthObtainedEvent = function()
    {
        this.currentMediaDuration = this.pci.GetMediaDuration();
        mPlayer.EventManager.PostEvent(SfKernel.EventType.MediaLengthObtained, this, { Left: 0, Right: this.currentMediaDuration });
    }

    this.PlayEventHandler = function(sender, args)
    {
        this.Play();
    }

    this.PauseEventHandler = function(sender, args)
    {
        this.Pause();
    }

    this.StopEventHandler = function(sender, args)
    {
        this.Stop();
    }

    this.SkipBackEventHandler = function(sender, args)
    {
        this.SkipInVideo(this.SkipAmount * -1);
    }
    this.SkipForwardEventHandler = function(sender, args)
    {
        this.SkipInVideo(this.SkipAmount);
    }

    this.SkipInVideo = function(SkipInMS)
    {
        var position = this.pci.GetPosition();

        if (position < this.LastSeekPosition)
        {
            position = this.LastSeekPosition;
        }

        var newPosition = position += SkipInMS;

        if (newPosition < 0)
        {
            newPosition = 0;
        }
        else if (newPosition > this.pci.GetMediaDuration())
        {
            return;
        }

        this.SetStartTime(newPosition);
    }

    this.MuteEventHandler = function(sender, args)
    {
        this.Mute();
    }

    this.SetVolumeEventHandler = function(sender, args)
    {
        this.SetVolume(args.Volume);
    }

    this.NavigateToSlideEventHandler = function(sender, args)
    {
        this._NavigateToSlide(args.SlideNumber);
    }

    this.NavigateToTimeEventHandler = function(sender, args)
    {
        this._NavigateToTime(args.Time);
    }

    this.NavigateToChapterEventHandler = function(sender, args)
    {
        this._NavigateToChapter(args.Number, args.Time);
    }

    this.FullScreenEventHandler = function(sender, args)
    {
        this.FullScreen();
    }

    this.ChangePlaybackSpeedEventHandler = function(sender, args)
    {
        this.pci.SetPlaybackSpeed(args["PlaybackSpeed"]);
    }

    this.ScriptEventHandler = function(sender, args)
    {
        switch (args.Command)
        {
            case SfKernel.ScriptCmdType.Pause:
                this.HandleLiveStatusDisplay(SfKernel.PresentationPlayStatus.Paused);
                break;
            case SfKernel.ScriptCmdType.Resume:
                this.HandleLiveStatusDisplay(SfKernel.PresentationPlayStatus.Live);
                break;
            case SfKernel.ScriptCmdType.EndPresentation:
                if (Manifest.PlayStatus == SfKernel.PresentationPlayStatus.Live)
                {
                    Manifest.PlayStatus = SfKernel.PresentationPlayStatus.LiveEnded
                    this.HandleLiveStatusDisplay(Manifest.PlayStatus);
                }
                mPlayer.PresentationEnded = true;
                mPlayer.CurrentSlideNumber = -1;
                this.Stop();
                break;
        }
    }

    this.SliderNotifyEventHandler = function(sender, objNotify)
    {
        switch (objNotify.NotifyType)
        {
            case SfKernel.SliderNotifyType.NewPosition:
                this.SetStartTime(objNotify.Position);
                this.StopTime = null;
                var state = this.pci.GetPlayState();
                if (state != SfKernel.MediaState.Playing && state != SfKernel.MediaState.Paused)
                {
                    mPlayer.EventManager.PostCommandEvent(SfKernel.CommandEventId.Play, this, null);
                }
                break;
        }
    }

    this.Play = function()
    {
        var currentPos = this.pci.GetPosition();
        if (currentPos == 0 && Manifest.PlayStatus == SfKernel.PresentationPlayStatus.OnDemand)
        {
            mPlayer.CurrentSlideNumber = -1;
            mPlayer.EventManager.PostEvent(SfKernel.EventType.PlayingFromBeginning, this, null);
        }

        if (Manifest.PlayStatus == SfKernel.PresentationPlayStatus.Live)
        {
            SonicFoundry.Mediasite.Player.DataAccess.PlayerService.GetLiveStatus
		    (
			    Manifest.PresentationId,
			    Function.createDelegate(this, this.LiveStatusCheckStartOnSuccess),
			    Function.createDelegate(this, this.GetPresentationStatusInformationOnFailure),
			    'GetLiveStatus'
		    );
		    
            this.SetMediaSource();
        }

        this.pci.Play();
        this.timerManager.Start();
        mPlayer.PresentationEnded = false;
    }

    this.Pause = function()
    {
        this.pci.Pause();
        this.timerManager.Stop();
    }

    this.Stop = function()
    {
        this.pci.Stop();
        this.timerManager.Stop();
        this.StopTime = null;
        mPlayer.SamiDropDownPanelInstance.Reset();
        this.LastSeekPosition = 0;
    }

    this.Mute = function()
    {
        if (this.Volume)
        {
            this.Volume.ToggleMute();
        }
    }

    this.SetVolume = function(vol)
    {
        this.Volume.SetVolume(vol);
    }

    this.FullScreen = function()
    {
        if (this.pci.GetPlayState() != SfKernel.MediaState.Stopped)
        {
            this.pci.SetFullScreen(true);
        }
    }

    this.SetAudioLanguageIndex = function(index)
    {
        this.pci.SetAudioLanguageIndex(index);
    }

    this._NavigateToTime = function(timeInMS)
    {
        this.SetStartTime(timeInMS);
        mPlayer.EventManager.PostCommandEvent(SfKernel.CommandEventId.Play, this, null);
    }

    this.SetStartTime = function(timeInMS)
    {
        this.pci.SetPosition(timeInMS);
        this.timerManager.PostTimerUpdatedEvent(timeInMS);
        mPlayer.SamiDropDownPanelInstance.Seek(timeInMS);
        this.LastSeekPosition = timeInMS;
        mPlayer.PresentationEnded = false;

        if (mPlayer.CurrentSlidePanelInstance)
        {
            this.HandleSlideChangeWhenPositionChanges(timeInMS);
        }
    }

    this.SetMediaSource = function()
    {
        this.pci.SetMedia(Manifest.VideoUrl);
    }

    this._NavigateToSlide = function(slideNumber)
    {
        if (Manifest.PlayStatus != SfKernel.PresentationPlayStatus.OnDemand)
        {
            return;
        }
        if (slideNumber < 0)
        {
            return;
        }
        if (Manifest.Slides.length < slideNumber)
        {
            return;
        }

        var timeCode = (slideNumber == 0) ? 0.00 : Manifest.Slides[slideNumber - 1].Time;

        this._NavigateToTime(timeCode);
    }

    this._NavigateToChapter = function(number, timeInMilliSeconds)
    {
        this._NavigateToTime(timeInMilliSeconds);
    }

    this.HandleSlideChangeWhenPositionChanges = function(pos)
    {
        if (mPlayer.PresentationEnded)
        {
            return;
        }

        var slideNumber = this.partitionManager.GetSlideNumberToShow(pos);

        if (slideNumber < 1)
        {
            mPlayer.CurrentSlideNumber = -1;
            mPlayer.EventManager.PostEvent(SfKernel.EventType.PlayingFromBeginning, this, null);
            return;
        }

        if (slideNumber != mPlayer.CurrentSlideNumber)
        {
            var args = mPlayer.CreateShowSlideEventArgs(slideNumber);
            mPlayer.CurrentSlideNumber = slideNumber;
            mPlayer.EventManager.PostEvent(SfKernel.EventType.Script, this, args);
        }

    }

}
MediaPlayerArea.registerClass('MediaPlayerArea');


Type.registerNamespace('SfMediaPlayer');

SfMediaPlayer.ScriptParser = function()
{
	this.ParseScriptFromStream = function(sType, sParam)
	{
	    if(sType == "MS5")
	    {
	        eval("this." + sParam);
	    }	
	}
		
	this.HE = function()
	{
		if (arguments.length < 1)
		{
			return;
		}
		
		var command = arguments[0];
		switch (command)
		{
			case "S":
			    if(!mPlayer.CurrentSlidePanelInstance)
			    {
			        return;
			    }
				if (arguments.length < 2)
				{
					return;
				}

				var slideNumber = Number(arguments[1]);

				if (slideNumber <= mPlayer.CurrentSlideNumber)
				{
				    return;
				}

				if (Manifest.Slides.length < slideNumber)
				{
					mPlayer.KeepAddingToSlideTimings(slideNumber);
					mPlayer.DynamicAdd = true;
				}
				else
				{
					mPlayer.DynamicAdd = false;
				}
				
				mPlayer.CurrentSlideNumber = slideNumber;
				var args = mPlayer.CreateShowSlideEventArgs(slideNumber);
				this.NotifyScriptEvent(args);
				break;
			case "E":
				this.NotifyScriptEvent({Command: SfKernel.ScriptCmdType.EndPresentation});
				break;
			case "P":                        
				this.NotifyScriptEvent({Command: SfKernel.ScriptCmdType.Pause});
				break;
			case "R":
				this.NotifyScriptEvent({Command: SfKernel.ScriptCmdType.Resume});
				break;
		}
	} 

	this.NotifyScriptEvent = function(oArgs)
	{
		mPlayer.EventManager.PostEvent(SfKernel.EventType.Script, this, oArgs);
	}
		
}

SfMediaPlayer.Partition = function(left, right)
{
	this.MinIndex = left;
	this.MaxIndex = right;
	this.Left = null;
	this.Right = null;
}
SfMediaPlayer.Partition.prototype = 
{
	GetCount : function()
	{
		return this.MaxIndex - this.MinIndex + 1;
	},
	
	CreateSubPartitions : function()
	{
		var middle = Math.floor( (this.MinIndex + this.MaxIndex) / 2 );
		this.Left = new SfMediaPlayer.Partition(this.MinIndex, middle);
		this.Right = new SfMediaPlayer.Partition(middle+1, this.MaxIndex);
		return {Left:this.Left, Right:this.Right};
	}
}
SfMediaPlayer.Partition.registerClass('SfMediaPlayer.Partition', null);

SfMediaPlayer.PartitionManager = function(timings)
{
	this._timings = timings;
	this._timeCode = null;
}
SfMediaPlayer.PartitionManager.prototype = 
{
	GetSlideNumberToShow : function(timeCode)
	{
		if (this._timings.length == 0)
		{
			return 0;
		}
		
		this._timeCode = timeCode;
	
		return this._FindInPartition(new SfMediaPlayer.Partition(0, this._timings.length-1));	
	},
	
	_FindInPartition : function(partition)
	{
		var count = partition.GetCount();
		if (count == 1)
		{
			return this._FindIn1Partition(partition);
		}
		else if (count == 2)
		{
			return this._FindIn2Partition(partition);
		}
		
		partition.CreateSubPartitions();

		if (this._timeCode < this._timings[partition.Left.MinIndex].Time)
		{
			return partition.Left.MinIndex;
		}
		else if (this._IsPresentInPartition(partition.Left) == true)
		{
			return this._FindInPartition(partition.Left);			
		}
		else if (this._timeCode < this._timings[partition.Right.MinIndex].Time)
		{
			return partition.Right.MinIndex;
		}
		else if (this._IsPresentInPartition(partition.Right) == true)
		{
			return this._FindInPartition(partition.Right);
		}
		else
		{
			return partition.Right.MaxIndex+1;
		}
	},
	
	_FindIn1Partition : function(partition)
	{
		var partitionTime = this._timings[partition.MinIndex].Time;
		
		if (this._timeCode < partitionTime)
		{
			return partition.MinIndex;
		}
		else
		{
			return partition.MinIndex+1;			
		}
	},
	
	_FindIn2Partition : function(partition)
	{
		var time1 = this._timings[partition.MinIndex].Time;
		var time2 = this._timings[partition.MaxIndex].Time

		if (this._timeCode < time1)
		{
			return partition.MinIndex;
		}
		else if (this._timeCode >= time1 && this._timeCode < time2)
		{
			return partition.MinIndex+1;
		}
		else
		{
			return partition.MaxIndex+1;
		}
	},
	
	_IsPresentInPartition : function(partition)
	{
		return (this._timeCode >= this._timings[partition.MinIndex].Time && this._timeCode <= this._timings[partition.MaxIndex].Time);
	}
}


/// MediaPlayer Volume Helper Class
SfMediaPlayer.Volume = function(pci)
{
	this.IsMuted = false;
	this.PreviousVolume = null;
	this.pci = pci;
	
	this.InitializeVolume = function(vol)
	{
		this.PostVolumeInitializedEvent(vol);
	}
		
	this._GetVolumeFromPlayer = function()
	{
	    if (this.pci == null)
	    {
	        return 0;
	    }
	     
	        
	    return this.pci.GetVolume();
	}
	
	this.ToggleMute = function()
	{
	    if (this.IsMuted)
	    {
	    	this.pci.SetMute(false);
	    	
	    	if (this.PreviousVolume != null)
			{
				this.pci.SetVolume(this.PreviousVolume);
	        }
	        
	        this.IsMuted = false;
	        this.PostMuteCommand(false);
	    }
	    else
	    {
			this.PreviousVolume = this.pci.GetVolume();
	    	this.pci.SetMute(true);
	        
	        this.IsMuted = true;
	        this.PostMuteCommand(true);
	    }
	}
	
	this.SetVolume = function(val)
	{
		if (this.IsMuted)
		{
			if (val > 0)
			{
				this.ToggleMute();
			}
		}
		else
		{
			if (val == 0)
			{
				this.ToggleMute();
			}
		}
		
		this.pci.SetVolume(val);
		
		new MediasitePlayerCookie().SetValue("Volume", Math.round(val));

	}
	
	this.PostVolumeInitializedEvent = function(val)
	{
		mPlayer.EventManager.PostEvent(SfKernel.EventType.VolumeInitialized, this, {Volume:val});
	}
	
	this.PostMuteCommand = function(mute)
	{
		mPlayer.EventManager.PostEvent(SfKernel.EventType.MuteToggled, this, mute);
	}
}


