getDeviceOrientation = function () {

    if ('matchMedia' in window) {
      if (window.matchMedia("(orientation: portrait)").matches) {
        return 'portrait';
      }
      else if (window.matchMedia("(orientation: landscape)").matches) {
        return 'landscape';
      }
    }

    if (screen.height > screen.width) {
      return 'portrait';
    }

    // Even square devices have orientation,
    //   but a desktop browser may be too old for `matchMedia`.
    // Defaulting to `landscape` for the VERY rare case of a square desktop screen is good enough.
    return 'landscape';
  }
This will load the youtube API (if not loaded yet)
Use $(window).one('youtube_api_load', ...) to listen for API loaded event