jQuery(function(){
  // Add a player
  jQuery('a').each(function(){
  
    // Check the extensension:
    if ( this.href.toLowerCase().substr(-4).indexOf('.flv') < 0 &&
         this.href.toLowerCase().substr(-4).indexOf('.mp4') < 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 + '" />';
    
    player_swf = video_player_home_path + '/jw_player.swf';
    
    flash_vars.file = flash_vars.link = encodeURIComponent(this.href);
    //flash_vars.skin = video_player_home_path + '/jw_player_skin.swf';
    
    attributes.styleclass = jQuery(this).attr('class');
    
    height = 240;
    width = 320;
    
    if ( jQuery(this).text() == '' && jQuery(this).find('img').length == 1){
      // with preview image
      flash_vars.image = encodeURIComponent(jQuery(this).find('img').attr('src'));
      attributes.styleclass = jQuery(this).find('img').attr('class');
      height = jQuery(this).find('img:eq(0)').height();
      width = jQuery(this).find('img:eq(0)').width();
    }

    // Add the player:
    swfobject.embedSWF(player_swf, object_id, width, height, "9.0.0", null, flash_vars, flash_param, attributes );
    
    // Hide the Original
    jQuery(this).before(object_code);
    jQuery(this).hide();    
  });
});
