﻿// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Library of methods responsible for media manipiulation such as
 * resizing images and videos, dynamically adding videos and images.
 *
 * @author Michal Drewniak
 */

/**
 * See if the global namespace object 'gweb' exists. If it doesn't, create it.
 */
var gweb = gweb || {};

/**
 * See if the object gweb.media exists. If it doesn't create it.
 */
gweb.media = gweb.media || {};

/**
 * See if the object gweb.media.youtube exists. If it doesn't create it.
 */
gweb.media.youtube = gweb.media.youtube || {};


/**
 * Adds youtube video to the specified container.
 * @param {string} ytId ID of the youtube video.
 * @param {string} containerId ID of the container to which we add the video.
 * @param {number} width Width of the video.
 * @param {number} height Height of the video.
 * @param {number} relative Parameter indicating whether relative videos
 *    should be shown. Options: 0, 1.
 * @param {number} auto Parameter controling autoplay option of the video.
 * @param {boolean} overwrite If true, content of the container will be
 *    overwriten. If false, youtube will be appended. Default = false.
 * @return {string} String added to the container.
 */
gweb.media.youtube.addVideo = function(ytId, containerId,
                                       width, height,
                                       relative, auto, overwrite) {
  var container = document.getElementById(containerId);
  var width = width || '560';
  var height = height || '340';
  var relative = relative || '0';
  var auto = auto || '0';
  var overwrite = overwrite || true;

  if (!container || !ytId) return;

  var ytVideo = '<object type="application/x-shockwave-flash"' +
        'style="width:' + width + 'px; height:' + height + 'px;"' +
        'data="http://www.youtube.com/v/' + ytId +
        '&rel=' + relative + '&auto=' + auto + '">' +
        '<param name="movie" value="http://www.youtube.com/v/' + ytId +
        '&rel=' + relative + '&auto=' + auto + '" />' +
        '</object>';

  // Clear container
  if (overwrite) container.innerHTML = '';

  container.innerHTML += ytVideo;
  return ytVideo;
};
