var detectVideoSupport = function (){
  var detect = document.createElement('video') || false;
  this.html5 = detect && typeof detect.canPlayType !== "undefined" && $('html.ie9').length == 0;
  this.mp4 = this.html5 && (detect.canPlayType("video/mp4") === "maybe" || detect.canPlayType("video/mp4") === "probably");
  this.ogg = this.html5 && (detect.canPlayType("video/ogg") === "maybe" || detect.canPlayType("video/ogg") === "probably");
  return this;
};

$(document).ready(function(){

  var replaceVideoWithObject = function (video){
    if( $(video).length == 0 ) { return false; }
    
    var div_id = video.id + '_flash';
    var div = $('<div id="'+div_id+'" style="height: 360px; width: 640px;">');

    $(video).after(div);
    
    flowplayer(div_id, '/player/flowplayer.commercial-3.2.7.swf', {
      key: flowPlayerKey(), 
      plugins: {
        rtmp: {
          url: '/player/flowplayer.rtmp-3.2.3.swf',
          netConnectionUrl: 'rtmp://dna-bucket-video.rtmp.rawcdn.com/cfx/st'
        }
      },
      clip: { 
        provider: 'rtmp',
        url: 'mp4:'+$(video).find('source').attr('src').replace(/.*\//, '').replace(/\....\?+.*/, '')
      } 
    });
    $(div).show();
    $(video).remove();
  };

    var video = detectVideoSupport();

    $("video").each( function() { 
      if(!video.html5 || (video.ogg && !video.mp4)){
        replaceVideoWithObject(this);
      } else {
        $(this).gVideo();
      }
    });
});

