//
// gallery.js
//

// setting

// maximum number of photos
var max = 1;

// thumbnail size (original: 160x160)
var width = 160;
var height = 160;

var divid = "picasa_gallery";

// list picasa ids: http://picasaweb.google.com/[picasa id]

// picasaGallery
function picasaGallery() {
    var urlprefix = "http://picasaweb.google.com/data/feed/api/user/";
    var urlsuffix = "?kind=album&alt=json-in-script&callback=picasaCallback";

    var index = 0;
    
    while (max > 0 && idlist.length > 0) {
        var index = Math.floor(Math.random()*idlist.length);
        var picasaId = idlist.splice(index, 1);
        var script = document.createElement("script");
	      script.type = "text/javascript";
 	      script.src = urlprefix + picasaId + urlsuffix;
 	      $("head").append(script);
    };
};

// thumbnailPrint
function thumbnailPrint(url, src, alt, target) {
    var out = "<a href='" + url + "'><img class='picasathumb' src='" + src + "' width='" + width + "' height='" + height + "' alt='" + alt + "' /></a>";
    $(target).append(out);
};

// picasaCallback
function picasaCallback(response) {
    var baseurl = "http://picasaweb.google.com/" + response.feed.gphoto$user.$t + "/";

    for (var i=0; i<3; i++) {
        if (response.feed.entry.length == i) break;
        if (max == 0) break;
	      thumbnailPrint(baseurl + response.feed.entry[i].gphoto$name.$t,
		                   response.feed.entry[i].media$group.media$thumbnail[0].url,
		                   response.feed.entry[i].media$group.media$title.$t,
		                   "#"+divid);
        max--;
    };
};

$(document).ready(function(){
    picasaGallery();
});