var PicasaUser = Class.create();
PicasaUser.prototype = {
	proxy_service : 'services/proxy.php?ref=',
	user_id : 'asad.sheth',
	user_loaded : false,
	albums : [],
	
	load_user : function(user_id)	{
		new Ajax.Request(
			this.proxy_service + escape('http://picasaweb.google.com/data/feed/api/user/' + this.user_id + '?kind=album&alt=json'), 
			{
				method: 'get', 
				parameters: '', 
				onComplete: this.process_user_json.bind(this)
			}
		);
	},
	
	process_user_json : function(ajax)	{
		try	{
			this.g_user_data = eval('(' + ajax.responseText + ')');
			this.albums = [];
			
			for(var i = 0; i < this.g_user_data['feed']['entry'].length; i++)	{
				this.albums[i] = new PicasaAlbum(this.g_user_data['feed']['entry'][i]);
			}
		}
		catch(e)	{
			this.g_user_data = false;
		}
		
		this.user_loaded = true;			
		if(this.on_user_load)
			this.on_user_load(this.g_user_data);
	},
	
	num_albums : function()	{
		return this.albums.length;
	},
	
	initialize : function(params)	{
		Object.extend(this, params || {});
		this.load_user();
		return;
	}
};

var PicasaAlbum = Class.create();
PicasaAlbum.prototype = {
	proxy_service : 'services/proxy.php?ref=',
	album_loaded : false,
	album_data : null,
	g_album_data : null,
	photos : [],
		
	load_album : function()	{
		new Ajax.Request(
			this.proxy_service + escape('http://picasaweb.google.com/data/feed/api/user/' + this.user_id() + '/albumid/' + this.album_id() + '?kind=photo&alt=json'), 
			{
				method: 'get', 
				parameters: '', 
				onComplete: this.process_album_json.bind(this)
			}
		);
	},
	
	process_album_json : function(ajax)	{
		try	{
			this.g_album_data = eval('(' + ajax.responseText + ')');			
			
			this.photos = [];
			
			for(var i = 0; i < this.g_album_data['feed']['entry'].length; i++)	{
				this.photos[i] = new PicasaPhoto(this.g_album_data['feed']['entry'][i]);
			}
			
			// reverse it so it is ordered by date, most recent first
			this.photos.reverse();
		}
		catch(e)	{
			// alert('Whoops! Couldn\'t get this album\'s data!')
			this.g_album_data = false;
		}		

		this.album_loaded = true;
		
		if(this.on_album_load)
			this.on_album_load(this.g_album_data);
	},
	
	// album properties
	title : function()	{
		return this.album_data['title']['$t'];
	},
	summary : function()	{
		return this.album_data['summary']['$t'];
	},
	album_id : function()	{
		return this.album_data['gphoto$id']['$t'];
	},
	timestamp : function()	{
		return this.album_data['gphoto$timestamp']['$t'];
	},
	numphotos : function()	{
		return this.album_data['gphoto$numphotos']['$t'];
	},
	thumbnail : function()	{
		return this.album_data['media$group']['media$thumbnail'];
	},
	user_id : function()	{
		return this.album_data['gphoto$user']['$t'];
	},
	
	initialize : function(album_data)	{
		this.album_data = album_data;
		this.load_album();
		return;
	}
};

var PicasaPhoto = Class.create();
PicasaPhoto.prototype = {
	photo_data : null,
	g_photo_data : null,
	
	// photo properties
	title : function()	{
		return this.photo_data['title']['$t'];
	},
	exif_data : function()	{
		var tags = this.photo_data['exif$tags'];
		return tags;
	},
	aspect_ratio : function()	{
		return this.thumbnail_small().width / this.thumbnail_small().height;
	},
	summary : function()	{
		return this.photo_data['summary']['$t'];
	},
	photo_id : function()	{
		return this.photo_data['gphoto$id']['$t'];
	},
	timestamp : function()	{
		return this.photo_data['gphoto$timestamp']['$t'];
	},
	numphotos : function()	{
		return this.photo_data['gphoto$numphotos']['$t'];
	},
	thumbnail : function()	{
		return this.photo_data['media$group']['media$thumbnail'];
	},
	thumbnail_small : function()	{
		return this.photo_data['media$group']['media$thumbnail'][0];
	},
	thumbnail_medium : function()	{
		return this.photo_data['media$group']['media$thumbnail'][1];
	},
	thumbnail_large : function()	{
		return this.photo_data['media$group']['media$thumbnail'][2];
	},
	content : function()	{
		return this.photo_data['entry']['content'][1];
	},
	user_id : function()	{
		return this.photo_data['gphoto$user']['$t'];
	},
	content_url : function(sval)	{
		if(sval)
			return this.thumbnail_small().url.replace('s72', sval);
		else
			return this.thumbnail_small().url.replace('s72', 's800');
		
	},
	picasa_link : function()	{
		return this.photo_data['link'][1]['href'];
	},
	
	initialize : function(photo_data)	{
		this.photo_data = photo_data;
		this.g_photo_data = photo_data;
	}
};