Ext.namespace('Abacus');
Abacus.SimpleAudioPlayer = Ext.extend(Abacus.AudioPreview, {
    /** @cfg {Boolean} autoPlay
     *  If true, as soon as sound manager and this component are ready, the audio will start to play
     *
     */
    autoPlay:true,
    /** @cfg {Boolean} loopMode
     *  If true, will loop around the list of audio files (or a single one)
     *
     */
    loopMode:true,
    /** @cfg {Ext.data.Store} store
     *  The store of audio files to play. If not specified, then playCategory or playId can be used to auto create a store
     *
     */
    store:null,
    /** @cfg {String} playCategory
     *  Using this config will create a store loading its contents using all audio files within the specified category
     *
     */
    playCategory:null,
    /** @cfg {String} playId
     *  Using this config instead of playCategory or store will load a store with just this id or ids in it
     *
     */
    playId:null,
    /** @cfg {String} categorizedMediaUrl
     *  The url to fetch categorized media with :category_id as a placeholder for the category id
     *
     */
    categorizedMediaUrl:'/medium_categories/:category_id/audio_files',
                                        

    initComponent: function(){
        // Called during component initialization

        // Config object has already been applied to 'this' so properties can
        // be overriden here or new properties (e.g. items, tools, buttons)
        // can be added, eg:
        Ext.apply(this, {

        });
        if(!this.store && (this.playCategory || this.playId)){
            if(this.playCategory){
                this.store=new Abacus.JsonStore({url:this.categorizedMediaUrl.replace(/:category_id/,this.playCategory),autoLoad:true,storeId:this.id+'_media_store'});
                if(this.autoPlay){
                    this.store.on('load',function(){
                        if(this.store.getCount()>0){
                            this.playAt(0);
                        }
                    },this)
                }
            }
            if(this.playId){
                throw 'playId not yet supported';
            }
        }
        // Before parent code

        // Call parent (required)
        Abacus.SimpleAudioPlayer.superclass.initComponent.apply(this, arguments);

        // After parent code
        // e.g. install event handlers on rendered component
    },
    playAt:function(index){
        var rec=this.store.getAt(index);
        this.load(rec.data);
        this.play();
        this.currentlyPlaying=index;
    },
    onFinish:function(){
        /* Caution - this is the sound that has finished playing */
        var me=this.abacusPlayer;
        if((me.currentlyPlaying+1)>=me.store.getCount()){
            if(me.loopMode){
                me.playAt(0);
            }
        }else{
            me.playAt(me.currentlyPlaying+1);
        }
    }

    // Override other inherited methods
});

// register xtype to allow for lazy initialization
Ext.reg('abacussimpleaudioplayer', Abacus.SimpleAudioPlayer);
