local p = {}

function p.example(frame)
	
	local params = mw.getCurrentFrame():getParent().args
		-- build HTML table
	local exampleTable = mw.html.create('table')
	exampleTable:addClass('wikitable')
	local align = params['align'] or 'center'
	if align == 'center' then
		exampleTable:css('margin-left', 'auto')
		exampleTable:css('margin-right','auto')
	elseif align == 'right' then
		exampleTable:css('float',align)
	end
	
	local columns = {'description','code','output','comment'}
	--local headers = {'Description','Code','Output','Comment'}
    -- create header row and add requested headers
    local row = exampleTable:tag('tr')
    local headerText
    for k,v in pairs(columns) do
		if params[v] then 
			if params[v]~='' then headerText=params[v]  else headerText = firstToUpper(v) end
			row:tag('th'):wikitext(headerText) 
	    end
    end

	-- now deal with the data rows
	local i=0
	while i<10 do
		i=i+1 
		local moreRows = false
		for k,v in pairs(columns) do
			if params[v..i] then moreRows = true end
		end
		if not moreRows then break end
			
		row = exampleTable:tag('tr')
		for k,v in pairs(columns) do
			if params[v] then
				if params[v] then 
					row:tag('td'):css('text-align', 'left'):wikitext(params[v..i]) 
				else
					row:tag('td')
				end
			end
		end
    end

	return tostring(exampleTable)
end
function firstToUpper(str)
    return (str:gsub("^%l", string.upper))
end

-- display a gallery of cladograms using floating div elements

function p.gallery(frame)
	
	local params = mw.getCurrentFrame():getParent().args

    local outerBorders = false
    if params['caption'] or params['footer'] then outerBorders = true end
    
	-- the gallery container
	local gallery = mw.html.create('div'):addClass('clade-gallery'):css('float',params['align'])

	-- main caption for gallery if there is one
	
	if params['main-caption'] then
		gallery:tag('div'):wikitext(params['main-caption']):addClass('main-caption')
	end

	-- a gallery of floating divs with content

    local shading = ''
	--if params['shading'] then shading = 'shading'	end -- class for shading
	if params['shading'] then shading = 'wikitable'	end -- class for shading
	
	local vertical = '' -- defaults to horizontal
	if params['vertical'] then vertical = 'vertical'	end -- class for vertical display
	 
	local sizeStyling = ''
	if params['width'] then sizeStyling = sizeStyling .. 'width:' .. params['width'] .. ';' end
	if params['height'] then sizeStyling = sizeStyling .. 'height:' .. params['height'] .. ';' end

    local noborder = ''
    if 	not params['main-borders'] then noborder = 'no-border' end
    	
    -- the gallery	contents
    local content = gallery:tag('div'):addClass('main-content'):addClass(noborder)
                                      :css('height',params['height'])  
   	local i=0
	while i<15 do
		i=i+1 
		local caption = params['caption'..i] --or ''
		local header = params['header'..i] --or ''
		local footer = params['footer'..i] --or ''
		
		-- for each gallery element containing  a cladogram
        if params['cladogram'..i] then
		    local cladogramTable = content:tag('table')
		                  :addClass(shading):addClass('gallery-element'):addClass(vertical)
		                  :css('width',params['width'])    
		                  :css('height',params['height'])    
            if caption then 
	        	cladogramTable:tag('caption'):wikitext(caption):addClass('caption')
	        end
	        if header then 
	        	local row = cladogramTable:tag('tr'):addClass('header')
	        	row:tag('th'):wikitext(header)
	        end
	        -- if params['cladogram'..i] already checked
	        local row = cladogramTable:tag('tr')
	        row:tag('td'):wikitext("\n" .. params['cladogram'..i])
	                      :addClass('cladogram')
	                      --:css("padding", "20px")
		                  :css('width',params['cladogram-width'])    
		                  :css('height',params['cladogram-height'])    
	        
	        if footer then
	        	local row = cladogramTable:tag('tr')
	        	row:tag('td'):wikitext(footer):addClass('footer'):addClass(shading)
	        end
		    
		    --[[DIV VERSION - different browsers behave differently
		    -- the div containing each element in the gallery
		    local div = content:tag('div'):addClass('gallery-element'):addClass(direction)
		                  :css('width',params['width'])    
		                  :css('height',params['height'])    
		    
		    -- the caption, header, content and footer for each element
		    if caption then div:tag('div'):wikitext(caption):addClass('caption'):addClass(shading) end
		    
		    if header then div:tag('div'):wikitext(header):addClass('header'):addClass(shading) end
		    
		    div:tag('div'):wikitext(params['cladogram'..i]):addClass('cladogram'):addClass(shading)
		                  :css('width',params['cladogram-width'])    
		                  :css('height',params['cladogram-height'])    
		    
		    if footer then div:tag('div'):wikitext(footer):addClass('footer'):addClass(shading) end
		    END DIV VERSION--]] 
	    else
	       break	
	    end
	end
	
	-- main footer
	if params['main-footer'] then
		gallery:tag('div'):wikitext(params['main-footer']):addClass('main-footer'):addClass(noborder)
	end
	-- TemplateStyles file
	local src = "Bản mẫu:Clade gallery/styles.css"
    	
	return p.templateStyle( frame, src ) .. tostring(gallery)
end
function p.templateStyle( frame, src )
   return frame:extensionTag( 'templatestyles', '', { src = src } );
end

-- this must be at end
return p