﻿function ZooTubePlayer(parentId) {
    try {
    	eval('background:document.body.style.backgroundColor="#ck"');
    } catch(e){}

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {source: '/video/player/player.xaml',
                                  parentElement: $get(parentId),
                                  id: this._hostname, 
//                                  properties: { width:'100%', height:'100%', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'false', inplaceInstallPrompt:true },
                                  properties: { width: '100%', height: '100%', version: '1.0', isWindowless: 'false', inplaceInstallPrompt: true },
                                  events: { onLoad: Function.createDelegate(this, this._handleLoad) }
                                });
}

ZooTubePlayer.prototype = {
    _handleLoad: function() {
        this._currentMediainfo = 0;
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay       : false,
                                    autoCue        : true,
				                    scaleMode 	   : 1,
                                    muted          : storedMutedParam(),
				                    enableCaptions : true,
                                    volume         : storedVolumeParam()
                                  }, 
                                  { // event handlers
                                    mediaOpened: Function.createDelegate(this, this._onMediaOpened),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  );   

        this._currentAutoPlay = false;
    },

    setSource: function(source) {
        this._currentMediainfo = 0;
        this._currentAutoPlay = true;
        this._playlist = [{"mediaSource" : source, "placeholderImageSource" : "", "chapters" : []}];
	    this._playNextVideo();
    },

    _playNextVideo: function() {
        if (this._playlist!=null) {
            if (this._currentMediainfo<this._playlist.length)
                this._player.set_mediainfo( this._playlist[ this._currentMediainfo++ ] );    
        }
    },

    _onMediaOpened: function(sender, eventArgs) {
        if(this._currentAutoPlay) {
            this._player.play();
        }
    },

    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Sys.UI.Silverlight.MediaPlayer.Res.mediaFailed, this._player.get_mediaSource() ) );
    }
}