var picasaWebData = [];

function doShowPicasaWebMovie(album, videoId, objectId, width, height, autoplay, fs) {
    for( var i = 0; i < picasaWebData[album]["json"].feed.entry.length; ++i ) { 
	var entry = picasaWebData[album]["json"].feed.entry[i];

	var id = entry.id.$t;

	if ( id.search(videoId) != -1 ) 
        {
	    for ( var j = 0; j < entry.media$group.media$content.length; j++ ) 
	    {
		if ( entry.media$group.media$content[j].type == 
		     "application/x-shockwave-flash" ) {
		    
		    var url = entry.media$group.media$content[j].url;
		    
		    swfobject.embedSWF("http://video.google.com/googleplayer.swf?videoUrl=" + escape(url), objectId, width, height, "9", vvqexpressinstall, { wmode: "opaque", allowfullscreen: "true", "fs": "true" }, vvqparams, vvqattributes);
		}
	    }
	}
    }
}

function displayPicasaWebVideo(json) {
    var id = json.feed.id.$t;
    var re = /^.*album\/([^\/]+).*$/;
    var albumName = id.replace(re, "$1");

    picasaWebData[albumName]["json"] = json;
    
    if ( picasaWebData[albumName]["jobs"] ) {
	for ( var i = 0; i < picasaWebData[albumName]["jobs"].length; ++i ) {
	    doShowPicasaWebMovie(albumName,
				 picasaWebData[albumName]["jobs"][i].videoId,
				 picasaWebData[albumName]["jobs"][i].objectId,
				 picasaWebData[albumName]["jobs"][i].width,
				 picasaWebData[albumName]["jobs"][i].height);
	}
    }
}

function vvq_picasawebvideo(objectID, videoWidth, videoHeight, username, album, videoID) {
    
    if ( !picasaWebData[album] ) {
	picasaWebData[album] = [];
	picasaWebData[album]["started"] = true;

	var scriptElement = document.createElement("script");
	scriptElement.setAttribute("id", "jsonScript");
	scriptElement.setAttribute("src", 
				   "http://picasaweb.google.com/data/feed/base/user/" + username + "/album/" + album + "?kind=photo&alt=json&hl=en_US&callback=displayPicasaWebVideo");
	scriptElement.setAttribute("type", "text/javascript");
	
	var id = "http://picasaweb.google.com/data/feed/base/user" + username + "/album/" + album;

	document.documentElement.firstChild.appendChild(scriptElement);
    }
    
    if ( !picasaWebData[album]["json"] ) {
	var index;

	if ( !picasaWebData[album]["jobs"] ) {
	    picasaWebData[album]["jobs"] = [];
	    index = 0;
	}
	else {
	    index = picasaWebData[album]["jobs"].length;
	}

	picasaWebData[album]["jobs"][index] = [];
	picasaWebData[album]["jobs"][index].videoId = videoID;
	picasaWebData[album]["jobs"][index].objectId = objectID;
	picasaWebData[album]["jobs"][index].width = videoWidth;
	picasaWebData[album]["jobs"][index].height = videoHeight;
    }
    else {
	doShowPicasaWebMovie(album,
			     videoID,
			     objectID,
			     videoWidth,
			     videoHeight);
    }
}
