﻿$.fn.inlineVideo = function(o) {
    o = o || { WIDTH: 480, HEIGHT: 320 };
    return this.each(function() {
        var videoFrame = $(this);
        var img = videoFrame.find(o.coverImage || "img.coverImg");
        var box = videoFrame.find(o.videoBox || "div.videoBox");
        var player = videoFrame.find(o.videoBox || "div.videoPlayer");
        var playButton = videoFrame.find(o.playButton || "a.playButton");
        var closeButton = videoFrame.find(o.closeButton || "a.closeButton");
        var doWork = function(){
          var isLoaded = false;
          var hi = img.height();
          var wi = img.width();
          //alert("wi:"+wi+" hi:"+hi);
          var H = o.HEIGHT;
          var W = o.WIDTH;
          var a = H*1.0/W;
          var b = 0;
          if ( W-wi!=0 ) {
            a = (H - hi) * 1.0 / (W - wi);
            b = (hi * W - H * wi) * 1.0 / (W - wi);
          }
          var prevWidthImg = wi;
          var imgNotActive = false;
          
          var doPlay = function() {
              if ( imgNotActive )
                return;
              imgNotActive = true;
              playButton.hide();
              //in the animation, h will be h(w)=a*w+b
              videoFrame.animate({ width: W }, {
                  duration: o.duration || 700,
                  easing: "swing",
                  step: function() {
                      var w = $(this).width();
                      if ( w > prevWidthImg ) {
                        box.width(w);
                        img.width(w);
                        img.height(parseInt(a * w + b));
                        prevWidthImg = w;
                      }
                  },
                  complete: function() {
                      if (!isLoaded) {
                          isLoaded = true;
                          try {
                              var flashvars = o.flashvars || { videoLink: "/common/flash/demo.flv", autoplay: true, loop: true, image: "/images/firstlogo.jpg" };
                              var params = o.flashparams || { allowFullScreen: true };
                              var attributes = o.flashattr || {};
                              var flashsettings = o.flashsettings || {};
                              flashsettings.swfurl = flashsettings.swfurl || "/common/flash/videoplayer.swf";
                              flashsettings.alternateId = flashsettings.alternateId || "alternateContent";
                              flashsettings.version = flashsettings.version || "9.0.0";
                              flashsettings.xiswfurl = flashsettings.xiswfurl||"/common/flash/expressInstall.swf";
                              swfobject.embedSWF(
                                  flashsettings.swfurl,
                                  flashsettings.alternateId,
                                  "" + W,
                                  "" + H,
                                  flashsettings.version,
                                  flashsettings.xiswfurl,
                                  flashvars,
                                  params,
                                  attributes
                              );
                          } catch (ex) {
                              alert(ex.message);
                          }
                      }
                      img.hide();
                      player.show();
                      closeButton.show();
                  }
              });
          };
          img.click(doPlay);
          playButton.click(doPlay);
          closeButton.click(function() {
              $(this).hide();
              player.hide();
              img.show();
              videoFrame.animate({ width: wi }, {
                  duration: o.duration || 700,
                  easing: "swing",
                  step: function() {
                      var w = $(this).width();
                      if ( w<prevWidthImg ) {
                        box.width(w);
                        img.width(w);
                        img.height(parseInt(a * w + b));
                        prevWidthImg = w;
                      }
                  },
                  complete: function() {
                      imgNotActive = false;
                      playButton.show();
                      
                  }
              });
          });
        };
        if ( img[0].complete )
          doWork();
        else
          img.load(doWork);
    });
}	    

