﻿$.fn.inlineVideoYoutube = function(o) {
  o = o || { };
  return this.each(function() {
    try{
    var videoContent = $(this);
    var videoFrame = videoContent.find("div.videoFrameYoutube");
    var videoId = videoFrame.attr("videoid")||o.videoId;
    var videoWidth = videoFrame.attr("videowidth")||o.width;
    var videoHeight = videoFrame.attr("videoheight")||o.height;
    var thumb_width = o.thumb_width || 150;
    var base_url = location.protocol + '//' + location.host;
    var embed_url = o.embed_url||'/common/popups/youtube_embed.aspx?v=';
    if ( !videoId || videoId.length!=11 )
      return;
    function makeYoutubeVideo(data) {
      try{
        var DefaultData = {
            title: 'Video',
            width: 320,
            height: 240,
            thumbnail_width: 150,
            thumbnail_height: 113
        };
        var DefaultThumbUrl = '/common/images/youtube-test.png';
        data = data || {};
        data.title = data.title || DefaultData.title;
        data.width = data.width || DefaultData.width;
        data.height = data.height || DefaultData.height;
        data.thumbnail_width = data.thumbnail_width || DefaultData.thumbnail_width;
        data.thumbnail_height = data.thumbnail_height || DefaultData.thumbnail_height;
        var videoPlayer = videoFrame.find('div.videoPlayer');
        var title = data["title"];
        var width = data["width"];
        var height = data["height"];
        if ( videoWidth && width>0 ) {
          height = parseInt(videoWidth * height / width);
          width = videoWidth;
        }
        if ( videoHeight )
          height = videoHeight;
        var t_width = data["thumbnail_width"];
        var t_height = data["thumbnail_height"];
        var t_url = data["thumbnail_url"];
        var coverImg = videoFrame.find(o.coverImage||"img.coverImg");
        var coverImgPlay = videoFrame.find(o.coverImagePlay||"img.coverImgPlay");
        var coverImg2 = false;
        var superParent = videoContent.parents("div.popup-video-container");
        if ( superParent.length>0 ) {
          superParent = $(superParent[0]);
          coverImg2 = superParent.find(o.coverImage||"img.coverImg");
        }
        if (!t_url) {
            t_url = DefaultData.thumbnail_url;
            t_width = DefaultData.thumbnail_width;
            t_height = DefaultData.thumbnail_height;
        }
        if (t_width > thumb_width) {
            t_height = parseInt(thumb_width * t_height / t_width);
            t_width = thumb_width;
        }
        if ( t_url ) {
          coverImg.attr('src', t_url);
          if ( coverImg2 ) coverImg2.attr('src', t_url);
        }
        else if ( !coverImg.attr('src') )
          coverImg.attr('src',DefaultThumbUrl);
        if ( o.isFixed ) {
          coverImg.css("width",width+"px");
          coverImg.css("height",height+"px");
        } else {
          coverImg.css("width",t_width+"px");
          coverImg.css("height",t_height+"px");
        }
        if (title)
            videoContent.find("span.VideoTitle").html(title);
        if (!width) width = 320;
        if (!height) height = 240;
        //alert("width is " + width + " and thumbnail width is " + t_width);
        var alternateContent = videoPlayer.find(".alternateContent");
        alternateContent.width(width).height(height).css('display','block').css('text-align','center').css('vertical-align','middle');
        var alternateId = alternateContent.attr("id");
        var callParams = {
            WIDTH: width,
            HEIGHT: height,
            closeButton: 'a.closeButtonYoutube',
            flashsettings: {
                alternateId: alternateId,
                swfurl: 'http://www.youtube.com/v/'+videoId+'&hl=en_US&fs=1&rel=0&autoplay='+((o.isFixed&&!o.isPopup)?'0':'1')
            },
            flashparams: {
                allowFullScreen: true,
                allowscriptaccess: "always"
            },
            flashvars: {}
        };
        if ( o.isPopup )
          videoContent.width(width);
        if ( o.isFixed ) {
          videoFrame.show();
          if ( data.isError ) {} else {
            if ( !o.isPopup ) {
              if ( coverImgPlay ) coverImgPlay.hide();
              coverImg.hide();
              if ( coverImg2 ) coverImg2.hide();
            }
            videoPlayer.show();
            videoFrame.inlineFixVideo(callParams);
          }
        }else
          videoFrame.inlineVideo(callParams);
      } catch(ex){alert("[inlineVideoYoutube>makeYoutubeVideo] exception: "+ex.message);}
    }
    //should be added to a queue
    //jQuery.getJSON(embed_url+videoId, makeYoutubeVideo);        
    jQuery.ajax({
      url:embed_url+videoId,
      dataType: 'json',
      success:function(data, textStatus, XMLHttpRequest){
        makeYoutubeVideo(data);
      },
      error:function(XMLHttpRequest, textStatus, errorThrown){
        makeYoutubeVideo({isError: true});
      }
    });
    } catch(ex){alert("[inlineVideoYoutube] exception: "+ex.message);}
  });
}
$.fn.inlineFixVideoYoutube = function(o) {
  o = o || {};
  o.isFixed = true;
  return this.each(function() {$(this).inlineVideoYoutube(o);});
}

