function glue(id, event, memo) {
    $(id).fire(event, memo);
}

var MP3Player = Behavior.create({
    initialize: function() {
        this.element.identify();
        this.bindSoundEvents();
        this.path = this.element.readAttribute('data-path');
        this.loaded = false;
        this.playing = false;
    },
    bindSoundEvents: function() {
        this.element.observe('sound:loadingCompleted', this.loadingCompletedHandler.bind(this));
        this.element.observe('sound:stopping', this.stoppingHandler.bind(this));
        this.element.observe('sound:playing', this.playingHandler.bind(this));
    },
    onclick: Event.distribute(function(callback) {
        callback.define('.paused .control button', function(event) { this.play(); event.element().blur(); event.stop(); });
        callback.define('.playing .control button', function(event) { this.stop(); event.element().blur(); event.stop(); });
    }),
    play: function() {
        if(this.path) {
            $('sound_swf').fire('sound:stopAll', {});
            $('sound_swf').fire('sound:play', {id: this.element.id, path: this.path});
            return true;
        }
    },
    stop: function() {
        if(this.path) {
            $('sound_swf').fire('sound:stop', {id: this.element.id, path: this.path});
            return true;
        }
    },
    toggleButtonClasses: function() {
        this.element.toggleClassName('playing').toggleClassName('paused');
    },
    loadingCompletedHandler: function(event) {
        this.loaded = true;
    },
    playingHandler: function(event) {
        if (!this.playing) {
            this.toggleButtonClasses();
            this.playing = true;
        }
    },
    stoppingHandler: function(event) {
        this.toggleButtonClasses();
        this.playing = false;
    }
});

var TrackMP3Player = Behavior.create(MP3Player, {
    initialize: function($super) {
        $super();
        this.loadProgress = this.element.down('.progress.download');
        this.playProgress = this.element.down('.progress.playback');
    },
    bindSoundEvents: function($super) {
        $super();
        this.element.observe('sound:loading', this.loadingHandler.bind(this));
    },
    stop: function($super) {
        if($super()) {
            this.updateElementWidth(this.playProgress, 0);
            if (!this.loaded) {
                this.updateElementWidth(this.loadProgress, 0);
            }
        }
    },
    playingHandler: function($super, event) {
        $super();
        this.updateElementWidth(this.playProgress, event.memo.percent);
    },
    loadingHandler: function(event) {
        this.updateElementWidth(this.loadProgress, event.memo.percent)
    },
    updateElementWidth: function(element, percent) {
        if (element) {
            element.setStyle({width: percent + '%'});
        }
    }
});

Event.addBehavior({
    'div.mp3_player': TrackMP3Player
});

document.observe('dom:loaded', function(event) {
    $$('body').first().insert({bottom: $div({id: 'sound_swf'})});
    swfobject.embedSWF("/flash/sound.swf", "sound_swf", "1", "1", "9.0.0", null, {}, {wmode:'transparent'});
});