(function($) {
  Clameurs = function(){
    function init(){
      console.log("Clameurs Mod");
      initVideoEvents();
    }
    function initVideoEvents(){
      $('.field-type-video-embed-field a').on('click', clickVideo);
    };
    function clickVideo(event){
      event.preventDefault();
      console.log('Click video', this);
      var vid_src = $(this).attr('href');
      console.log("vid_src", vid_src);
      getEmVidField(vid_src);
      return false;
    };
    function getEmVidField(src){
      $.getJSON('clameursmod/getemvidfield',
        {'src':src},
        function(json){
          console.log("json loaded", json);
          displayVid(json)
        }
      );
    };
    function displayVid(json){
      $('#videoframe').remove();
      $vid = $('
')
        .attr('id', 'videoframe')
        .append(json.embed)
        .hide()
        .appendTo('body');
      var iframe = $('iframe', $vid)[0];
      var winwidth = $(window).width();
      var winheight = $(window).height();
      console.log('win :'+winwidth+' | '+winheight);
      if (winwidth / winheight < 640 / 360) {
        // console.log("Width target");
        var w = winwidth*0.9;
        var h = w*(360/640);
      }else{
        // console.log("height target");
        var h = winheight*0.9;
        var w = h*(640/360);
      }
      console.log('win :'+w+' | '+h);
      $.openDOMWindow({
        loader:0,
  			windowPadding:10,
        overlay:1,
  			overlayColor:'#fff',
  			overlayOpacity:'90',
        borderColor:'transparent',
  			borderSize:'0',
        width:w,
        height:h,
        windowSourceID:"#videoframe",
        // functionCallOnClose:function(){
        //   console.log('overlay closed');
        //   $('#videoframe').remove();
        // },
        // functionCallAfterClose:function(){
        //   console.log('overlay closed');
        //   $('#videoframe').remove();
        // }
      });
      $vid.on('DOMSubtreeModified', function(e){
        console.log('video frame changed', e);
        $(this).off('DOMSubtreeModified').remove();
      });
      // autoplay vid youtube
      // see patch https://www.drupal.org/node/2685721#comment-11032685
    };
    function onCloseOverlay(){
      $('#videoframe').remove();
    };
    init();
  };
  $(document).ready(function() {
    var clameurs = new Clameurs();
  });
})(jQuery);