var pluckPhotoGrabber = {
	resultsArray: [],
	oncomplete: null,
	customItemPrefix: "recFirstPhoto_",
	batchesReturned: 0,
	batchesSent: 0,
	defaultImageUri: "/assets/images/recipebox/kraftkitchens.jpg",
	
	enqueueRecipe: function(recipeId) {
		this.resultsArray.push(
			{id: recipeId, imageUri: this.defaultImageUri}
		);
	}
	,
	go: function(callbackFtn) {
		this.oncomplete = callbackFtn;
		//compose the batch
		var request = new RequestBatch();
		for (var recipeIdx = 0; recipeIdx < this.resultsArray.length; recipeIdx++) {
			request.AddToRequest( new CustomItemKey(this.customItemPrefix + this.resultsArray[recipeIdx].id) );
			
			//are we past the 20-item limit? if so, send this batch and start a new one
			if ( (recipeIdx + 1) % 20 == 0 ) {
				this.sendRequest(request);
				request = new RequestBatch();
			}
		}
		this.sendRequest(request);
	}
	,
	sendRequest: function(requestBatch) {
		this.batchesSent++;
		requestBatch.BeginRequest(serverUrl, this.onbatchcomplete);
	}
	,
	addResult: function(resultId, resultValue) {
		for (var recipeIdx = 0; recipeIdx < pluckPhotoGrabber.resultsArray.length; recipeIdx++) {
			var thisRecipe = pluckPhotoGrabber.resultsArray[recipeIdx];
			if (thisRecipe.id == resultId) {
				thisRecipe.imageUri = resultValue;
			}
		}
	}
	,
	onbatchcomplete: function(responseBatch) {
		pluckPhotoGrabber.batchesReturned++;
		
		//add results to the array
		var recipeId, itemId, item;
		for (var respIdx = 0; respIdx < responseBatch.Responses.length; respIdx++) {
			var thisResponse = responseBatch.Responses[respIdx];
			if (thisResponse.CustomItem) {
				item = thisResponse.CustomItem;
				itemId = item.CustomItemKey.Key;
				recipeId = itemId.substr(pluckPhotoGrabber.customItemPrefix.length);
				pluckPhotoGrabber.addResult(recipeId, item.Content);
			}
		}
		
		//if we're done, fire the callback
		if (pluckPhotoGrabber.batchesReturned >= pluckPhotoGrabber.batchesSent) {
			//scrub the hacky prefix off of the result identifier first
			pluckPhotoGrabber.oncomplete( pluckPhotoGrabber.resultsArray );
		}
	}
}
