-<!--- PUBLIC CONTROLLER INITIALIZATION FUNCTIONS --->
-
-<cffunction name="provides" access="public" output="false" returntype="void">
-	<cfargument name="formats" type="string" required="false" default="">
-	<cfscript>
-		var loc = {};
+<cfscript>
+	/*
+	* PUBLIC CONTROLLER INITIALIZATION FUNCTIONS
+	*/
+	public void function provides(string formats="") {
 		$combineArguments(args=arguments, combine="formats,format", required=true);
 		arguments.formats = $listClean(arguments.formats);
-		loc.possibleFormats = StructKeyList(get("formats"));
-		loc.iEnd = ListLen(arguments.formats);
-		for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
-		{
-			loc.item = ListGetAt(arguments.formats, loc.i);
-			if (get("showErrorInformation") && !ListFindNoCase(loc.possibleFormats, loc.item))
-			{
-				$throw(type="Wheels.InvalidFormat", message="An invalid format of `#loc.item#` has been specified. The possible values are #loc.possibleFormats#.");
+		local.possibleFormats = StructKeyList(get("formats"));
+		local.iEnd = ListLen(arguments.formats);
+		for (local.i=1; local.i <= local.iEnd; local.i++)
+		{
+			local.item = ListGetAt(arguments.formats, local.i);
+			if (get("showErrorInformation") && !ListFindNoCase(local.possibleFormats, local.item))
+			{
+				$throw(type="Wheels.InvalidFormat", message="An invalid format of `#local.item#` has been specified. The possible values are #local.possibleFormats#.");
 			}
 		}
 		variables.$class.formats.default = ListAppend(variables.$class.formats.default, arguments.formats);
-	</cfscript>
-</cffunction>
-
-<!--- PUBLIC CONTROLLER REQUEST FUNCTIONS --->
-
-<cffunction name="onlyProvides" access="public" output="false" returntype="void">
-	<cfargument name="formats" type="string" required="false" default="">
-	<cfargument name="action" type="string" required="false" default="#variables.params.action#">
-	<cfscript>
-		var loc = {};
+	}
+	
+	/*
+	* PUBLIC CONTROLLER REQUEST FUNCTIONS
+	*/
+
+	public void function onlyProvides(string formats="", string action=variables.params.action) {
 		$combineArguments(args=arguments, combine="formats,format", required=true);
 		arguments.formats = $listClean(arguments.formats);
-		loc.possibleFormats = StructKeyList(get("formats"));
-		loc.iEnd = ListLen(arguments.formats);
-		for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
-		{
-			loc.item = ListGetAt(arguments.formats, loc.i);
-			if (get("showErrorInformation") && !ListFindNoCase(loc.possibleFormats, loc.item))
-			{
-				$throw(type="Wheels.InvalidFormat", message="An invalid format of `#loc.item#` has been specified. The possible values are #loc.possibleFormats#.");
+		local.possibleFormats = StructKeyList(get("formats"));
+		local.iEnd = ListLen(arguments.formats);
+		for (local.i=1; local.i <= local.iEnd; local.i++)
+		{
+			local.item = ListGetAt(arguments.formats, local.i);
+			if (get("showErrorInformation") && !ListFindNoCase(local.possibleFormats, local.item))
+			{
+				$throw(type="Wheels.InvalidFormat", message="An invalid format of `#local.item#` has been specified. The possible values are #local.possibleFormats#.");
 			}
 		}
 		variables.$class.formats.actions[arguments.action] = arguments.formats;
-	</cfscript>
-</cffunction>
-
-<cffunction name="renderWith" access="public" returntype="any" output="false">
-	<cfargument name="data" type="any" required="true">
-	<cfargument name="controller" type="string" required="false" default="#variables.params.controller#">
-	<cfargument name="action" type="string" required="false" default="#variables.params.action#">
-	<cfargument name="template" type="string" required="false" default="">
-	<cfargument name="layout" type="any" required="false">
-	<cfargument name="cache" type="any" required="false" default="">
-	<cfargument name="returnAs" type="string" required="false" default="">
-	<cfargument name="hideDebugInformation" type="boolean" required="false" default="false">
-	<cfargument name="status" type="any" required="false" default="200">
-	<cfscript>
-		var loc = {};
+	}
+
+	public any function renderWith(
+		required any data,
+		string controller=variables.params.controller,
+		string action=variables.params.action,
+		string template="",
+		any layout,
+		any cache="",
+		string returnAs="",
+		boolean hideDebugInformation=false,
+		any status="200"
+	) {
 		$args(name="renderWith", args=arguments);
-		loc.contentType = $requestContentType();
-		loc.acceptableFormats = $acceptableFormats(action=arguments.action);
+		local.contentType = $requestContentType();
+		local.acceptableFormats = $acceptableFormats(action=arguments.action);
 
 		// default to html if the content type found is not acceptable
-		if (!ListFindNoCase(loc.acceptableFormats, loc.contentType))
-		{
-			loc.contentType = "html";
+		if (!ListFindNoCase(local.acceptableFormats, local.contentType))
+		{
+			local.contentType = "html";
 		}
 
 		// call render page if we are just rendering html
-		if (loc.contentType == "html")
+		if (local.contentType == "html")
 		{
 			StructDelete(arguments, "data");
-			loc.rv = renderPage(argumentCollection=arguments);
+			local.rv = renderPage(argumentCollection=arguments);
 		}
 		else
 		{
-			loc.templateName = $generateRenderWithTemplatePath(argumentCollection=arguments, contentType=loc.contentType);
-			loc.templatePathExists = $formatTemplatePathExists($name=loc.templateName);
-			if (loc.templatePathExists)
-			{
-				loc.content = renderPage(argumentCollection=arguments, template=loc.templateName, returnAs="string", layout=false, hideDebugInformation=true);
+			local.templateName = $generateRenderWithTemplatePath(argumentCollection=arguments, contentType=local.contentType);
+			local.templatePathExists = $formatTemplatePathExists($name=local.templateName);
+			if (local.templatePathExists)
+			{
+				local.content = renderPage(argumentCollection=arguments, template=local.templateName, returnAs="string", layout=false, hideDebugInformation=true);
 			}
 
 			// throw an error if we rendered a pdf template and we got here, the cfdocument call should have stopped processing
-			if (loc.contentType == "pdf" && get("showErrorInformation") && loc.templatePathExists)
+			if (local.contentType == "pdf" && get("showErrorInformation") && local.templatePathExists)
 			{
 				$throw(type="Wheels.PdfRenderingError", message="When rendering the a PDF file, don't specify the filename attribute. This will stream the PDF straight to the browser.");
 			}
 
 			// throw an error if we do not have a template to render the content type that we do not have defaults for
-			if (!ListFindNoCase("json,xml", loc.contentType) && !StructKeyExists(loc, "content") && get("showErrorInformation"))
-			{
-				$throw(type="Wheels.RenderingError", message="To render the #loc.contentType# content type, create the template `#loc.templateName#.cfm` for the #arguments.controller# controller.");
+			if (!ListFindNoCase("json,xml", local.contentType) && !StructKeyExists(local, "content") && get("showErrorInformation"))
+			{
+				$throw(type="Wheels.RenderingError", message="To render the #local.contentType# content type, create the template `#local.templateName#.cfm` for the #arguments.controller# controller.");
 			}
 
 			// set our header based on our mime type
-			loc.formats = get("formats");
-			loc.value = loc.formats[loc.contentType] & "; charset=utf-8";
-			$header(name="content-type", value=loc.value, charset="utf-8");
+			local.formats = get("formats");
+			local.value = local.formats[local.contentType] & "; charset=utf-8";
+			$header(name="content-type", value=local.value, charset="utf-8");
 
 			// If custom statuscode passed in, then set appropriate header
 			// status may be a numeric value such as 404, or a text value such as 'Forbidden';
 			if(structKeyExists(arguments, "status")){
-				loc.status=arguments.status;
-				if(isNumeric(loc.status)){
-					loc.statusCode=loc.status;
-					loc.statusText=$returnStatusText(loc.status); 
+				local.status=arguments.status;
+				if(isNumeric(local.status)){
+					local.statusCode=local.status;
+					local.statusText=$returnStatusText(local.status); 
 				} else { 
 					// Try for statuscode;
-					loc.statusCode=$returnStatusCode(loc.status);
-					loc.statusText=loc.status;
-				}
-				$header(statusCode=loc.statusCode, statusText=loc.statusText); 
-			}
-
-			// if we do not have the loc.content variable and we are not rendering html then try to create it
-			if (!StructKeyExists(loc, "content"))
-			{
-				switch (loc.contentType)
+					local.statusCode=$returnStatusCode(local.status);
+					local.statusText=local.status;
+				}
+				$header(statusCode=local.statusCode, statusText=local.statusText); 
+			}
+
+			// if we do not have the local.content variable and we are not rendering html then try to create it
+			if (!StructKeyExists(local, "content"))
+			{
+				switch (local.contentType)
 				{
 					case "json":
-						loc.namedArgs = {};
+						local.namedArgs = {};
 						if (StructCount(arguments) > 8)
 						{
-							loc.namedArgs = $namedArguments(argumentCollection=arguments, $defined="data,controller,action,template,layout,cache,returnAs,hideDebugInformation");
+							local.namedArgs = $namedArguments(argumentCollection=arguments, $defined="data,controller,action,template,layout,cache,returnAs,hideDebugInformation");
 						}
-						for (loc.key in loc.namedArgs)
+						for (local.key in local.namedArgs)
 						{
-							if (loc.namedArgs[loc.key] == "string")
+							if (local.namedArgs[local.key] == "string")
 							{
 								if (IsArray(arguments.data))
 								{
-									loc.iEnd = ArrayLen(arguments.data);
-									for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
+									local.iEnd = ArrayLen(arguments.data);
+									for (local.i=1; local.i <= local.iEnd; local.i++)
 									{
 										// force to string by wrapping in non printable character (that we later remove again)
-										arguments.data[loc.i][loc.key] = Chr(7) & arguments.data[loc.i][loc.key] & Chr(7);
+										arguments.data[local.i][local.key] = Chr(7) & arguments.data[local.i][local.key] & Chr(7);
 									}
 								}
 							}
 						}
-						loc.content = SerializeJSON(arguments.data);
-						if (Find(Chr(7), loc.content))
+						local.content = SerializeJSON(arguments.data);
+						if (Find(Chr(7), local.content))
 						{
-							loc.content = Replace(loc.content, Chr(7), "", "all");
+							local.content = Replace(local.content, Chr(7), "", "all");
 						}
-						for (loc.key in loc.namedArgs)
+						for (local.key in local.namedArgs)
 						{
-							if (loc.namedArgs[loc.key] == "integer")
+							if (local.namedArgs[local.key] == "integer")
 							{
 								// force to integer by removing the .0 part of the number
-								loc.content = REReplaceNoCase(loc.content, '([{|,]"' & loc.key & '":[0-9]*)\.0([}|,"])', "\1\2", "all");
+								local.content = REReplaceNoCase(local.content, '([{|,]"' & local.key & '":[0-9]*)\.0([}|,"])', "\1\2", "all");
 							}
 						}
 						break;
 					case "xml":
-						loc.content = $toXml(arguments.data);
+						local.content = $toXml(arguments.data);
 						break;
 				}
 			}

				}
			}
		}
		if (!loc.rv && ListFindNoCase(variables.$class.formats.existingTemplates, arguments.$name))
		{
			loc.rv = true;
		}
	</cfscript>
	<cfreturn loc.rv>
</cffunction>

<cffunction name="$requestContentType" access="public" output="false" returntype="string">
	<cfargument name="params" type="struct" required="false" default="#variables.params#">
	<cfargument name="httpAccept" type="string" required="false" default="#request.cgi.http_accept#">
	<cfscript>
		var loc = {};
		loc.rv = "html";
		if (StructKeyExists(arguments.params, "format"))
		{
			loc.rv = arguments.params.format;
		}
		else
		{
			loc.formats = get("formats");
			for (loc.item in loc.formats)
			{
				if (FindNoCase(loc.formats[loc.item], arguments.httpAccept))
				{
					loc.rv = loc.item;
					break;
				}
			}
		}
	</cfscript>
	<cfreturn loc.rv>
</cffunction>
<cffunction name="$returnStatusText" access="public" output="false" returntype="string">
	<cfargument name="status" type="numeric" required="false" default="OK">
	<cfscript>
		var loc={};
		loc.status      = arguments.status;
		loc.statusCodes = $getStatusCodes();
		loc.rv          =  "";		 
		if(structKeyExists(loc.statuscodes, loc.status)){
			loc.rv = loc.statuscodes[loc.status];
		} else {
			$throw(type="Wheels.RenderingError", message="An invalid http response code #loc.status# was passed in.");
		} 
	</cfscript>
	<cfreturn loc.rv>
</cffunction>

<cffunction name="$returnStatusCode" access="public" output="false" returntype="string">
	<cfargument name="status" required="false" default="200" hint="Can be either a numeric value or text">
	<cfscript>
		var loc={};
		loc.status      = arguments.status;
		loc.statusCodes = $getStatusCodes();
		loc.rv          =  "";	 
		loc.lookup=StructFindValue(loc.statuscodes, loc.status);
		if(arrayLen(loc.lookup)){
      		loc.rv = loc.lookup[1]["key"];
      	} else {
			$throw(type="Wheels.RenderingError", message="An invalid http response text #loc.status# was passed in.");
		} 
	</cfscript>
	<cfreturn loc.rv>
</cffunction>

<cffunction name="$getStatusCodes" access="public" output="false" returntype="struct">
	<cfscript>
	var loc={};
		loc.statuscodes={
			100 = 'Continue',
			101 = 'Switching Protocols',
			102 = 'Processing',

			510 = 'Not Extended',
			511 = 'Network Authentication Required'
		};
		return local.rv;
	}
</cfscript>  