/**
* dynamically transform youtube links into embedded ytube iframes.
*
* Essentially the same as the old youtube flash embed - BUT without the flash.
* In this code, the <a href=""></a> tag is hidden and the video embedded via an iframe.
*
* NOTE:
*
* You can set the width / height by
* custom attributes videoheight and videowidth  (lowercase)  which should be NUMERIC ONLY
*
* by using a class, one of : small-video, mid-video , large-video, huge-video
* leaving blank will resort to the default size of 560 x 349 (youtubes default at time of writing)
*
**/

$(document).ready(function(){

    var sSel = 'a[href^="http://www.youtube.com/watch"]:not(.dont-embed)';
        sSel += ',a[href^="http://youtube.com/watch"]:not(.dont-embed)';
        sSel += ',a[href^="http://www.youtube.com/view_play_list"]:not(.dont-embed)';
        sSel += ',a[href^="http://youtube.com/view_play_list"]:not(.dont-embed)';
        sSel += ',a[href^="http://youtu.be/"]:not(.dont-embed)';
        sSel += ',a[href^="https://youtu.be/"]:not(.dont-embed)';
        // No Cookie Links as well
               
        
    $(sSel).each( function() {
      
       var nWidth  = '' ;
       var nHeight = '' ;
       var href    = '' ;
       
       if( typeof($(this).attr('videowidth'))!='undefined') {
          nWidth  = parseInt($(this).attr('videowidth')) ;
       }

        if($(this).attr('videoheight')!='') {
          nHeight  = parseInt($(this).attr('videoheight')) ;
        }
                
        /*
        * Various Youtube Default Sizes
        * 560 x 349
        * 640 x 390
        * 853 x 510
        * 1280 x 750
        */

        if (nWidth=='' || nHeight=='') {
        
          if($(this).hasClass('small-video')) {
            nWidth  = 256 ;
            nHeight = 168 ; // 16: 9 is 256:144 you need to add 28px on for the control bar
          } else if($(this).hasClass('mid-video')) {
            nWidth  = 480;
            nHeight = 295;
          } else if($(this).hasClass('large-video')) {
            nWidth  = 853;
            nHeight = 510;
          } else if($(this).hasClass('huge-video')) {
            // you probably should never use this one  
            nWidth  = 1280;
            nHeight = 750;
          } else {
            // Default = a Good size!
            nWidth  = 560;
            nHeight = 349;
          }
        }
                     
        // Ok we have the width/height and href
        // transform this into an iFrame embed tag
        // FORMAT is
        //
        // <iframe width="560" height="349" src="https://www.youtube-nocookie.com/embed/CX2cIMao9ek" frameborder="0" allowfullscreen></iframe>
        //
          // Short YouTube link  
        if($(this).attr('href').match('view_play_list')) {
          bPlaylist = true
          // aMatches = /p=(.+?)(&|$)/i.exec($(this).attr('href'))
          aMatches = /[\?|&]p=([^&]+)/i.exec($(this).attr('href')) ;
          if (aMatches) {
            href = aMatches[1] ;
          }
        }           
        if(href=='' || typeof(href)=='undefined') {
          bPlaylist = false 
          // aMatches = /v=(.+?)(&|$)/i.exec($(this).attr('href'))
          // aMatches = /[\?|&]v=([^&]+)/i.exec($(this).attr('href')) // OLD flash embed version
          aMatches = /[\?|&]v=([^&]+)/i.exec($(this).attr('href')) ;
          if (aMatches) {
            href = aMatches[1] ;
          }
        }
        
        if( (href=='' || typeof(href)=='undefined') &&  $(this).attr('href').match('youtu.be') ) {
          aMatches = /\.be\/([a-zA-Z0-9]+)/i.exec($(this).attr('href')) ;
          if (aMatches) {
            href = aMatches[1] ;
          }
        }
        
        if( href !='' && typeof(href)!='undefined') {
          
         //NB theme can be dark or light
         // color can be red or white
         // dark + red is the deafult flash version, if non falsh is detected and HTML 5 is served it will serve the light theme
         // therefore you should specifiy your preferred theme/color combo
         // more: http://apiblog.youtube.com/2011/08/coming-soon-dark-player-for-embeds.html
        
          var thisHtml = '<'+'ifr'+'ame ';
              thisHtml+= 'width="'+nWidth+'" height="'+nHeight+'" ';
              thisHtml+= ' src="http'+'://www.youtube'+'-nocookie'+'.com/embed/'+href+'?theme=dark&color=red" ';
              thisHtml+= ' frameborder="0" allowfullscreen';
              thisHtml+= ' >'+'<'+'/'+'ifr'+'ame'+'>';

          $(this).css("display","block") ;
          $(this).css("text-align","center") ;
          $(this).parent().before(thisHtml); //assuems the link is in a <p> or <div>
          $(this).hide();
        }
    
    });// end each link
          
});
