/* Copyright 2009 Palm, Inc.  All rights reserved. */

Photos.Model.FacebookAlbums = Class.create(Photos.Model.Albums, {
	initialize: function($super, accountID, contactID, contactName) {
		if (accountID === undefined || contactID === undefined ||
			contactName === undefined) {
			throw "Invalid parameters to new FacebookAlbums()";
		}
		this.accountID = accountID;
		this.contactID = contactID;
		this.contactName = contactName;
	},

	getHeader: function() {
		return $L("#{name}'s Albums").interpolate({
			name: this.contactName
		});
	},

	newThumbsModel: function(item) {
		return new Photos.Model.FacebookThumbs(
			item.aid, item.name,
			this.accountID, this.contactName,
			item.description);
	},

	request: function(filter, offset, limit, done) {
		this.controller.serviceRequest(
			'palm://com.palm.photo.facebook', {
				method: this.contactID ?
					"getContactAlbums" : "getAlbums",
				onSuccess:
					this.translate.bind(this, offset, done),
				parameters: {
					cid: this.contactID,
					accountTag: this.accountID
				}
		});
	},

	translate: function(offset, done, response) {
		if (!response || !response.returnValue) {
			done([], 0);
			return;
		}

		for (var i = 0, len = response.albums.length; i < len; i++) {
			var album = response.albums[i];
			album.albumID = album.aid;
			album.albumName = album.name;
			album.albumIcon = album.icon;
			album.albumCount = album.count;
			album.row = i + offset;
		}
		done(response.albums, parseInt(response.albums.length, 10));
	}
});

