(function($){
    $.fn.jamp3 = function(options){
        if(!options){
            options = {};
        }
        if(!options.flashlocation){
            options.flashlocation = '';
        }
        return this.each(function(index, el){
                var playerhtml = '<span class="jamp3player" id="jamp3player'+index+'">'
                +' <a class="play">play</a>'
                +' <a class="stop">stop</a>'
                +'<object id="jamp3playermovie'+index+'" type="application/x-shockwave-flash" data="'
                +options.flashlocation+'player_mp3_js.swf" width="0" height="0">'
                +'<param name="movie" value="'
                +options.flashlocation+'player_mp3_js.swf" />'
                +'<param name="AllowScriptAccess" value="always" />'
                +'<param name="FlashVars" value="listener=jamp3listener'+index+'&amp;interval=1000" />'
                +'</object></span>';
                $(el).after(playerhtml);
                var listener = new Object();
                listener.onInit = function(){this.position=0;};
                listener.onUpdate = function(){
                    $('#jamp3player'+index+' a.pause')[0].title = 
                    Math.round((this.position/this.duration)*100)+'% completed';
                };
                // listeners are defined as global objects |-(
                window['jamp3listener'+index] = listener;
                // The actual eventhandler
                $('#jamp3player'+index+' a').click(function(ev){
                        var player = $('#jamp3playermovie'+index)[0];
                        if(!player.url){
                            player.SetVariable("method:setUrl", el.href);
                        }
                        switch(ev.target.className){
                        case 'play':
                            $('.jamp3player object').each(function(index, el){
                              if($(el).prev('a').prev('a')[0].className=="pause"){
                                $(el).prev('a').prev('a')[0].className = 'play';
                                el.SetVariable("method:stop", "");
                                el.SetVariable("enabled", "false");
                              }
                            });
                            ev.target.className = 'pause';
                            player.SetVariable("method:play", "");
                            player.SetVariable("enabled", "true");
                            break;
                        case 'pause':
                            ev.target.className = 'play';
                            player.SetVariable("method:pause", "");
                            player.SetVariable("enabled", "false");
                            break;
                        case 'stop':
                            $('#jamp3player'+index+' a')[0].className='play';
                            player.SetVariable("method:stop", "");
                            player.SetVariable("enabled", "false");
                            break;
                        }
                    });
            });
    };
})(jQuery);

