 // Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Javascript file for Latin America press center homepage.
 * Allows control over text content box for new news stories and YouTube
 * video featured content.
 * Requires use of http://www.google.com/js/feed.js.
 *
 * @author dave carlsson
 */

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

/**
 * The ATOM feed URL for the Spreadsheet containing news related information.
 * This is parsed in press.displayRecentNews.
 * @type {string}
 */
press.newsFeedUrl = 'http://spreadsheets.google.com/feeds/list/' +
    'r1lvoTeeQwriS3eL9m-e6Dg/od6/public/basic';

/**
 * The ATOM feed URL for the Spreadsheet containing YouTube movierelated
 * information.
 * This is parsed in press.displayFeaturedMovie.
 * @type {string}
 */
press.movieFeedUrl = 'http://spreadsheets.google.com/feeds/list/' +
    'r1lvoTeeQwriS3eL9m-e6Dg/od7/public/basic';

/**
 * Displays rich text information from a Google Spreadsheet that can be
 * used to quickly update custom content.
 * @param {Object} feedData the Spreadsheet data parsed by /js/feed.js.
 */
press.displayRecentNews = function(feedData) {
  var newsContainer = document.getElementById('news-updates-container');
  var activeRow = 0;

  // If no story content, show a blank line.
  if (typeof feedData[activeRow]['storytext'] === 'undefined') {
    feedData[activeRow]['storytext'] = '';
  }

  // If no link text is provided, set the link text to the URL.
  if (typeof feedData[activeRow]['linktext'] === 'undefined' ||
      feedData[activeRow]['linktext'] == '') {
    if (typeof feedData[activeRow]['linkurl'] !== 'undefined' ||
      feedData[activeRow]['linkurl'] != '') {
      feedData[activeRow]['linktext'] = feedData[activeRow]['linkurl'];
    }
  }

  // If no URL is given, don't show any link.
  if (typeof feedData[activeRow]['linkurl'] === 'undefined' ||
          feedData[activeRow]['linkurl'] === '') {
    feedData[activeRow]['linkurl'] = '';
    feedData[activeRow]['linktext'] = '';
  }

  // If there is no story title, don't show the container at all.
  if (typeof feedData[activeRow]['storytitle'] === 'undefined') {
    newsContent = '';
  } else {
    var newsContent = '<div class="news-updates-container">' +
        '<h3>' + feedData[activeRow]['storytitle'] +
        '</h3><p>' + feedData[activeRow]['storytext'] + '</p><p><a href="' +
        feedData[activeRow]['linkurl'] + '">' +
        feedData[activeRow]['linktext'] + '</a></p></div>';
  }

  newsContainer.innerHTML = newsContent;
}

/**
 * Shows a YouTube video with title based on data in a Spreadsheet.
 * @param {Object} feedData the Spreadsheet data parsed by /js/feed.js.
 */
press.displayFeaturedMovie = function(feedData) {
  var movieContainer = document.getElementById('featured-movie-container');
  var activeRow = 0;
  var movieTitle = '';
  var movieContent = '';

  // Don't show 'undefined' message.
  if (typeof feedData[activeRow]['youtubecaption'] === 'undefined') {
    movieTitle = '';
  } else {
    var movieTitle = '<h3>' + feedData[activeRow]['youtubecaption'] + '</h3>';
  }

  // If no video is given, don't show anything.
  if (typeof feedData[activeRow]['youtubeurl'] === 'undefined') {
    movieTitle = '';
    movieContent = '';
  } else {
    var movieContent = '<object type="application/x-shockwave-flash"' +
        'style="width:400px; height:300px;" data="' +
        feedData[activeRow]['youtubeurl'] +
        '%26rol=0%26showinfo=0%26autoplay=0">' +
        '<param name="movie" value="' + feedData[activeRow]['youtubeurl'] +
        '%26rel=0%26showinfo=0%26autoplay=0" /></object>';
  }

  movieContainer.innerHTML = movieTitle + movieContent;
}

