
function StartWithParent(parentId, appId) {
    new FourPlayer(parentId);
}


//MyPlayer = new FourPlayer("parentId", skinFile, { "mediaUrl": "pathToVideo.wmv","placeholderImage": "pathToPreview.wmv","chapters": [] }
function FourPlayer(parentId, skinFile, mediaInfo) {
    this.MediaInfo = new Array();
    if (mediaInfo && mediaInfo.length)
	this.MediaInfo = mediaInfo;
    else if (mediaInfo)
	    this.MediaInfo.push(mediaInfo);
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: skinFile, 
                                        parentElement: $get(parentId ||"divPlayer_0"), 
                                        id:this._hostname, 
                                        properties:{ width:'100%', height:'100%', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'true' }, 
                                        events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = 0;      
}

FourPlayer.prototype= {
    _handleLoad: function() {
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay    : true, 
                                    volume      : .5,
                                    muted       : false,
                                    scaleMode   : Ee.UI.Xaml.ScaleMode.zoom
                                  }, 
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  ); 
        this._player.add_resize(this._onResize);
        this._playNextVideo();     
    },
    _onResize: function(sender, eventArgs) {
        var width = sender._element.offsetWidth;
        var height = sender._element.offsetHeight;
        var playerWidth = sender._mediaElement.width;
        var playerHeight = sender._mediaElement.height;
        var ratio = playerHeight/playerWidth;
        height = width*ratio;
        sender._element.parentNode.style.height = (parseInt(height) + 100) + 'px';
    },
    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout( Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function(sender, eventArgs) {
        alert('Unable to load media.\n\nThe content may be temporarily unavailable or you may be behind a firewall that blocks streaming media.');
	//alert(String.format( Ee.UI.Xaml.Media.Res.mediaFailed, this._player.get_mediaUrl() ) );
    },
    _playNextVideo: function() {
        if (this._currentMediainfo<this.MediaInfo.length)
            this._player.set_mediainfo(this.MediaInfo[this._currentMediainfo++]);    
    }
}

