/**
 * @author Brian Sage
 * @projectDescription Requires jQuery and SWFObject JS libraries
 */
/*
Copyright 2009  Brian Sage  (email : brian at sagehome dot com)

All rights are reserved by the author. 

This program is NOT for redistribution, customization, nor modification.
If you have obtained this program for free, please contact the author. A
reward may be issued for information regarding piracy.

======================

WordPress-Mu is distributed under the GNU General Public License.

You should have received a copy of the GNU General Public License
along with your distribution of WordPress; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/





var getQueryParams = function(qs){
  var params = {};
  var search_params = (qs.indexOf('&') != 0) ? qs.split("&") : [qs];
  
  for (var i=0;i<search_params.length; i++) {
    params[search_params[i].split('=')[0]] = search_params[i].split('=')[1];
  }
  
  return params;
}



//Failsafe console object
if (typeof window.console == 'undefined') window.console = {log:function(){}};

//Failsafe multiplus object
if (typeof multiplus == 'undefined') var multiplus = [];
if (typeof multiplus.plugins == 'undefined') multiplus.plugins = [];

//Multiplus Media Plugin
if (typeof multiplus.plugins.youtube == 'undefined') {
  
  
  multiplus.plugins.youtube = {
    


    
    init: function(){
      console.log('multiplus.plugins.youtube.init()');
      
      jQuery('.multiplus_media_youtube').each(function(){
        multiplus.plugins.youtube.flv(jQuery(this));
      });
    },
    
    
    
    
    flv: function(imgObj){
      console.log('multiplus.plugins.youtube.flv()');
      
      /*
       * Translate
       * http://www.youtube.com/watch?v=LrgcIfLa_cc
       * 
       * into
       * http://www.youtube.com/v/LrgcIfLa_cc&hl=en&fs=1&
       * 
       * into
       * <object width="853" height="505">
       *   <param name="movie" value="http://www.youtube.com/v/LrgcIfLa_cc&hl=en&fs=1&rel=0&hd=1"></param>
       *   <param name="allowFullScreen" value="true"></param>
       *   <param name="allowscriptaccess" value="always"></param>
       *   <embed src="http://www.youtube.com/v/LrgcIfLa_cc&hl=en&fs=1&rel=0&hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed>
       * </object>
       */
      var videoUrl = jQuery(imgObj).attr('alt');
      
      if((videoUrl.indexOf('youtube.com/watch?') + videoUrl.indexOf('v=')) >= 21){
        console.log('manipulating worthy YouTube video url');
      }
      
      var yt_GET = getQueryParams(videoUrl.split('?')[1]);
      if (typeof yt_GET['v'] != 'undefined') {
        var youtubeUrl = 'http://www.youtube.com/v/' + yt_GET['v'] + '&hl=en&fs=1&rel=0&hd=1';
      } else {
        return false;
        console.log('Invalid YouTube URL in embeded video');
      }
      
      var videoWidth = parseInt(jQuery(imgObj).attr('width'));
      var videoHeight = parseInt(jQuery(imgObj).attr('height'));
      
      var videoBox = document.createElement('div');
      jQuery(videoBox).width(videoWidth).height(videoHeight).addClass('multiplus_media_box').addClass('multiplus_media_youtube');

      jQuery(imgObj).replaceWith(videoBox);

      // Test for Flash Player plugin            
      if (typeof jQuery().flash == 'undefined') {
        jQuery(videoBox).addClass('multiplus_youtube_needsflash').unbind('click').bind('click',function(){
          window.location='http://get.adobe.com/flashplayer/';
        });
      }
      
      // Write flash <object> to the DOM
      else {
        jQuery(videoBox).flash({
          expressInstall: '/wp-content/plugins/multiplus_media/flash/expressInstall.swf',
          swf: youtubeUrl,
          height: videoHeight,
          width: videoWidth,
          hasVersion: 10,
          params: {
            wmode: "transparent",
            allowFullScreen: "true",
            allowScriptAccess: "always"
          }
        });
      }
    }
    
    
    
    
  }
  jQuery(function(){
    multiplus.plugins.youtube.init();
  });
}
