// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Functions responsible for generating content
 * on the Earth Outreach homepage (http://earth.google.com/outreach/).
 *
 * @author Michal Drewniak
 */

/**
 * Featured KMLs class.
 * Connects FeaturedKml class instance with DOM elements
 * it will be manipulating.
 * @constructor
 */
earth.outreach.GeoHomepage = function() {
  // Show video
  var ytFeedUrl = 'http://spreadsheets.google.com/feeds/list/' +
                  'rw1WNpbJlHGXKiUxUO5ExbQ/oda/public/basic';
  gweb.feed.loadSpreadsheet(this.displayYoutube, ytFeedUrl);

  // Show featured KML
  var kmlFeedUrl = 'http://spreadsheets.google.com/feeds/list/' +
                   'rw1WNpbJlHGXKiUxUO5ExbQ/od5/public/basic';
  gweb.feed.loadSpreadsheet(this.displayFeaturedKml, kmlFeedUrl);
};

/**
 * Displays outreach homepage video.
 * @param {Array.<Object>} data Spreadsheet feed data.
 */
earth.outreach.GeoHomepage.prototype.displayYoutube = function(data) {
  for (var i = 0, size = data.length; i < size; i++) {
    if (data[i]['rowTitle'] == earth.globals.locale['lang']) {
      gweb.media.youtube.addVideo(data[i]['youtubeid'],
                                  'homepage-video', 400, 300);
    }
  }
};

/**
 * Displays featured KML entry.
 * @param {Array.<Object>} data Array of rows form the spreadsheet feed.
 */
earth.outreach.GeoHomepage.prototype.displayFeaturedKml = function(data) {
  for (var i = 0, size = data.length; i < size; i++) {
    if (data[i]['rowTitle'] == earth.globals.locale['lang']) {
      var entry = data[i];
    }
  }

  if (!entry) return;

  var featured = document.getElementById('featuredkml');
  var content = '';
  content = '<div class="g-section"><img src="' + entry['screenshot'] +
            '" /></div><strong>' + entry['title'] + '</strong>' +
            '<br />' + entry['description'] + ' ';

  if (entry['linktext'] && entry['linkurl']) {
    content += ' <a href="' + entry['linkurl'] + '">' +
    entry['linktext'] + '</a>.';
  }

  featured.innerHTML = content;
};
