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

/**
 * @fileoverview Tour class. Sample at earth.google.com/tour.html.
 * @author Michal Drewniak
 */


/**
 * Tour class. Connects Tour class instance with DOM elements
 * it will be manipulating. Initializes function responsible for pulling
 * data from the spreadsheet.
 * @constructor
 */
earth.Tour = function() {
  this.data = [];
  if (!tourFeed) return;    // tourFeed set on the news webpage

  gweb.feed.loadSpreadsheet(this.displayTour, tourFeed);
};

/**
 * Populates individual pieces of the tour, such as:       filmstrip, headers, titles,
 * youtube, content with the data from the feed.
 * @param {Array.<Object>} feedData Tour data from the feed.
 */
earth.Tour.prototype.displayTour = function(feedData) {
  earth.tour.hideContents();
  earth.tour.displayEntry(feedData);

  earth.tour.scroller = new gweb.ui.Scroller();

  var thumbs = document.getElementById('scroller-slide-holder').
               getElementsByTagName('a');

  for (var i = 0, size = thumbs.length; i < size; i++) {
    thumbs[i].onclick = function() {
      earth.tour.hideContents();
      earth.tour.displayEntry(feedData, this.id);
    };
  }
};

/**
 * Displays contents of the specific tour.
 * @param {string} contents Text assocciated with the tour.
 */
earth.Tour.prototype.displayContents = function(vid) {
  var contentsContainer = document.getElementById('content_' + vid);
  contentsContainer.className = 'tour-contents';
};

/**
 * Displays selected feed entry.
 * @param {Array.<Object>} feedData Tour data from the feed.
 * @param {number} vid Index of the video to display.
 */
earth.Tour.prototype.displayEntry = function(feedData, vid) {
  var error = false;

  if (!vid) {
    var vid = earth.tour.extractVideoId();
    var thumbs = document.getElementById('scroller-slide-holder').
               getElementsByTagName('img');

    vid = thumbs[vid] ? vid : 0;
    if (!thumbs[vid]) {
      error = true;
      earth.tour.displayError();
    } else {
      thumbs[vid].selected = true;
      thumbs[vid].className = 'scroller-thumb-selected';
    }
  }

  if (!error) {
    earth.tour.displayYoutube(feedData[vid]['youtube']);
    earth.tour.displayContents(vid);
  }
};

/**
 * Displays error message.
 */
earth.Tour.prototype.displayError = function() {
  var error = document.getElementById('tour-youtube');
  error.innerHTML = '<h3><span class="red">We\'re sorry. This page is ' +
                    'currently unavailable.\n</span></h3>';
};


/**
 * Displays youtube video on the tour page.
 * @param {string} youtubeId Id of the YouTube video.
 */
earth.Tour.prototype.displayYoutube = function(youtubeId) {
  var ytContainer = document.getElementById('tour-youtube');

  ytContainer.innerHTML = '<object width="390" height="245">' +
      '<param name="movie" value="http://www.youtube.com/v/' + youtubeId +
      '&amp;autoplay=0&amp;rel=0fs=1&amp;ap=%2526fmt%3D18"></param>' +
      '<param name="allowFullScreen" value="true"></param>' +
      '<param name="allowscriptaccess" value="always"></param>' +
      '<embed src="http://www.youtube.com/v/' + youtubeId +
      '&amp;autoplay=0&amp;rel=0fs=1&amp;ap=%2526fmt%3D18"' +
      'type="application/x-shockwave-flash" allowscriptaccess="always" ' +
      'allowfullscreen="true" width="390" height="245"></embed></object>';
};
/**
 * Extracts video Id from the current url. If none found, defaults to 0.
 * @return {number} Id of the video.
 */
earth.Tour.prototype.extractVideoId = function() {
  if (!location.hash) return 0;

  var params = earth.parseURLVar();
  var vid = parseInt(params['v']);

  if (vid) return vid;
  return 0;
};

/**
 * Hides contents of all content divs.
 */
earth.Tour.prototype.hideContents = function() {
  var divs = document.getElementsByTagName('div');
  for (var i = 0, div; div = divs[i]; i++) {
    if (div.id.match('content_')) {
      div.className += ' hidden';
    }
  }
};
