if (typeof(cp) == "undefined") {var cp = {};}
if (typeof(cp.service) == "undefined") {cp.service = {};}

cp.service.Ajax =
{
	/**
	 * Fetches a file from the server in an asynchronous call.
	 * @param pageId  the number without "page" prefix
	 * @param errMsg  the message we should display if we can't find the file;
	 * do not send ampersands in the error message or the message will get cut off
	 * just before the ampersand (PHP will read this as a new request parameter)
	 * @param successHandler  the handler to call
	 * after we receive the HTML
	 * @param errorHandler  the handler to call
	 * if there is an error
	 */
	bringLight: function(pageId, errMsg, successHandler, errorHandler) {
		this.bringLightForData("pageId=" + pageId + "&errMsg=" + errMsg, successHandler, errorHandler);
	},
	
	bringLightFromPath: function(path, errMsg, successHandler, errorHandler) {
		this.bringLightForData("path=" + path + "&errMsg=" + errMsg, successHandler, errorHandler);
	},
	
	bringLightForData: function(data, successHandler, errorHandler) {
		$.ajax({
			type: "GET",
			url: "/poetry.php",
			data: data,
			success: successHandler,
			error: errorHandler
		});
	},
	
	getCss: function(url) {
		$(document.createElement("link")).attr({
			href: url,
			media: "screen",
			type: "text/css",
			rel: "stylesheet"
		}).appendTo("head");
	},
	
	loadFiles: function(dirPath, files) {
		for (var i = 0; i < files.length; ++i) {
			var filePath = dirPath + files[i];
			
			if (files[i].indexOf(".js") > 0) {
				$.getScript(filePath);
			}
			
			else if (files[i].indexOf(".css") > 0) {
				cp.service.Ajax.getCss(filePath);
			}
			
			else {
				alert("Unexpected file type for path [" + filePath + "].");
			}
		}
	}
}

