(function(global) {
  var s150 = global.s150 || (global.s150 = {}), $ = global.jQuery;
  
  var theIntervalID = -1, playerVerticalCropAmount = 100, thePlayer, videoId;
  global.__bg_video_started = false;
  global.__bg_video_play = false;

  global.onYouTubePlayerReady = function onYouTubePlayerReady() {   
    //global._log('onYouTubePlayerReady');
    if ( global.__bg_video_play == false ) {
      return;
    };
    theIntervalID = -1;
    playerVerticalCropAmount = 100;
  
    thePlayer = document.getElementById("chromelessPlayer");
    thePlayer.addEventListener("onStateChange", "onYouTubePlayerStateChange");
    thePlayer.cueVideoById(videoId);    
    //we don't want audio all the time, so add this back later on in the day (9/17/2010).
    thePlayer.mute();
    thePlayer.playVideo();
    
    $(window).bind("resize", adjustPlayerHeight);
    
    adjustPlayerHeight();
  }
  
  global.onYouTubePlayerStateChange = function onYouTubePlayerStateChange(stateCode){
    // if playback has begun and the timer hasn't started
    if(stateCode == 1 && theIntervalID == -1 && global.__bg_video_started == true){
      // start the timer
      theIntervalID = setInterval(checkPlayPoint, 500);
    }
  }
  
  global.checkPlayPoint = function checkPlayPoint(){
    // if the duration is known and we're within 5 seconds of it
    //global._log(thePlayer.getCurrentTime());
    if(thePlayer.getDuration() > 0 && thePlayer.getDuration() - thePlayer.getCurrentTime() < 3 /* 2 seconds or less to the end of the video */){ 
      // restart the video
      //global._log('player seek back to 0');
      thePlayer.seekTo(0);
    }
  }
    
  function getTargetHeight(){
    return $(window).height() + (playerVerticalCropAmount * 2); 
  }
  
  function adjustPlayerHeight(e){
    $("#chromelessInner").height(getTargetHeight()).css("margin-top", -playerVerticalCropAmount);
  }
  
  function initPlayer(){
    var __params = {
      allowScriptAccess: "always",
      wmode: "transparent",
      allowFullScreen: "true"
    };
  
    var __attributes = {
      id: "chromelessPlayer"
    };
  
    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3&playerapiid=chromelessPlayer", "chromelessPlayer", "100%", "100%", "8", null, null, __params, __attributes);
    
    adjustPlayerHeight();
  }
  
  function handleWindowResize() {
    $('body')[$(window).height() > 955 ? 'addClass' : 'removeClass']('fixed');
  }
  
  global.__play_bg_video = function (vid_id) {
    if ( ! vid_id || vid_id == '' ) {
      // no video - just skip that
      return false;
    };
    global.__bg_video_play = true;
    videoId = vid_id;
    if ( global.__bg_video_started == false ) {
      initPlayer();
      //global._log('__bg_video_started == false');
      global.__bg_video_started = true;
    } else {
      //global._log('__bg_video_started == true');
      $('#chromelessPlayer').css('visibility', 'visible');
      //global._log(thePlayer);
      thePlayer.cueVideoById(videoId);
      thePlayer.mute();
      thePlayer.playVideo();
      thePlayer.addEventListener("onStateChange", "onYouTubePlayerStateChange");
    };
  }

  global.__stop_bg_video = function () {
      global.__bg_video_play = false;
      
      try {
        thePlayer.stopVideo();
      } catch (err) {
        // we've already stopped and hid the video
      }
      clearInterval(theIntervalID);
      theIntervalID = -1;
      $('#chromelessPlayer').css({visibility: 'hidden'});
  }

  global.__unpause_yt_vid = function () {
    thePlayer.playVideo();
  }

  global.__pause_yt_vid = function () {
    thePlayer.pauseVideo();
  }

  $(document).ready(function() {
    $(window).bind('resize', handleWindowResize);
    handleWindowResize();
    
    if ($.browser.msie && $.browser.version.substr(0,1)<8) {
      // search for selectors you want to add hover behavior to
      $('body').addClass('no-support');
    }
    
  });
})(this);
