var demo_video = function() {
  function supports_video() {
     return !!document.createElement('video').canPlayType;
   }

   function supports_h264_baseline_video() {
        if (!supports_video()) { return false; }
        var v = document.createElement("video");
        return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
   }

   function insert_html5(opts) {
         var videotag = "<video height='{{height}}' width='{{width}}' controls> <source src='{{mp4}}' type=\"video/mp4; codecs='avc1.42E01E, mp4a.40.2'\" /> </video>";
         $(".video_container").append(Mustache.to_html(videotag,opts));
         $(".video_container video").attr("autoplay", "true");
   }

    function insert_flash(opts) {
       var videotag = "<a class='flash_video' href='{{m4v}}' id='player' style='height:{{height}}px;width:{{width}}px;display:block;'></a>";
       $(".video_container").append(Mustache.to_html(videotag,opts));
       flowplayer("player", "/swfs/flowplayer-3.2.1.swf");
    }
   return({
     insert_video : function(opts) {
       if(supports_h264_baseline_video()) {
         insert_html5(opts)
       } else {
         insert_flash(opts)
      }
     }
   });
}();

$(function() {
   $(".landing .graphic").click(function() {
      demo_video.insert_video({
          mp4: "http://s3.amazonaws.com/mergefm-signup/mergefm.mp4",
          m4v: "http://s3.amazonaws.com/mergefm-signup/mergeFM.m4v",
          height: 265,
          width: 460
        });

      $(".graphic").remove();
   });

   $(".for_artists .video_container").one("click", function() {
    demo_video.insert_video({
        mp4: "http://s3.amazonaws.com/mergefm/artist_tutorial.m4v",
        m4v: "http://s3.amazonaws.com/mergefm/artist_tutorial.m4v",
        height: 328,
        width: 437
      });
   });
});

