function onYouTubePlayerReady(playerId) {
  var player = document.getElementById(playerId + '_ytplayer');
  var overlay = document.getElementById(playerId + '_overlay');
  var youtubeId = YouTube.videos[playerId];
  var cbFuncName = playerId.replace(/[^a-z0-9_]/gi, '_');
  if (overlay.__ytautostart) {
    player.loadVideoById(youtubeId);
  } else {
    player.cueVideoById(youtubeId);
  }
  YouTube.callbacks[cbFuncName] = function(newState) {
    if (newState == 0) {
      if (player.onEnd) { player.onEnd(); }
      if (overlay.__ytoverlayimg) {
        if (YouTube.detectMacXFF2()) {
          overlay.style.backgroundImage = 'url(' + overlay.__ytbgimg + ')';
        } else {
          overlay.style.filter = 'alpha(opacity=100)';
    			overlay.style['-moz-opacity'] = '1';
    			overlay.style.opacity = '1';
        }
      } else {
        player.cueVideoById(youtubeId);
      }
    }
  };
  player.addEventListener('onStateChange', 'YouTube.callbacks.' + cbFuncName);
}

var YouTube = {
  videos: {},
  callbacks: {},
  apibase: 'http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=',
  pixel: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABBJREFUeNpi+v//PwNAgAEACQYDAPgPh2QAAAAASUVORK5CYII=',
  detectMacXFF2: function() {
    var userAgent = navigator.userAgent.toLowerCase();
    if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
      var ffversion = new Number(RegExp.$1);
      if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
        return true;
      }
    }
  },
  load: function(containerId, youtubeId, width, height, autoStart, overlayUrl) {
    var container = document.getElementById(containerId);
    var overlayContainer = document.createElement('div');
    var overlayTop = document.createElement('div');
    var videoDiv = document.createElement('div');
    var videoDivId = containerId + '_ytvideo';
    var videoPlayerId = containerId + '_ytplayer';
    var overlayId = containerId + '_overlay';
    var macFF2 = this.detectMacXFF2();
    var params = { allowScriptAccess: 'always', wmode: 'transparent' };
    var atts = { id: videoPlayerId, style: 'position:absolute; left:0px; right:0px' };
    this.videos[containerId] = youtubeId;
    videoDiv.setAttribute('id', videoDivId);
    overlayTop.setAttribute('id', overlayId);
    container.style.position = 'relative';
    container.style.width = width + 'px';
    container.style.height = height + 'px';
    overlayContainer.style.position = 'absolute';
    overlayContainer.style.left = '0px';
    overlayContainer.style.right = '0px';
    overlayContainer.style.width = width + 'px';
    overlayContainer.style.height = '0px';
    overlayContainer.style.zIndex = '999';
    if (macFF2) {
      overlayTop.backgroundImage = 'url(' + this.pixel + ')';
      overlayTop.__ytbgimg = overlayUrl;
    } else {
      overlayTop.style.position = 'absolute';
      overlayTop.style.filter = 'alpha(opacity=0)';
      overlayTop.style.MozOpacity = '0';
      overlayTop.style.opacity = '0';
      overlayTop.style.backgroundColor = '#000000';
      if (overlayUrl) {
        overlayTop.style.backgroundImage = 'url(' + overlayUrl + ')';
      }
    }
    overlayTop.style.top = '0px';
    overlayTop.style.left = '0px';
    overlayTop.style.width = width + 'px';
    overlayTop.style.height = height + 'px';
    overlayTop.style.zIndex = '1000';
    if (overlayUrl) {
      overlayTop.__ytoverlayimg = true;
    } else {
      overlayTop.__ytoverlayimg = false;
    }
    overlayTop.__ytautostart = autoStart;
    container.appendChild(overlayContainer);
    overlayContainer.appendChild(overlayTop);
    container.appendChild(videoDiv);
    var self = this;
    overlayTop.onclick = function() {
      var player = document.getElementById(videoPlayerId);
      switch(player.getPlayerState()) {
      case 1:
        player.pauseVideo();
        break;
      default:
        player.playVideo();
        if (player.onPlay) { player.onPlay(); }
        if (YouTube.detectMacXFF2()) {
          overlayTop.style.backgroundImage = 'url(' + self.pixel + ')';
        } else {
          overlayTop.style.filter = 'alpha(opacity=0)';
    			overlayTop.style['-moz-opacity'] = '0';
    			overlayTop.style.opacity = '0';
        }
  			break;
      }
      return false;
    }
    swfobject.embedSWF(this.apibase + containerId, videoDivId, width, height, '8', null, null, params, atts);
  }
};