function replaceVideo (obj) {
  var attachment = $(obj).attr("href");
  var attachmentFileExtension = attachment.split('.').pop();
  var attachmentPoster = attachment.replace("." + attachmentFileExtension, "-poster.jpg");
  
  var iPad = navigator.userAgent.match(/iPad/i) != null;
  var iPhone = navigator.userAgent.match(/iPhone/i) != null;
  
  if (attachmentFileExtension == "png" || attachmentFileExtension == "jpg") { // image
    var imageMarkup = '<img src="' + attachment + '" />';
    $(".video-js").remove();
    $(".video-js-box").html(imageMarkup);
  } else { // video
    if (iPhone == false && iPad == false) {
      var videoPlayerMarkup = '<span>\
        <object class="vjs-flash-fallback" width="822" height="462" type="application/x-shockwave-flash" data="http://baked-beans.tv/bb/wp-content/plugins/videoplayer/flowplayer.commercial-3.2.5-1.swf">\
          <param name="movie" value="http://baked-beans.tv/bb/wp-content/plugins/videoplayer/flowplayer.commercial-3.2.5-1.swf" />\
          <param name="allowfullscreen" value="true" />\
          <param name="flashvars" value=\'config={"clip":{"url":"' + attachment + '","autoPlay":false,"autoBuffering":true}}\' />\
          <param name="bgcolor" value="#000000" />\
          <img src="' + attachmentPoster + '" width="822" height="462" alt="Poster Image" title="No video playback capabilities." />\
        </object>\
      </span>'
      $(".video-js").remove();
      $(".video-js-box").html(videoPlayerMarkup);
      VideoJS.setup();
    } else if (iPad) {
      var videoPlayerMarkup = '<video class="video-js" width="822" height="462" src="' + attachment.replace(".mp4", ".ipad_.mp4") + '" poster="' + attachmentPoster + '" controls preload></video>';
      $(".video-js").remove();
      $(".video-js-box").html(videoPlayerMarkup);
    } else if (iPhone) {
      var videoPlayerMarkup = '<video class="video-js" width="822" height="462" src="' + attachment.replace(".mp4", ".iphone.mp4") + '" poster="' + attachmentPoster + '" controls preload></video>';
      $(".video-js").remove();
      $(".video-js-box").html(videoPlayerMarkup);
    }
  }
}

