﻿function StartMediaPlayer(parentId, playerWidth, playerHeight) {

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: this.xamlSource, 
                                    parentElement: $get( parentId || "mediaPlayer_0" ), 
                                    id:this._hostname, 
                                    properties:{ width: playerWidth, height: playerHeight, version:'1.0', background:"Black", isWindowless:'false', inplaceInstallPrompt:true }, 
                                    events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = -1;     
}

StartMediaPlayer.prototype= {
    _handleLoad: function() {
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay       : this.autoPlayParam, 
                                    autoLoad       : this.autoLoadParam,
				    				scaleMode 	   : this.scaleModeParam,
                                    muted          : this.mutedParam,
								    enableCaptions : this.enableCaptionsParam,
                                    volume         : 1.0
                                  }, 
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed),
                                    playPreviousVideo: Function.createDelegate(this, this._onPlayPreviousVideo),
                                    playNextVideo: Function.createDelegate(this, this._onPlayNextVideo)
                                  },
                                  null, $get(this._hostname)  );   

    this._playlist = this.getPlaylist();
    this._onPlayNextVideo(null,null);                                        
    },
    
    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout( Function.createDelegate(this, this._onPlayNextVideo), 1000);
    },
    
    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Sys.UI.Silverlight.MediaRes.mediaFailed, this._player.get_mediaSource() ) );
    },
      

    _onPlayPreviousVideo: function(sender, eventArgs) {
        if (this._playlist!=null) {
            if (this._currentMediainfo>0) {
                this._player.set_mediainfo( this._playlist[ --this._currentMediainfo ] );    
            }
        }        
    },
    
    _onPlayNextVideo: function(sender, eventArgs) {
        if (this._playlist!=null) {
            if (this._currentMediainfo<this._playlist.length-1) {                
                this._player.set_mediainfo( this._playlist[ ++this._currentMediainfo ] );                    
            }
        }
    }
}

function StartWithParent(parentId, appId) {
    new StartMediaPlayer(parentId);
}

