$(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).each( function(){

        if($(this).hasClass('smaller-video')) {
          nWidth  = 400
          nHeight = 245
        } else if($(this).hasClass('trust-video')) {
          nWidth  = 354
          nHeight = 213
        } else if($(this).hasClass('news-story')) {
          nWidth  = 440
          nHeight = 270
        } else {
          nWidth  = 480
          nHeight = 295
        }
        $(this).flash(
          { width: nWidth, height: nHeight, wmode: 'transparent' },
          { version: 8 },
          function(htmlOptions) {

              // first check for playlist
              if($(this).attr('href').match('view_play_list')) {
                bPlaylist = true
                // aMatches = /p=(.+?)(&|$)/i.exec($(this).attr('href'))
                aMatches = /[\?|&]p=([^&]+)/i.exec($(this).attr('href'))
                href = aMatches[1]
              } else {
                bPlaylist = false
                // aMatches = /v=(.+?)(&|$)/i.exec($(this).attr('href'))
                aMatches = /[\?|&]v=([^&]+)/i.exec($(this).attr('href'))
                href = aMatches[1]
              }
              if(bPlaylist) {
                htmlOptions.src = "http://youtube.com/p/" + href;
              } else {
                htmlOptions.src = "http://youtube.com/v/" + href;
              }
              $(this).parent().css("text-align","center")
              $(this).css("display","block")
              $(this).css("text-align","center")
              $(this).before($.fn.flash.transform(htmlOptions));
          }
        );
    })
});



/** Trails Page Video Toggles **/
  $('a[rel="youtubevideo"]').live("click",function(){
    var nWidth  = 448;
    var nHeight = 320;

    $(this).flash(
      { width: nWidth, height: nHeight },
      { version: 8 },
      function(htmlOptions) {
        aLink = $(this).attr('href').split("v=")
        href = aLink[1]
        htmlOptions.src = "http://youtube.com/v/" + href;
        $('#mainyoutubevideo').css("display","block")
        $('#maingooglevideo').css("display","none")
        //now remove our
        $('#mainyoutubevideo').css('background','url(/img/ci-loader.gif) 50% 50% no-repeat;');
        $('#mainyoutubevideo').empty();
        $('#mainyoutubevideo').append($.fn.flash.transform(htmlOptions));
       }
     );

     return false;

  });


/** Trails Page Video Toggles **/
  $('a[rel="googlevideo"]').live("click",function(){
    var nWidth  = 448;
    var nHeight = 320;

    $(this).flash(
      { width: nWidth, height: nHeight },
      { version: 8 },
      function(htmlOptions) {
        aLink = $(this).attr('href').split("docid=")
        href = aLink[1]
        htmlOptions.src = "http://video.google.com/googleplayer.swf?docid=" + href;
        $('#mainyoutubevideo').css("display","none");
        $('#maingooglevideo').css("display","block")
        //now remove our
        $('#maingooglevideo').css('background','url(/img/ci-loader.gif) 50% 50% no-repeat;');
        $('#maingooglevideo').empty();
        $('#maingooglevideo').append($.fn.flash.transform(htmlOptions));
       }
     );

     return false;

  });