/* Class for audio modules
 *
 * The player used in this file is the standalone verion of the WordPress Audio Player, whose
 * documentation can be found at http://wpaudioplayer.com/standalone/
 */
var media_audio = $.inherit(Module, {
    __constructor: function(elem) {
        this.__base(elem);
        this.comptype = "media.audio";
        this.fromTab = 'Media';
        this.autoplay = false;
        
        this.file = '';
        this.caption = '';
    },

    initialize: function() {

        // Get file information as stored in the div inside the module div 
        // (IE is horrible; see the "saveModule" function below for why it has to be like this)
        var options = $('div', this.container).html().split('|');

        var caption = options[1];
        var id = this.id;
        var file = options[0];

        // Figure out how large the module should be
        var resizeWidth = ((this.element.parent().width() > 290) ? 290 :  this.element.parent().width());

        // Get the audio-player.js script so we can add it to the module
        $.getScript('/adm/zbl/js/lib/audio-player/audio-player.js', function() {

            // Set up the audio player options
            AudioPlayer.setup("/adm/zbl/js/lib/audio-player/player.swf", {
                transparentpagebg: 'yes',
                titles: caption,
                width: '100%'
            });

            // Put an "Audio Player" flash file in this module to play our audio file
            AudioPlayer.embed('player' + id, {soundFile: file});
        });
    },


    loadModuleCallback: function(data) {
        var _this = this;
        var _data = data;

        this.file = data.file;
        this.caption = data.name;

        // Add a loading message
        this.container.html("Loading...");

        // Figure out how large the module and media player should be, with 290px being the max width
        var resizeWidth = ((this.element.parent().width() > 290) ? 290 :  this.element.parent().width());

        this.element.width(resizeWidth);

        // Add a draghandle
        this.addDragHandle(data);

        this.container.html(data.html);

        // Get the audio-player.js script so we can add it to the module
        $.getScript('/adm/zbl/js/lib/audio-player/audio-player.js', function() {

            // Set up the audio player options
            AudioPlayer.setup("/adm/zbl/js/lib/audio-player/player.swf", {  
                transparentpagebg: 'yes',
                titles: _data.name,
                width: '100%'
            });

            // Put an "Audio Player" flash file in this module to play our audio file
            AudioPlayer.embed('player' + _data.id, {soundFile: _data.file});
        });
    },

    /* Appropriately size this module based on how long the document link is inside of it */
    appropriatelyResize: function() {
        // Figure out how large the module and media player should be, with 290px being the max width
        var resizeWidth = ((this.element.parent().width() > 290) ? 290 :  this.element.parent().width());
        this.element.width(resizeWidth);
    },

    /* This handles what should be done after saving the module */
    handleModuleSaveResult: function(postSaveData) {
        // Save the region containing this module (mainly so the PHP side can access the options attribute)
        _EDITOR.forceAllSave(null,true);
    },

    saveModule: function() {
        // Just save the div in the module container that the audio player instantiates into so
        // that the javscript on the published side can take care of creating the player. We unfortunately,
        // due to many bizarre and ridiculous quirks in IE, have to end up storing the data we need
        // on the published side as a regular string inside of this div.
        return '<div id="player' + this.id + '">'+ this.file +'|' + this.caption + '</div>';
    }
});

