local p={}
	local function style_internal(layout, adjust, leading, indent, style, frame)
		local ret='style="margin-left:';
		local margin
		if (p.layout(frame) == 'left')
		then
			if (adjust ~= nil and adjust ~= '')
			then
				margin=(adjust + 8.1);
			else
				margin=8.1;
			end
		else
			if (adjust ~= nil and adjust ~= '')
			then
				margin=adjust;
			else
				margin=0;
			end
		end
		ret = ret .. margin .. 'em;padding-left:';
		local tab
		if (indent ~= nil and indent ~= '')
		then
			tab=indent;
		else
			tab=0;
		end
		ret = ret .. tab .. 'em;padding-bottom:';
		local pad
		if (leading ~= nil and leading ~= '')
		then
			pad=leading;
		else
			pad=0.5;
		end
		ret = ret .. pad .. 'em;'
		if (p.layout(frame) == 'right')
		then
			ret = ret .. 'margin-right:8.1em;'
		end
		if (style ~= nil)
		then
			ret = ret .. style;
		end
		ret = ret .. '"'
		return ret;
	end
	local function namespace(frame)
		return mw.site.namespaces[mw.title.getCurrentTitle().namespace].name;
	end
	local function begin_internal(split, frame, args)
		local div;
		local title;
		local ret='';
		if (split == nil or split == '')
		then
			div=true
			title=true
		elseif (split == "table")
		then
			div=false;
			title=false;
		elseif (split == "heading")
		then
			div=true;
			title=true;
			args['style'] = "clear:both;";
		elseif (split == "footer" and namespace(frame) ~= "Page")
		then
			div=false;
			title=false;
		elseif(split == "end")
		then
			div=false;
			title=false;
		elseif (split == "cont")
		then
			if (namespace(frame) == "Page")
			then
				div=true;
			else
				div=false;
			end
			title=false;
		else
			div=true;
			title=true;
		end
		if(div)
		then
			ret=frame:extensionTag( 'templatestyles', '', { src = 'Template:Cl-act-p/styles.css' } );
			ret =ret .. "<div " .. "class='__cl-act-p'" .. p.style(frame) .. '>';
			if(title)
			then
				ret = ret .. p.h_internal(frame)
			end		
		end
		return ret .. '\n';
	end
	local function p_s_internal(split, frame, args)
		if (split == "cont" or split == "end")
		then
			args['split'] = "start";
		elseif (split == "footer")
		then
			if (namespace(frame) == "Page")
			then
				args['split'] = "start";
			else
				return "";
			end
		elseif (split == nil or split == "")
		then
			args['split'] = "start";
		end
		return p.begin(frame) .. p.body(frame);
	end
	local function p_e_internal(split, frame, args)
		if (split == "footer")
		then
			if (namespace(frame) == "Page")
			then
				args['split'] = "start";
			else
				return "";
			end
		elseif (split == "start" or split == "cont")
		then
			args['split'] = "end";
		elseif (split == nil or split == "")
		then
			args['split'] = "end";
		end
		return p.body(frame) .. p.finish(frame);
	end
	local function get_args(frame)
		local args = {}
		if (frame:getParent():getParent() ~= nil)
		then
			for a,b in pairs(frame:getParent():getParent().args)
			do
				args[a] = (b ~= nil) and b;
			end
		end
		for a,b in pairs(frame:getParent().args)
		do
			args[a] = (b ~= nil) and b;
		end
		for a,b in pairs(frame.args)
		do
			args[a] = (b ~= nil) and b;
		end
		frame.args = args;
		return frame.args;
	end
	local function finish_internal(split, frame, args)
		if (split == nil or split == '')
		then
			return '\n'.."</div>";
		elseif (split == "table")
		then
			return "";
		elseif (split == "heading")
		then
			return '\n'.."</div>";
		elseif (split == "footer" and namespace(frame) ~= "Page")
		then
			return "";
		elseif (split == "cont" or split == "start")
		then
			return "";
		else
			return '\n'.."</div>"
		end
	end
function p.style(frame)
	local args = get_args(frame);
	return style_internal(args['layout'], args['adjust'], args['leading'], args['indent'], args['style'], frame);
end
function p.layout(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-layout', args=args};
end
function p.p(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p', args=args};
end
function p.begin(frame)
	local args = get_args(frame);
	
	if args['1'] then 
		args['text']=args['1'];
		args['1']=nil;
	end
	return begin_internal(args['split'], frame, args);
end
function p.h_internal(frame)
			--[[  Not entirly sure this works as intended yet.
			
			It reprocess certain arguments so that un-named paramaters are 
			correctly passed into cl-act-h.
			
			Previously  cl-act-h was passed the frame args directly which meant 
			that 1= which meant different things in the two different templates 
			{{cl-act-p}} vs {{cl-act-h}} was not correctly handled.
			
			Here the arguments are sperated out into a seperate set of argument calls 
			so that the right values are passed on.
	]]--
	
	local args = get_args(frame);
    local args2 = {}
	args2['title']=args['title']
	args2['anchor']=args['anchor']
	args2['layout']=args['layout']
	return frame:expandTemplate{title='cl-act-h', args=args2};
end

function p.h(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h', args=args};
end



function p.body(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-body', args=args};
end
function p.finish(frame)
	local args = get_args(frame);
	return finish_internal(args['split'], frame, args);
end
function p.p_s(frame)
	local args = get_args(frame);
	return p_s_internal(args['split'], frame, args);
end
function p.p_e(frame)
	local args = get_args(frame);
	return p_e_internal(args['split'], frame, args);
end
function p.p_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/c', args=args};
end
function p.p_a(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/a', args=args};
end
function p.h_1(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h/1', args=args};
end
function p.h_2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h/2', args=args};
end
function p.h_1_style(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h/1-style', args=args};
end
function p.h_1_body(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h/1-body', args=args};
end
function p.preamb(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-preamb', args=args};
end
function p.preamb_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-preamb/s', args=args};
end
function p.preamb_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-preamb/c', args=args};
end
function p.preamb_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-preamb/e', args=args};
end
function p.p_x(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/x', args=args};
end
function p.p_x_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/x/s', args=args};
end
function p.p_x_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/x/c', args=args};
end
function p.p_x_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/x/e', args=args};
end
function p.p_indexstyle(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/indexstyle', args=args};
end
function p.p_1(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1', args=args};
end
function p.p_1_2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1-2', args=args};
end
function p.p_2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2', args=args};
end
function p.p_2a(frame)
	local args = get_args(frame);
	if (args['index-style'] ~= nil and args['index-style'] ~= '')
		then
		args['index-style'] = "lower-alphab";
	end
	return p.p_2(frame);
end
function p.p_3(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/3', args=args};
end
function p.p_4(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/4', args=args};
end
function p.p_1_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1/s', args=args};
end
function p.p_1_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1/c', args=args};
end
function p.p_1_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1/e', args=args};
end
function p.p_1_2_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1-2/s', args=args};
end
function p.p_1_2_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1-2/c', args=args};
end
function p.p_1_2_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/1-2/e', args=args};
end
function p.p_2_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2/s', args=args};
end
function p.p_2_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2/c', args=args};
end
function p.p_2_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2/e', args=args};
end
function p.p_3_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/3/s', args=args};
end
function p.p_3_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/3/c', args=args};
end
function p.p_3_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/3/e', args=args};
end
function p.p_4_s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/4/s', args=args};
end
function p.p_4_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/4/c', args=args};
end
function p.p_4_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/4/e', args=args};
end
function p.p_2a_s(frame)
	local args = get_args(frame);
		if (args['index-style'] ~= nil and args['index-style'] ~= '')
		then
		args['index-style'] = "lower-alphab";
	end
	return p.p_2_s(frame);
end
function p.p_2a_c(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2a/c', args=args};
end
function p.p_2a_e(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-p/2a/e', args=args};
end
function p.t(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-t', args=args};
end
function p.cl_act(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act', args=args};
end
function p.s(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s', args=args};
end
function p.s1(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s1', args=args};
end
function p.s1_2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s1/2', args=args};
end
function p.s2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s2', args=args};
end
function p.s3(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s3', args=args};
end
function p.s4(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-s4', args=args};
end
function p.h1(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h1', args=args};
end
function p.h2(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-h2', args=args};
end
function p.pre(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-pre', args=args};
end
function p.pre_nt(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-pre-nt', args=args};
end
function p.t_int(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-t-int', args=args};
end
function p.t_int1(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-t-int1', args=args};
end
function p.missing(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-missing', args=args};
end
function p.amend(frame)
	local args = get_args(frame);
	return frame:expandTemplate{title='cl-act-amend', args=args};
end
function p.call(func, args)
	return func(mw.getCurrentFrame():newChild{ title = "Module:Cl-act", args = args });
end
return p;