function PicasaApi() {
	this.initialize.apply(this, arguments);
}
PicasaApi.prototype = {
	initialize: function(response) { 
		this.feed = new Array();

		this.feed.entry = new Array();
		for (var i = 0; i < response.feed.entry.length; i ++) {
			this.feed.entry[i] = {
				id: response.feed.entry[i].gphoto$id.$t,
				albumid: response.feed.entry[i].gphoto$albumid.$t,
				title: response.feed.entry[i].title.$t,
				link: response.feed.entry[i].link[1].href,
				published: response.feed.entry[i].published.$t,
				updated: response.feed.entry[i].updated.$t,
				size: response.feed.entry[i].gphoto$size.$t,
				media: {
					content: response.feed.entry[i].media$group.media$content,
					thumbnail: response.feed.entry[i].media$group.media$thumbnail 
				}
			};
		};
	},

	debug: function() {
		$("#debug").text(this.feed.entry[0].media.content[0].url);
	},
	
	generateHtml: function(id, checkedboxes) {
		var output = "";
		var smalloutput = "";

		var maxwidth = 500;
		var width = 0;
		var checkedId = "";
		
		for (var i = 0; i < checkedboxes.length; i ++) {
			checkedId = checkedboxes[i].id;
				if (this.myimages[checkedId].width < maxwidth) {
					
					smalloutput += this.getOutput(checkedId);
					width += this.myimages[checkedId].width;
					
					if ( maxwidth < width ) {
						smalloutput += "<br />\n";
						width = 0;
					}
					
				} else {
					output += this.getOutput(checkedId);
					output += "<br />\n";    
				}
		}
		
		output += "\n" + smalloutput;
		$("#target").text(output);
	},

	getList: function() {
		var output = "";
		for (var i = 0; i < this.feed.entry.length; i ++) {
			output += this.getOutput(i, "1");
		}
		return output;
	},
	
	getOutput: function(id, size) {


	var output = "";
		output = '<a href="'+ this.feed.entry[id].link +'"><img src="'+ this.feed.entry[id].media.thumbnail[size].url +'" width="64px" height="64px" alt="'+ this.feed.entry[id].title +'" /></a>';
	//	var output = "<a href=\"" + this.feed.entry[id].link
	//	+ "\" rel=\"tb-photos\" style=\"margin:5px;\" >"
	//	+ "<img src=\"" + this.feed.entry[id].media.thumbnail[size].url + "\" "
	//	+ "width=\""  + this.feed.entry[id].media.thumbnail[size].width + "\" "
	//	+ "height=\""  + this.feed.entry[id].media.thumbnail[size].height + "\" "
	//	+ "title=\""  + this.feed.entry[id].title + "\" /></a>";  
		return output;
	}
};
var g_id1="";
function picasaRequestAlbum1(_id,_num) {
        g_id1=_id;
	var query = "http://picasaweb.google.com/data/feed/api/user/info@shanghaibooks.com/albumid/5555742914527833649?kind=photo&alt=json-in-script&callback=picasaCallback1&start-index=1&max-results="+_num;
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = query;
  $('head').append(script);
};
function picasaCallback1(data) {
  $("#"+g_id1).html(new PicasaApi(data).getList());
};
var g_id2="";
function picasaRequestAlbum2(_id,_num) {
        g_id2=_id;
	var query = "http://picasaweb.google.com/data/feed/api/user/info@shanghaibooks.com/albumid/5555742914527833649?kind=photo&alt=json-in-script&callback=picasaCallback2&start-index=1&max-results="+_num;
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = query;
  $('head').append(script);
};
function picasaCallback2(data) {
  $("#"+g_id2).html(new PicasaApi(data).getList());
};
