
function MediaPlayer()
{
    this.lastState="";
    this.Element=mss.FindName(Media.Names.VideoElement);
    this.LastSeekTime=0;
    this.eventList=new EventList(MediaPlayer.Events.MarkerReached,MediaPlayer.Events.CurrentStateChanged,MediaPlayer.Events.MediaOpened,MediaPlayer.Events.VolumeChanged);
	this.Element.addEventListener("MarkerReached", Event.createDelegate(this, this.OnVideoMarkerReached));
    this.Element.addEventListener("CurrentStateChanged", Event.createDelegate(this, this.OnCurrentStateChanged));
	this.Element.addEventListener("MediaOpened", Event.createDelegate(this, this.OnMediaOpened));
}

MediaPlayer.Events={};
MediaPlayer.Events.MarkerReached="MarkerReached";
MediaPlayer.Events.CurrentStateChanged="CurrentStateChanged";
MediaPlayer.Events.MediaOpened="MediaOpened";
MediaPlayer.Events.VolumeChanged="VolumeChanged";
MediaPlayer.States={};
MediaPlayer.States.Closed="Closed";
MediaPlayer.States.Opening="Opening";
MediaPlayer.States.Buffering="Buffering";
MediaPlayer.States.Playing="Playing";
MediaPlayer.States.Paused="Paused";
MediaPlayer.States.Stopped="Stopped";

MediaPlayer.prototype =
{
    SetMute: function(bool)
    {
        if (bool)
        {
            mss.Animate(MediasitePlayer.AnimationEvent.MuteOn);
        }
        else
        {
            mss.Animate(MediasitePlayer.AnimationEvent.MuteOff);
        }

        this.Element.IsMuted = bool;
    },

    SetVolume: function(level)
    {
        this.Element.Volume = level;
        this.eventList.Invoke(MediaPlayer.Events.VolumeChanged, level);
    },

    GetVolume: function()
    {
        return this.Element.Volume;
    },

    IsMuted: function()
    {
        return this.Element.IsMuted;
    },

    Play: function()
    {
        var state = this.GetState();

        if (state == MediaPlayer.States.Paused || state == MediaPlayer.States.Closed)
        {
            var pos = this.GetPosition();
            var dur = this.GetDuration();

            if (pos >= dur)
            {
                this.SeekTime(0);
            }
        }
        this.Element.Play();
    },

    Pause: function()
    {
        this.Element.Pause();
    },

    Stop: function()
    {
        this.LastSeekTime = 0;
        this.Element.Stop();
    },
    GetState: function()
    {
        return this.Element.CurrentState;
    },
    GetDuration: function()
    {
        return this.Element.NaturalDuration.Seconds * 1000;
    },
    GetPosition: function()
    {

        var tmp = this.Element.Position.Seconds;

        tmp = tmp * 1000;

        // silverlight has a bug that you don't get accurate seek with setting position.. it can be off a few seconds
        if (tmp < this.LastSeekTime)
        {
            tmp = this.LastSeekTime;
        }
        return tmp;
    },
    SeekTime: function(positionInMilliseconds)
    {
        var position = mss.FormatTimeHHMMSS(positionInMilliseconds);
        this.Element.setValue("position", position);

        this.LastSeekTime = positionInMilliseconds;
    },

    OnVideoMarkerReached: function(sender, eventArgs)
    {

        if (eventArgs.marker.type != Mediasite.ScriptID)
        {
            return;
        }
        var markerText = eventArgs.marker.text;
        
        if (markerText.indexOf("HE('") == 0)
        {
            var command = markerText.substring(4, 5);
            
            switch (command)
            {
                case Mediasite.ScriptCommand.EndPresentation:
                case Mediasite.ScriptCommand.Pause:
                case Mediasite.ScriptCommand.Resume:
                    var args = new VideoMarkerReachedArgs(command, 0);
                    this.eventList.Invoke(MediaPlayer.Events.MarkerReached, args);
                    break;
                case Mediasite.ScriptCommand.Slide:

                    var temp = new Array();

                    temp = markerText.split(',');

                    var slideNumber = temp[1]; // now have "37);";
                    temp = slideNumber.split(")");

                    slideNumber = temp[0];

                    slideNumber = parseInt(slideNumber);

                    var args = new VideoMarkerReachedArgs(command, slideNumber);
                    this.eventList.Invoke(MediaPlayer.Events.MarkerReached, args);
                    break;
                default:
                    break;
            }
        }
    },

    OnCurrentStateChanged: function(sender, eventArgs)
    {
        var newState = this.GetState()

        if (this.lastState != newState)
        {
            this.lastState = newState;
            this.eventList.Invoke(MediaPlayer.Events.CurrentStateChanged, newState);
        }

    },

    OnMediaOpened: function(sender, eventArgs)
    {
        this.eventList.Invoke(MediaPlayer.Events.MediaOpened, eventArgs);
    }
}

function VideoMarkerReachedArgs(command, slideNumber)
{
    this.Command = command;
    this.SlideNumber = slideNumber;
}
