jQuery(function(){

  // Add an mp3 player
  jQuery('a').each(function(){
    
    // Selection:
    if ( this.href.toLowerCase().substr(-4).indexOf('.mp3') < 0 ) return;

    // Definitions:
    var object_id;
    var object_code;
    var player_swf;
    var flash_vars = {};
    var flash_param = { wmode: 'transparent' };
    var attributes = {};
    var width = 0;
    var height = 0;
    
    // create a new object
    // find a unique object name
    do {
      object_id = 'object' + Math.round( Math.random() * 10000 );
    } while( jQuery('#' + object_id).length > 0 );
    
    // generate an object code
    object_code = '<div id="' + object_id + '" />';
    
    // decide which player
    if (jQuery(this).text() == '' && jQuery(this).find('img').length == 1){
      // A linked image
      jQuery(this).before(object_code);

      flash_vars.file = flash_vars.link = encodeURIComponent(this.href);
      flash_vars.image = jQuery(this).find('img:eq(0)').attr('src');
      flash_vars.skin = audio_player_home_path + '/jw_player_skin.swf';
      
      player_swf = audio_player_home_path + '/jw_player5.swf';
      attributes.styleclass = jQuery(this).find('img').attr('class');
      
      height = jQuery(this).find('img').height();
      width = jQuery(this).find('img').width();

      
      jQuery(this).hide();
    }
    else if (jQuery(this).text() == jQuery(this).parent().text()){
      // the link is the only element in the paragraph
      jQuery(this).after(object_code);
      
      player_swf = audio_player_home_path + '/line_player.swf'; 
      flash_vars.soundFile = encodeURIComponent(this.href);
      
      width = 190;
      height = 15;
    }
    else {
      jQuery(this).after(object_code);
      
      player_swf = audio_player_home_path + '/line_player.swf'; 
      flash_vars.soundFile = encodeURIComponent(this.href);
      
      width = 190;
      height = 15;
      // the link is an inline element in a floating text
      /*jQuery(this).before(object_code);
      player_swf = audio_player_home_path + '/button_player.swf?song_url=' + encodeURIComponent(this.href);
      
      width = 20;
      height = 17;*/
    }
    
    // Add the player:
    swfobject.embedSWF(player_swf, object_id, width, height, "9.0.0", null, flash_vars, flash_param, attributes );
  });

});
