var picasa_class = application.extend({
	init : function(p,h) {
		this.gxml = false;
		this._super(p,h);
	},
	end : function () {
		if(this.gxml) {
			this.gxml.hide(); 
			Rimory.map.instance.removeOverlay(this.gxml);
			this.gxml = false;
		}
		return this._super();
	},
	list : function (user) {
		if(this.gxml) {
			this.gxml.hide(); 
			Rimory.map.instance.removeOverlay(this.gxml);
			this.gxml = false;
		}
		$.ajax({
			type:		'GET',
			url:		"http://picasaweb.google.com/data/feed/api/user/" + user + "?kind=album&alt=json&thumbsize=32",
			dataType:	"jsonp",
			cache:		true,
			success:	function(data) {
				var str = "";
				var mstr = "";
				$.each(data["feed"]["entry"], function (i) {
					str += "<a style='background-image:url(" + data["feed"]["entry"][i]["media$group"]["media$thumbnail"][0]["url"] + ")' href='http://picasaweb.google.com/data/feed/base/user/" + user + "/albumid/" + data["feed"]["entry"][i]["gphoto$id"]["$t"] + "?kind=photo&alt=kml' onclick='Rimory.inst.picasa.album(this.href); this.blur(); return false;'>" + data["feed"]["entry"][i]["title"]["$t"] + "</a>";
				});
				$("#albums").html(str).parents("fieldset:eq(0)").show();
			}
		});
	},
	album : function(url) {
		if(this.gxml) {
			this.gxml.hide(); 
			Rimory.map.instance.removeOverlay(this.gxml);
			this.gxml = false;
		}
		GEvent.addListener(Rimory.map.instance,"addoverlay",function(overlay) {
			// If this is a named overlay (e.g. not the GGeoXml itself, or the infowindow)
			if (overlay.name) {
				if (overlay.Known) { } 
				else {
					overlay.Known = true;
					var point = overlay.getLatLng();
					var tmp = $(overlay.description);
					tmp = tmp.find("a:eq(0)").html();

					GEvent.addListener(overlay, "click", function(latlng) {
						Rimory.map.show_bubble(latlng, { 
							title		: latlng.lat() + "<br />" + latlng.lng(), 
							content		: tmp,
							callback	: function () { 
								Rimory.map.geocoder.getLocations(latlng, function(data) {
									if(data.Status.code == 200) { $("#addr").html(data.Placemark[0].address); }
								}); 
							}
						});
					});
				}
			}
		}); 
		GEvent.addListener(Rimory.map.instance,"infowindowprepareopen", function(iwtabs) {
			this.hide();
			this.close();
			iwtabs[0].contentElem.innerHTML+="Additional infowindow content";
		});
		this.gxml = new GGeoXml(url);
		this.hidden = false;
		Rimory.map.instance.addOverlay(this.gxml);
		this.gxml.gotoDefaultViewport(Rimory.map.instance);
	}
});
Rimory.inst.picasa = new picasa_class();