 // Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Javascript file to display a custom YouTube player.
 * @author dave carlsson
 */

/**
 * Namespace for press website.
 * @type {Object}
 */
var press = {};

/**
 * Media class. Wrapper for all media related Javascript content.
 * @constructor
 */
press.Media = function() {
  this.generateCustomPlayer(this.videoId, this.container);
};

/**
 * Called on initial page load to set the correct custom player on the page.
 * Sets either a default player (set in youtube_custom_player_strings.js)
 * or the appropriate player parsed from the URL hash string.
 * Calls generateCustomPlayer to load the correct player.
 * @param {string} container The ID value for the container that will hold
 *     the custom player.
 */
press.Media.loadCustomPlayer = function(container) {
  var customPlayerContainer = document.getElementById(container);

  if (document.location.hash) {
    var preLoadedPlayerName = document.location.hash.substr(1);
  } else {
    var preLoadedPlayerName = this.defaultPlayerName;
  }

  if (preLoadedPlayerName in this.moviePlayers) {
    var player = this.moviePlayers[preLoadedPlayerName];
    var name = this.moviePlayerNames[preLoadedPlayerName];
  }

  this.generateCustomPlayer(name, player, container);
}

/**
 * Writes the inner HTML of an indicated container with a custom YouTube
 * player.
 * @param {string} videoTitle The unique ID for the YouTube video player.
 * @param {string} videoId The unique ID for the YouTube video player.
 * @param {string} container The ID value for the container that will hold
 *     the custom player.
 */
press.Media.generateCustomPlayer = function(videoReference,
                                            videoId,
                                            container) {
 if (typeof videoReference.firstChild === 'undefined') {
    var videoTitle = videoReference;
  } else {
    var videoTitle = videoReference.firstChild.nodeValue;
  }

  var customPlayerContainer = document.getElementById(container);
  var customPlayerHtml = '<object width="610" height="344">' +
      '<param name="movie" value="http://www.youtube.com/cp/' +
      videoId + '="></param><embed src="http://www.youtube.com/cp/' +
      videoId + '=" type="application/x-shockwave-flash"' +
      'width="610" height="344"></embed></object>';

  customPlayerContainer.innerHTML = '<h2>' + videoTitle + '</h2>';
  customPlayerContainer.innerHTML += customPlayerHtml;
}
