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


var MediaDBImageService = Class.create(
{
	initialize: function()
	{
	},
	
	/**
	 * Subscribe to the fileindexer's state so we can see when the indexer is "idle" and therefore able to handle 
	 * subscription requests on the photoroll.
	 */
	getState: function(callback, sceneController){
		return sceneController.serviceRequest(
				'palm://com.palm.mediadb',
				{
				method : 'getstate',
				onSuccess : callback,
				onFailure : callback,
				resubscribe: true,
				parameters : {subscribe : true}
			},{resubscribe: true});
		
	},

	 
	/**
	 * Get details of the photoroll album
	 * 
	 * @param {Function}  callback  The function to be called once the data is retrieved
	 *                              from the media indexer service
	 *                              
	 * @param {Object}    sceneController  This scene's controller - which has a reference to the 
	 *                                     service requester thing.
	 * @return returns a reference to the service call.
	 */
	retrievePhotoRoll : function(callback, sceneController)
	{
		return sceneController.serviceRequest(
				MediaDBImageService.imageIdentifier,
				{
					method: 'detailphotoroll',
					onSuccess: callback,
					parameters: { }
				}
			);
	},
	

	
	/**
	 * 
	 * Get details of the Images in an album.
	 * 
	 * This function is intended to be used to retrieve the first image in the photoroll album.
	 * The image can then be displayed on the photoroll icon in the UI.
	 * 
	 * @param {Hash}      album     The album details that represents the photoroll.  This will be 
	 *                              returned from the indexer service by calling it with 'detailphotoroll'.
	 *                              Contains 'albumID' and 'albumName' fields. 
	 *                              
	 * @param {Function}  callback  The function to be called once the data is retrieved
	 *                              from the media indexer service
	 *                              
	 * @param {Object}    sceneController  This scene's controller - which has a reference to the 
	 *                                     service requester thing.
	 *                                     
	 * @return returns a reference to the service call.
	 */
	retrievePhotoRollImages: function(album, callback, sceneController){
		var args = {
				method: 'listimages',
				onSuccess: callback,
				parameters: {
					subscribe: true,
					albumID: album.albumID,
					offset: 0,
					limit: 1
				}
			};
		llog("Calling ", MediaDBImageService.imageIdentifier, " with args ", Object.toJSON(args));

		return sceneController.serviceRequest(MediaDBImageService.imageIdentifier, args);
	},
	
	retrieveVideoRollImages: function(callback, sceneController){
		var args = {
			method: 'listvideos',
			onSuccess: callback,
			parameters: {
				subscribe: true, 
				captured: true,
				offset:0, 
				limit: 1,
				order: "videoCreation desc"
			}
		};

		llog("Calling ", MediaDBImageService.videoIdentifier, " with args ", Object.toJSON(args));

		return sceneController.serviceRequest(MediaDBImageService.videoIdentifier, args);
		
		
	}
	

});

MediaDBImageService.imageIdentifier = 'palm://com.palm.mediadb/image';
MediaDBImageService.videoIdentifier = 'palm://com.palm.mediadb/video';



