/**
 * @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
*/




//Failsafe console object
if (typeof console == 'undefined') var 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.media == 'undefined') {
  
  
  multiplus.plugins.media = {
    


    
    init: function(){
      console.log('multiplus.plugins.media.init()');
      
      jQuery('.multiplus_media_thumb').each(function(){
        mpls_media_ext = jQuery(this).attr('alt').slice(-4);
        switch(mpls_media_ext){
          case '.flv':
          case '.f4v':
            multiplus.plugins.media.flv(jQuery(this));
            break;
          case '.swf':
            multiplus.plugins.media.swf(jQuery(this));
            break;
          default:
            if(jQuery(this).hasClass('multiplus_media_embed')){
              multiplus.plugins.media.embed(jQuery(this));
            }
            break;
        }
      });
    },
    
    
    
    
    flv: function(imgObj){
      console.log('multiplus.plugins.media.flv()');
      
      var videoUrl = jQuery(imgObj).attr('alt');
      var videoWidth = parseInt(jQuery(imgObj).attr('width'));
      var videoHeight = parseInt(jQuery(imgObj).attr('height'));
      var videoRatio = (videoWidth > (videoHeight*1.5)) ? '_ws':'';
      var videoPlayer = '/wp-content/plugins/multiplus_media/flash/multiplus_flvplayer'+videoRatio+'.swf';
      var videoAutoplay = 'false';
      
      var videoBox = document.createElement('div');
      jQuery(videoBox).width(videoWidth).height(videoHeight).addClass('multiplus_media_box').addClass('multiplus_media_flv');

      jQuery(imgObj).replaceWith(videoBox);

      // Test for Flash Player plugin            
      if (typeof jQuery().flash == 'undefined') {
        jQuery(videoBox).addClass('multiplus_media_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: videoPlayer,
          height: videoHeight,
          width: videoWidth,
          hasVersion: 10,
          flashvars: {
            vidurl: videoUrl,
            autoplay: videoAutoplay
          },
          params: {
            wmode: "transparent",
            allowScriptAccess: "always"
          }
        });
      }
    },
    
    
    
    
    swf: function(imgObj){
      console.log('multiplus.plugins.media.swf()');
      
      var videoUrl = jQuery(imgObj).attr('alt');
      var videoWidth = parseInt(jQuery(imgObj).attr('width'));
      var videoHeight = parseInt(jQuery(imgObj).attr('height'));
      var videoRatio = (videoWidth > (videoHeight*1.5)) ? '_ws':'';
      
      var videoBox = document.createElement('div');
      jQuery(videoBox).width(videoWidth).height(videoHeight).addClass('multiplus_media_box').addClass('multiplus_media_swf');

      jQuery(imgObj).replaceWith(videoBox);

      // Test for Flash Player plugin            
      if (typeof jQuery().flash == 'undefined') {
        jQuery(videoBox).addClass('multiplus_media_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: videoUrl,
          height: videoHeight,
          width: videoWidth,
          hasVersion: 10,
          params: {
            wmode: "transparent",
            allowScriptAccess: "always"
          }
        });
      }
    },
    
    
    
    
    embed: function(imgObj){
      console.log('multiplus.plugins.media.embed()');
      
      var embedParams = jQuery(imgObj).attr('alt');
      var embedParamsObj = {
          wmode: "transparent",
          allowScriptAccess: "always"
        }
      if(embedParams.indexOf('{') > 0) return false;
      
      
      console.log( embedParams );
      var embedParamsArr = embedParams.replace(/[\{\}]+/g,'').split("',");
      for (var i=0; i < embedParamsArr.length; i++){
        embedParamsObj[embedParamsArr[i].split(":'")[0].toLowerCase()] = embedParamsArr[i].split(":'")[1];
      }
      console.log( embedParamsObj );
      
      
      
      var embedUrl = (typeof embedParamsObj['movie'] != 'undefined') ? embedParamsObj['movie'] : jQuery(imgObj).attr('alt');
      var embedWidth = parseInt(jQuery(imgObj).attr('width'));
      var embedHeight = parseInt(jQuery(imgObj).attr('height'));
      

      
      var embedBox = document.createElement('div');
      jQuery(embedBox).width(embedWidth).height(embedHeight).addClass('multiplus_media_box').addClass('multiplus_media_embed');

      jQuery(imgObj).replaceWith(embedBox);

      // Test for Flash Player plugin            
      if (typeof jQuery().flash == 'undefined') {
        jQuery(embedBox).addClass('multiplus_media_needsflash').unbind('click').bind('click',function(){
          window.location='http://get.adobe.com/flashplayer/';
        });
      }
    
      // Write flash <object> to the DOM
      else {
        jQuery(embedBox).flash({
          expressInstall: '/wp-content/plugins/multiplus_media/flash/expressInstall.swf',
          swf: embedUrl,
          height: embedHeight,
          width: embedWidth,
          hasVersion: 10,
          params: embedParamsObj
        });
      }
    }
    
    
    
    
  }
  jQuery(function(){
    multiplus.plugins.media.init();
  });
}







// SpokesPerson JS Object avoids re-using any global variables
var SP = {
  videoUrl : 0,
  
  browserSucks: function(){
    return (typeof(document.body.style.filter) != 'undefined' ) ? true : false;
  },

  init: function(videoUrl){
    if(videoUrl) SP.videoUrl = videoUrl;
    
    $('spokesperson').innerHTML = '<div id="spokesperson_stopped"><div id="spokesperson_open" onclick="SP.show();" title="Play video guide"></div><div id="spokesperson_poster" onclick="SP.show();" title="Play video guide"></div></div><div id="spokesperson_playing"><div id="spokesperson_close" onclick="SP.hide();" title="Hide video guide"></div><div id="spokesperson_video"></div></div>';

    $('spokesperson_playing').style.cssText = 'display: none; position:fixed; bottom:0; right:0; width:320px; height:320px; padding:0; margin:0; line-height:0;';
    $('spokesperson_stopped').style.cssText = 'position:fixed; bottom:0; right:0; width:186px; height:166px; padding:0; margin:0; line-height:0;';
    $('spokesperson_video').style.cssText = 'width:320px; height:320px;';
    $('spokesperson_poster').style.cssText = 'width:186px; height:166px; background: transparent url(/images/video_poster.png) no-repeat 0 0;';
    $('spokesperson_open').style.cssText = 'position:fixed; bottom: 36px; right: 39px; z-index:10000; width:20px; height:20px; background: transparent url(/images/video_play.png) no-repeat 0 -20px;';
    $('spokesperson_close').style.cssText = 'position:fixed; bottom: 36px; right: 39px; z-index:10000; width:20px; height:20px; background: transparent url(/images/video_close.png) no-repeat 0 0;';
    
    SP.dontShow = SP.readCookie('spokesperson');
    if (!SP.dontShow) {
      SP.show(videoUrl);
    }
  },
    
  hide: function(){
    $("spokesperson_video").flvplayerPause();
    if (SP.browserSucks()) {
      $('spokesperson_playing').hide();
      $('spokesperson_stopped').style.display = 'block';
    } else {
      $('spokesperson_playing').fade({ from:1, to:0 });
      $('spokesperson_stopped').appear();
    }
  },
  
  show: function(videoUrl){
    if(!videoUrl) videoUrl = SP.videoUrl;;
    
    if (SP.browserSucks()) {
      $('spokesperson_stopped').hide();
      $('spokesperson_playing').style.display='block';
    } else {
      $('spokesperson_stopped').fade({ from:1, to:0 });
      $('spokesperson_playing').appear();
    }
    
    // Write flash <object> to teh DOM
    swfobject.embedSWF('/flash/spokesperson_320_walpha.swf', "spokesperson_video", "320", "320", "9.0.0", "/flash/expressInstall.swf", {
      vidurl: videoUrl
    }, {
      wmode: "transparent",
      allowScriptAccess: "always"
    });
    
    // Create or refresh the cookie, so video remains hidden for frequent visitors
    SP.createCookie('spokesperson','dontShow',7)
  },
  
  // Great thanks to http://www.quirksmode.org/js/cookies.html
  createCookie: function(name, value, days){
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
    }
    else 
      var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  },
  
  readCookie: function(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') 
        c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) 
        return c.substring(nameEQ.length, c.length);
    }
    return false;
  },
  
  eraseCookie: function(name){
    createCookie(name, "", -1);
  }
};
