// Copyright 2007 Google Inc.
// All Rights Reserved.

/*
 * @fileoverview Dynamically generate content for sidebar of Google.org home page
 * @author adowning@google.com (Anne Downing)
 */

// set maximum items to display
var numVideos = 4;
var numPhotos = 9;

// set thumbnail size for Picasa photos
// NOTE: As of 5/11/07 only certain sizes are supported by the API (via sven@google)
// 		 Sizes that will automatically crop the image to a square: 32, 48, 64, 160
//  	 Sizes that will retain original aspect ratio: 0, 72, 144, 160, 200, 288, 320, 400, 512, 576, 640, 720, 800, 912, 1024, 1152, 1280, 1440, 1600
var thumbsize = 48; 

// load feed API
google.load("feeds", "1");

/**
 * Parse YouTube channel feed and use content to populate sidebar container
*/

function listVideos() {
  var feed = new google.feeds.Feed("http://toolbox.veilleperso.com/youtube/playlist/BBBFE0D24BD3BA78");
  feed.setNumEntries(numVideos); // set maximum number of items to return
  feed.load(function(result) {
    if (!result.error) {
      var videofeed = document.getElementById("videofeed"); 
      for (var i = 0; i < result.feed.entries.length; i++) { 
        var entry = result.feed.entries[i];
        // print list of items
        var li = document.createElement("li");
        var a = document.createElement("a");
        a.href = entry.link;
        a.appendChild(document.createTextNode(entry.title));
        li.appendChild(a);
        videofeed.appendChild(li);
      }
    }
  });
}

/**
 * Parse Picasa album feed and use content to populate sidebar container
 * This functionality was removed from launch, but I'm retaining it for future reference.
*/

function listPhotos() {
  var feed = new google.feeds.Feed("http://picasaweb.google.com/data/feed/api/user/dotorgvolunteers/?kind=album&thumbsize=" + thumbsize);
  feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);  // set to mixed JSON and XML format to allow access to extension elements
  feed.setNumEntries(numPhotos); // set maximum number of items to return
  feed.load(function(result) {
    if (!result.error) {
      var photofeed = document.getElementById("photofeed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var thumbElement = google.feeds.getElementsByTagNameNS(entry.xmlNode, "http://search.yahoo.com/mrss/", "thumbnail")[0]; 
        var thumb = thumbElement.getAttribute("url"); // pull out thumbnail URLs from media:thumbnail
        // print list of items
        var li = document.createElement("li");
        var a = document.createElement("a");
        var img = document.createElement("img");
        a.href = entry.link;
        img.src = thumb; 
        a.title = entry.title; 
        a.appendChild(img);
        li.appendChild(a);
        photofeed.appendChild(li);
      }
    }
  });
}

google.setOnLoadCallback(listVideos);
// google.setOnLoadCallback(listPhotos);