
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]);  
}

var g_timeSliderChangedCallback = null;
var g_playStateChangedCallback = 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 StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}

function StartPlayer_0(parentEl) {
    this.parentEl = parentEl;
    
}


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', 
                                        parentElement: this.parentEl, 
                                        id:this._hostname, 
                                        properties:{ width:'100%', height:'100%', version:'1.0', background:"#555555", isWindowless:'false' }, 
                                        events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
        this._currentMediainfo = 0;  
    
    },
    AspectRatio: 1.156,
    Height: 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.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";
        }
    },
    GetHeight: function() 
    { 
        return this.Height; 
    },
    GetWidth: function()
    {
        return this.Width;
    },
    SetVideoPosition: function( fTime )
    {
        expressionVideo.Position = CreateTimeSpan(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");
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay    : true, 
                                    volume      : 1.0,
                                    muted       : false
                                  }, 
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  ); 
        this._playNextVideo();     
    },    
    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout( Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Ee.UI.Xaml.Media.Res.mediaFailed, this._player.get_mediaUrl() ) );
    },
    _playNextVideo: function() {
        var cVideos = 1;
        if (this._currentMediainfo<cVideos)
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++ ) );    
    }        
}
