
Ee.UI.Xaml.Res={
'runtimeErrorWithoutPosition': "Runtime error {2} in control '{0}', method {6}: {3}",
'scaleModeRequiresScaleTransform': "When ScaleMode is set to zoom or stretch, the root Canvas must have not have a RenderTransform applied, or must only have a ScaleTransform.",
'mediaError_NotFound': "Media '{3}' in control '{0}' could not be found.",
'runtimeErrorWithPosition': "Runtime error {2} in control '{0}', method {6} (line {4}, col {5}): {3}",
'silverlightVersionFormat': "Must be in the format 'MajorVersion.MinorVersion'.",
'otherError': "{1} error #{2} in control '{0}': {3}",
'cannotChangeXamlSource': "You cannot change the XAML source after initialization.",
'parserError': "Invalid XAML for control '{0}'. [{7}] (line {4}, col {5}): {3}"
};

Ee.UI.Xaml.Media.Res={
'volumeRange':  "Volume must be a number greater than or equal to 0 and less than or equal to 1.",
'mediaFailed':  "Unable to load media '{0}'.",
'noMediaElement':  "The XAML document does not contain a Media Element.",
'invalidChapterIndex':  "Must be greater than or equal to 0 and less than the length of the chapter's array."
};



///////////////////////////////////////////////////////////////////////////////
//
//  ExtendedPlayer
//
//  This extends the base player class, you may override the base player
//  member functions or add additional player functionality here. 
//
///////////////////////////////////////////////////////////////////////////////
Type.registerNamespace('ExtendedPlayer');

ExtendedPlayer.Player = function(domElement) {
    ExtendedPlayer.Player.initializeBase(this, [domElement]);  
}

// These are the unscaled XAML sizes of these elements
var g_iPlayerNaturalWidth = 638;
var g_iPlayerNaturalChromeHeight = 76;

var g_dTicksPerSecond = 10000000;

var g_timeSliderChangedCallback = null;
var g_playStateChangedCallback = null;

var g_onLoadVideoStartPosition = null;

ExtendedPlayer.Player.prototype =  {
	_onTimeSliderChanged: function(args)	
	{
        ExtendedPlayer.Player.callBaseMethod(this, "_onTimeSliderChanged", [args]);
	    if( g_timeSliderChangedCallback != null )
	    {
	        g_timeSliderChangedCallback(this._mediaElement.Position.Seconds, true);
	    }
	},
	onPlayStateChanged: function(args)
	{
        ExtendedPlayer.Player.callBaseMethod(this, "onPlayStateChanged", [args]);
        if( g_playStateChangedCallback != null )
	    {
	        g_playStateChangedCallback(this._mediaElement.currentState);
	    }
	}
}
ExtendedPlayer.Player.registerClass('ExtendedPlayer.Player', EePlayer.Player);

var expressionPlayerUrl = "";
var expressionVideo = null;
function get_mediainfo(mediainfoIndex) {
    switch (mediainfoIndex) {        

        case 0:
            return  { "mediaUrl": expressionPlayerUrl,
                      "placeholderImage": null,
                      "chapters": [               
                                  ] };                                                                
                          
        default:
             throw Error.invalidOperation("No such mediainfo");
     }
}

function StartPlayer_0(parentEl, viewer)
{
    this.parentEl = parentEl;
    this.version = viewer.version;
}


StartPlayer_0.prototype = {
    Initialize: function (url, sliderChangedCallback, playStateChangedCallback)
    {
        expressionPlayerUrl = url.Url;
        g_timeSliderChangedCallback = sliderChangedCallback;
        g_playStateChangedCallback = playStateChangedCallback;
        this._hostname = EePlayer.Player._getUniqueName("xamlHost");
        Silverlight.createObjectEx({ source: 'player.xaml?ver=' + this.version,
            parentElement: this.parentEl,
            id: this._hostname,
            properties: { width: '100%', height: '100%', version: '1.0', background: "#ECECEC", isWindowless: 'false' },
            events: { onLoad: Function.createDelegate(this, this._handleLoad) }
        });
        this._currentMediainfo = 0;

    },
    // Width/Height from player.xaml
    AspectRatio: 638 / 554,
    Height: 0,
    Width: 0,
    OnResize: function (maxHeight, maxWidth)
    {
        var desiredWidth = maxWidth == undefined ? (this.parentEl.offsetWidth - 20) : maxWidth;
        this.Height = desiredWidth / this.AspectRatio;
        if (this.Height > maxHeight)
        {
            this.Height = maxHeight;
        }
        this.Width = this.Height * this.AspectRatio;

        this.Height = Math.floor(this.Height);
        this.Width = Math.floor(this.Width);

        this.parentEl.style.height = this.Height + "px";

        var slEl = document.getElementById(this._hostname);
        if (slEl)
        {
            slEl.style.height = this.Height + "px";
            slEl.style.width = this.Width + "px";
        }
    },
    SetVideoPosition: function (fTime)
    {
        if (expressionVideo)
        {
            expressionVideo.Position = CreateTimeSpan(fTime);
        }
        else
        {
            g_onLoadVideoStartPosition = fTime;
        }
    },

    GetVideoPosition: function ()
    {
        if (!expressionVideo)
            return 0;
        else
            return expressionVideo.Position.Seconds;
    },

    GetPlayState: function ()
    {
        if (!expressionVideo)
        {
            return "Stopped";
        }
        else
        {
            return expressionVideo.CurrentState;
        }
    },

    SetPlayState: function (playState)
    {
        if (playState == "Playing")
            expressionVideo.Play();
        else if (playState == "Paused")
            expressionVideo.Pause();
        else if (playState == "Stopped")
            expressionVideo.Stop();
    },

    _handleLoad: function (sender)
    {
        expressionVideo = sender.content.findName("VideoWindow");
        playerChrome = sender.content.findName("PlayerChrome");
        player = sender.content.findName("Player");

        this._player = $create(ExtendedPlayer.Player,
                                  { // properties
                                      autoPlay: true,
                                      volume: 1.0,
                                      muted: false
                                  },
                                  { // event handlers
                                      mediaOpened: Function.createDelegate(this, this._onMediaOpened),
                                      mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                      mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname));
        this._playNextVideo();
    },
    _onMediaOpened: function (sender, eventArgs)
    {
        // Adjust player aspect ratio for audio-only to display only controls
        if (!expressionVideo.NaturalVideoWidth)
        {
            this.AspectRatio = g_iPlayerNaturalWidth / g_iPlayerNaturalChromeHeight;
            expressionVideo.Visibility = "Collapsed";
            playerChrome["Canvas.Top"] = 0;
            player.height = g_iPlayerNaturalChromeHeight;
        }

        if (g_onLoadVideoStartPosition)
        {
            this.SetVideoPosition(g_onLoadVideoStartPosition);
            g_onLoadVideoStartPosition = null;
        }

        g_pViewer.Resize();
    },
    _onMediaEnded: function (sender, eventArgs)
    {
        window.setTimeout(Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function (sender, eventArgs)
    {
        // If we fail to connect, wait two seconds and retry.
        setTimeout(
                Function.createDelegate(this, function () { this._player.set_mediaUrl(this._player.get_mediaUrl()) }),
                2000
            );
    },
    _playNextVideo: function ()
    {
        var cVideos = 1;
        if (this._currentMediainfo < cVideos)
            this._player.set_mediainfo(get_mediainfo(this._currentMediainfo++));
    }
}
