 var PhotoFerret = function(galleryId, optNumOfPhotosToGet) {

	if (typeof galleryId != "string" || galleryId == "") {
		throw new Error("PhotoFerret instantiated without galleryId");
	}

	var numberOfPhotosToGet = optNumOfPhotosToGet || 10;
	var thisFerret = null;

	var galleryId = galleryId;
	var templateId = null;	//DOM element containing the Trimpath template; will be filled in at runtime
	var outputElemId = null;//DOM element that contains the resulting UI

	var ondaapicomplete = function(responseBatch) {
		var outElem = document.getElementById(outputElemId);
		if (outElem != null) {
			if (outElem.length) {
				outElem = outElem[0];
			}
		}
		if (outElem != null && responseBatch.Responses.length > 0) {
			var photoCount = responseBatch.Responses[0].PhotoPage.Photos.length;
			if (photoCount < numberOfPhotosToGet) {
				handleError("PhotoFerret needed " + numberOfPhotosToGet + " photos from DAAPI but only got " + photoCount);
			} else {
				var contentForTrimpath = {items: responseBatch.Responses[0].PhotoPage.Photos, tool: thisFerret};
			}
			outElem.innerHTML = TrimPath.processDOMTemplate(templateId, contentForTrimpath);
		} else {
			handleError("PhotoFerret needed encountered a DAAPI error or an invalid output element ID");
		}
	}
	
	var handleError = function(errorMsg) {
		if (outElem != null) {
			outElem.innerHTML = "";
		}
	}

	return {
		go: function(domTemplateElemId, domOutputElemId) {
			if (typeof domTemplateElemId != "string" || domTemplateElemId == "") {
				throw new Error("PhotoFerret.go called without domTemplateElemId");
			} else if (typeof domOutputElemId != "string" || domOutputElemId == "") {
				throw new Error("PhotoFerret.go called without domOutputElemId");
			}
			thisFerret = this;	//self-referencing closure for use on callback
			templateId = domTemplateElemId;
			outputElemId = domOutputElemId;

			var requestBatch = new RequestBatch();
			var photoPage = new PhotoPage( new GalleryKey(galleryId) , numberOfPhotosToGet, 1, "TimeStampDescending");
			requestBatch.AddToRequest(photoPage);
			requestBatch.BeginRequest(daapiServerUrl, ondaapicomplete);
		}
		,
			makeAttrib: function(value) {
			value = value.replace(/"/g, "'"); //'
			return value;
		}
	}
}

