﻿var strMovie = ""; //need this so we can load movies from filmStrim.xaml

//we also added a parameter to StartPlayer_0, get_mediainfo, _playNextVideo
 
function get_mediainfo(mediainfoIndex, strMovie) {
    switch (mediainfoIndex) {        

        case 0:
            return  { "mediaUrl": strMovie,
                      "placeholderImage": "fba_footer_grayscale.jpg",
                      "chapters": [               
                                  ] 
                    };                                                                
                          
        default:
             throw Error.invalidOperation("No such mediainfo");
     }
}

function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}

function StartPlayer_0(parentId, movie) {
    strMovie = movie;
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    
    //STREAMING SILVERLIGHT OBJECT   
    Silverlight.createHostedObjectEx( {   source: 'player.xaml', 
                                            parentElement: $get(parentId ||"divPlayer_0"), 
                                            id:this._hostname, 
                                            properties:
                                            { width:'100%'
                                                , height:'100%'
                                                , version:'1.0'
                                                , isWindowless:'false;'
                                                , background:'#383838' 
                                            }, 
                                            events:
                                            { 
                                                onLoad:Function.createDelegate(this, this._handleLoad) 
                                            },
                                            initParams:[strMovie]
                                }
                              );
    //NON STREAMING SILVERLIGHT
//    Silverlight.createObjectEx( {   source: 'player.xaml', 
//                                            parentElement: $get(parentId ||"divPlayer_0"), 
//                                            id:this._hostname, 
//                                            properties:
//                                            { width:'100%'
//                                                , height:'100%'
//                                                , version:'1.0'
//                                                , isWindowless:'false;'
//                                                , background:'#383838' 
//                                            }, 
//                                            events:
//                                            { 
//                                                onLoad:Function.createDelegate(this, this._handleLoad) 
//                                            }
//                                }
//                              );
    this._currentMediainfo = 0; 
}
StartPlayer_0.prototype= {
    _handleLoad: function() {
        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(strMovie);     
    },    
    _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(movie) {
        var params = $get(this._hostname).InitParams; //used for streaming
        var cVideos = 1;
        if (this._currentMediainfo<cVideos)
        {
            //used for local
           // this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++, movie ) ); 
            //used for streaming
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++, params ) ); 
        }       
    }        
}
