(function( $ ) {

    var defaultSettings = {
        file        : "/shared/flash/video/flvplayer_v4.swf",
		containerId   : 'audioPlayerContainer',     //container     : null, 
		elemId        : 'flvAudioPlayer',
		self          : 'MediaPlayerLite.flvPlayer.playerObj', //name          : null,				
		type          : 'audio',                    //type          : 'video',
		debugMode     : false,
		scale         : 'noScale',
		defaultVolume : 70,
		autoHideSkin  : true,

		config        : function(){
			var that = this;
			that.width  = (that.type==='audio') ? 138 : 400;
			that.height = (that.type==='audio') ? 29 : 273;
			that.skin   = (that.type==='audio') ? '/shared/flash/gameday/v5/flvplayer/mlb_penguin_audio.swf' : '/flash/video/v2/skins/mlb_mediaLandingSkin.swf';
		},

		onPlayerLoaded : function() {
			throw new Error('FlvPlayer.create: onPlayerLoaded handler is undefined');
		},

		onPlaylistComplete : function() {},

		onError        : function( errString ) {
			// throw new Error('FlvPlayer.create: onError handler is undefined');
            // console.log( errString );
		}
	};

    /**
     * Constructor
     */
    function FlvPlayerProxy( s ) {
        if ( !(this instanceof FlvPlayerProxy) ) {
            return new FlvPlayerProxy( s );
        };    

        this.settings = $.extend( defaultSettings, s );

        return this;
    }

	/**
	 * Initializes instance of bam.FlvPlayer
	 * @private
	 */
	function initialize() {
        this.settings.config();
		this.playerObj = new bam.FlvPlayer( this.settings );

        this.playerObj.bind("playlistComplete", $.eventProxy(function() {
                var args = Array.prototype.slice.call(arguments);
                this.trigger("playlistComplete", args);
            },this));
	}

    FlvPlayerProxy.prototype = {

		playerObj : null,

		/** 
		 * Destroys instance of FlvPlayer
		 */
		destroy : function() {
			$( '#' + this.settings.elemId ).remove();
            this.unbind();
			this.playerObj = null;
		},

		/**
		 * Queues and plays playlist item(s) in FLV Player
		 * @public
		 * example playlist item: {type:"video", videoPath:[URL] }
		 */
		play : function( playlist ) {

            var thisPlayer = this;

			// instantiate flv player if null
			if( ! thisPlayer.playerObj ){ 
                
                settings = $.extend( this.settings, {
					onPlayerLoaded: function() { 
                        //console.log( 'player loaded' );
						thisPlayer.play( playlist ); 
					}
				});

				initialize.call( thisPlayer );

            // pass playlist to FLV player
			} else {

				if ( !playlist.length ){ 
                    playlist = [ playlist ]; 
                }

				this.playerObj.startPlaylist( playlist );
			}
            return this;
		},

		/**
		 * Queues and playlist item(s) in FLV Player
		 * @public
		 * example playlist item: {type:"video", videoPath:[URL] }
		 */
		set : function( playlist ) {

            var thisPlayer = this;

			// instantiate flv player if null
			if( ! thisPlayer.playerObj ){ 
                
                settings = $.extend( this.settings, {
					onPlayerLoaded: function() { 
                        //console.log( 'player loaded' );
						thisPlayer.set( playlist ); 
					}
				});

				initialize.call( thisPlayer );

            // pass playlist to FLV player
			} else {

				if ( !playlist.length ){ 
                    playlist = [ playlist ]; 
                }

				this.playerObj.setPlaylist( playlist );
			}
            return this;
		},


        /**
         * Resumes playback. Only available after pause
         */
		resume : function() {
			if( !!this.playerObj ) { 
				this.playerObj.execute('resumePlayback'); // after pause only	
			}
		},

        /**
         * Pauses playback
         */
		pause : function() {
			if( !!this.playerObj ) { 
				this.playerObj.execute('pausePlayback');
			}
            return this;
		},

        /**
         * Stops playback
         */
		stop : function() {
			if( !!this.playerObj ) {
                this.playerObj.stopPlaylist();    
            }
            return this;
		},

        /**
         * Updates volume of player
         */
		changeVolume : function( volumeLevel ) {
			if( !!this.playerObj ) { 
				// console.log('FlvPlayer.changeVolume --> new volume is '+volumeLevel);
				this.playerObj.execute('changeVolume', volumeLevel); // changes volume to value 15; string or integer 
			}
            return this;
		}
	};

    $.bindable(FlvPlayerProxy);

    window.FlvPlayerProxy = FlvPlayerProxy;

})( jQuery );

