﻿  /* 
     this script needs to go after the document element with ID="albums" 
  */
  function listAlbums(root) {
    var pattern = /Honduras/; /* use to search for specific albums e.g. pattern = /TST/; /*
    /* 
       navigate to the entries (albums) in the xml doc, and start the unordered list 
    */
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = [''];
    /* 
       loop thru all the entries in the xml doc. 
       each entry in the xml represents an album 
    */
    for (var i = 0; i < entries.length; ++i) {
      var entry = entries[i];  
      var title = entry.title.$t; /* grab the title of the album */
      var pcount = entry.gphoto$numphotos.$t;
      var lupdate = entry.updated.$t;
      var d1 = lupdate.substring(0,4);
      var d2 = lupdate.substring(5,7);
      var d3 = lupdate.substring(8,10);
      var d4 = lupdate.substring(12,13);
      var d5 = lupdate.substring(14,16);
      var d6 = lupdate.substring(17,19);
      dlupdate = new Date (d1,d2,d3,d4,d5,d6);
      var slupdate = dlupdate.toDateString();
	  /* 
	     only deal with the album's that meet the search criteria you setup in 
	     the pattern variable, above... 
	     NOTE: we have to do this ourselves,as of Feb 2009 Google doesn't have the 
	     q= query parameters reference working for Picasa Web Albums 
	  */
      var xsearch = 0;
      if (pattern != null) 
	      xsearch = title.search(pattern);
      if (xsearch >= 0) {
          /* 
             now that we have an album that qualifies, start grabbing
             information we require. First, info about the thumbnail Google made 
          */
	      var mgroup = entry['media$group'] || [];
	      var mthumburl = mgroup['media$thumbnail'][0].url;
	      var mthumbwid = mgroup['media$thumbnail'][0].width;
	      var mthumbhei = mgroup['media$thumbnail'][0].height;
		  /* 
		     the #feed link as the url we need for the slideshow, 
		     which is /feed/api/user/<username>/albumid/<albumid>.
		     Use the pattern2 regex to make sure we find the correct 'link'
		     as there are usually 3 of them.
		  */
          var links = entry.link || [];
          var href = null;
          var cingoogle = null;
          for (var j = 0; j < links.length; ++j) {
            var link = links[j];
            var pattern2 = /feed$/;
            if (link.rel.search(pattern2) >= 0) {
              /* 
              	 now that we found the correct link, grab the uri in the href.
                 make sure we don't bring with us any parameters (after the 
                 question mark).  
                 Following code allows you to use either projection (base or api).
                 For the slideshows to work, we need to use api.
              */
              href = unescape(link.href);
              var hrefarray = href.split('?');
              href = hrefarray[0].replace(/\/base\//, '/api/');
            }
            if (link.rel == "alternate") {
              cingoogle = link.href
            }
          } /* for j */
          /* 
             now that we have all the variables gathered, we need to construct
             the list entry in our unordered list for this album. 
             First build the parameters that we'll pass to the page that will 
             display our slideshow.
          */
          var param = 'host=picasaweb.google.com&captions=1&RGB=0x000000&feed=' + 
            encodeURIComponent(href) + 
            encodeURIComponent('?kind=photo&alt=rss');
//          param = "host=picasaweb.google.com&captions=1&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2FStBasil.Brecksville%2Falbumid%2F5299117142117070513%3Fkind%3Dphoto%26alt%3Drss";

		  /* 
		     add the list entry, with a hyperlink to the slideshow page on the thumbnail 
		  */
	      html.push('<div class="halbum"> <a href=slideshow.asp?', param, '><image alt="" src="', 
	      mthumburl, '" width="', mthumbwid, '" height="', mthumbhei, 
	      '" style="margin: 1px 0 0 4px;" /></a>',
	      '<p>   <b>Album:</b> ', title, '<br />',
	      '   <b>Picture Count:</b> ', pcount, '<br />',
	      '   <b>Last Update:</b> ', slupdate, '<br />',
	      (cingoogle==null)?'':'<a href="' + cingoogle + '">   See it in Google</a>',
	      '</p></div>');
	  } /* if xsearch */
    } /* for i */
	/* 
	   add the html we've been building
	   to the document tag with ID=albums
	*/
    document.getElementById("halbums").innerHTML = html.join("");
  }