/***********************************************************************
  playVideo initializes the SWFObject for video play and
  sets up the parameters of the video
  required: videoURL
  params:
***********************************************************************/
function playVideo (videoURL, lbid, vidWidth, vidHeight, vidDuration,  playerURL, widescreen, optionalParamStr) {
  // SWFplayer and ContainerDiv sizes are dynamic based on vidWidth & vidHeight
  
  flashPrefix = "/flash/";
  
  // test function variables
  if (videoURL == "") {return false;}
  if (lbid == "") {lbid = "lbVideo";}
  if (vidWidth == "") {vidWidth = "532";}
  if (vidHeight == "") {vidHeight = "420";}
  if (vidDuration == "") {vidDuration = "9999";}
  if (playerURL == null || playerURL == "") {
    if (!widescreen) { playerURL = flashPrefix + "FlashVideoPlayer.swf"; }
    else {playerURL = flashPrefix + "FlashVideoPlayer_16x9.swf";}
  }
  if (widescreen == "" || widescreen == null) widescreen = false;
  
  //get browser info
  var Browser = {             
    Version: function() {
      var version = 999; //assume not ie by default
      if (navigator.appVersion.indexOf("MSIE") != -1) //oh no, it IS ie!
        version = parseFloat(navigator.appVersion.split("MSIE")[1]);
      return version;
    }
  }
  var playerWidth = parseInt (vidWidth);
  var playerHeight = parseInt(vidHeight) + 40; // Player Controls in Flash are 40px tall
  var containerWidth = parseInt(playerWidth) + 32;
  if (Browser.Version() < 8){var containerHeight = parseInt(playerHeight) + 60;}
  else{var containerHeight = parseInt(playerHeight) + 32;}
  
  if (optionalParamStr == null || optionalParamStr == "") {
    optionalParamStr = "";
  }
  var flash1_contentVersion = "9.0.115";


  // get div if exists, create if it doesn't
  var lightboxDiv = $("#" + lbid);
  if(lightboxDiv.length == 0) {
    lightboxDiv = createLBdiv(lbid,widescreen,'body');
  }

  // setup SWFObject
  if (swfobject.hasFlashPlayerVersion(flash1_contentVersion)) {
    //EMBED SWFObject Flash Player
    var vidDiv = lbid + "noflash";
    var flashvars = {};
    flashvars.videoUrl = videoURL;
    flashvars.videoWidth = vidWidth;
    flashvars.videoHeight = vidHeight;
    flashvars.videoDuration = vidDuration;
    var flashparams = {};
    flashparams.menu = "false";
    flashparams.quality = "high";
    flashparams.wmode = "opaque";
    flashparams.bgcolor = "#3E3C3C";
    var flashattributes = {};

    swfobject.embedSWF(playerURL, vidDiv, playerWidth, playerHeight, flash1_contentVersion, null, flashvars, flashparams, flashattributes);

  } else if (supports_html5_h264_video()) {
    //EMBED HTML5 Video Player
    // NOTE that iPhone OS2 does not support HTML5 Video
    var videoattributes = {};
    videoattributes.src = videoURL;
    videoattributes.width = vidWidth;
    videoattributes.height = vidHeight;
    videoattributes.controls = "true";
    /*console.log(videoattributes);*/
    var video = $("<video></video>").attr(videoattributes);
    lightboxDiv.html('');
		lightboxDiv.append(video);

  }
  
  // open lightbox to display
  $.fn.colorbox({
    width: containerWidth,
    height: containerHeight,
    inline: true,
    href: "#" + lbid,
    onOpen: function() { //prevent scrolling
      var windTop = $(window).scrollTop();
      $('html').css('overflow','hidden');
      $(window).scrollTop(windTop);
    },
    onCleanup: function() {  //re-enable scrolling
      lightboxDiv.hide();
      var windTop = $(window).scrollTop();
      $('html').css('overflow','auto');
      $(window).scrollTop(windTop);
    },
    onClosed: function() { lightboxDiv.remove(); }
  });
  //tb_show("","#TB_inline?height=" + tb_height + "&width=" + tb_width + "&inlineId=lbVideo" + optionalParamStr,"");
  
}

/***********************************************************************
  supports_html5_h264_video tests the browser to see if it can
  play HTML5_h264 video natively
 ***********************************************************************/
function supports_html5_h264_video() {
	  return !!document.createElement('video').canPlayType('video/mp4;');
}


/***********************************************************************
  createsLBdiv creates a div for the video to go into
  params: id for the lightbox div, width of the video, where to append it
  returns the jQuery div object
 ***********************************************************************/
function createLBdiv (lbid,widescreen,appendIdent) {
  var lightboxDiv = $("<div></div>").attr({id: lbid});

  // Check for widescreen video, tag if true
  if (widescreen) { lightboxDiv.addClass("wide_video"); } //Is it wideformat?

  //create noflash div and insert
  var noflashDiv = $("<div></div>").attr("id",lbid+"noflash").addClass("no-flash");
  if (widescreen) {noflashDiv.addClass("wide_format");} //Is it wideformat?
  var noflashP1 = $("<p></p>").text("To see this video you need JavaScript enabled and the latest version of Flash.  You can also use a browser that supports H.264 HTML5 Video.").attr("style","padding-top:30px");
  var noflashP2 = $("<p></p>").text(" to go to the Adobe Flash download center.");
  noflashP2link = $("<a></a>").attr({href: "http://www.adobe.com/go/getflashplayer", target: "_blank"}).text("Click here");
  noflashP2.prepend(noflashP2link);
  noflashDiv.append(noflashP1).append(noflashP2);
  lightboxDiv.append(noflashDiv);

  //Insert lightboxDiv
  $(appendIdent).append(lightboxDiv);
  return lightboxDiv;
}
